1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.document.validation.impl;
17
18 import org.apache.commons.beanutils.PropertyUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.ole.module.purap.PurapConstants.PaymentRequestStatuses;
21 import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
22 import org.kuali.ole.module.purap.businessobject.PurApItem;
23 import org.kuali.ole.module.purap.document.PaymentRequestDocument;
24 import org.kuali.ole.sys.businessobject.AccountingLine;
25 import org.kuali.ole.sys.context.SpringContext;
26 import org.kuali.ole.sys.document.AccountingDocument;
27 import org.kuali.ole.sys.document.OLEAccountingDocument;
28 import org.kuali.ole.sys.document.validation.BranchingValidation;
29 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
30 import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
31 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
32 import org.kuali.rice.krad.bo.PersistableBusinessObject;
33 import org.kuali.rice.krad.util.ObjectUtils;
34
35 import java.lang.reflect.InvocationTargetException;
36 import java.util.LinkedList;
37 import java.util.Queue;
38
39
40
41
42 public class PurchasingAccountsPayableObjectCodeOverrideBranchingValidation extends BranchingValidation {
43 protected String propertyPath;
44 protected String parameterToCheckAgainst;
45 protected ParameterService parameterService;
46 protected String responsibleProperty;
47 protected OLEAccountingDocument accountingDocumentForValidation;
48 protected AccountingLine accountingLineForValidation;
49
50 protected final static String OBJECT_CODE_OVERRIDEN = "ObjectCodeOverriden";
51 protected final static String OBJECT_CODE_NOT_OVERRIDEN = "ObjectCodeNotOverriden";
52
53 @Override
54 protected String determineBranch(AttributedDocumentEvent event) {
55 if (!StringUtils.isBlank(propertyPath)) {
56 refreshByPath(accountingLineForValidation);
57 }
58
59 boolean isTaxApproval = false;
60
61
62 if (accountingDocumentForValidation instanceof PaymentRequestDocument) {
63 PaymentRequestDocument preq = (PaymentRequestDocument) accountingDocumentForValidation;
64 PurApAccountingLine purapAccountingLine = (PurApAccountingLine) accountingLineForValidation;
65 PurApItem item = purapAccountingLine.getPurapItem();
66
67 if (StringUtils.equals(PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW, preq.getApplicationDocumentStatus())) {
68 isTaxApproval = true;
69 } else if (StringUtils.equals(PaymentRequestStatuses.APPDOC_DEPARTMENT_APPROVED, preq.getApplicationDocumentStatus()) &&
70 (ObjectUtils.isNotNull(item) && item.getItemType().getIsTaxCharge())) {
71 isTaxApproval = true;
72 }
73 }
74
75 if (isTaxApproval) {
76 return null;
77 } else if (isAccountingLineValueAllowed(accountingDocumentForValidation.getClass(), accountingLineForValidation, parameterToCheckAgainst, propertyPath, (responsibleProperty != null ? responsibleProperty : propertyPath))) {
78 return OBJECT_CODE_OVERRIDEN;
79 } else {
80 return OBJECT_CODE_NOT_OVERRIDEN;
81 }
82 }
83
84
85
86
87
88
89
90
91
92
93
94 protected boolean isAccountingLineValueAllowed(Class documentClass, AccountingLine accountingLine, String parameterName, String propertyName, String userEnteredPropertyName) {
95 boolean isAllowed = false;
96 String exceptionMessage = "Invalid property name provided to PurchasingAccountsPayableObjectCodeOverrideBranchingValidation isAccountingLineValueAllowed method: " + propertyName;
97 try {
98 String propertyValue = (String) PropertyUtils.getProperty(accountingLine, propertyName);
99 if (getParameterService().parameterExists(documentClass, parameterName)) {
100 isAllowed = SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(documentClass, parameterName, propertyValue).evaluationSucceeds();
101 }
102 } catch (IllegalAccessException e) {
103 throw new RuntimeException(exceptionMessage, e);
104 } catch (InvocationTargetException e) {
105 throw new RuntimeException(exceptionMessage, e);
106 } catch (NoSuchMethodException e) {
107 throw new RuntimeException(exceptionMessage, e);
108 }
109 return isAllowed;
110 }
111
112
113
114
115
116
117 public void refreshByPath(AccountingLine line) {
118 refreshByQueue(line, convertPathToQueue(propertyPath));
119 }
120
121
122
123
124
125
126
127 protected Queue<String> convertPathToQueue(String path) {
128 Queue<String> pathQueue = new LinkedList<String>();
129 for (String property : path.split("\\.")) {
130 pathQueue.add(property);
131 }
132 return pathQueue;
133 }
134
135
136
137
138
139
140
141 protected void refreshByQueue(PersistableBusinessObject bo, Queue<String> path) {
142 if (path.size() > 1) {
143 String currentProperty = path.remove();
144 bo.refreshReferenceObject(currentProperty);
145 PersistableBusinessObject childBO = (PersistableBusinessObject) ObjectUtils.getPropertyValue(bo, currentProperty);
146 if (!ObjectUtils.isNull(childBO)) {
147 refreshByQueue(childBO, path);
148 }
149 }
150 }
151
152
153
154
155
156
157 public String getPropertyPath() {
158 return propertyPath;
159 }
160
161
162
163
164
165
166 public void setPropertyPath(String refreshPath) {
167 this.propertyPath = refreshPath;
168 }
169
170
171
172
173
174
175 public ParameterService getParameterService() {
176 return parameterService;
177 }
178
179
180
181
182
183
184 public void setParameterService(ParameterService parameterService) {
185 this.parameterService = parameterService;
186 }
187
188
189
190
191
192
193 public String getParameterToCheckAgainst() {
194 return parameterToCheckAgainst;
195 }
196
197
198
199
200
201
202 public void setParameterToCheckAgainst(String parameterToCheckAgainst) {
203 this.parameterToCheckAgainst = parameterToCheckAgainst;
204 }
205
206
207
208
209
210
211 public String getResponsibleProperty() {
212 return responsibleProperty;
213 }
214
215
216
217
218
219
220 public void setResponsibleProperty(String responsibleProperty) {
221 this.responsibleProperty = responsibleProperty;
222 }
223
224
225
226
227
228
229 public OLEAccountingDocument getAccountingDocumentForValidation() {
230 return accountingDocumentForValidation;
231 }
232
233
234
235
236
237
238
239 public void setAccountingDocumentForValidation(OLEAccountingDocument accountingDocumentForValidation) {
240 this.accountingDocumentForValidation = accountingDocumentForValidation;
241 }
242
243
244
245
246
247
248 public AccountingLine getAccountingLineForValidation() {
249 return accountingLineForValidation;
250 }
251
252
253
254
255
256
257 public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
258 this.accountingLineForValidation = accountingLineForValidation;
259 }
260
261 }