1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.util;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.ole.module.purap.PurapConstants;
20 import org.kuali.ole.module.purap.PurapPropertyConstants;
21 import org.kuali.ole.module.purap.businessobject.*;
22 import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
23 import org.kuali.ole.module.purap.document.service.PurapService;
24 import org.kuali.ole.sys.OLEConstants;
25 import org.kuali.ole.sys.context.SpringContext;
26 import org.kuali.rice.kew.api.document.DocumentStatus;
27 import org.kuali.rice.kew.api.exception.WorkflowException;
28 import org.kuali.rice.kim.api.KimConstants;
29 import org.kuali.rice.kim.api.services.IdentityManagementService;
30 import org.kuali.rice.kns.service.DataDictionaryService;
31 import org.kuali.rice.krad.datadictionary.exception.UnknownDocumentTypeException;
32 import org.kuali.rice.krad.document.Document;
33 import org.kuali.rice.krad.service.DocumentService;
34 import org.kuali.rice.krad.service.KRADServiceLocatorInternal;
35 import org.kuali.rice.krad.util.GlobalVariables;
36 import org.kuali.rice.krad.util.ObjectUtils;
37 import org.springframework.transaction.PlatformTransactionManager;
38 import org.springframework.transaction.TransactionStatus;
39 import org.springframework.transaction.support.TransactionCallback;
40 import org.springframework.transaction.support.TransactionTemplate;
41
42 import java.util.*;
43
44 public class PurApRelatedViews {
45 private String documentNumber;
46 private Integer accountsPayablePurchasingDocumentLinkIdentifier;
47
48 private transient List<RequisitionView> relatedRequisitionViews;
49 private transient List<PurchaseOrderView> relatedPurchaseOrderViews;
50 private transient List<PaymentRequestView> relatedPaymentRequestViews;
51 private transient List<PaymentRequestView> paymentHistoryPaymentRequestViews;
52 private transient List<CreditMemoView> relatedCreditMemoViews;
53 private transient List<CreditMemoView> paymentHistoryCreditMemoViews;
54 private transient List<InvoiceView> relatedInvoiceViews;
55 private transient List<InvoiceView> paymentHistoryInvoiceViews;
56 private transient List<LineItemReceivingView> relatedLineItemReceivingViews;
57 private transient List<CorrectionReceivingView> relatedCorrectionReceivingViews;
58 private transient List<BulkReceivingView> relatedBulkReceivingViews;
59 private transient List<PurchaseOrderViewGroup> groupedRelatedPurchaseOrderViews;
60 private transient List<ReceivingViewGroup> groupedRelatedReceivingViews;
61 private transient List<ElectronicInvoiceRejectView> relatedRejectViews;
62
63 public PurApRelatedViews(String documentNumber, Integer accountsPayablePurchasingDocumentLinkIdentifier) {
64 super();
65 this.documentNumber = documentNumber;
66 this.accountsPayablePurchasingDocumentLinkIdentifier = accountsPayablePurchasingDocumentLinkIdentifier;
67 }
68
69
70
71
72 public void resetRelatedViews() {
73 relatedRequisitionViews = null;
74 relatedPurchaseOrderViews = null;
75 relatedPaymentRequestViews = null;
76 paymentHistoryPaymentRequestViews = null;
77 relatedInvoiceViews = null;
78 paymentHistoryInvoiceViews = null;
79 relatedCreditMemoViews = null;
80 paymentHistoryCreditMemoViews = null;
81 relatedLineItemReceivingViews = null;
82 relatedCorrectionReceivingViews = null;
83 relatedBulkReceivingViews = null;
84 groupedRelatedPurchaseOrderViews = null;
85 groupedRelatedReceivingViews = null;
86 relatedRejectViews = null;
87 }
88
89 private static PlatformTransactionManager transactionManager;
90
91 public static PlatformTransactionManager getTransactionManager() {
92 if (transactionManager == null) {
93 transactionManager = KRADServiceLocatorInternal.getTransactionManager();
94 }
95 return transactionManager;
96 }
97
98 public List updateRelatedView(final Class<?> clazz, List<? extends AbstractRelatedView> relatedList, final boolean removeCurrentDocument) {
99 if (relatedList == null) {
100 TransactionTemplate template = new TransactionTemplate(getTransactionManager());
101 relatedList = template.execute(new TransactionCallback<List<? extends AbstractRelatedView>>() {
102 @Override
103 public List<? extends AbstractRelatedView> doInTransaction(TransactionStatus status) {
104 List<? extends AbstractRelatedView> relatedList = SpringContext.getBean(PurapService.class).getRelatedViews(clazz, accountsPayablePurchasingDocumentLinkIdentifier);
105 if (removeCurrentDocument) {
106 for (AbstractRelatedView view : relatedList) {
107
108 maskPONumberIfUnapproved(view);
109 if (documentNumber.equals(view.getDocumentNumber())) {
110 relatedList.remove(view);
111 break;
112 }
113 }
114 }
115 return relatedList;
116 }
117 });
118 }
119
120 return relatedList;
121 }
122
123
124
125
126
127
128
129
130 protected void maskPONumberIfUnapproved(AbstractRelatedView view) {
131 Document document = findDocument(view.getDocumentNumber());
132
133 String poIDstr = "";
134
135 if (ObjectUtils.isNotNull(view.getPurapDocumentIdentifier())) {
136 poIDstr = view.getPurapDocumentIdentifier().toString();
137 }
138
139 if (PurapConstants.PurapDocTypeCodes.PO_DOCUMENT.equals(document.getDocumentHeader().getWorkflowDocument().getDocumentTypeName())) {
140 if ((document != null) && (document.getDocumentHeader().getWorkflowDocument() != null)) {
141 if (!document.getDocumentHeader().getWorkflowDocument().getStatus().equals(DocumentStatus.FINAL)) {
142
143 String principalId = GlobalVariables.getUserSession().getPrincipalId();
144
145 String namespaceCode = OLEConstants.ParameterNamespaces.KNS;
146 String permissionTemplateName = KimConstants.PermissionTemplateNames.FULL_UNMASK_FIELD;
147
148 Map<String, String> roleQualifiers = new HashMap<String, String>();
149
150 Map<String, String> permissionDetails = new HashMap<String, String>();
151 permissionDetails.put(KimConstants.AttributeConstants.COMPONENT_NAME, PurchaseOrderDocument.class.getSimpleName());
152 permissionDetails.put(KimConstants.AttributeConstants.PROPERTY_NAME, PurapPropertyConstants.PURAP_DOC_ID);
153
154 IdentityManagementService identityManagementService = SpringContext.getBean(IdentityManagementService.class);
155 Boolean isAuthorized = identityManagementService.isAuthorizedByTemplateName(principalId, namespaceCode, permissionTemplateName, permissionDetails, roleQualifiers);
156 if (!isAuthorized) {
157
158 poIDstr = "";
159 int strLength = SpringContext.getBean(DataDictionaryService.class).getAttributeMaxLength(PurApGenericAttributes.class.getName(), PurapPropertyConstants.PURAP_DOC_ID);
160 for (int i = 0; i < strLength; i++) {
161 poIDstr = poIDstr.concat("*");
162 }
163 }
164 }
165 }
166 }
167
168 view.setPoNumberMasked(poIDstr);
169 }
170
171
172
173
174
175
176
177 protected Document findDocument(String documentHeaderId) {
178 Document document = null;
179
180 try {
181 document = SpringContext.getBean(DocumentService.class).getByDocumentHeaderId(documentHeaderId);
182 } catch (WorkflowException ex) {
183 } catch (UnknownDocumentTypeException ex) {
184
185 }
186
187 return document;
188 }
189
190
191
192
193 public List<RequisitionView> getRelatedRequisitionViews() {
194 relatedRequisitionViews = updateRelatedView(RequisitionView.class, relatedRequisitionViews, true);
195 return relatedRequisitionViews;
196 }
197
198 public List<ElectronicInvoiceRejectView> getRelatedRejectViews() {
199 relatedRejectViews = updateRelatedView(ElectronicInvoiceRejectView.class, relatedRejectViews, true);
200 return relatedRejectViews;
201 }
202
203
204
205
206
207
208
209 public List<PurchaseOrderView> getRelatedPurchaseOrderViews() {
210 if (relatedPurchaseOrderViews != null) {
211 return relatedPurchaseOrderViews;
212 }
213
214
215 relatedPurchaseOrderViews = updateRelatedView(PurchaseOrderView.class, relatedPurchaseOrderViews, true);
216
217
218 Collections.sort(relatedPurchaseOrderViews,
219 new Comparator<PurchaseOrderView>() {
220 @Override
221 public int compare(PurchaseOrderView v1, PurchaseOrderView v2) {
222 if ((v1 != null) && (v2 != null) &&
223 (v1.getPurapDocumentIdentifier() != null) &&
224 (v2.getPurapDocumentIdentifier() != null)) {
225
226 int compare = -v1.getPurapDocumentIdentifier().compareTo(v2.getPurapDocumentIdentifier());
227
228 if (compare == 0) {
229 compare = v1.getPurchaseOrderCurrentIndicator() ? -1 :
230 v2.getPurchaseOrderCurrentIndicator() ? 1 :
231 -v1.getCreateDate().compareTo(v2.getCreateDate());
232 }
233 return compare;
234 }
235 return 0;
236 }
237 }
238 );
239
240 return relatedPurchaseOrderViews;
241 }
242
243
244
245
246
247
248
249
250
251 public List<PurchaseOrderViewGroup> getGroupedRelatedPurchaseOrderViews() {
252 if (groupedRelatedPurchaseOrderViews != null) {
253 return groupedRelatedPurchaseOrderViews;
254 }
255
256
257
258
259
260
261
262 groupedRelatedPurchaseOrderViews = new ArrayList<PurchaseOrderViewGroup>();
263 PurchaseOrderViewGroup group = new PurchaseOrderViewGroup();
264 int previousPOID = 0;
265 relatedPurchaseOrderViews = getRelatedPurchaseOrderViews();
266 for (PurchaseOrderView view : relatedPurchaseOrderViews) {
267 if (previousPOID == 0) {
268 previousPOID = view.getPurapDocumentIdentifier();
269
270 }
271 if (view.getPurapDocumentIdentifier() == previousPOID) {
272 group.getViews().add(view);
273 } else {
274 groupedRelatedPurchaseOrderViews.add(group);
275 group = new PurchaseOrderViewGroup();
276 group.getViews().add(view);
277 previousPOID = view.getPurapDocumentIdentifier();
278 }
279 if (relatedPurchaseOrderViews.size() == relatedPurchaseOrderViews.indexOf(view) + 1) {
280 groupedRelatedPurchaseOrderViews.add(group);
281 }
282 }
283
284 return groupedRelatedPurchaseOrderViews;
285 }
286
287
288
289
290 public List<PaymentRequestView> getRelatedPaymentRequestViews() {
291 relatedPaymentRequestViews = updateRelatedView(PaymentRequestView.class, relatedPaymentRequestViews, true);
292 return relatedPaymentRequestViews;
293 }
294
295
296
297
298 public List<CreditMemoView> getRelatedCreditMemoViews() {
299 relatedCreditMemoViews = updateRelatedView(CreditMemoView.class, relatedCreditMemoViews, true);
300 return relatedCreditMemoViews;
301 }
302
303
304
305
306
307 public List<InvoiceView> getRelatedInvoiceViews() {
308 relatedInvoiceViews = updateRelatedView(InvoiceView.class, relatedInvoiceViews, true);
309 return relatedInvoiceViews;
310 }
311
312
313
314
315
316
317 public List<PaymentRequestView> getPaymentHistoryPaymentRequestViews() {
318 paymentHistoryPaymentRequestViews = updateRelatedView(PaymentRequestView.class, paymentHistoryPaymentRequestViews, false);
319 return paymentHistoryPaymentRequestViews;
320 }
321
322
323
324
325
326
327 public List<CreditMemoView> getPaymentHistoryCreditMemoViews() {
328 paymentHistoryCreditMemoViews = updateRelatedView(CreditMemoView.class, paymentHistoryCreditMemoViews, false);
329 return paymentHistoryCreditMemoViews;
330 }
331
332
333
334
335
336
337 public List<InvoiceView> getPaymentHistoryInvoiceViews() {
338 paymentHistoryInvoiceViews = updateRelatedView(InvoiceView.class, paymentHistoryInvoiceViews, false);
339 return paymentHistoryInvoiceViews;
340 }
341
342
343
344
345 public List<LineItemReceivingView> getRelatedLineItemReceivingViews() {
346 relatedLineItemReceivingViews = updateRelatedView(LineItemReceivingView.class, relatedLineItemReceivingViews, true);
347 return relatedLineItemReceivingViews;
348 }
349
350
351
352
353 public List<CorrectionReceivingView> getRelatedCorrectionReceivingViews() {
354 relatedCorrectionReceivingViews = updateRelatedView(CorrectionReceivingView.class, relatedCorrectionReceivingViews, true);
355 return relatedCorrectionReceivingViews;
356 }
357
358 public List<BulkReceivingView> getRelatedBulkReceivingViews() {
359 relatedBulkReceivingViews = updateRelatedView(BulkReceivingView.class, relatedBulkReceivingViews, true);
360 return relatedBulkReceivingViews;
361 }
362
363
364
365
366
367
368
369 public List<ReceivingViewGroup> getGroupedRelatedReceivingViews() {
370 if (groupedRelatedReceivingViews != null) {
371 return groupedRelatedReceivingViews;
372 }
373
374 groupedRelatedReceivingViews = new ArrayList<ReceivingViewGroup>();
375 PurapService purapService = SpringContext.getBean(PurapService.class);
376 List<LineItemReceivingView> liviews = purapService.getRelatedViews(LineItemReceivingView.class, accountsPayablePurchasingDocumentLinkIdentifier);
377 List<CorrectionReceivingView> crviews = purapService.getRelatedViews(CorrectionReceivingView.class, accountsPayablePurchasingDocumentLinkIdentifier);
378
379
380 for (LineItemReceivingView liview : liviews) {
381 ReceivingViewGroup group = new ReceivingViewGroup();
382 group.lineItemView = liview;
383 for (CorrectionReceivingView crview : crviews) {
384 if (StringUtils.equals(crview.getLineItemReceivingDocumentNumber(), liview.getDocumentNumber()) &&
385 !documentNumber.equals(crview.getDocumentNumber())) {
386 group.addCorrectionView(crview);
387 }
388 }
389 groupedRelatedReceivingViews.add(group);
390 }
391
392 return groupedRelatedReceivingViews;
393 }
394
395
396
397
398
399 public class PurchaseOrderViewGroup {
400 protected List<PurchaseOrderView> views = new ArrayList<PurchaseOrderView>();
401
402 protected PurchaseOrderViewGroup() {
403 }
404
405 public List<PurchaseOrderView> getViews() {
406 return views;
407 }
408 }
409
410
411
412
413 public class ReceivingViewGroup {
414 protected LineItemReceivingView lineItemView;
415 protected List<CorrectionReceivingView> correctionViews = new ArrayList<CorrectionReceivingView>();
416
417 protected ReceivingViewGroup() {
418 }
419
420 public LineItemReceivingView getLineItemView() {
421 return lineItemView;
422 }
423
424 public List<CorrectionReceivingView> getCorrectionViews() {
425 return correctionViews;
426 }
427
428 public void addCorrectionView(CorrectionReceivingView correctionView) {
429 correctionViews.add(correctionView);
430 }
431
432 public boolean getIsLineItemViewCurrentDocument() {
433 return (lineItemView != null && documentNumber.equals(lineItemView.getDocumentNumber()));
434 }
435 }
436
437 }