| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kns.datadictionary.validation.processor; |
| 17 | |
|
| 18 | |
import java.util.Collection; |
| 19 | |
|
| 20 | |
import org.kuali.rice.core.util.RiceKeyConstants; |
| 21 | |
import org.kuali.rice.kns.datadictionary.exception.AttributeValidationException; |
| 22 | |
import org.kuali.rice.kns.datadictionary.validation.AttributeValueReader; |
| 23 | |
import org.kuali.rice.kns.datadictionary.validation.ValidationUtils; |
| 24 | |
import org.kuali.rice.kns.datadictionary.validation.ValidationUtils.Result; |
| 25 | |
import org.kuali.rice.kns.datadictionary.validation.constraint.CollectionSizeConstraint; |
| 26 | |
import org.kuali.rice.kns.datadictionary.validation.constraint.Constraint; |
| 27 | |
import org.kuali.rice.kns.datadictionary.validation.result.ConstraintValidationResult; |
| 28 | |
import org.kuali.rice.kns.datadictionary.validation.result.DictionaryValidationResult; |
| 29 | |
import org.kuali.rice.kns.datadictionary.validation.result.ProcessorResult; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 0 | public class CollectionSizeConstraintProcessor implements CollectionConstraintProcessor<Collection<?>, CollectionSizeConstraint> { |
| 37 | |
|
| 38 | |
private static final String CONSTRAINT_NAME = "collection size constraint"; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
@Override |
| 44 | |
public ProcessorResult process(DictionaryValidationResult result, Collection<?> collection, CollectionSizeConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException { |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 0 | return new ProcessorResult(processSingleCollectionSizeConstraint(result, collection, constraint, attributeValueReader)); |
| 50 | |
} |
| 51 | |
|
| 52 | |
@Override |
| 53 | |
public String getName() { |
| 54 | 0 | return CONSTRAINT_NAME; |
| 55 | |
} |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
@Override |
| 61 | |
public Class<? extends Constraint> getConstraintType() { |
| 62 | 0 | return CollectionSizeConstraint.class; |
| 63 | |
} |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
@Override |
| 69 | |
public boolean isOptional() { |
| 70 | 0 | return false; |
| 71 | |
} |
| 72 | |
|
| 73 | |
protected ConstraintValidationResult processSingleCollectionSizeConstraint(DictionaryValidationResult result, Collection<?> collection, CollectionSizeConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException { |
| 74 | 0 | Integer sizeOfCollection = Integer.valueOf(collection.size()); |
| 75 | |
|
| 76 | 0 | Integer maxOccurances = constraint.getMaximumNumberOfElements(); |
| 77 | 0 | Integer minOccurances = constraint.getMinimumNumberOfElements(); |
| 78 | |
|
| 79 | 0 | Result lessThanMax = ValidationUtils.isLessThanOrEqual(sizeOfCollection, maxOccurances); |
| 80 | 0 | Result greaterThanMin = ValidationUtils.isGreaterThanOrEqual(sizeOfCollection, minOccurances); |
| 81 | |
|
| 82 | |
|
| 83 | 0 | if (lessThanMax != Result.INVALID && greaterThanMin != Result.INVALID) { |
| 84 | |
|
| 85 | 0 | if (lessThanMax == Result.UNDEFINED && greaterThanMin == Result.UNDEFINED) |
| 86 | 0 | return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME); |
| 87 | |
|
| 88 | |
|
| 89 | 0 | return result.addSuccess(attributeValueReader, CONSTRAINT_NAME); |
| 90 | |
} |
| 91 | |
|
| 92 | 0 | String maxErrorParameter = maxOccurances != null ? maxOccurances.toString() : null; |
| 93 | 0 | String minErrorParameter = minOccurances != null ? minOccurances.toString() : null; |
| 94 | |
|
| 95 | |
|
| 96 | 0 | if (lessThanMax != Result.UNDEFINED && greaterThanMin != Result.UNDEFINED) |
| 97 | 0 | return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_QUANTITY_RANGE, minErrorParameter, maxErrorParameter); |
| 98 | |
|
| 99 | 0 | else if (lessThanMax == Result.INVALID) |
| 100 | 0 | return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_MAX_OCCURS, maxErrorParameter); |
| 101 | |
|
| 102 | |
else |
| 103 | 0 | return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_MIN_OCCURS, minErrorParameter); |
| 104 | |
|
| 105 | |
|
| 106 | |
} |
| 107 | |
|
| 108 | |
} |