View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.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/ecl2.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.krad.datadictionary.validation.processor;
17  
18  import org.kuali.rice.core.api.util.RiceKeyConstants;
19  import org.kuali.rice.krad.datadictionary.DataObjectEntry;
20  import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
21  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
22  import org.kuali.rice.krad.datadictionary.validation.DictionaryObjectAttributeValueReader;
23  import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
24  import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
25  import org.kuali.rice.krad.datadictionary.validation.constraint.ExistenceConstraint;
26  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
27  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
28  import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
29  
30  /**
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org) 
33   */
34  public class ExistenceConstraintProcessor extends OptionalElementConstraintProcessor<ExistenceConstraint> {
35  
36  	private static final String CONSTRAINT_NAME = "existence constraint";
37  	
38  	/**
39  	 * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#process(org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult, Object, org.kuali.rice.krad.datadictionary.validation.constraint.Constraint, org.kuali.rice.krad.datadictionary.validation.AttributeValueReader) \
40  	 */
41  	@Override
42  	public ProcessorResult process(DictionaryValidationResult result, Object value, ExistenceConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
43  
44  		// To accommodate the needs of other processors, the ConstraintProcessor.process() method returns a list of ConstraintValidationResult objects
45  		// but since a definition that is existence constrained only provides a single isRequired field, there is effectively a single constraint
46  		// being imposed.
47  		return new ProcessorResult(processSingleExistenceConstraint(result, value, constraint, attributeValueReader));
48  	}
49  
50  	@Override 
51  	public String getName() {
52  		return CONSTRAINT_NAME;
53  	}
54  	
55  	/**
56  	 * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
57  	 */
58  	@Override
59  	public Class<? extends Constraint> getConstraintType() {
60  		return ExistenceConstraint.class;
61  	}
62  
63  	protected ConstraintValidationResult processSingleExistenceConstraint(DictionaryValidationResult result, Object value, ExistenceConstraint constraint, AttributeValueReader attributeValueReader) throws AttributeValidationException {
64  		// If it's not set, then there's no constraint
65  		if (constraint.isRequired() == null)
66  			return result.addNoConstraint(attributeValueReader, CONSTRAINT_NAME);
67  		
68  		if (constraint.isRequired().booleanValue() && !skipConstraint(attributeValueReader)) {
69  			// If this attribute is required and the value is null then 
70  			if (ValidationUtils.isNullOrEmpty(value)) 
71  				return result.addError(attributeValueReader, CONSTRAINT_NAME, RiceKeyConstants.ERROR_REQUIRED, attributeValueReader.getLabel(attributeValueReader.getAttributeName()));
72   			return result.addSuccess(attributeValueReader, CONSTRAINT_NAME);
73  		}
74  
75  		return result.addSkipped(attributeValueReader, CONSTRAINT_NAME);
76  	}
77  
78      /**
79       * Checks to see if existence constraint should be skipped.  Required constraint should be skipped if it is an attribute of a complex
80       * attribute and the complex attribute is not required.
81       * 
82       * @param attributeValueReader
83       * @return
84       */
85      private boolean skipConstraint(AttributeValueReader attributeValueReader) {
86          boolean skipConstraint = false;
87          if (attributeValueReader instanceof DictionaryObjectAttributeValueReader){
88              DictionaryObjectAttributeValueReader dictionaryValueReader = (DictionaryObjectAttributeValueReader)attributeValueReader;
89              skipConstraint = dictionaryValueReader.isNestedAttribute() && dictionaryValueReader.isParentAttributeNull();
90          }
91          return skipConstraint;
92      }
93  	
94  	
95  }