View Javadoc

1   /**
2    * Copyright 2005-2014 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.exception.AttributeValidationException;
20  import org.kuali.rice.krad.datadictionary.validation.AttributeValueReader;
21  import org.kuali.rice.krad.datadictionary.validation.ErrorLevel;
22  import org.kuali.rice.krad.datadictionary.validation.ValidationUtils;
23  import org.kuali.rice.krad.datadictionary.validation.constraint.Constraint;
24  import org.kuali.rice.krad.datadictionary.validation.constraint.PrerequisiteConstraint;
25  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
26  
27  import java.util.Collection;
28  
29  /**
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public abstract class BasePrerequisiteConstraintProcessor<C extends Constraint> extends MandatoryElementConstraintProcessor<C> {
33  
34      protected ConstraintValidationResult processPrerequisiteConstraint(PrerequisiteConstraint constraint,
35              AttributeValueReader attributeValueReader) throws AttributeValidationException {
36          ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(getName());
37  
38          if (constraint == null) {
39              constraintValidationResult.setStatus(ErrorLevel.NOCONSTRAINT);
40              return constraintValidationResult;
41          }
42  
43          // TODO: Does this code need to be able to look at more than just the other immediate members of the object?
44          String attributeName = constraint.getPropertyName();
45  
46          if (ValidationUtils.isNullOrEmpty(attributeName)) {
47              throw new AttributeValidationException(
48                      "Prerequisite constraints must include the name of the attribute that is required");
49          }
50  
51          Object value = attributeValueReader.getValue(attributeName);
52  
53          boolean isSuccessful = true;
54  
55          if (value instanceof java.lang.String) {
56              isSuccessful = ValidationUtils.hasText((String) value);
57          } else if (value instanceof Collection) {
58              isSuccessful = (((Collection<?>) value).size() > 0);
59          } else {
60              isSuccessful = (null != value) ? true : false;
61          }
62  
63          if (!isSuccessful) {
64              String label = attributeValueReader.getLabel(attributeName);
65              if (label != null) {
66                  attributeName = label;
67              }
68  
69              constraintValidationResult.setError(RiceKeyConstants.ERROR_REQUIRES_FIELD, attributeName);
70              constraintValidationResult.setConstraintLabelKey(constraint.getMessageKey());
71              constraintValidationResult.setErrorParameters(constraint.getValidationMessageParamsArray());
72          }
73  
74          return constraintValidationResult;
75      }
76  
77  }