/*
 * The contents of this file are subject to the terms 
 * of the Common Development and Distribution License 
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at 
 * https://glassfish.dev.java.net/public/CDDLv1.0.html or
 * glassfish/bootstrap/legal/CDDLv1.0.txt.
 * See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL 
 * Header Notice in each file and include the License file 
 * at glassfish/bootstrap/legal/CDDLv1.0.txt.  
 * If applicable, add the following below the CDDL Header, 
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information: 
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */



package com.sun.enterprise.tools.verifier.tests.web.ias;

import java.util.*;
import com.sun.enterprise.deployment.*;
import com.sun.enterprise.deployment.web.*;
import com.sun.enterprise.tools.verifier.*;
import com.sun.enterprise.tools.verifier.tests.*;
import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
import com.sun.enterprise.deployment.WebBundleDescriptor;
import com.sun.enterprise.tools.verifier.tests.web.*;
import com.sun.enterprise.tools.common.dd.webapp.*;

//<addition author="irfan@sun.com" [bug/rfe]-id="4711198" >
/* Changed the result messages to reflect consistency between the result messages generated 
 * for the EJB test cases for SunONE specific deployment descriptors*/
//</addition>

public class ASCacheMappingFieldConstraint extends ASCache implements WebCheck {



    public Result check(WebBundleDescriptor descriptor) {


	Result result = getInitializedResult();
	WebComponentNameConstructor compName = new WebComponentNameConstructor(descriptor);

        boolean oneFailed = false;
        boolean notApp = false;
        boolean doneAtleastOnce=false;
         
        Cache cache = getCache(descriptor);

        CacheMapping[] cacheMapp=null;
        String servletName;
        String urlPattern;
        ConstraintField[] fieldConstraints=null;

        String mappingFor=null;

        if (cache != null ){
          cacheMapp=cache.getCacheMapping();
        }


         if (cache != null && cacheMapp !=null && cacheMapp.length !=0 ) {
            for(int rep=0;rep < cacheMapp.length;rep++){

		servletName = cacheMapp[rep].getServletName();
                urlPattern = cacheMapp[rep].getUrlPattern();

                if(servletName !=null)
                    mappingFor=servletName;
                else
                    mappingFor=urlPattern;

                fieldConstraints = cacheMapp[rep].getConstraintField();

                if(fieldConstraints !=null && fieldConstraints.length != 0)
                {
                    for(int rep1=0;rep1 < fieldConstraints.length;rep1++){
                        if(fieldConstraints[rep1] !=null){
                            doneAtleastOnce=true;
                            if(checkFieldConstraint(fieldConstraints[rep1],result,mappingFor,descriptor,compName)){
                                //nothing more required
                            }
                            else{
                                oneFailed = true;
                                result.failed(smh.getLocalString
                                    (getClass().getName() + ".failed",
                                    "FAILED [AS-WEB cache-mapping] List of field-constraint in cache-mapping for [ {0} ] is not proper,  within the web archive/sun-web.xml of [ {1} ].",
                                    new Object[] {mappingFor,descriptor.getName()}));

                            }
                        } 
                    }//end of for(int rep1=0;rep1 < fieldConstraints.length;rep1++)
                } 
                
            }//end of for(int rep=0;rep < cacheMapp.length;rep++)

          }else {
              notApp=true;
          }

        if (oneFailed) {
            result.setStatus(Result.FAILED);
        } else if(notApp || !doneAtleastOnce) {
            result.setStatus(Result.NOT_APPLICABLE);
	    result.notApplicable(smh.getLocalString
                                 (getClass().getName() + ".notApplicable",
                                  "NOT APPLICABLE [AS-WEB cache-mapping ] constraint-field not defined in [ {0} ].",
                                  new Object[] {descriptor.getName()}));



        }else {
            result.setStatus(Result.PASSED);
        } 
	return result;
    }

    

    boolean checkFieldConstraint(ConstraintField fieldCons, Result result,String mappingFor,
                        WebBundleDescriptor descriptor,WebComponentNameConstructor compName) {

          boolean valid=true;
          String fieldName;
          String[] values;
          if (fieldCons != null) {
             fieldName= fieldCons.getAttributeValue("name");
             values=fieldCons.getValue();
             if(fieldName!=null && ! fieldName.equals("")){
                result.passed(smh.getLocalString
				       ("tests.componentNameConstructor",
					"For [ {0} ]",
					new Object[] {compName.toString()}));
                result.passed(smh.getLocalString
					  (getClass().getName() + ".passed1",
					   "PASSED [AS-WEB cache-mapping] Proper field-constraint/fieldName  [ {0} ]  defined for [ {1} ].",
					   new Object[] {fieldName,mappingFor}));

             }else{
                valid=false;
                result.failed(smh.getLocalString
				       ("tests.componentNameConstructor",
					"For [ {0} ]",
					new Object[] {compName.toString()}));
                result.failed(smh.getLocalString
                                      (getClass().getName() + ".failed1",
                                      "FAILED [AS-WEB cache-mapping] field-constraint/fieldName [ {0} ] defined for [ {1} ], attribute can not be empty.",
                                      new Object[] {fieldName,mappingFor}));

             }

             for(int rep=0;values !=null && rep < values.length;rep++){
                if(values[rep]!=null && ! values[rep].equals("")) {
                    result.passed(smh.getLocalString
				       ("tests.componentNameConstructor",
					"For [ {0} ]",
					new Object[] {compName.toString()}));
                    result.passed(smh.getLocalString
					  (getClass().getName() + ".passed2",
					   "PASSED [AS-WEB cache-mapping]Proper field-constraint/value   [ {0} ]  defined for [ {1} ], within the web archive/sun-web.xml of [ {2} ].",
					   new Object[] {values[rep],mappingFor,descriptor.getName()}));

                }else {
                    valid=false;
                    result.failed(smh.getLocalString
				       ("tests.componentNameConstructor",
					"For [ {0} ]",
					new Object[] {compName.toString()}));
                    result.failed(smh.getLocalString
                                      (getClass().getName() + ".failed2",
                                      "FAILED [AS-WEB cache-mapping] field-constraint/value [ {0} ] defined for [ {1} ], can not be empty, within the web archive/sun-web.xml of [ {2} ].",
                                      new Object[] {values[rep],mappingFor,descriptor.getName()}));

                }

             }
          }

          return valid;
    }
     

}

