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.List;
23  
24  /**
25   * Must occur constraints are constraints that indicate some range of acceptable valid results. So a must occur
26   * constraint
27   * might indicate that between 1 and 3 prequisite constraints must be valid. For example, on a person object, it might
28   * be
29   * that one of three fields must be filled in:
30   *
31   * 1. username
32   * 2. email
33   * 3. phone number
34   *
35   * By imposing a must occur constraint on the person object iself, and setting three prequisite constraints below it,
36   * with a min of 1
37   * and a max of 3, this requirement can be enforced.
38   *
39   * A more complicated example might be that a US address is only valid if it provides either:
40   * (a) a city and state, or
41   * (b) a postal code
42   *
43   * To enforce this, a single must occur constraint would have two children: (1) a prequisite constraint on postal code,
44   * and (2) a must occur constraint
45   * with two child prequisite constraints, on city and state, respectively. By setting min=1/max=2 at the top must occur
46   * constraint,
47   * and min=2/max=2 at the leaf constraint, this requirement can be enforced.
48   *
49   * @author Kuali Rice Team (rice.collab@kuali.org)
50   * @since 1.1
51   */
52  @BeanTag(name = "mustOccurConstraint-bean", parent = "MustOccurConstraint")
53  public class MustOccurConstraint extends BaseConstraint {
54  
55      private List<PrerequisiteConstraint> prerequisiteConstraints;
56      private List<MustOccurConstraint> mustOccurConstraints;
57      private Integer min;
58      private Integer max;
59  
60      @BeanTagAttribute(name = "prerequisiteConstraints", type = BeanTagAttribute.AttributeType.LISTBEAN)
61      public List<PrerequisiteConstraint> getPrerequisiteConstraints() {
62          return prerequisiteConstraints;
63      }
64  
65      public void setPrerequisiteConstraints(List<PrerequisiteConstraint> prerequisiteConstraints) {
66          this.prerequisiteConstraints = prerequisiteConstraints;
67      }
68  
69      @BeanTagAttribute(name = "mustOccurConstraints", type = BeanTagAttribute.AttributeType.LISTBEAN)
70      public List<MustOccurConstraint> getMustOccurConstraints() {
71          return mustOccurConstraints;
72      }
73  
74      public void setMustOccurConstraints(List<MustOccurConstraint> occurs) {
75          this.mustOccurConstraints = occurs;
76      }
77  
78      @BeanTagAttribute(name = "min")
79      public Integer getMin() {
80          return min;
81      }
82  
83      public void setMin(Integer min) {
84          this.min = min;
85      }
86  
87      @BeanTagAttribute(name = "max")
88      public Integer getMax() {
89          return max;
90      }
91  
92      public void setMax(Integer max) {
93          this.max = max;
94      }
95  
96      /**
97       * Validates different requirements of component compiling a series of reports detailing information on errors
98       * found in the component.  Used by the RiceDictionaryValidator.
99       *
100      * @param tracer Record of component's location
101      */
102     @Override
103     public void completeValidation(ValidationTrace tracer) {
104         tracer.addBean("MustOccurConstraint", getMessageKey());
105 
106         if (getMax() <= 0) {
107             String currentValues[] = {"max =" + getMax()};
108             tracer.createWarning("Max must be greater than 0", currentValues);
109         }
110 
111         if (getPrerequisiteConstraints() == null) {
112             String currentValues[] = {"prerequisiteConstraints =" + getPrerequisiteConstraints()};
113             tracer.createWarning("PrerequisiteConstraints cannot be null or empty", currentValues);
114         } else if (getPrerequisiteConstraints().size() == 0) {
115             String currentValues[] = {"prerequisiteConstraints.size =" + getPrerequisiteConstraints().size()};
116             tracer.createWarning("PrerequisiteConstraints cannot be null or empty", currentValues);
117             ;
118         } else {
119             for (int i = 0; i < getPrerequisiteConstraints().size(); i++) {
120                 getPrerequisiteConstraints().get(i).completeValidation(tracer.getCopy());
121             }
122         }
123 
124         if (getMustOccurConstraints() == null) {
125             String currentValues[] = {"mustOccurConstraints =" + getMustOccurConstraints()};
126             tracer.createWarning("MustOccurConstraints cannot be null or empty", currentValues);
127         } else if (getMustOccurConstraints().size() == 0) {
128             String currentValues[] = {"mustOccurConstraints.size =" + getMustOccurConstraints().size()};
129             tracer.createWarning("MustOccurConstraints cannot be null or empty", currentValues);
130         } else {
131             for (int i = 0; i < getMustOccurConstraints().size(); i++) {
132                 getMustOccurConstraints().get(i).completeValidation(tracer.getCopy());
133             }
134         }
135 
136         super.completeValidation(tracer.getCopy());
137     }
138 }