001/** 002 * Copyright 2005-2018 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.data.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 * @author Kuali Rice Team (rice.collab@kuali.org) 032 */ 033public class LengthConstraintProcessor extends MandatoryElementConstraintProcessor<LengthConstraint> { 034 035 private static final String MIN_LENGTH_KEY = "validation.minLengthConditional"; 036 private static final String MAX_LENGTH_KEY = "validation.maxLengthConditional"; 037 private static final String RANGE_KEY = "validation.lengthRange"; 038 039 private static final String CONSTRAINT_NAME = "length constraint"; 040 041 /** 042 * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#process(org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult, 043 * Object, org.kuali.rice.krad.datadictionary.validation.constraint.Constraint, 044 * org.kuali.rice.krad.datadictionary.validation.AttributeValueReader) 045 */ 046 @Override 047 public ProcessorResult process(DictionaryValidationResult result, Object value, LengthConstraint constraint, 048 AttributeValueReader attributeValueReader) throws AttributeValidationException { 049 050 // To accommodate the needs of other processors, the ConstraintProcessor.process() method returns a list of ConstraintValidationResult objects 051 // but since a definition that is length constrained only constrains a single field, there is effectively always a single constraint 052 // being imposed 053 return new ProcessorResult(processSingleLengthConstraint(result, value, constraint, attributeValueReader)); 054 } 055 056 @Override 057 public String getName() { 058 return CONSTRAINT_NAME; 059 } 060 061 /** 062 * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#getConstraintType() 063 */ 064 @Override 065 public Class<? extends Constraint> getConstraintType() { 066 return LengthConstraint.class; 067 } 068 069 protected ConstraintValidationResult processSingleLengthConstraint(DictionaryValidationResult result, Object value, 070 LengthConstraint constraint, 071 AttributeValueReader attributeValueReader) throws AttributeValidationException { 072 // Can't process any range constraints on null values 073 if (ValidationUtils.isNullOrEmpty(value)) { 074 return result.addSkipped(attributeValueReader, CONSTRAINT_NAME); 075 } 076 077 DataType dataType = constraint.getDataType(); 078 Object typedValue = value; 079 080 if (dataType != null) { 081 typedValue = ValidationUtils.convertToDataType(value, dataType, dateTimeService); 082 } 083 084 // The only thing that can have a length constraint currently is a string. 085 if (typedValue instanceof String) { 086 return validateLength(result, (String) typedValue, constraint, attributeValueReader); 087 } 088 089 return result.addSkipped(attributeValueReader, CONSTRAINT_NAME); 090 } 091 092 protected ConstraintValidationResult validateLength(DictionaryValidationResult result, String value, 093 LengthConstraint constraint, AttributeValueReader attributeValueReader) throws IllegalArgumentException { 094 Integer valueLength = Integer.valueOf(value.length()); 095 096 Integer maxLength = constraint.getMaxLength(); 097 Integer minLength = constraint.getMinLength(); 098 099 Result lessThanMax = ValidationUtils.isLessThanOrEqual(valueLength, maxLength); 100 Result greaterThanMin = ValidationUtils.isGreaterThanOrEqual(valueLength, minLength); 101 102 // 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 103 if (lessThanMax != Result.INVALID && greaterThanMin != Result.INVALID) { 104 // Of course, if they're both undefined then we didn't actually have a real constraint 105 if (lessThanMax == Result.UNDEFINED && greaterThanMin == Result.UNDEFINED) { 106 return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME); 107 } 108 109 // In this case, we've succeeded 110 return result.addSuccess(attributeValueReader, CONSTRAINT_NAME); 111 } 112 113 String maxErrorParameter = maxLength != null ? maxLength.toString() : null; 114 String minErrorParameter = minLength != null ? minLength.toString() : null; 115 116 // If both comparisons happened then if either comparison failed we can show the end user the expected range on both sides. 117 if (lessThanMax != Result.UNDEFINED && greaterThanMin != Result.UNDEFINED) { 118 return result.addError(RANGE_KEY, attributeValueReader, CONSTRAINT_NAME, 119 RiceKeyConstants.ERROR_OUT_OF_RANGE, minErrorParameter, maxErrorParameter); 120 } 121 // If it's the max comparison that fails, then just tell the end user what the max can be 122 else if (lessThanMax == Result.INVALID) { 123 return result.addError(MAX_LENGTH_KEY, attributeValueReader, CONSTRAINT_NAME, 124 RiceKeyConstants.ERROR_INCLUSIVE_MAX, maxErrorParameter); 125 } 126 // Otherwise, just tell them what the min can be 127 else { 128 return result.addError(MIN_LENGTH_KEY, attributeValueReader, CONSTRAINT_NAME, 129 RiceKeyConstants.ERROR_EXCLUSIVE_MIN, minErrorParameter); 130 } 131 132 } 133 134}