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.util.LinkedList;
20 import java.util.Queue;
21
22 import org.apache.commons.beanutils.PropertyUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.kuali.ole.sys.businessobject.AccountingLine;
25 import org.kuali.ole.sys.businessobject.SourceAccountingLine;
26 import org.kuali.ole.sys.context.SpringContext;
27 import org.kuali.ole.sys.document.AccountingDocument;
28 import org.kuali.ole.sys.document.validation.GenericValidation;
29 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
30 import org.kuali.ole.sys.service.impl.OleParameterConstants;
31 import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
32 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
33 import org.kuali.rice.krad.bo.PersistableBusinessObject;
34 import org.kuali.rice.krad.util.ObjectUtils;
35
36
37
38
39 public class AccountingLineValueAllowedValidation extends GenericValidation {
40 protected String propertyPath;
41 protected String parameterToCheckAgainst;
42 protected ParameterService parameterService;
43 protected String responsibleProperty;
44 protected AccountingDocument accountingDocumentForValidation;
45 protected AccountingLine accountingLineForValidation;
46
47
48
49
50
51
52 public boolean validate(AttributedDocumentEvent event) {
53
54 if (!StringUtils.isBlank(propertyPath)) {
55 refreshByPath(accountingLineForValidation);
56 }
57
58 return isAccountingLineValueAllowed(accountingDocumentForValidation.getDocumentClassForAccountingLineValueAllowedValidation(), accountingLineForValidation, parameterToCheckAgainst, propertyPath, (responsibleProperty != null ? responsibleProperty : propertyPath));
59 }
60
61
62
63
64
65
66
67
68
69
70 protected boolean isAccountingLineValueAllowed(Class documentClass, AccountingLine accountingLine, String parameterName, String propertyName, String userEnteredPropertyName) {
71 boolean isAllowed = true;
72 String exceptionMessage = "Invalue property name provided to AccountingDocumentRuleBase isAccountingLineValueAllowed method: " + propertyName;
73 try {
74 String propertyValue = (String) PropertyUtils.getProperty(accountingLine, propertyName);
75 if (getParameterService().parameterExists(OleParameterConstants.FINANCIAL_PROCESSING_DOCUMENT.class, parameterName)) {
76 isAllowed = SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(OleParameterConstants.FINANCIAL_PROCESSING_DOCUMENT.class, parameterName, propertyValue).evaluateAndAddError(SourceAccountingLine.class, propertyName, userEnteredPropertyName);
77 }
78 if (getParameterService().parameterExists(documentClass, parameterName)) {
79 isAllowed = SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(documentClass, parameterName, propertyValue).evaluateAndAddError(SourceAccountingLine.class, propertyName, userEnteredPropertyName);
80 }
81 }
82 catch (IllegalAccessException e) {
83 throw new RuntimeException(exceptionMessage, e);
84 }
85 catch (InvocationTargetException e) {
86 throw new RuntimeException(exceptionMessage, e);
87 }
88 catch (NoSuchMethodException e) {
89 throw new RuntimeException(exceptionMessage, e);
90 }
91 return isAllowed;
92 }
93
94
95
96
97
98 public void refreshByPath(AccountingLine line) {
99 refreshByQueue(line, convertPathToQueue(propertyPath));
100 }
101
102
103
104
105
106
107 protected Queue<String> convertPathToQueue(String path) {
108 Queue<String> pathQueue = new LinkedList<String>();
109 for (String property: path.split("\\.")) {
110 pathQueue.add(property);
111 }
112 return pathQueue;
113 }
114
115
116
117
118
119
120 protected void refreshByQueue(PersistableBusinessObject bo, Queue<String> path) {
121 if (path.size() > 1) {
122 String currentProperty = path.remove();
123 bo.refreshReferenceObject(currentProperty);
124 PersistableBusinessObject childBO = (PersistableBusinessObject)ObjectUtils.getPropertyValue(bo, currentProperty);
125 if (!ObjectUtils.isNull(childBO)) {
126 refreshByQueue(childBO, path);
127 }
128 }
129 }
130
131
132
133
134
135 public String getPropertyPath() {
136 return propertyPath;
137 }
138
139
140
141
142
143 public void setPropertyPath(String refreshPath) {
144 this.propertyPath = refreshPath;
145 }
146
147
148
149
150
151 public ParameterService getParameterService() {
152 return parameterService;
153 }
154
155
156
157
158
159 public void setParameterService(ParameterService parameterService) {
160 this.parameterService = parameterService;
161 }
162
163
164
165
166
167 public String getParameterToCheckAgainst() {
168 return parameterToCheckAgainst;
169 }
170
171
172
173
174
175 public void setParameterToCheckAgainst(String parameterToCheckAgainst) {
176 this.parameterToCheckAgainst = parameterToCheckAgainst;
177 }
178
179
180
181
182
183 public String getResponsibleProperty() {
184 return responsibleProperty;
185 }
186
187
188
189
190
191 public void setResponsibleProperty(String responsibleProperty) {
192 this.responsibleProperty = responsibleProperty;
193 }
194
195
196
197
198
199 public AccountingDocument getAccountingDocumentForValidation() {
200 return accountingDocumentForValidation;
201 }
202
203
204
205
206
207 public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
208 this.accountingDocumentForValidation = accountingDocumentForValidation;
209 }
210
211
212
213
214
215 public AccountingLine getAccountingLineForValidation() {
216 return accountingLineForValidation;
217 }
218
219
220
221
222
223 public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
224 this.accountingLineForValidation = accountingLineForValidation;
225 }
226 }