View Javadoc

1   /**
2    * Copyright 2005-2013 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       * gets the list of values defined for this constraint
51       *
52       * @return a list of values for which to activate the associated constraint
53       */
54      @BeanTagAttribute(name = "values", type = BeanTagAttribute.AttributeType.LISTBEAN)
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       * gets the value defined for this constraint
70       *
71       * @param value - a values for which to activate the associated constraint
72       */
73      public void setValue(Object value) {
74          values = new ArrayList<Object>();
75          values.add(value);
76      }
77  
78      /**
79       * gets a path that can retrieve an attributes value
80       *
81       * @return a string representation of specifically which attribute (at some depth) is being accessed
82       */
83      @BeanTagAttribute(name = "valuePath")
84      public String getValuePath() {
85          return valuePath;
86      }
87  
88      /**
89       * setter for the value path
90       *
91       * @param valuePath - the value path to set
92       */
93      public void setValuePath(String valuePath) {
94          this.valuePath = valuePath;
95      }
96  
97      /**
98       * gets the constraint to apply to the field when the {@code WhenConstraint} value/values match
99       *
100      * @return
101      */
102     @BeanTagAttribute(name = "constraint", type = BeanTagAttribute.AttributeType.SINGLEBEAN)
103     public Constraint getConstraint() {
104         return constraint;
105     }
106 
107     /**
108      * setter for constraint
109      *
110      * @param constraint - the constraint to set
111      */
112     public void setConstraint(Constraint constraint) {
113         this.constraint = constraint;
114     }
115 
116     /**
117      * Validates different requirements of component compiling a series of reports detailing information on errors
118      * found in the component.  Used by the RiceDictionaryValidator.
119      *
120      * @param tracer Record of component's location
121      */
122     public void completeValidation(ValidationTrace tracer) {
123         tracer.addBean("WhenConstraint", ValidationTrace.NO_BEAN_ID);
124 
125         if (getConstraint() == null) {
126             String currentValues[] = {"constraint = " + getConstraint()};
127             tracer.createWarning("Constraint must be set", currentValues);
128         }
129 
130         if (getValuePath() == null || getValues() == null) {
131             String currentValues[] = {"valuePath = " + getValuePath(), "values = " + getValues()};
132             tracer.createWarning("Value Path or Values must be set", currentValues);
133         }
134     }
135 }