1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
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  
42  
43  
44  
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  
57  
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  
78  
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  
98  
99  
100     public String getAccountingLineGroupName() {
101         return accountingLineGroupName;
102     }
103 
104     
105 
106 
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 
116 
117 
118     public int getMinimumNumber() {
119         return minimumNumber;
120     }
121 
122     
123 
124 
125 
126     public void setMinimumNumber(int minimumNumber) {
127         this.minimumNumber = minimumNumber;
128     }
129 
130     
131 
132 
133 
134     public AccountingDocument getAccountingDocumentForValidation() {
135         return accountingDocumentForValidation;
136     }
137 
138     
139 
140 
141 
142     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
143         this.accountingDocumentForValidation = accountingDocumentForValidation;
144     }
145 }