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   * CaseConstraint is imposed only when a certain condition is met
26   *
27   * <p>For example, if the country attribute value is "USA",
28   * then a prerequisite constraint may be imposed that the 'State' attribute is non-null.</p>
29   *
30   * <p>
31   * This class is a direct copy of one that was in Kuali Student.</p>
32   * 
33   * @author Kuali Rice Team (rice.collab@kuali.org)
34   * @since 1.1
35   */
36  public class CaseConstraint extends BaseConstraint {
37  
38  	protected String propertyName;
39  	protected String operator;
40  	protected boolean caseSensitive;
41  
42      protected List<WhenConstraint> whenConstraint;
43  
44      /**
45       * get the {@code WhenConstraint}'s defined by this case constraint
46       *
47       * @return a list of constraints, null if not initialized
48       */
49  	public List<WhenConstraint> getWhenConstraint() {
50  		return whenConstraint;
51  	}
52  
53      /**
54       * sets the {@code WhenConstraint}'s defined by this case constraint
55       *
56       * @param whenConstraint - the list of constraints
57       */
58  	public void setWhenConstraint(List<WhenConstraint> whenConstraint) {
59  		this.whenConstraint = whenConstraint;
60  	}
61  
62      /**
63       * gets the property name for the attribute to which the case constraint is applied to
64       *
65       * @return the property name
66       */
67  	public String getPropertyName() {
68  		return propertyName;
69  	}
70  
71      /**
72       * setter for property name
73       *
74       * @param propertyName a valid property name
75       */
76  	public void setPropertyName(String propertyName) {
77  		this.propertyName = propertyName;
78  	}
79  
80      /**
81       * specifies the kind of relationship to be checked between the actual value and the ones defined in the {@link #getWhenConstraint()}
82       *
83       * @see org.kuali.rice.krad.uif.UifConstants.CaseConstraintOperators
84       * @return an operator name
85       */
86  	public String getOperator() {
87  		return operator;
88  	}
89  
90      /**
91       * setter for the operator
92       *
93       * @see org.kuali.rice.krad.uif.UifConstants.CaseConstraintOperators
94       * @param operator
95       */
96  	public void setOperator(String operator) {
97  		this.operator = operator;
98  	}
99  
100     /**
101      * checks whether string comparison will be carried out in a case sensitive fashion
102      *
103      * @return true if string comparison is case sensitive, false if not
104      */
105     public boolean isCaseSensitive() {
106         return caseSensitive;
107     }
108 
109     /**
110      * setter for case sensitive
111      *
112      * @param caseSensitive - the case sensitive value to set
113      */
114     public void setCaseSensitive(boolean caseSensitive) {
115         this.caseSensitive = caseSensitive;
116     }
117 
118     /**
119      * Validates different requirements of component compiling a series of reports detailing information on errors
120      * found in the component.  Used by the RiceDictionaryValidator.
121      *
122      * @param tracer Record of component's location
123      * @return A list of ErrorReports detailing errors found within the component and referenced within it
124      */
125     @Override
126     public ArrayList<ErrorReport> completeValidation(TracerToken tracer){
127         ArrayList<ErrorReport> reports=new ArrayList<ErrorReport>();
128         tracer.addBean("CaseConstraint", getMessageKey());
129 
130         if(getWhenConstraint()==null){
131             ErrorReport error = new ErrorReport(ErrorReport.ERROR);
132             error.setValidationFailed("WhenCaseConstraints should at least have 1 item");
133             error.setBeanLocation(tracer.getBeanLocation());
134             error.addCurrentValue("whenCaseConstraint = "+getWhenConstraint());
135             reports.add(error);
136         }else{
137             if(getWhenConstraint().size()==0){
138                 ErrorReport error = new ErrorReport(ErrorReport.ERROR);
139                 error.setValidationFailed("WhenCaseConstraints should at least have 1 item");
140                 error.setBeanLocation(tracer.getBeanLocation());
141                 error.addCurrentValue("whenCaseConstraint.size() = "+getWhenConstraint().size());
142                 reports.add(error);
143             }else{
144                 for(int i=0;i<getWhenConstraint().size();i++){
145                     reports.addAll(getWhenConstraint().get(i).completeValidation(tracer.getCopy()));
146                 }
147             }
148         }
149 
150         reports.addAll(super.completeValidation(tracer.getCopy()));
151 
152         return reports;
153     }
154 }