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.kns.datadictionary.validation.processor;
17  
18  import org.kuali.rice.core.util.RiceKeyConstants;
19  import org.kuali.rice.kns.datadictionary.exception.AttributeValidationException;
20  import org.kuali.rice.kns.datadictionary.validation.AttributeValueReader;
21  import org.kuali.rice.kns.datadictionary.validation.ValidationUtils;
22  import org.kuali.rice.kns.datadictionary.validation.constraint.Constraint;
23  import org.kuali.rice.kns.datadictionary.validation.constraint.ExistenceConstraint;
24  import org.kuali.rice.kns.datadictionary.validation.result.ConstraintValidationResult;
25  import org.kuali.rice.kns.datadictionary.validation.result.DictionaryValidationResult;
26  import org.kuali.rice.kns.datadictionary.validation.result.ProcessorResult;
27  
28  /**
29   * 
30   * @author Kuali Rice Team (rice.collab@kuali.org) 
31   */
32  public class ExistenceConstraintProcessor extends OptionalElementConstraintProcessor<ExistenceConstraint> {
33  
34  	private static final String CONSTRAINT_NAME = "existence constraint";
35  	
36  	/**
37  	 * @see org.kuali.rice.kns.datadictionary.validation.processor.ConstraintProcessor#process(DictionaryValidationResult, Object, org.kuali.rice.kns.datadictionary.validation.Validatable, org.kuali.rice.kns.datadictionary.validation.AttributeValueReader)
38  	 */
39  	@Override
40  	public ProcessorResult process(DictionaryValidationResult result, Object value, ExistenceConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
41  
42  		// To accommodate the needs of other processors, the ConstraintProcessor.process() method returns a list of ConstraintValidationResult objects
43  		// but since a definition that is existence constrained only provides a single isRequired field, there is effectively a single constraint
44  		// being imposed.
45  		return new ProcessorResult(processSingleExistenceConstraint(result, value, constraint, attributeValueReader));
46  	}
47  
48  	@Override 
49  	public String getName() {
50  		return CONSTRAINT_NAME;
51  	}
52  	
53  	/**
54  	 * @see org.kuali.rice.kns.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
55  	 */
56  	@Override
57  	public Class<? extends Constraint> getConstraintType() {
58  		return ExistenceConstraint.class;
59  	}
60  
61  	protected ConstraintValidationResult processSingleExistenceConstraint(DictionaryValidationResult result, Object value, ExistenceConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
62  		// If it's not set, then there's no constraint
63  		if (constraint.isRequired() == null)
64  			return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME);
65  		
66  		if (constraint.isRequired().booleanValue()) {
67  			// If this attribute is required and the value is null then 
68  			if (ValidationUtils.isNullOrEmpty(value)) 
69  				return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_REQUIRED);
70  			
71  			return result.addSuccess(attributeValueReader, CONSTRAINT_NAME);
72  		}
73  
74  		return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);
75  	}
76  	
77  }