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.util.RiceKeyConstants;
019import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
020import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
021import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
022import org.kuali.rice.krad.datadictionary.validation.ValidationUtils.Result;
023import org.kuali.rice.krad.datadictionary.validation.constraint.CollectionSizeConstraint;
024import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
025import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
026import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
027import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
028
029import java.util.Collection;
030
031/**
032 * This class validates attributes that are collection size constrained - ones that can only have between x and y number 
033 * 
034 * @author Kuali Rice Team (rice.collab@kuali.org) 
035 */
036public class CollectionSizeConstraintProcessor implements CollectionConstraintProcessor<Collection<?>, CollectionSizeConstraint> {
037
038        private static final String CONSTRAINT_NAME = "collection size constraint";
039        
040        /**
041         * @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)
042         */
043        @Override
044        public ProcessorResult process(DictionaryValidationResult result, Collection<?> collection, CollectionSizeConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
045                
046                // To accommodate the needs of other processors, the ConstraintProcessor.process() method returns a list of ConstraintValidationResult objects
047                // but since a definition that is collection size constrained only provides a single max and minimum, there is effectively a single constraint
048                // being imposed.
049        return new ProcessorResult(processSingleCollectionSizeConstraint(result, collection, constraint, attributeValueReader));
050        }
051
052        @Override 
053        public String getName() {
054                return CONSTRAINT_NAME;
055        }
056        
057        /**
058         * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
059         */
060        @Override
061        public Class<? extends Constraint> getConstraintType() {
062                return CollectionSizeConstraint.class;
063        }
064
065        /**
066         * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#isOptional()
067         */
068        @Override
069        public boolean isOptional() {
070                return false;
071        }
072
073        protected ConstraintValidationResult processSingleCollectionSizeConstraint(DictionaryValidationResult result, Collection<?> collection, CollectionSizeConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {           
074            Integer sizeOfCollection = new Integer(0);
075            if (collection != null){
076                sizeOfCollection = Integer.valueOf(collection.size());
077            }
078                
079                Integer maxOccurances = constraint.getMaximumNumberOfElements();
080                Integer minOccurances = constraint.getMinimumNumberOfElements();
081                
082                Result lessThanMax = ValidationUtils.isLessThanOrEqual(sizeOfCollection, maxOccurances);
083                Result greaterThanMin = ValidationUtils.isGreaterThanOrEqual(sizeOfCollection, minOccurances);
084
085                // 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 
086        if (lessThanMax != Result.INVALID && greaterThanMin != Result.INVALID) { 
087                // Of course, if they're both undefined then we didn't actually have a real constraint
088                if (lessThanMax == Result.UNDEFINED && greaterThanMin == Result.UNDEFINED)
089                        return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME);
090                
091                // In this case, we've succeeded
092                return result.addSuccess(attributeValueReader, CONSTRAINT_NAME);
093        }
094        
095                String maxErrorParameter = maxOccurances != null ? maxOccurances.toString() : null;
096                String minErrorParameter = minOccurances != null ? minOccurances.toString() : null;
097        
098        // If both comparisons happened then if either comparison failed we can show the end user the expected range on both sides.
099        if (lessThanMax != Result.UNDEFINED && greaterThanMin != Result.UNDEFINED) 
100                return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_QUANTITY_RANGE, minErrorParameter, maxErrorParameter);
101        // If it's the max comparison that fails, then just tell the end user what the max can be
102        else if (lessThanMax == Result.INVALID)
103                return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_MAX_OCCURS, maxErrorParameter);
104        // Otherwise, just tell them what the min can be
105        else 
106                return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_MIN_OCCURS, minErrorParameter);
107        
108        // Obviously the last else above is unnecessary, since anything after it is dead code, but keeping it seems clearer than dropping it
109        }
110        
111}