001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.datadictionary.validation.processor;
017
018import org.kuali.rice.core.api.uif.DataType;
019import org.kuali.rice.core.api.util.RiceKeyConstants;
020import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
021import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
022import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
023import org.kuali.rice.krad.datadictionary.validation.ValidationUtils.Result;
024import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
025import org.kuali.rice.krad.datadictionary.validation.constraint.LengthConstraint;
026import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
027import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
028import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
029
030/**
031 * 
032 * @author Kuali Rice Team (rice.collab@kuali.org) 
033 */
034public class LengthConstraintProcessor extends MandatoryElementConstraintProcessor<LengthConstraint> {
035
036    private static final String MIN_LENGTH_KEY = "validation.minLengthConditional";
037    private static final String MAX_LENGTH_KEY = "validation.maxLengthConditional";
038    private static final String RANGE_KEY = "validation.lengthRange";
039
040        private static final String CONSTRAINT_NAME = "length constraint";
041        
042        /**
043         * @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)
044         */
045        @Override
046        public ProcessorResult process(DictionaryValidationResult result, Object value, LengthConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
047
048                // To accommodate the needs of other processors, the ConstraintProcessor.process() method returns a list of ConstraintValidationResult objects
049                // but since a definition that is length constrained only constrains a single field, there is effectively always a single constraint
050                // being imposed
051                return new ProcessorResult(processSingleLengthConstraint(result, value, constraint, attributeValueReader));
052        }
053
054        @Override 
055        public String getName() {
056                return CONSTRAINT_NAME;
057        }
058        
059        /**
060         * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
061         */
062        @Override
063        public Class<? extends Constraint> getConstraintType() {
064                return LengthConstraint.class;
065        }
066        
067        protected ConstraintValidationResult processSingleLengthConstraint(DictionaryValidationResult result, Object value, LengthConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
068                // Can't process any range constraints on null values
069                if (ValidationUtils.isNullOrEmpty(value))
070                        return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);
071
072                DataType dataType = constraint.getDataType();
073                Object typedValue = value;
074
075                if (dataType != null) {
076                        typedValue = ValidationUtils.convertToDataType(value, dataType, dateTimeService);
077                }       
078
079                // The only thing that can have a length constraint currently is a string. 
080                if (typedValue instanceof String) {
081                        return validateLength(result, (String)typedValue, constraint, attributeValueReader);
082                } 
083                
084                return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);
085        }
086        
087        
088        protected ConstraintValidationResult validateLength(DictionaryValidationResult result, String value, LengthConstraint constraint, AttributeValueReader attributeValueReader) throws IllegalArgumentException {
089                Integer valueLength = Integer.valueOf(value.length());
090                
091                Integer maxLength = constraint.getMaxLength();
092                Integer minLength = constraint.getMinLength();
093                
094                Result lessThanMax = ValidationUtils.isLessThanOrEqual(valueLength, maxLength);
095                Result greaterThanMin = ValidationUtils.isGreaterThanOrEqual(valueLength, minLength);
096                
097        // 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 
098        if (lessThanMax != Result.INVALID && greaterThanMin != Result.INVALID) { 
099                // Of course, if they're both undefined then we didn't actually have a real constraint
100                if (lessThanMax == Result.UNDEFINED && greaterThanMin == Result.UNDEFINED)
101                        return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME);
102                
103                // In this case, we've succeeded
104                return result.addSuccess(attributeValueReader, CONSTRAINT_NAME);
105        }
106        
107                String maxErrorParameter = maxLength != null ? maxLength.toString() : null;
108                String minErrorParameter = minLength != null ? minLength.toString() : null;
109        
110        // If both comparisons happened then if either comparison failed we can show the end user the expected range on both sides.
111        if (lessThanMax != Result.UNDEFINED && greaterThanMin != Result.UNDEFINED) 
112                return result.addError(RANGE_KEY, attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_OUT_OF_RANGE, minErrorParameter, maxErrorParameter);
113        // If it's the max comparison that fails, then just tell the end user what the max can be
114        else if (lessThanMax == Result.INVALID)
115                return result.addError(MAX_LENGTH_KEY, attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_INCLUSIVE_MAX, maxErrorParameter);
116        // Otherwise, just tell them what the min can be
117        else 
118                return result.addError(MIN_LENGTH_KEY, attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_EXCLUSIVE_MIN, minErrorParameter);
119        
120        }
121
122}