1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.document.service.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.ole.module.purap.PurapConstants;
21 import org.kuali.ole.module.purap.PurapKeyConstants;
22 import org.kuali.ole.module.purap.document.BulkReceivingDocument;
23 import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
24 import org.kuali.ole.module.purap.document.dataaccess.BulkReceivingDao;
25 import org.kuali.ole.module.purap.document.service.BulkReceivingService;
26 import org.kuali.ole.module.purap.document.service.PrintService;
27 import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
28 import org.kuali.ole.module.purap.document.validation.event.AttributedContinuePurapEvent;
29 import org.kuali.ole.sys.OLEConstants;
30 import org.kuali.ole.sys.OLEPropertyConstants;
31 import org.kuali.rice.core.api.config.property.ConfigurationService;
32 import org.kuali.rice.kew.api.WorkflowDocument;
33 import org.kuali.rice.kew.api.exception.WorkflowException;
34 import org.kuali.rice.krad.exception.ValidationException;
35 import org.kuali.rice.krad.service.DocumentService;
36 import org.kuali.rice.krad.util.GlobalVariables;
37 import org.kuali.rice.krad.util.ObjectUtils;
38 import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
39 import org.springframework.transaction.annotation.Transactional;
40
41 import java.io.ByteArrayOutputStream;
42 import java.text.MessageFormat;
43 import java.util.Collection;
44 import java.util.HashMap;
45 import java.util.List;
46
47 @Transactional
48 public class BulkReceivingServiceImpl implements BulkReceivingService {
49
50 private static final Logger LOG = Logger.getLogger(BulkReceivingServiceImpl.class);
51
52 protected PurchaseOrderService purchaseOrderService;
53 protected BulkReceivingDao bulkReceivingDao;
54 protected DocumentService documentService;
55 protected WorkflowDocumentService workflowDocumentService;
56 protected ConfigurationService configurationService;
57 protected PrintService printService;
58
59 @Override
60 public boolean canPrintReceivingTicket(BulkReceivingDocument blkRecDoc) {
61
62 boolean canCreate = false;
63 WorkflowDocument workflowDocument = null;
64
65 try {
66 workflowDocument = workflowDocumentService.createWorkflowDocument(blkRecDoc.getDocumentHeader().getWorkflowDocument().getDocumentTypeName(), GlobalVariables.getUserSession().getPerson());
67 } catch (WorkflowException we) {
68 throw new RuntimeException(we);
69 }
70
71 if (workflowDocument.isFinal()) {
72 canCreate = true;
73 }
74
75 return canCreate;
76 }
77
78 @Override
79 public void populateAndSaveBulkReceivingDocument(BulkReceivingDocument blkRecDoc)
80 throws WorkflowException {
81 try {
82 documentService.saveDocument(blkRecDoc, AttributedContinuePurapEvent.class);
83 } catch (WorkflowException we) {
84 String errorMsg = "Error saving document # " + blkRecDoc.getDocumentHeader().getDocumentNumber() + " " + we.getMessage();
85 throw new RuntimeException(errorMsg, we);
86 }
87 }
88
89 @Override
90 public HashMap<String, String> bulkReceivingDuplicateMessages(BulkReceivingDocument blkRecDoc) {
91 HashMap<String, String> msgs;
92 msgs = new HashMap<String, String>();
93 Integer poId = blkRecDoc.getPurchaseOrderIdentifier();
94 StringBuffer currentMessage = new StringBuffer("");
95 List<String> docNumbers = null;
96
97
98 if (blkRecDoc.getShipmentReceivedDate() != null) {
99 docNumbers = bulkReceivingDao.duplicateVendorDate(poId, blkRecDoc.getShipmentReceivedDate());
100 if (hasDuplicateEntry(docNumbers)) {
101 appendDuplicateMessage(currentMessage, PurapKeyConstants.MESSAGE_DUPLICATE_RECEIVING_LINE_VENDOR_DATE, blkRecDoc.getPurchaseOrderIdentifier());
102 }
103 }
104
105
106 if (!StringUtils.isEmpty(blkRecDoc.getShipmentPackingSlipNumber())) {
107 docNumbers = bulkReceivingDao.duplicatePackingSlipNumber(poId, blkRecDoc.getShipmentPackingSlipNumber());
108 if (hasDuplicateEntry(docNumbers)) {
109 appendDuplicateMessage(currentMessage, PurapKeyConstants.MESSAGE_DUPLICATE_RECEIVING_LINE_PACKING_SLIP_NUMBER, blkRecDoc.getPurchaseOrderIdentifier());
110 }
111 }
112
113
114 if (!StringUtils.isEmpty(blkRecDoc.getShipmentBillOfLadingNumber())) {
115 docNumbers = bulkReceivingDao.duplicateBillOfLadingNumber(poId, blkRecDoc.getShipmentBillOfLadingNumber());
116 if (hasDuplicateEntry(docNumbers)) {
117 appendDuplicateMessage(currentMessage, PurapKeyConstants.MESSAGE_DUPLICATE_RECEIVING_LINE_BILL_OF_LADING_NUMBER, blkRecDoc.getPurchaseOrderIdentifier());
118 }
119 }
120
121
122 if (currentMessage.length() > 0) {
123
124 appendDuplicateMessage(currentMessage, PurapKeyConstants.MESSAGE_DUPLICATE_RECEIVING_LINE_SUFFIX, blkRecDoc.getPurchaseOrderIdentifier());
125
126
127 msgs.put(PurapConstants.BulkReceivingDocumentStrings.DUPLICATE_BULK_RECEIVING_DOCUMENT_QUESTION, currentMessage.toString());
128 }
129
130 return msgs;
131 }
132
133
134
135
136
137
138
139
140 protected boolean hasDuplicateEntry(List<String> docNumbers) {
141
142 boolean isDuplicate = false;
143 WorkflowDocument workflowDocument = null;
144
145 for (String docNumber : docNumbers) {
146
147 try {
148 workflowDocument = workflowDocumentService.loadWorkflowDocument(docNumber, GlobalVariables.getUserSession().getPerson());
149 } catch (WorkflowException we) {
150 throw new RuntimeException(we);
151 }
152
153
154 if (workflowDocument.isFinal()) {
155 isDuplicate = true;
156 break;
157 }
158 }
159
160 return isDuplicate;
161
162 }
163
164 protected void appendDuplicateMessage(StringBuffer currentMessage,
165 String duplicateMessageKey,
166 Integer poId) {
167
168
169 if (currentMessage.length() == 0) {
170 String messageText = configurationService.getPropertyValueAsString(PurapKeyConstants.MESSAGE_BULK_RECEIVING_DUPLICATE_PREFIX);
171 String prefix = MessageFormat.format(messageText, poId.toString());
172
173 currentMessage.append(prefix);
174 }
175
176
177 currentMessage.append(configurationService.getPropertyValueAsString(duplicateMessageKey));
178 }
179
180 @Override
181 public String getBulkReceivingDocumentNumberInProcessForPurchaseOrder(Integer poId,
182 String bulkReceivingDocumentNumber) {
183
184 String docNumberInProcess = StringUtils.EMPTY;
185
186 List<String> docNumbers = bulkReceivingDao.getDocumentNumbersByPurchaseOrderId(poId);
187 WorkflowDocument workflowDocument = null;
188
189 for (String docNumber : docNumbers) {
190
191 try {
192 workflowDocument = workflowDocumentService.loadWorkflowDocument(docNumber,
193 GlobalVariables.getUserSession().getPerson());
194 } catch (WorkflowException we) {
195 throw new RuntimeException(we);
196 }
197
198 if (!(workflowDocument.isCanceled() ||
199 workflowDocument.isException() ||
200 workflowDocument.isFinal()) &&
201 !docNumber.equals(bulkReceivingDocumentNumber)) {
202
203 docNumberInProcess = docNumber;
204 break;
205 }
206 }
207
208 return docNumberInProcess;
209 }
210
211 @Override
212 public void populateBulkReceivingFromPurchaseOrder(BulkReceivingDocument blkRecDoc) {
213
214 if (blkRecDoc != null) {
215 PurchaseOrderDocument poDoc = purchaseOrderService.getCurrentPurchaseOrder(blkRecDoc.getPurchaseOrderIdentifier());
216 if (poDoc != null) {
217 blkRecDoc.populateBulkReceivingFromPurchaseOrder(poDoc);
218 }
219 }
220
221 }
222
223 public BulkReceivingDocument getBulkReceivingByDocumentNumber(String documentNumber) {
224
225 if (ObjectUtils.isNotNull(documentNumber)) {
226 try {
227 BulkReceivingDocument doc = (BulkReceivingDocument) documentService.getByDocumentHeaderId(documentNumber);
228 if (ObjectUtils.isNotNull(doc)) {
229 WorkflowDocument workflowDocument = doc.getDocumentHeader().getWorkflowDocument();
230 doc.refreshReferenceObject(OLEPropertyConstants.DOCUMENT_HEADER);
231 doc.getDocumentHeader().setWorkflowDocument(workflowDocument);
232 }
233 return doc;
234 } catch (WorkflowException e) {
235 String errorMessage = "Error getting bulk receiving document from document service";
236 throw new RuntimeException(errorMessage, e);
237 }
238 }
239 return null;
240 }
241
242 @Override
243 public void performPrintReceivingTicketPDF(String blkDocId,
244 ByteArrayOutputStream baosPDF) {
245
246 BulkReceivingDocument blkRecDoc = getBulkReceivingByDocumentNumber(blkDocId);
247 Collection<String> generatePDFErrors = printService.generateBulkReceivingPDF(blkRecDoc, baosPDF);
248
249 if (!generatePDFErrors.isEmpty()) {
250 addStringErrorMessagesToMessageMap(PurapKeyConstants.ERROR_BULK_RECEIVING_PDF, generatePDFErrors);
251 throw new ValidationException("printing bulk receiving ticket failed");
252 }
253
254 }
255
256 protected void addStringErrorMessagesToMessageMap(String errorKey,
257 Collection<String> errors) {
258
259 if (ObjectUtils.isNotNull(errors)) {
260 for (String error : errors) {
261 LOG.error("Adding error message using error key '" + errorKey + "' with text '" + error + "'");
262 GlobalVariables.getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, errorKey, error);
263 }
264 }
265
266 }
267
268 public void setPrintService(PrintService printService) {
269 this.printService = printService;
270 }
271
272 public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
273 this.purchaseOrderService = purchaseOrderService;
274 }
275
276 public void setBulkReceivingDao(BulkReceivingDao bulkReceivingDao) {
277 this.bulkReceivingDao = bulkReceivingDao;
278 }
279
280 public void setDocumentService(DocumentService documentService) {
281 this.documentService = documentService;
282 }
283
284 public void setWorkflowDocumentService(WorkflowDocumentService workflowDocumentService) {
285 this.workflowDocumentService = workflowDocumentService;
286 }
287
288 public void setConfigurationService(ConfigurationService configurationService) {
289 this.configurationService = configurationService;
290 }
291 }
292