1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.document.service.impl;
17
18
19 import org.kuali.ole.DataCarrierService;
20 import org.kuali.ole.deliver.processor.LoanProcessor;
21 import org.kuali.ole.module.purap.PurapConstants;
22 import org.kuali.ole.module.purap.PurapConstants.PurchaseOrderStatuses;
23 import org.kuali.ole.module.purap.PurapKeyConstants;
24 import org.kuali.ole.module.purap.PurapParameterConstants;
25 import org.kuali.ole.module.purap.businessobject.PurApItem;
26 import org.kuali.ole.module.purap.businessobject.PurchaseOrderType;
27 import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
28 import org.kuali.ole.module.purap.document.RequisitionDocument;
29 import org.kuali.ole.module.purap.document.service.LogicContainer;
30 import org.kuali.ole.module.purap.document.service.PaymentRequestService;
31 import org.kuali.ole.module.purap.document.service.PrintService;
32 import org.kuali.ole.module.purap.document.service.PurApWorkflowIntegrationService;
33 import org.kuali.ole.module.purap.document.service.impl.PurchaseOrderServiceImpl;
34 import org.kuali.ole.pojo.OleTxRecord;
35 import org.kuali.ole.select.OleSelectConstant;
36 import org.kuali.ole.select.document.service.OlePurchaseOrderService;
37 import org.kuali.ole.select.document.service.OleSelectDocumentService;
38 import org.kuali.ole.sys.OLEConstants;
39 import org.kuali.ole.sys.context.SpringContext;
40 import org.kuali.ole.sys.document.validation.event.DocumentSystemSaveEvent;
41 import org.kuali.rice.core.api.config.property.ConfigurationService;
42 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
43 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
44 import org.kuali.rice.kew.api.KewApiConstants;
45 import org.kuali.rice.kew.api.KewApiServiceLocator;
46 import org.kuali.rice.kew.api.action.ActionRequestType;
47 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
48 import org.kuali.rice.kew.api.exception.WorkflowException;
49 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
50 import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
51 import org.kuali.rice.kim.api.identity.Person;
52 import org.kuali.rice.krad.exception.ValidationException;
53 import org.kuali.rice.krad.util.GlobalVariables;
54 import org.kuali.rice.krad.util.ObjectUtils;
55 import org.springframework.transaction.annotation.Transactional;
56
57 import java.io.ByteArrayOutputStream;
58 import java.sql.Timestamp;
59 import java.util.Collection;
60 import java.util.HashMap;
61 import java.util.List;
62 import java.util.Map;
63
64 @Transactional
65 public class OlePurchaseOrderServiceImpl extends PurchaseOrderServiceImpl implements OlePurchaseOrderService {
66
67 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePurchaseOrderServiceImpl.class);
68
69
70 private ConfigurationService kualiConfigurationService;
71 private PrintService printService;
72 private PurApWorkflowIntegrationService purapWorkflowIntegrationService;
73 private OleSelectDocumentService oleSelectDocumentService;
74
75 @Override
76 public void setPrintService(PrintService printService) {
77 this.printService = printService;
78 }
79
80 @Override
81 public void setConfigurationService(ConfigurationService kualiConfigurationService) {
82 this.kualiConfigurationService = kualiConfigurationService;
83 }
84
85 @Override
86 public void setPurapWorkflowIntegrationService(PurApWorkflowIntegrationService purapWorkflowIntegrationService) {
87 this.purapWorkflowIntegrationService = purapWorkflowIntegrationService;
88 }
89
90
91
92
93 @Override
94 public void createAutomaticPurchaseOrderDocument(RequisitionDocument reqDocument) {
95 String newSessionUserId = getOleSelectDocumentService().getSelectParameterValue(OLEConstants.SYSTEM_USER);
96 try {
97 LogicContainer logicToRun = new LogicContainer() {
98 @Override
99 public Object runLogic(Object[] objects) throws Exception {
100 RequisitionDocument doc = (RequisitionDocument) objects[0];
101
102 doc.setPurchaseOrderAutomaticIndicator(Boolean.TRUE);
103
104 PurchaseOrderDocument po = generatePurchaseOrderFromRequisition(doc);
105 po.setDefaultValuesForAPO();
106 po.setContractManagerCode(PurapConstants.APO_CONTRACT_MANAGER);
107 populatePOValuesFromProfileAttributes(doc,po);
108 String purchaseOrderType = "";
109 if (doc.getPurchaseOrderTypeId() != null) {
110 Map purchaseOrderTypeIdMap = new HashMap();
111 purchaseOrderTypeIdMap.put("purchaseOrderTypeId", doc.getPurchaseOrderTypeId());
112 org.kuali.rice.krad.service.BusinessObjectService
113 businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
114 LOG.debug("before calling findMatching");
115 List<PurchaseOrderType> purchaseOrderTypeDocumentList = (List) businessObjectService.findMatching(PurchaseOrderType.class, purchaseOrderTypeIdMap);
116 LOG.debug("after calling findMatching");
117 if (purchaseOrderTypeDocumentList != null && purchaseOrderTypeDocumentList.size() > 0) {
118 PurchaseOrderType purchaseOrderTypeDoc = purchaseOrderTypeDocumentList.get(0);
119 purchaseOrderType = purchaseOrderTypeDoc.getPurchaseOrderType();
120 }
121 if (LOG.isDebugEnabled()) {
122 LOG.debug("purchaseOrderType >>>>>>>>>>>" + purchaseOrderType);
123 LOG.debug("purchaseOrder DocumentNumber >>>>>>>>>>>" + po.getDocumentNumber());
124 }
125 if (purchaseOrderType != null ) {
126
127 LOG.debug("before calling document service impl");
128 documentService.routeDocument(po, null, null);
129 LOG.debug("after calling document service impl ");
130 } else {
131 documentService.saveDocument(po, DocumentSystemSaveEvent.class);
132 }
133 }
134 final DocumentAttributeIndexingQueue documentAttributeIndexingQueue = KewApiServiceLocator.getDocumentAttributeIndexingQueue();
135 documentAttributeIndexingQueue.indexDocument(po.getDocumentNumber());
136 return null;
137 }
138 };
139 purapService.performLogicWithFakedUserSession(newSessionUserId, logicToRun, new Object[]{reqDocument});
140 } catch (WorkflowException e) {
141 String errorMsg = "Workflow Exception caught: " + e.getLocalizedMessage();
142 LOG.error(errorMsg, e);
143 throw new RuntimeException(errorMsg, e);
144 } catch (Exception e) {
145 throw new RuntimeException(e);
146 }
147 }
148
149 private void populatePOValuesFromProfileAttributes(RequisitionDocument doc,PurchaseOrderDocument po){
150 DataCarrierService dataCarrierService = GlobalResourceLoader.getService(org.kuali.ole.OLEConstants.DATA_CARRIER_SERVICE);
151 OleTxRecord oleTxRecord = (OleTxRecord)dataCarrierService.getData(org.kuali.ole.OLEConstants.OLE_TX_RECORD);
152 if(oleTxRecord != null && doc.getRequisitionSource() != null && doc.getRequisitionSource().getRequisitionSourceCode().equalsIgnoreCase(OleSelectConstant.REQUISITON_SRC_TYPE_AUTOINGEST)){
153 po.setPurchaseOrderConfirmedIndicator(oleTxRecord.isPurchaseOrderConfirmationIndicator());
154 if(oleTxRecord.getVendorChoice() != null){
155 po.setPurchaseOrderVendorChoiceCode(oleTxRecord.getVendorChoice());
156 }
157 if(oleTxRecord.getAssignToUser() != null){
158 po.setAssignedUserPrincipalName(oleTxRecord.getAssignToUser());
159 }
160 }
161 }
162
163
164
165
166
167 @Override
168 public void performPurchaseOrderFirstTransmitViaPrinting(String documentNumber, ByteArrayOutputStream baosPDF) {
169 PurchaseOrderDocument po = getPurchaseOrderByDocumentNumber(documentNumber);
170 String environment = kualiConfigurationService.getPropertyValueAsString(OLEConstants.ENVIRONMENT_KEY);
171 Collection<String> generatePDFErrors = printService.generatePurchaseOrderPdf(po, baosPDF, environment, null);
172 if (!generatePDFErrors.isEmpty()) {
173 addStringErrorMessagesToMessageMap(PurapKeyConstants.ERROR_PURCHASE_ORDER_PDF, generatePDFErrors);
174 throw new ValidationException("printing purchase order for first transmission failed");
175 }
176 Timestamp currentDate = dateTimeService.getCurrentTimestamp();
177 po.setPurchaseOrderFirstTransmissionTimestamp(currentDate);
178 po.setPurchaseOrderLastTransmitTimestamp(currentDate);
179 po.setOverrideWorkflowButtons(Boolean.FALSE);
180 boolean performedAction = purapWorkflowIntegrationService.takeAllActionsForGivenCriteria(po, "Action taken automatically as part of document initial print transmission", PurapConstants.PurchaseOrderStatuses.NODE_DOCUMENT_TRANSMISSION, GlobalVariables.getUserSession().getPerson(), null);
181 if (!performedAction) {
182 Person systemUserPerson = getPersonService().getPersonByPrincipalName(getOleSelectDocumentService().getSelectParameterValue(OLEConstants.SYSTEM_USER));
183 purapWorkflowIntegrationService.takeAllActionsForGivenCriteria(po, "Action taken automatically as part of document initial print transmission by user " + GlobalVariables.getUserSession().getPerson().getName(), PurapConstants.PurchaseOrderStatuses.NODE_DOCUMENT_TRANSMISSION, systemUserPerson, getOleSelectDocumentService().getSelectParameterValue(OLEConstants.SYSTEM_USER));
184 }
185 po.setOverrideWorkflowButtons(Boolean.TRUE);
186 if (!po.getApplicationDocumentStatus().equals(PurapConstants.PurchaseOrderStatuses.APPDOC_OPEN)) {
187 attemptSetupOfInitialOpenOfDocument(po);
188 }
189 purapService.saveDocumentNoValidation(po);
190 }
191
192 @Override
193 public void purchaseOrderFirstTransmitViaPrinting(String documentNumber, ByteArrayOutputStream baosPDF) {
194 PurchaseOrderDocument po = getPurchaseOrderByDocumentNumber(documentNumber);
195 String environment = kualiConfigurationService.getPropertyValueAsString(OLEConstants.ENVIRONMENT_KEY);
196 Collection<String> generatePDFErrors = printService.generatePurchaseOrderPdf(po, baosPDF, environment, null);
197 LOG.error("PDF Errors" + generatePDFErrors);
198 if (!generatePDFErrors.isEmpty()) {
199 addStringErrorMessagesToMessageMap(PurapKeyConstants.ERROR_PURCHASE_ORDER_PDF, generatePDFErrors);
200 throw new ValidationException("printing purchase order for first transmission failed");
201 }
202 }
203
204
205
206
207
208
209
210
211 @Override
212 protected void attemptSetupOfInitialOpenOfDocument(PurchaseOrderDocument po) {
213 if (LOG.isDebugEnabled()) {
214 LOG.debug("attemptSetupOfInitialOpenOfDocument() started using document with doc id " + po.getDocumentNumber());
215 }
216
217 if (!PurchaseOrderStatuses.APPDOC_OPEN.equals(po.getApplicationDocumentStatus())) {
218 if (OLEConstants.FinancialDocumentTypeCodes.PURCHASE_ORDER_AMENDMENT.equalsIgnoreCase(po.getDocumentHeader().getWorkflowDocument().getDocumentTypeName())) {
219 po.setPurchaseOrderInitialOpenTimestamp(null);
220 }
221 if (ObjectUtils.isNull(po.getPurchaseOrderInitialOpenTimestamp())) {
222 LOG.debug("attemptSetupOfInitialOpenOfDocument() setting initial open date on document");
223 po.setPurchaseOrderInitialOpenTimestamp(dateTimeService.getCurrentTimestamp());
224 } else {
225 throw new RuntimeException("Document does not have status code '" + PurchaseOrderStatuses.APPDOC_OPEN + "' on it but value of initial open date is " + po.getPurchaseOrderInitialOpenTimestamp());
226 }
227 if (LOG.isDebugEnabled()) {
228 LOG.debug("attemptSetupOfInitialOpenOfDocument() Setting po document id " + po.getDocumentNumber() + " status from '" + po.getApplicationDocumentStatus() + "' to '" + PurchaseOrderStatuses.APPDOC_OPEN + "'");
229 }
230 po.setApplicationDocumentStatus(PurchaseOrderStatuses.APPDOC_OPEN);
231
232 } else {
233 LOG.error("attemptSetupOfInitialOpenOfDocument() Found document already in '" + PurchaseOrderStatuses.APPDOC_OPEN + "' status for PO#" + po.getPurapDocumentIdentifier() + "; will not change or update");
234 }
235 }
236
237
238 @Override
239 public void setStatusCompletePurchaseOrderAmendment(PurchaseOrderDocument poa) {
240
241 LOG.debug("setStatusCompletePurchaseOrderAmendment() started");
242 setupDocumentForPendingFirstTransmission(poa);
243 if (!PurchaseOrderStatuses.STATUSES_BY_TRANSMISSION_TYPE.values().contains(poa.getApplicationDocumentStatus())) {
244 attemptSetupOfInitialOpenOfDocument(poa);
245 } else if (PurchaseOrderStatuses.APPDOC_PENDING_PRINT.equals(poa.getApplicationDocumentStatus())) {
246
247 String userToRouteFyi = poa.getDocumentHeader().getWorkflowDocument().getRoutedByPrincipalId();
248
249
250
251
252
253
254
255
256 poa.getDocumentHeader().getWorkflowDocument().adHocToPrincipal(ActionRequestType.FYI, poa.getDocumentHeader().getWorkflowDocument().getCurrentNodeNames().iterator().next(), "This POA is ready for printing and distribution.", userToRouteFyi, "", true, "PRINT");
257 }
258 }
259
260 @Override
261 public void completePurchaseOrderAmendment(PurchaseOrderDocument poa) {
262 LOG.debug("completePurchaseOrderAmendment() started");
263
264 setCurrentAndPendingIndicatorsForApprovedPODocuments(poa);
265
266 if (SpringContext.getBean(PaymentRequestService.class).hasActivePaymentRequestsForPurchaseOrder(poa.getPurapDocumentIdentifier())) {
267 poa.setPaymentRequestPositiveApprovalIndicator(true);
268 poa.setReceivingDocumentRequiredIndicator(false);
269 }
270
271 else if (!poa.isReceivingDocumentRequiredIndicator()) {
272 setReceivingRequiredIndicatorForPurchaseOrder(poa);
273 }
274
275
276
277
278
279 DocumentRouteHeaderValue routeHeader = SpringContext.getBean(RouteHeaderService.class).getRouteHeader(poa.getDocumentNumber());
280 String status = routeHeader.getDocRouteStatus();
281 if (status.equals(KewApiConstants.ROUTE_HEADER_PROCESSED_CD)) {
282 List<PurApItem> items = poa.getItems();
283 for (PurApItem item : items) {
284 initiateTransmission(poa, item);
285 }
286
287 }
288
289 }
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312 @Override
313 public PurchaseOrderDocument createPurchaseOrderDocument(RequisitionDocument reqDocument, String newSessionUserId, Integer contractManagerCode) {
314 try {
315 LogicContainer logicToRun = new LogicContainer() {
316 @Override
317 public Object runLogic(Object[] objects) throws Exception {
318 RequisitionDocument doc = (RequisitionDocument) objects[0];
319 PurchaseOrderDocument po = generatePurchaseOrderFromRequisition(doc);
320 Integer cmCode = (Integer) objects[1];
321 po.setContractManagerCode(cmCode);
322 String paramName = PurapParameterConstants.DEFAULT_B2B_VENDOR_CHOICE;
323 String paramValue = SpringContext.getBean(ParameterService.class).getParameterValueAsString(PurchaseOrderDocument.class, paramName);
324 po.setPurchaseOrderVendorChoiceCode(paramValue);
325 purapService.saveDocumentNoValidation(po);
326 return po;
327 }
328 };
329 return (PurchaseOrderDocument) purapService.performLogicWithFakedUserSession(newSessionUserId, logicToRun, new Object[]{reqDocument, contractManagerCode});
330 } catch (WorkflowException e) {
331 String errorMsg = "Workflow Exception caught: " + e.getLocalizedMessage();
332 LOG.error(errorMsg, e);
333 throw new RuntimeException(errorMsg, e);
334 } catch (Exception e) {
335 throw new RuntimeException(e);
336 }
337 }
338
339 public boolean getIsATypeOfRCVGDoc() {
340 return false;
341 }
342
343 public boolean getIsATypeOfCORRDoc() {
344 return false;
345 }
346
347 public OleSelectDocumentService getOleSelectDocumentService() {
348 if(oleSelectDocumentService == null){
349 oleSelectDocumentService = SpringContext.getBean(OleSelectDocumentService.class);
350 }
351 return oleSelectDocumentService;
352 }
353
354 public void setOleSelectDocumentService(OleSelectDocumentService oleSelectDocumentService) {
355 this.oleSelectDocumentService = oleSelectDocumentService;
356 }
357
358 }