View Javadoc
1   /**
2    * Copyright 2005-2015 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.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.util.RiceKeyConstants;
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.ErrorLevel;
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.MustOccurConstraint;
26  import org.kuali.rice.krad.datadictionary.validation.constraint.PrerequisiteConstraint;
27  import org.kuali.rice.krad.datadictionary.validation.result.ConstraintValidationResult;
28  import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
29  import org.kuali.rice.krad.datadictionary.validation.result.ProcessorResult;
30  import org.kuali.rice.krad.uif.UifConstants;
31  
32  import java.util.List;
33  
34  /**
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class MustOccurConstraintProcessor extends BasePrerequisiteConstraintProcessor<MustOccurConstraint> {
38  
39      private static final String CONSTRAINT_NAME = "must occur constraint";
40      private static final String FALLBACK_KEY = "mustoccursFallback";
41  
42      /**
43       * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#process(org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult,
44       *      Object, org.kuali.rice.krad.datadictionary.validation.constraint.Constraint,
45       *      org.kuali.rice.krad.datadictionary.validation.AttributeValueReader)
46       */
47      @Override
48      public ProcessorResult process(DictionaryValidationResult result, Object value, MustOccurConstraint constraint,
49              AttributeValueReader attributeValueReader) throws AttributeValidationException {
50  
51          if (ValidationUtils.isNullOrEmpty(value)) {
52              return new ProcessorResult(result.addSkipped(attributeValueReader, CONSTRAINT_NAME));
53          }
54  
55          ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME);
56          if (StringUtils.isNotBlank(constraint.getMessageKey())) {
57              constraintValidationResult.setConstraintLabelKey(constraint.getMessageKey());
58          } else {
59              constraintValidationResult.setConstraintLabelKey(
60                      UifConstants.Messages.VALIDATION_MSG_KEY_PREFIX + FALLBACK_KEY);
61          }
62  
63          constraintValidationResult.setErrorParameters(constraint.getValidationMessageParamsArray());
64  
65          // If the processing of this constraint is not successful then it's an error
66          if (!processMustOccurConstraint(constraintValidationResult, constraint, attributeValueReader)) {
67              // if attributeName is null, use the entry name since we are processing a must occur constraint that may be referencing multiple attributes
68              if (attributeValueReader.getAttributeName() == null) {
69                  constraintValidationResult.setAttributeName(attributeValueReader.getEntryName());
70              } else {
71                  constraintValidationResult.setAttributeName(attributeValueReader.getAttributeName());
72                  constraintValidationResult.setAttributePath(attributeValueReader.getPath());
73              }
74              constraintValidationResult.setError(RiceKeyConstants.ERROR_OCCURS);
75          }
76  
77          // Store the label key (if one exists) for this constraint on the constraint validation result so it can be shown later
78  
79          // Add it to the DictionaryValidationResult object
80          result.addConstraintValidationResult(attributeValueReader, constraintValidationResult);
81  
82          return new ProcessorResult(constraintValidationResult);
83  
84      }
85  
86      @Override
87      public String getName() {
88          return CONSTRAINT_NAME;
89      }
90  
91      /**
92       * @see org.kuali.rice.krad.datadictionary.validation.processor.ConstraintProcessor#getConstraintType()
93       */
94      @Override
95      public Class<? extends Constraint> getConstraintType() {
96          return MustOccurConstraint.class;
97      }
98  
99      protected boolean processMustOccurConstraint(ConstraintValidationResult topLevelResult,
100             MustOccurConstraint constraint,
101             AttributeValueReader attributeValueReader) throws AttributeValidationException {
102 
103         boolean isSuccessful = false;
104         int trueCount = 0;
105 
106         List<PrerequisiteConstraint> prerequisiteConstraints = constraint.getPrerequisiteConstraints();
107         if (prerequisiteConstraints != null) {
108             for (PrerequisiteConstraint prerequisiteConstraint : prerequisiteConstraints) {
109                 ConstraintValidationResult constraintValidationResult = processPrerequisiteConstraint(
110                         prerequisiteConstraint, attributeValueReader);
111                 constraintValidationResult.setConstraintLabelKey(prerequisiteConstraint.getMessageKey());
112                 constraintValidationResult.setErrorParameters(prerequisiteConstraint.getValidationMessageParamsArray());
113                 // Add the result of each prerequisite constraint validation to the top level result object as a child
114                 topLevelResult.addChild(constraintValidationResult);
115                 trueCount += (constraintValidationResult.getStatus().getLevel() <= ErrorLevel.WARN.getLevel()) ? 1 : 0;
116             }
117         }
118 
119         List<MustOccurConstraint> mustOccurConstraints = constraint.getMustOccurConstraints();
120         if (mustOccurConstraints != null) {
121             for (MustOccurConstraint mustOccurConstraint : mustOccurConstraints) {
122                 // Create a new constraint validation result for this must occur constraint and make it child of the top-level constraint,
123                 // then pass it in to the recursive call so that prerequisite constraints can be placed under it
124                 ConstraintValidationResult constraintValidationResult = new ConstraintValidationResult(CONSTRAINT_NAME);
125                 constraintValidationResult.setConstraintLabelKey(mustOccurConstraint.getMessageKey());
126                 constraintValidationResult.setErrorParameters(mustOccurConstraint.getValidationMessageParamsArray());
127                 topLevelResult.addChild(constraintValidationResult);
128                 trueCount += (processMustOccurConstraint(constraintValidationResult, mustOccurConstraint,
129                         attributeValueReader)) ? 1 : 0;
130             }
131         }
132 
133         int minimum = constraint.getMin() != null ? constraint.getMin().intValue() : 0;
134         int maximum = constraint.getMax() != null ? constraint.getMax().intValue() : 0;
135 
136         isSuccessful = (trueCount >= minimum && trueCount <= maximum) ? true : false;
137 
138         return isSuccessful;
139     }
140 
141 }