View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.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.DataType;
22  import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
23  import org.kuali.rice.krad.datadictionary.validation.ValidationUtils.Result;
24  import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
25  import org.kuali.rice.krad.datadictionary.validation.constraint.LengthConstraint;
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  /**
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org) 
33   */
34  public class LengthConstraintProcessor extends MandatoryElementConstraintProcessor<LengthConstraint> {
35  
36  	private static final String CONSTRAINT_NAME = "length constraint";
37  	
38  	/**
39  	 * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#process(DictionaryValidationResult, Object, org.kuali.rice.krad.datadictionary.validation.capability.Validatable, org.kuali.rice.krad.datadictionary.validation.AttributeValueReader)
40  	 */
41  	@Override
42  	public ProcessorResult process(DictionaryValidationResult result, Object value, LengthConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
43  
44  		// To accommodate the needs of other processors, the ConstraintProcessor.process() method returns a list of ConstraintValidationResult objects
45  		// but since a definition that is length constrained only constrains a single field, there is effectively always a single constraint
46  		// being imposed
47  		return new ProcessorResult(processSingleLengthConstraint(result, value, constraint, attributeValueReader));
48  	}
49  
50  	@Override 
51  	public String getName() {
52  		return CONSTRAINT_NAME;
53  	}
54  	
55  	/**
56  	 * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
57  	 */
58  	@Override
59  	public Class<? extends Constraint> getConstraintType() {
60  		return LengthConstraint.class;
61  	}
62  	
63  	protected ConstraintValidationResult processSingleLengthConstraint(DictionaryValidationResult result, Object value, LengthConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
64  		// Can't process any range constraints on null values
65  		if (ValidationUtils.isNullOrEmpty(value))
66  			return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);
67  
68  		DataType dataType = constraint.getDataType();
69  		Object typedValue = value;
70  
71  		if (dataType != null) {
72  			typedValue = ValidationUtils.convertToDataType(value, dataType, dateTimeService);
73  		}	
74  
75  		// The only thing that can have a length constraint currently is a string. 
76  		if (typedValue instanceof String) {
77  			return validateLength(result, (String)typedValue, constraint, attributeValueReader);
78  		} 
79  		
80  		return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);
81  	}
82  	
83  	
84  	protected ConstraintValidationResult validateLength(DictionaryValidationResult result, String value, LengthConstraint constraint, AttributeValueReader attributeValueReader) throws IllegalArgumentException {
85  		Integer valueLength = Integer.valueOf(value.length());
86  		
87  		Integer maxLength = constraint.getMaxLength();
88  		Integer minLength = constraint.getMinLength();
89  		
90  		Result lessThanMax = ValidationUtils.isLessThanOrEqual(valueLength, maxLength);
91  		Result greaterThanMin = ValidationUtils.isGreaterThanOrEqual(valueLength, minLength);
92  		
93          // It's okay for one end of the range to be undefined - that's not an error. It's only an error if one of them is invalid 
94          if (lessThanMax != Result.INVALID && greaterThanMin != Result.INVALID) { 
95          	// Of course, if they're both undefined then we didn't actually have a real constraint
96          	if (lessThanMax == Result.UNDEFINED && greaterThanMin == Result.UNDEFINED)
97          		return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME);
98          	
99          	// In this case, we've succeeded
100         	return result.addSuccess(attributeValueReader, CONSTRAINT_NAME);
101         }
102         
103 		String maxErrorParameter = maxLength != null ? maxLength.toString() : null;
104 		String minErrorParameter = minLength != null ? minLength.toString() : null;
105         
106         // If both comparisons happened then if either comparison failed we can show the end user the expected range on both sides.
107         if (lessThanMax != Result.UNDEFINED && greaterThanMin != Result.UNDEFINED) 
108         	return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_OUT_OF_RANGE, minErrorParameter, maxErrorParameter);
109         // If it's the max comparison that fails, then just tell the end user what the max can be
110         else if (lessThanMax == Result.INVALID)
111         	return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_INCLUSIVE_MAX, maxErrorParameter);
112         // Otherwise, just tell them what the min can be
113         else 
114         	return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_EXCLUSIVE_MIN, minErrorParameter);
115         
116 	}
117 
118 }