View Javadoc
1   /*
2    * Copyright 2008 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.ole.sys.document.validation.impl;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.ole.sys.businessobject.AccountingLine;
23  import org.kuali.ole.sys.document.AccountingDocument;
24  import org.kuali.ole.sys.document.validation.Validation;
25  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
26  import org.kuali.rice.krad.util.ObjectUtils;
27  
28  /**
29   * A cleaner way to conglomerate closely related rules together, this validation checks that
30   * all of the values on the accounting line are allowed by a given document.  The advantage of this hutch
31   * over normal composites is that the hutch has "named slots" for given validations, which makes it a bit easier to keep track
32   * of everything.
33   */
34  public class AccountingLineValuesAllowedValidationHutch implements Validation {
35      protected Validation objectCodeAllowedValidation;
36      protected Validation objectTypeAllowedValidation;
37      protected Validation fundGroupAllowedValidation;
38      protected Validation subFundGroupAllowedValidation;
39      protected Validation objectSubTypeAllowedValidation;
40      protected Validation objectLevelAllowedValidation;
41      protected Validation objectConsolidationAllowedValidation;
42      
43      protected String accountingDocumentParameterPropertyName;
44      protected String accountingLineParameterPropertyName;
45      protected AccountingDocument accountingDocumentForValidation;
46      protected AccountingLine accountingLineForValidation;
47      
48      protected boolean quitOnFail;
49      
50      /**
51       * @see org.kuali.ole.sys.document.validation.Validation#shouldQuitOnFail()
52       */
53      public boolean shouldQuitOnFail() {
54          return quitOnFail;
55      }
56      
57      /**
58       * Sets whether the validation hutch should quit on the failure of the entire validation case failing.
59       * @param b
60       */
61      public void setQuitOnFail(boolean b) {
62          quitOnFail = b;
63      }
64  
65      /**
66       * @see org.kuali.ole.sys.document.validation.Validation#stageValidation(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
67       */
68      public boolean stageValidation(AttributedDocumentEvent event) {
69          grabDocumentAndLineForValidationFromEvent(event);
70          updateValidationsWithParameters();
71          return validate(event);
72      }
73      
74      /**
75       * Returns a list of all the validations the hutch has to pass in order to pass as a whole.
76       * @return a List of Validations
77       */
78      protected List<Validation> getValidationGauntlet() {
79          List<Validation> gauntlet = new ArrayList<Validation>();
80          if (objectCodeAllowedValidation != null) {
81              gauntlet.add(objectCodeAllowedValidation);
82          }
83          if (objectTypeAllowedValidation != null) {
84              gauntlet.add(objectTypeAllowedValidation);
85          }
86          if (fundGroupAllowedValidation != null) {
87              gauntlet.add(fundGroupAllowedValidation);
88          }
89          if (subFundGroupAllowedValidation != null) {
90              gauntlet.add(subFundGroupAllowedValidation);
91          }
92          if (objectSubTypeAllowedValidation != null) {
93              gauntlet.add(objectSubTypeAllowedValidation);
94          }
95          if (objectLevelAllowedValidation != null) {
96              gauntlet.add(objectLevelAllowedValidation);
97          }
98          if (objectConsolidationAllowedValidation != null) {
99              gauntlet.add(objectConsolidationAllowedValidation);
100         }
101         return gauntlet;
102     }
103     
104     /**
105      * Using the parameter property names set, finds the accounting document and accounting line to be validate
106      * from the property, like an anteater getting tasty termites from a hill.  Yummy.
107      * @param event the event to take properties from
108      */
109     protected void grabDocumentAndLineForValidationFromEvent(AttributedDocumentEvent event) {
110         if (StringUtils.isNotBlank(accountingDocumentParameterPropertyName)) {
111             accountingDocumentForValidation = (AccountingDocument)ObjectUtils.getPropertyValue(event, accountingDocumentParameterPropertyName);
112         }
113         if (StringUtils.isNotBlank(accountingLineParameterPropertyName)) {
114             accountingLineForValidation = (AccountingLine)ObjectUtils.getPropertyValue(event, accountingLineParameterPropertyName);
115         }
116     }
117     
118     /**
119      * Goes through each of the validations in the hutch, making sure each has the accounting document and accounting line to validate
120      */
121     protected void updateValidationsWithParameters() { 
122         for (Validation v: getValidationGauntlet()) {
123             if (v instanceof AccountingLineValueAllowedValidation) {
124                 addParametersToValidation((AccountingLineValueAllowedValidation)v);
125             } else if (v instanceof CompositeValidation) {
126                 addParametersToValidation((CompositeValidation)v);
127             } else {
128                 throw new IllegalStateException("Validations in the AccountingLineValuesAllowedValidationHutch must either extend AccountingLineValueAllowedValidation or be a CompositeValidation made up of AccountingLineValueAllowedValidation instances");
129             }
130         }
131     }
132     
133     /**
134      * Adds the parameter properties to an instance of the AccountingLinevAlueAllowedValidation
135      * @param validation the validation to add the correct properties to
136      */
137     protected void addParametersToValidation(AccountingLineValueAllowedValidation validation) {
138         validation.setAccountingDocumentForValidation(accountingDocumentForValidation);
139         validation.setAccountingLineForValidation(accountingLineForValidation);
140     }
141     
142     /**
143      * Adds the parameter properties to the children validations of a CompositeValidation
144      * @param validation the validation to add the correct parameters to
145      */
146     protected void addParametersToValidation(CompositeValidation validation) {
147         for (Validation val : validation.getValidations()) {
148             if (val instanceof CompositeValidation) {
149                 addParametersToValidation((CompositeValidation)val);
150             } else if (val instanceof AccountingLineValueAllowedValidation) {
151                 addParametersToValidation((AccountingLineValueAllowedValidation)val);
152             } else {
153                 throw new IllegalStateException("Validations in the AccountingLineValuesAllowedValidationHutch must either extend AccountingLineValueAllowedValidation or be a CompositeValidation made up of AccountingLineValueAllowedValidation instances");
154             }
155         }
156     }
157 
158     /**
159      * 
160      * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
161      */
162     public boolean validate(AttributedDocumentEvent event) {
163         boolean result = true;
164         for (Validation validation : getValidationGauntlet()) {
165             result &= validation.validate(event);
166         }
167         return result;
168     }
169 
170     /**
171      * Gets the fundGroupAllowedValidation attribute. 
172      * @return Returns the fundGroupAllowedValidation.
173      */
174     public Validation getFundGroupAllowedValidation() {
175         return fundGroupAllowedValidation;
176     }
177 
178     /**
179      * Sets the fundGroupAllowedValidation attribute value.
180      * @param fundGroupAllowedValidation The fundGroupAllowedValidation to set.
181      */
182     public void setFundGroupAllowedValidation(Validation fundGroupAllowedValidation) {
183         this.fundGroupAllowedValidation = fundGroupAllowedValidation;
184     }
185 
186     /**
187      * Gets the objectCodeAllowedValidation attribute. 
188      * @return Returns the objectCodeAllowedValidation.
189      */
190     public Validation getObjectCodeAllowedValidation() {
191         return objectCodeAllowedValidation;
192     }
193 
194     /**
195      * Sets the objectCodeAllowedValidation attribute value.
196      * @param objectCodeAllowedValidation The objectCodeAllowedValidation to set.
197      */
198     public void setObjectCodeAllowedValidation(Validation objectCodeAllowedValidation) {
199         this.objectCodeAllowedValidation = objectCodeAllowedValidation;
200     }
201 
202     /**
203      * Gets the objectConsolidationAllowedValidation attribute. 
204      * @return Returns the objectConsolidationAllowedValidation.
205      */
206     public Validation getObjectConsolidationAllowedValidation() {
207         return objectConsolidationAllowedValidation;
208     }
209 
210     /**
211      * Sets the objectConsolidationAllowedValidation attribute value.
212      * @param objectConsolidationAllowedValidation The objectConsolidationAllowedValidation to set.
213      */
214     public void setObjectConsolidationAllowedValidation(Validation objectConsolidationAllowedValidation) {
215         this.objectConsolidationAllowedValidation = objectConsolidationAllowedValidation;
216     }
217 
218     /**
219      * Gets the objectLevelAllowedValidation attribute. 
220      * @return Returns the objectLevelAllowedValidation.
221      */
222     public Validation getObjectLevelAllowedValidation() {
223         return objectLevelAllowedValidation;
224     }
225 
226     /**
227      * Sets the objectLevelAllowedValidation attribute value.
228      * @param objectLevelAllowedValidation The objectLevelAllowedValidation to set.
229      */
230     public void setObjectLevelAllowedValidation(Validation objectLevelAllowedValidation) {
231         this.objectLevelAllowedValidation = objectLevelAllowedValidation;
232     }
233 
234     /**
235      * Gets the objectSubTypeAllowedValidation attribute. 
236      * @return Returns the objectSubTypeAllowedValidation.
237      */
238     public Validation getObjectSubTypeAllowedValidation() {
239         return objectSubTypeAllowedValidation;
240     }
241 
242     /**
243      * Sets the objectSubTypeAllowedValidation attribute value.
244      * @param objectSubTypeAllowedValidation The objectSubTypeAllowedValidation to set.
245      */
246     public void setObjectSubTypeAllowedValidation(Validation objectSubTypeAllowedValidation) {
247         this.objectSubTypeAllowedValidation = objectSubTypeAllowedValidation;
248     }
249 
250     /**
251      * Gets the objectTypeAllowedValidation attribute. 
252      * @return Returns the objectTypeAllowedValidation.
253      */
254     public Validation getObjectTypeAllowedValidation() {
255         return objectTypeAllowedValidation;
256     }
257 
258     /**
259      * Sets the objectTypeAllowedValidation attribute value.
260      * @param objectTypeAllowedValidation The objectTypeAllowedValidation to set.
261      */
262     public void setObjectTypeAllowedValidation(Validation objectTypeAllowedValidation) {
263         this.objectTypeAllowedValidation = objectTypeAllowedValidation;
264     }
265 
266     /**
267      * Gets the subFundGroupAllowedValidation attribute. 
268      * @return Returns the subFundGroupAllowedValidation.
269      */
270     public Validation getSubFundGroupAllowedValidation() {
271         return subFundGroupAllowedValidation;
272     }
273 
274     /**
275      * Sets the subFundGroupAllowedValidation attribute value.
276      * @param subFundGroupAllowedValidation The subFundGroupAllowedValidation to set.
277      */
278     public void setSubFundGroupAllowedValidation(Validation subFundGroupAllowedValidation) {
279         this.subFundGroupAllowedValidation = subFundGroupAllowedValidation;
280     }
281 
282     /**
283      * Gets the accountingDocumentParameterPropertyName attribute. 
284      * @return Returns the accountingDocumentParameterPropertyName.
285      */
286     public String getAccountingDocumentParameterPropertyName() {
287         return accountingDocumentParameterPropertyName;
288     }
289 
290     /**
291      * Sets the accountingDocumentParameterPropertyName attribute value.
292      * @param accountingDocumentParameterPropertyName The accountingDocumentParameterPropertyName to set.
293      */
294     public void setAccountingDocumentParameterPropertyName(String accountingDocumentParameterPropertyName) {
295         this.accountingDocumentParameterPropertyName = accountingDocumentParameterPropertyName;
296     }
297 
298     /**
299      * Gets the accountingLineParameterPropertyName attribute. 
300      * @return Returns the accountingLineParameterPropertyName.
301      */
302     public String getAccountingLineParameterPropertyName() {
303         return accountingLineParameterPropertyName;
304     }
305 
306     /**
307      * Sets the accountingLineParameterPropertyName attribute value.
308      * @param accountingLineParameterPropertyName The accountingLineParameterPropertyName to set.
309      */
310     public void setAccountingLineParameterPropertyName(String accountingLineParameterPropertyName) {
311         this.accountingLineParameterPropertyName = accountingLineParameterPropertyName;
312     }
313 
314     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
315         this.accountingDocumentForValidation = accountingDocumentForValidation;
316     }
317 
318     public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
319         this.accountingLineForValidation = accountingLineForValidation;
320     }
321 
322     public AccountingDocument getAccountingDocumentForValidation() {
323         return accountingDocumentForValidation;
324     }
325 
326     public AccountingLine getAccountingLineForValidation() {
327         return accountingLineForValidation;
328     }
329 }