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.constraint;
17  
18  import org.kuali.rice.krad.datadictionary.parse.BeanTag;
19  import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
20  import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /**
26   * {@code WhenConstraint} is a child of a {@link CaseConstraint}
27   *
28   * <p>It provides a specific additional constraint that should be processed when
29   * the condition itself is true.</p>
30   *
31   * <p>So a case constraint on country, might have a when constraint with value='USA', and another with value='Canada'.
32   * Each of these
33   * {@code WhenConstraint}'s would define a constraint of their own that would only be processed when the country was
34   * USA, or when the country
35   * was Canada.</p>
36   *
37   * <p>A {@code WhenConstraint} either specifies an attribute path whose value it then provides or a constraint.
38   * The parent @{CaseConstraint} is defined on the field on which the constraints are desired to take effect.</p>
39   *
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   * @since 1.1
42   */
43  @BeanTag(name = "whenConstraint", parent = "WhenConstraint")
44  public class WhenConstraint implements Constraint {
45      protected List<Object> values;
46      protected String valuePath;
47      protected Constraint constraint;
48  
49      /**
50       * List of values to check for this constraint
51       *
52       * @return a list of values for which to activate the associated constraint
53       */
54      @BeanTagAttribute
55      public List<Object> getValues() {
56          return values;
57      }
58  
59      /**
60       * setter for values
61       *
62       * @param values - the values to set
63       */
64      public void setValues(List<Object> values) {
65          this.values = values;
66      }
67  
68      /**
69       * The value to check for this when constraint.  This is a convenience method that is the first value of the
70       * values array.
71       *
72       * @return the first value checking on, otherwise null
73       */
74      public Object getValue(){
75          if(values != null && !values.isEmpty()){
76              return values.get(0);
77          }
78          else{
79              return null;
80          }
81      }
82  
83      /**
84       * Sets a single value to check for this constraint.  This is a convenience method.
85       *
86       * @param value - a values for which to activate the associated constraint
87       */
88      public void setValue(Object value) {
89          values = new ArrayList<Object>();
90          values.add(value);
91      }
92  
93      /**
94       * Path that can retrieve an attributes value
95       *
96       * @return a string representation of specifically which attribute (at some depth) is being accessed
97       */
98      @BeanTagAttribute(name = "valuePath")
99      public String getValuePath() {
100         return valuePath;
101     }
102 
103     /**
104      * setter for the value path
105      *
106      * @param valuePath - the value path to set
107      */
108     public void setValuePath(String valuePath) {
109         this.valuePath = valuePath;
110     }
111 
112     /**
113      * The constraint to apply to the field when the {@code WhenConstraint} value/values match
114      *
115      * @return the constraint
116      */
117     @BeanTagAttribute(name = "constraint", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
118     public Constraint getConstraint() {
119         return constraint;
120     }
121 
122     /**
123      * setter for constraint
124      *
125      * @param constraint - the constraint to set
126      */
127     public void setConstraint(Constraint constraint) {
128         this.constraint = constraint;
129     }
130 
131     /**
132      * Validates different requirements of component compiling a series of reports detailing information on errors
133      * found in the component.  Used by the RiceDictionaryValidator.
134      *
135      * @param tracer Record of component's location
136      */
137     public void completeValidation(ValidationTrace tracer) {
138         tracer.addBean("WhenConstraint", ValidationTrace.NO_BEAN_ID);
139 
140         if (getConstraint() == null) {
141             String currentValues[] = {"constraint = " + getConstraint()};
142             tracer.createWarning("Constraint must be set", currentValues);
143         }
144 
145         if (getValuePath() == null || getValues() == null) {
146             String currentValues[] = {"valuePath = " + getValuePath(), "values = " + getValues()};
147             tracer.createWarning("Value Path or Values must be set", currentValues);
148         }
149     }
150 }