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.lang.reflect.InvocationTargetException;
19  import java.lang.reflect.Method;
20  import java.util.List;
21  
22  import org.kuali.ole.sys.document.AccountingDocument;
23  import org.kuali.ole.sys.document.validation.GenericValidation;
24  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  import org.kuali.rice.krad.util.ObjectUtils;
27  
28  /**
29   * GenericValidation to check if the required number of accounting lines in a given accounting line group has been met
30   */
31  public class RequiredAccountingLinesCountValidation extends GenericValidation {
32      private String accountingLineGroupName;
33      private int minimumNumber;
34      protected String accountingLineGroupPropertyName;
35      protected String errorMessageName;
36      private AccountingDocument accountingDocumentForValidation;
37      
38      private static final String ACCOUNTING_LINES_GROUP_PROPERTY_SUFFIX = "AccountingLines";
39  
40      /**
41       * Checks that the number of accounting lines in the accounting line group (named by the accountingLineGroupPropertyName property)
42       * is greater than the set minimum number of accounting lines.
43       * <strong>This validation expects the document to be sent in as a property.</strong>
44       * @see org.kuali.ole.sys.document.validation.GenericValidation#validate(java.lang.Object[])
45       */
46      public boolean validate(AttributedDocumentEvent event) {
47          List accountingLineGroup = (List)ObjectUtils.getPropertyValue(accountingDocumentForValidation, accountingLineGroupPropertyName);
48          if (accountingLineGroup.size() < minimumNumber) {
49              GlobalVariables.getMessageMap().putError(accountingLineGroupPropertyName, errorMessageName, new String[] { discoverGroupTitle(accountingDocumentForValidation) });
50              return false;
51          }
52          return true;
53      }
54      
55      /**
56       * Returns the title of the given accounting line group on the document
57       * @return an accounting line group title
58       */
59      protected String discoverGroupTitle(AccountingDocument document) {
60          String title = accountingLineGroupName;
61          Method groupTitleMethod = discoverGroupTitleMethod(document);
62          if (groupTitleMethod != null) {
63              try {
64                  title = (String)groupTitleMethod.invoke(document, new Object[0]);
65              }
66              catch (IllegalAccessException iae) {
67                  throw new RuntimeException(iae);
68              }
69              catch (InvocationTargetException ite) {
70                  throw new RuntimeException(ite);
71              }
72          }
73          return title;
74      }
75      
76      /**
77       * Looks up what should be the method on the AccountingDocument class that returns the group title
78       * @return
79       */
80      protected Method discoverGroupTitleMethod(AccountingDocument document) {
81          Method groupTitleMethod = null;
82          try {
83              String methodName = new StringBuilder().append("get").append(accountingLineGroupPropertyName.substring(0, 1).toUpperCase()).append(accountingLineGroupPropertyName.substring(1)).append("SectionTitle").toString();
84              groupTitleMethod = document.getClass().getMethod(methodName, new Class[0]);
85          }
86          catch (SecurityException se) {
87              throw new RuntimeException(se);
88          }
89          catch (NoSuchMethodException nsme) {
90              throw new RuntimeException(nsme);
91          }
92          
93          return groupTitleMethod;
94      }
95      
96      /**
97       * Gets the accountingLineGroupName attribute. 
98       * @return Returns the accountingLineGroupName.
99       */
100     public String getAccountingLineGroupName() {
101         return accountingLineGroupName;
102     }
103 
104     /**
105      * Sets the accountingLineGroupName attribute value.
106      * @param accountingLineGroupName The accountingLineGroupName to set.
107      */
108     public void setAccountingLineGroupName(String accountingLineGroupName) {
109         this.accountingLineGroupName = accountingLineGroupName;
110         this.accountingLineGroupPropertyName = new StringBuilder().append(this.accountingLineGroupName).append(RequiredAccountingLinesCountValidation.ACCOUNTING_LINES_GROUP_PROPERTY_SUFFIX).toString();
111         this.errorMessageName = new StringBuilder().append("error.document.").append(accountingLineGroupName).append("SectionNoAccountingLines").toString();
112     }
113 
114     /**
115      * Gets the minimumNumber attribute. 
116      * @return Returns the minimumNumber.
117      */
118     public int getMinimumNumber() {
119         return minimumNumber;
120     }
121 
122     /**
123      * Sets the minimumNumber attribute value.
124      * @param minimumNumber The minimumNumber to set.
125      */
126     public void setMinimumNumber(int minimumNumber) {
127         this.minimumNumber = minimumNumber;
128     }
129 
130     /**
131      * Gets the accountingDocumentForValidation attribute. 
132      * @return Returns the accountingDocumentForValidation.
133      */
134     public AccountingDocument getAccountingDocumentForValidation() {
135         return accountingDocumentForValidation;
136     }
137 
138     /**
139      * Sets the accountingDocumentForValidation attribute value.
140      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
141      */
142     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
143         this.accountingDocumentForValidation = accountingDocumentForValidation;
144     }
145 }