View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.datadictionary.validation.processor;
17  
18  import org.kuali.rice.core.api.util.RiceKeyConstants;
19  import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
20  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
21  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
22  import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
23  import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
24  import org.kuali.rice.krad.datadictionary.validation.constraint.MustOccurConstraint;
25  import org.kuali.rice.krad.datadictionary.validation.constraint.PrerequisiteConstraint;
26  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
27  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
28  import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
29  
30  import java.util.List;
31  
32  /**
33   * 
34   * @author Kuali Rice Team (rice.collab@kuali.org) 
35   */
36  public class MustOccurConstraintProcessor extends BasePrerequisiteConstraintProcessor<MustOccurConstraint> {
37  
38  	private static final String CONSTRAINT_NAME = "must occur constraint";
39  	
40  	/**
41  	 * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#process(org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult, Object, org.kuali.rice.krad.datadictionary.validation.constraint.Constraint, org.kuali.rice.krad.datadictionary.validation.AttributeValueReader)
42       */
43  	@Override
44  	public ProcessorResult process(DictionaryValidationResult result,
45  			Object value, MustOccurConstraint constraint, AttributeValueReader attributeValueReader)
46  			throws AttributeValidationException {
47  
48  		if (ValidationUtils.isNullOrEmpty(value))
49  			return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME));
50  		
51  
52  		ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME);
53          constraintValidationResult.setConstraintLabelKey(constraint.getLabelKey());
54          constraintValidationResult.setErrorParameters(constraint.getValidationMessageParamsArray());
55  
56          // If the processing of this constraint is not successful then it's an error
57  		if (!processMustOccurConstraint(constraintValidationResult, constraint, attributeValueReader)) {
58  			// if attributeName is null, use the entry name since we are processing a must occur constraint that may be referencing multiple attributes
59  		    if (attributeValueReader.getAttributeName() == null){
60  		        constraintValidationResult.setAttributeName(attributeValueReader.getEntryName());
61  		    } else{
62  		        constraintValidationResult.setAttributeName(attributeValueReader.getAttributeName());
63  		        constraintValidationResult.setAttributePath(attributeValueReader.getPath());
64  		    }
65  			constraintValidationResult.setError(RiceKeyConstants.ERROR_OCCURS);
66  		} 
67  
68  		// Store the label key (if one exists) for this constraint on the constraint validation result so it can be shown later
69  
70  		// Add it to the DictionaryValidationResult object
71  		result.addConstraintValidationResult(attributeValueReader, constraintValidationResult);
72  
73  		return new ProcessorResult(constraintValidationResult);
74  
75  	}
76  	
77  	@Override 
78  	public String getName() {
79  		return CONSTRAINT_NAME;
80  	}
81  
82  	/**
83  	 * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
84  	 */
85  	@Override
86  	public Class<? extends Constraint> getConstraintType() {
87  		return MustOccurConstraint.class;
88  	}
89  	
90      protected boolean processMustOccurConstraint(ConstraintValidationResult topLevelResult, MustOccurConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
91  
92          boolean isSuccessful = false;
93          int trueCount = 0;
94          
95          List<PrerequisiteConstraint> prerequisiteConstraints = constraint.getPrerequisiteConstraints();
96          if (prerequisiteConstraints != null) {
97  	        for (PrerequisiteConstraint prerequisiteConstraint : prerequisiteConstraints) {
98  	        	ConstraintValidationResult constraintValidationResult = processPrerequisiteConstraint(prerequisiteConstraint, attributeValueReader);
99                  constraintValidationResult.setConstraintLabelKey(prerequisiteConstraint.getLabelKey());
100                 constraintValidationResult.setErrorParameters(prerequisiteConstraint.getValidationMessageParamsArray());
101 	        	// Add the result of each prerequisite constraint validation to the top level result object as a child
102 	        	topLevelResult.addChild(constraintValidationResult);
103 	            trueCount += (constraintValidationResult.getStatus().getLevel() <= ErrorLevel.WARN.getLevel()) ? 1 : 0;
104 	        }
105         }
106 
107         List<MustOccurConstraint> mustOccurConstraints = constraint.getMustOccurConstraints();
108         if (mustOccurConstraints != null) {
109 	        for (MustOccurConstraint mustOccurConstraint : mustOccurConstraints) {
110 	        	// Create a new constraint validation result for this must occur constraint and make it child of the top-level constraint, 
111 	        	// then pass it in to the recursive call so that prerequisite constraints can be placed under it
112 	        	ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME);
113                 constraintValidationResult.setConstraintLabelKey(mustOccurConstraint.getLabelKey());
114                 constraintValidationResult.setErrorParameters(mustOccurConstraint.getValidationMessageParamsArray());
115 	        	topLevelResult.addChild(constraintValidationResult);
116 	            trueCount += (processMustOccurConstraint(constraintValidationResult, mustOccurConstraint, attributeValueReader)) ? 1 : 0;
117 	        }
118         }
119 
120         int minimum = constraint.getMin() != null ? constraint.getMin().intValue() : 0;
121         int maximum = constraint.getMax() != null ? constraint.getMax().intValue() : 0;
122         
123         isSuccessful = (trueCount >= minimum && trueCount <= maximum) ? true : false;
124 
125         return isSuccessful;
126     }
127 
128 }