View Javadoc

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