1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.service.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.ole.OLETranscationalRecordGenerator;
20 import org.kuali.ole.OleOrderRecords;
21 import org.kuali.ole.batch.bo.OLEBatchProcessJobDetailsBo;
22 import org.kuali.ole.batch.bo.OLEBatchProcessProfileBo;
23 import org.kuali.ole.batch.bo.OrderImportHelperBo;
24 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcNamedFieldsBean;
25 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
26 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
27 import org.kuali.ole.docstore.common.document.content.bib.marc.Collection;
28 import org.kuali.ole.pojo.OleOrderRecord;
29 import org.kuali.ole.pojo.edi.*;
30 import org.kuali.ole.select.OleSelectNotificationConstant;
31 import org.kuali.ole.select.businessobject.OleLoadFailureRecords;
32 import org.kuali.ole.select.businessobject.OleLoadProfile;
33 import org.kuali.ole.select.businessobject.OleLoadSumRecords;
34 import org.kuali.ole.select.constants.OleSelectPropertyConstants;
35 import org.kuali.ole.select.document.AcquisitionBatchInputFileDocument;
36 import org.kuali.ole.select.document.service.OleSelectDocumentService;
37 import org.kuali.ole.select.service.OleReqPOCreateDocumentService;
38 import org.kuali.ole.select.service.OleReqPOLoadTransactionsService;
39 import org.kuali.ole.sys.OLEConstants;
40 import org.kuali.ole.sys.OLEConstants.FinancialDocumentTypeCodes;
41 import org.kuali.ole.sys.batch.service.BatchInputFileService;
42 import org.kuali.ole.sys.context.SpringContext;
43 import org.kuali.rice.core.api.config.property.ConfigurationService;
44 import org.kuali.rice.core.api.datetime.DateTimeService;
45 import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
46 import org.kuali.rice.coreservice.api.parameter.Parameter;
47 import org.kuali.rice.coreservice.api.parameter.ParameterKey;
48 import org.kuali.rice.kew.api.exception.WorkflowException;
49 import org.kuali.rice.kim.api.identity.Person;
50 import org.kuali.rice.krad.UserSession;
51 import org.kuali.rice.krad.service.BusinessObjectService;
52 import org.kuali.rice.krad.service.DocumentService;
53 import org.kuali.rice.krad.util.GlobalVariables;
54 import org.springframework.beans.factory.InitializingBean;
55
56 import java.io.BufferedWriter;
57 import java.io.File;
58 import java.io.FileWriter;
59 import java.math.BigDecimal;
60 import java.sql.Timestamp;
61 import java.text.SimpleDateFormat;
62 import java.util.*;
63
64 public class OleReqPOLoadTransactionsServiceImpl implements OleReqPOLoadTransactionsService, InitializingBean {
65 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleReqPOLoadTransactionsServiceImpl.class);
66 private static final String dateFormat = "MMddyy";
67
68 protected BatchInputFileService batchInputFileService;
69 protected OleReqPOCreateDocumentService oleReqPOCreateDocumentService;
70 protected BuildVendorBibInfoBean buildVendorBibInfoBean;
71 protected Properties properties = null;
72 protected ConfigurationService kualiConfigurationService;
73 private OleSelectDocumentService oleSelectDocumentService;
74
75 @Override
76 public void afterPropertiesSet() throws Exception {
77
78 }
79
80
81 public BatchInputFileService getBatchInputFileService() {
82 return batchInputFileService;
83 }
84
85 public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
86 this.batchInputFileService = batchInputFileService;
87 }
88
89 public OleReqPOCreateDocumentService getOleReqPOCreateDocumentService() {
90 if (oleReqPOCreateDocumentService == null) {
91 oleReqPOCreateDocumentService = SpringContext.getBean(OleReqPOCreateDocumentService.class);
92 }
93 return oleReqPOCreateDocumentService;
94 }
95
96 public void setOleReqPOCreateDocumentService(OleReqPOCreateDocumentService oleReqPOCreateDocumentService) {
97 this.oleReqPOCreateDocumentService = oleReqPOCreateDocumentService;
98 }
99
100 public BuildVendorBibInfoBean getBuildVendorBibInfoBean() {
101 return buildVendorBibInfoBean;
102 }
103
104 public void setBuildVendorBibInfoBean(BuildVendorBibInfoBean buildVendorBibInfoBean) {
105 this.buildVendorBibInfoBean = buildVendorBibInfoBean;
106 }
107
108 public ConfigurationService getConfigurationService() {
109 if (kualiConfigurationService == null) {
110 kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
111 }
112 return kualiConfigurationService;
113 }
114
115 public void setConfigurationService(ConfigurationService kualiConfigurationService) {
116 this.kualiConfigurationService = kualiConfigurationService;
117 }
118
119
120 @Override
121 public List saveRequisitionDocument(OleOrderRecords oleOrderRecords, OLEBatchProcessJobDetailsBo job) throws Exception {
122
123
124
125
126
127
128 oleReqPOCreateDocumentService = getOleReqPOCreateDocumentService();
129 oleReqPOCreateDocumentService.saveRequisitionDocument(oleOrderRecords, job);
130 return job.getOrderImportHelperBo().getReqList();
131
132 }
133
134 @Override
135 public void createAcquisitionDocument(List reqList, OleOrderRecords oleOrderRecords, OLEBatchProcessJobDetailsBo job) {
136 OrderImportHelperBo orderImportHelperBo = job.getOrderImportHelperBo();
137 List<OleOrderRecord> oleOrderRecordList = oleOrderRecords.getRecords();
138 List<OleOrderRecord> failureOrderRecordList = new ArrayList<OleOrderRecord>();
139 List<OleOrderRecord> failureOrderRecordListForBFN = new ArrayList<OleOrderRecord>();
140 OleLoadSumRecords oleLoadSumRecords = new OleLoadSumRecords();
141
142
143
144 try {
145
146 String user = null;
147 if (oleOrderRecordList.size() > 0) {
148 user = GlobalVariables.getUserSession().getPrincipalName();
149 if (user == null) {
150 user = getConfigurationService().getPropertyValueAsString(
151 getOleSelectDocumentService().getSelectParameterValue(OleSelectNotificationConstant.ACCOUNT_DOCUMENT_INTIATOR));
152 }
153 GlobalVariables.setUserSession(new UserSession(user));
154 }
155 AcquisitionBatchInputFileDocument acqBatchDocument = createAcquisitionBatchUploadDocument();
156 oleLoadSumRecords.setDocumentNumber(acqBatchDocument.getDocumentNumber());
157 for (int i = 0; i < oleOrderRecordList.size(); i++) {
158 OleOrderRecord oleOrderRecord = oleOrderRecordList.get(i);
159
160
161 String isValidBFN = ((oleOrderRecord.getMessageMap().get("isValidBFN"))).toString();
162 String isValidRecord = ((oleOrderRecord.getMessageMap().get("isValidRecord"))).toString();
163
164 String isApoRule = ((oleOrderRecord.getMessageMap().get("isApoRule"))).toString();
165 if ((isValidBFN.equalsIgnoreCase("false")) || (isValidRecord.equalsIgnoreCase("false")) || ("true".equalsIgnoreCase(isApoRule))) {
166 failureOrderRecordList.add(oleOrderRecordList.get(i));
167 }
168 }
169 if(oleOrderRecordList.get(0).getOriginalEDIFileName() !=null) {
170 saveBatchSuccessRecord(oleLoadSumRecords, failureOrderRecordList.size(), reqList.size(), oleOrderRecordList.size() - failureOrderRecordList.size(), oleOrderRecordList.get(0).getOleOriginalBibRecordFileName() + " , " + oleOrderRecordList.get(0).getOriginalEDIFileName(), oleOrderRecordList.get(0).getDescription(), orderImportHelperBo.getOleBatchProcessProfileBo().getBatchProcessProfileId());
171 }
172 else {
173 saveBatchSuccessRecord(oleLoadSumRecords, failureOrderRecordList.size(), reqList.size(), oleOrderRecordList.size() - failureOrderRecordList.size(), oleOrderRecordList.get(0).getOleOriginalBibRecordFileName(), oleOrderRecordList.get(0).getDescription(), orderImportHelperBo.getOleBatchProcessProfileBo().getBatchProcessProfileId());
174 }
175 saveBatchFailureRecords(failureOrderRecordList, oleLoadSumRecords.getAcqLoadSumId());
176 if (reqList.size() > 0) {
177 createReqIdTextFile(reqList, oleLoadSumRecords.getAcqLoadSumId());
178 }
179
180 if (failureOrderRecordList.size() > 0) {
181 if(oleOrderRecordList.get(0).getOriginalEDIFileName() != null) {
182 createBibAndEdiFile(failureOrderRecordList, oleLoadSumRecords.getAcqLoadSumId());
183 }
184 else {
185 createBibFile(failureOrderRecordList, oleLoadSumRecords.getAcqLoadSumId());
186 }
187 }
188 } catch (Exception e) {
189 LOG.error("Exception in createAcquisitionDocument---- " + e);
190 throw new RuntimeException(e);
191 }
192 }
193
194 public void createBibAndEdiFile(List<OleOrderRecord> failureOrderRecordList, int acqLoadSumId) {
195 try {
196 BibMarcRecordProcessor bibMarcRecordProcessor=new BibMarcRecordProcessor();
197 OLETranscationalRecordGenerator transcationalRecordGenerator = new OLETranscationalRecordGenerator();
198 String bibXML = "";
199 String ediXML = "";
200 List<BibMarcRecord> bibMarcRecordList = new ArrayList<BibMarcRecord>();
201 List<EDIOrder> ediOrderList = new ArrayList<EDIOrder>();
202 List<EDIOrder> oleTxRecordList = new ArrayList<EDIOrder>();
203 Collection bibliographicRecords = new Collection();
204 EDIOrders ediOrders = new EDIOrders();
205 OleOrderRecord oleOrderRecord = null;
206 EDIOrder ediOrder = null;
207 String ybpNumber = "";
208 List<LineItemOrder> lineItemOrderList = new ArrayList<LineItemOrder>();
209 List<String> ybpNumberList = new ArrayList<String>();
210 for (int i = 0; i < failureOrderRecordList.size(); i++) {
211 String ediYbpNumber = "";
212 oleOrderRecord = failureOrderRecordList.get(i);
213 if (oleOrderRecord.getOriginalRecord() != null) {
214 bibMarcRecordList.add(oleOrderRecord.getOriginalRecord());
215 }
216 ediOrder = oleOrderRecord.getOriginalEdi();
217 if (oleOrderRecord.getOleTxRecord() != null) {
218 ybpNumber = oleOrderRecord.getOleTxRecord().getVendorItemIdentifier();
219 ybpNumberList.add(ybpNumber);
220 }
221 }
222 for (int j = 0; j < ybpNumberList.size(); j++) {
223 String ybp = ybpNumberList.get(j);
224 List<LineItemOrder> ediLineItemOrder = ediOrder.getLineItemOrder();
225 for (LineItemOrder lineItemOrder : ediLineItemOrder) {
226 BuyerReferenceInformation buyerReferenceInformation = lineItemOrder.getBuyerReferenceInformation().get(0);
227 BuyerLineItemReference buyerLineItemReference = buyerReferenceInformation.getBuyerLineItemReference().get(0);
228 String orderLineNumber = buyerLineItemReference.getOrderLineNumber();
229 if (ybp.equals(orderLineNumber)) {
230
231 lineItemOrderList.add(lineItemOrder);
232
233 }
234 }
235
236 }
237 if (lineItemOrderList.size() > 0) {
238 ediOrder.getLineItemOrder().clear();
239 ediOrder.setLineItemOrder(lineItemOrderList);
240 ediOrderList.add(ediOrder);
241 ediOrders.setOrders(ediOrderList);
242 ediXML = transcationalRecordGenerator.toXml(ediOrders);
243 }
244 if (bibMarcRecordList.size() > 0) {
245 bibliographicRecords.setRecords(bibMarcRecordList);
246 bibXML = bibMarcRecordProcessor.generateXML(bibMarcRecordList);
247 }
248
249
250 if (!"".equals(bibXML)) {
251 BufferedWriter bibOut = new BufferedWriter(new FileWriter(getDestinationPath() + acqLoadSumId + OLEConstants.BATCH_FAILURE_BIB_FILE_ETN));
252 bibOut.write(bibXML);
253 bibOut.close();
254 }
255 if (!"".equals(ediXML)) {
256 BufferedWriter ediOut = new BufferedWriter(new FileWriter(getDestinationPath() + acqLoadSumId + OLEConstants.BATCH_FAILURE_EDI_FILE_ETN));
257 ediOut.write(ediXML);
258 ediOut.close();
259 }
260 } catch (Exception ex) {
261 LOG.error("createBibAndEdiFile() method -- Exception : " + ex);
262 throw new RuntimeException(ex);
263 }
264
265 }
266
267 public void createBibFile(List<OleOrderRecord> failureOrderRecordList, int acqLoadSumId) {
268 try {
269 BibMarcRecordProcessor bibMarcRecordProcessor=new BibMarcRecordProcessor();
270
271 OLETranscationalRecordGenerator transcationalRecordGenerator = new OLETranscationalRecordGenerator();
272 String bibXML = "";
273
274 List<BibMarcRecord> bibliographicRecordList = new ArrayList<BibMarcRecord>();
275 List<EDIOrder> ediOrderList = new ArrayList<EDIOrder>();
276 List<EDIOrder> oleTxRecordList = new ArrayList<EDIOrder>();
277 Collection bibliographicRecords = new Collection();
278
279 OleOrderRecord oleOrderRecord = null;
280
281 String ybpNumber = "";
282 List<LineItemOrder> lineItemOrderList = new ArrayList<LineItemOrder>();
283 List<String> ybpNumberList = new ArrayList<String>();
284 for (int i = 0; i < failureOrderRecordList.size(); i++) {
285 String ediYbpNumber = "";
286 oleOrderRecord = failureOrderRecordList.get(i);
287 if (oleOrderRecord.getOriginalRecord() != null) {
288 bibliographicRecordList.add(oleOrderRecord.getOriginalRecord());
289 }
290
291 if (oleOrderRecord.getOleTxRecord() != null) {
292 ybpNumber = oleOrderRecord.getOleTxRecord().getVendorItemIdentifier();
293 ybpNumberList.add(ybpNumber);
294 }
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318 if (bibliographicRecordList.size() > 0) {
319 bibliographicRecords.setRecords(bibliographicRecordList);
320 bibXML = bibMarcRecordProcessor.generateXML(bibliographicRecordList);
321 }
322
323
324 if (!"".equals(bibXML)) {
325 BufferedWriter bibOut = new BufferedWriter(new FileWriter(getDestinationPath() + acqLoadSumId + OLEConstants.BATCH_FAILURE_BIB_FILE_ETN));
326 bibOut.write(bibXML);
327 bibOut.close();
328 }
329
330
331
332
333
334 } catch (Exception ex) {
335 LOG.error("createBibAndEdiFile() method -- Exception : " + ex);
336 throw new RuntimeException(ex);
337 }
338
339 }
340
341 protected AcquisitionBatchInputFileDocument createAcquisitionBatchUploadDocument() throws WorkflowException {
342 String user;
343 if (GlobalVariables.getUserSession() != null) {
344 user = GlobalVariables.getUserSession().getPrincipalName();
345 } else {
346 user = getConfigurationService().getPropertyValueAsString(getOleSelectDocumentService().getSelectParameterValue(OleSelectNotificationConstant.ACCOUNT_DOCUMENT_INTIATOR));
347 }
348 GlobalVariables.setUserSession(new UserSession(user));
349 AcquisitionBatchInputFileDocument acqBatchDocument = (AcquisitionBatchInputFileDocument) SpringContext.getBean(DocumentService.class).getNewDocument(FinancialDocumentTypeCodes.ACQ_BATCH_UPLOAD);
350 acqBatchDocument.getDocumentHeader().setDocumentDescription(OLEConstants.MANUAL_INGEST_DOCUMENT_DESCRIPTION + (StringUtils.isEmpty(getOperatorInitials()) ? "" : "_" + getOperatorInitials()) + "_" + getCurrentDate());
351 SpringContext.getBean(DocumentService.class).saveDocument(acqBatchDocument);
352 return acqBatchDocument;
353 }
354
355
356 protected String getCurrentDate() {
357 LOG.debug("Inside getCurrentDate()");
358
359 DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
360 Date date = dateTimeService.getCurrentDate();
361 SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
362 String currentDate = sdf.format(date);
363 LOG.debug("End of getCurrentDate()");
364 return currentDate;
365 }
366
367 protected String getOperatorInitials() {
368 LOG.debug("Inside getOperatorInitials()");
369 StringBuffer operatorInitials = new StringBuffer();
370 Person person = GlobalVariables.getUserSession().getPerson();
371 operatorInitials.append(StringUtils.isEmpty(person.getFirstName()) ? "" : person.getFirstName().toLowerCase().charAt(0));
372 operatorInitials.append(StringUtils.isEmpty(person.getMiddleName()) ? "" : person.getMiddleName().toLowerCase().charAt(0));
373 operatorInitials.append(StringUtils.isEmpty(person.getLastName()) ? "" : person.getLastName().toLowerCase().charAt(0));
374 LOG.debug("End of getOperatorInitials()");
375 return operatorInitials.toString();
376 }
377
378 protected void saveBatchFailureRecords(List<OleOrderRecord> failureOrderRecordList, Integer acqLoadId) {
379
380 for (int i = 0; i < failureOrderRecordList.size(); i++) {
381 OleOrderRecord oleOrderRecord = failureOrderRecordList.get(i);
382 String isValidRecord = ((oleOrderRecord.getMessageMap().get("isValidRecord"))).toString();
383 if ("true".equals(isValidRecord)) {
384 OleLoadFailureRecords oleLoadFailureRecords = new OleLoadFailureRecords();
385 oleLoadFailureRecords.setAcqLoadSumId(acqLoadId);
386
387 String isValidBFN = ((oleOrderRecord.getMessageMap().get("isValidBFN"))).toString();
388 String isBadControlField = ((oleOrderRecord.getMessageMap().get("isBadControlField"))).toString();
389 String isApoRule = ((oleOrderRecord.getMessageMap().get("isApoRule"))).toString();
390 if (isValidBFN.equals("false")) {
391 oleLoadFailureRecords.setErrorId(new BigDecimal("7"));
392 } else if ("true".equals(isBadControlField)) {
393 oleLoadFailureRecords.setErrorId(new BigDecimal("8"));
394 } else if ("true".equals(isApoRule)) {
395 oleLoadFailureRecords.setErrorId(new BigDecimal("9"));
396 } else {
397 oleLoadFailureRecords.setErrorId(new BigDecimal("5"));
398 }
399 oleLoadFailureRecords.setVendorId("");
400 setBibliographicInformation(oleLoadFailureRecords, oleOrderRecord.getOriginalRecord());
401 SpringContext.getBean(BusinessObjectService.class).save(oleLoadFailureRecords);
402 }
403 }
404 }
405
406 protected void saveBatchSuccessRecord(OleLoadSumRecords oleLoadSumRecords, int dupRecords, int poSucRecords, int sucRecords, String fileName, String description, String batchProcessProfileId) {
407 if (LOG.isDebugEnabled()) {
408 LOG.debug("dupRecords =====================" + dupRecords);
409 LOG.debug("sucRecords =====================" + sucRecords);
410 LOG.debug("poSucRecords =====================" + poSucRecords);
411 }
412 oleLoadSumRecords.setAcqLoadDescription(description);
413 oleLoadSumRecords.setFileName(fileName);
414 oleLoadSumRecords.setBatchProcessProfileId(batchProcessProfileId);
415 oleLoadSumRecords.setAcqLoadFailCount(dupRecords);
416 oleLoadSumRecords.setAcqLoadSuccCount(sucRecords);
417 oleLoadSumRecords.setAcqLoadTotCount(dupRecords + sucRecords);
418 oleLoadSumRecords.setAcqLoadPoTotCount(poSucRecords);
419 oleLoadSumRecords.setPrincipalId(GlobalVariables.getUserSession().getPrincipalName());
420 Timestamp ts = SpringContext.getBean(DateTimeService.class).getCurrentTimestamp();
421 oleLoadSumRecords.setLoadCreatedDate(ts);
422 SpringContext.getBean(BusinessObjectService.class).save(oleLoadSumRecords);
423
424 }
425
426 protected void createReqIdTextFile(List reqList, Integer acqSumId) {
427 try {
428 BufferedWriter out = new BufferedWriter(new FileWriter(getDestinationPath() + acqSumId + OLEConstants.BATCH_REQ_ID_FILE));
429 StringBuffer reqIds = new StringBuffer();
430 for (int i = 0; i < reqList.size(); i++) {
431 if (reqList.get(i) != null || "".equals(reqList.get(i))) {
432 reqIds.append(reqList.get(i).toString() + ",");
433 }
434 }
435 out.write(reqIds.toString());
436 out.close();
437 } catch (Exception ex) {
438 }
439
440 }
441
442 protected BigDecimal createAgendaProfile(String agendaName) {
443 Map profileMap = new HashMap();
444 profileMap.put("profile", agendaName);
445 OleLoadProfile oleLoadProfile = null;
446 int count = SpringContext.getBean(BusinessObjectService.class).countMatching(OleLoadProfile.class, profileMap);
447 if (count == 0) {
448 oleLoadProfile = new OleLoadProfile();
449 oleLoadProfile.setProfile(agendaName);
450 oleLoadProfile.setActive(true);
451 SpringContext.getBean(BusinessObjectService.class).save(oleLoadProfile);
452 } else {
453 oleLoadProfile = (OleLoadProfile) ((List) SpringContext.getBean(BusinessObjectService.class).findMatching(OleLoadProfile.class, profileMap)).get(0);
454 }
455
456 return oleLoadProfile.getProfileId();
457 }
458
459 public String getDestinationPath() {
460 String destinationPath = getConfigurationService().getPropertyValueAsString(OLEConstants.STAGING_DIRECTORY_KEY) + getParameter(OleSelectPropertyConstants.STAFF_UPLOAD_DESTINATIONPATH);
461 File dirCheck = (new File(destinationPath));
462 boolean isDir = dirCheck.exists();
463 if (LOG.isDebugEnabled()) {
464 LOG.debug("dirCheck =====================" + dirCheck);
465 }
466 if (!isDir) {
467 dirCheck.mkdir();
468 }
469 return destinationPath;
470 }
471
472 protected void setBibliographicInformation(OleLoadFailureRecords oleLoadFailureRecord, BibMarcRecord originalRecord) {
473 BibMarcNamedFieldsBean bibliographicNamedFieldsBean = new BibMarcNamedFieldsBean();
474 bibliographicNamedFieldsBean.setBibliographicRecord(originalRecord);
475 oleLoadFailureRecord.setTitle(bibliographicNamedFieldsBean.getTitle());
476 oleLoadFailureRecord.setIsbn(bibliographicNamedFieldsBean.getFieldNameFor("020", "a"));
477 }
478
479 public String getParameter(String name){
480 ParameterKey parameterKey = ParameterKey.create(org.kuali.ole.OLEConstants.APPL_ID, org.kuali.ole.OLEConstants.SELECT_NMSPC, org.kuali.ole.OLEConstants.SELECT_CMPNT,name);
481 Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
482 return parameter!=null?parameter.getValue():null;
483 }
484
485 public OleSelectDocumentService getOleSelectDocumentService() {
486 if(oleSelectDocumentService == null){
487 oleSelectDocumentService = SpringContext.getBean(OleSelectDocumentService.class);
488 }
489 return oleSelectDocumentService;
490 }
491
492 public void setOleSelectDocumentService(OleSelectDocumentService oleSelectDocumentService) {
493 this.oleSelectDocumentService = oleSelectDocumentService;
494 }
495
496 }