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