View Javadoc
1   package org.kuali.ole.ingest;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.*;
5   import org.kuali.ole.batch.bo.*;
6   import org.kuali.ole.batch.document.OLEBatchProcessDefinitionDocument;
7   import org.kuali.ole.batch.ingest.BatchProcessBibImport;
8   import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
9   import org.kuali.ole.docstore.common.document.Bib;
10  import org.kuali.ole.docstore.common.document.content.bib.marc.*;
11  import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
12  import org.kuali.ole.docstore.common.document.ids.BibId;
13  import org.kuali.ole.docstore.common.search.SearchParams;
14  import org.kuali.ole.docstore.common.search.SearchResponse;
15  import org.kuali.ole.docstore.common.search.SearchResult;
16  import org.kuali.ole.docstore.common.search.SearchResultField;
17  import org.kuali.ole.docstore.model.enums.DocType;
18  import org.kuali.ole.ingest.pojo.IngestRecord;
19  import org.kuali.ole.pojo.OleBibRecord;
20  import org.kuali.ole.pojo.OleOrderRecord;
21  import org.kuali.ole.pojo.OleTxRecord;
22  import org.kuali.ole.pojo.edi.*;
23  import org.kuali.ole.service.OleOrderRecordService;
24  import org.kuali.ole.sys.context.SpringContext;
25  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
26  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
27  import org.kuali.rice.coreservice.api.parameter.Parameter;
28  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
29  import org.kuali.rice.krad.service.BusinessObjectService;
30  import org.kuali.rice.krad.service.KRADServiceLocator;
31  import org.kuali.rice.krms.api.engine.*;
32  import org.xml.sax.SAXException;
33  
34  import javax.xml.parsers.ParserConfigurationException;
35  import javax.xml.xpath.XPathExpressionException;
36  import java.io.IOException;
37  import java.net.URISyntaxException;
38  import java.util.*;
39  
40  /**
41   * AbstractIngestProcessor which does the pre-processing for Staff Upload process to create Requisition.
42   */
43  public abstract class AbstractIngestProcessor {
44      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AbstractIngestProcessor.class);
45      private static final String NAMESPACE_CODE_SELECTOR = "namespaceCode";
46      private static final String NAME_SELECTOR = "name";
47      private BusinessObjectService businessObjectService;
48      private OleOrderRecordService oleOrderRecordService;
49      private List<EngineResults> engineResults = new ArrayList<EngineResults>();
50      private String user;
51      String marcXMLContent;
52      String ediXMLContent;
53      private IngestRecord ingestRecord;
54      private DocstoreClientLocator docstoreClientLocator;
55      private List<OleOrderRecord> oleOrderRecordList = new ArrayList<OleOrderRecord>();
56      //private DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
57  
58      public List<OleOrderRecord> getOleOrderRecordList() {
59          return oleOrderRecordList;
60      }
61  
62      public void setOleOrderRecordList(List<OleOrderRecord> oleOrderRecordList) {
63          this.oleOrderRecordList = oleOrderRecordList;
64      }
65  
66      public DocstoreClientLocator getDocstoreClientLocator() {
67          if (null == docstoreClientLocator) {
68              return  SpringContext.getBean(DocstoreClientLocator.class);
69          }
70          return docstoreClientLocator;
71      }
72  
73      public void setUser(String user) {
74          this.user = user;
75      }
76  
77      /**
78       *  Gets the engineResults attribute.
79       * @return engineResults
80       */
81  
82      public List<EngineResults> getEngineResults() {
83          return engineResults;
84      }
85  
86  
87      /**
88       *   This method takes the initial request when Ingesting a record.
89       * @param ingestRecord
90       * @param processDef
91       * @param job
92       */
93  
94      public int start(IngestRecord ingestRecord, boolean failure_flag, OLEBatchProcessDefinitionDocument processDef, OLEBatchProcessJobDetailsBo job) {
95          this.ingestRecord = ingestRecord;
96          this.setUser(ingestRecord.getUser());
97          if(ingestRecord.getEdiFileContent() != null) {
98              byPassLogicForPreProcess(ingestRecord.getMarcFileContent(), ingestRecord.getEdiFileContent(), ingestRecord.getByPassPreProcessing(), job);
99          }
100         else {
101             byPassLogicForPreProcess(ingestRecord.getMarcFileContent(), ingestRecord.getByPassPreProcessing(), job);
102         }
103         try {
104             if(ingestRecord.getEdiFileContent() != null){
105                 marcEdiprocess(processDef, job);
106             }
107             else {
108                 marcProcess(processDef, job);
109             }
110             ingestRecord.setUpdate(this.ingestRecord.isUpdate());
111         } catch (URISyntaxException e) {
112             LOG.info(e.getMessage());
113         } catch (IOException e) {
114             LOG.info(e.getMessage());
115         } catch (SAXException e) {
116             LOG.info(e.getMessage());
117         } catch (XPathExpressionException e) {
118             LOG.info(e.getMessage());
119         } catch (ParserConfigurationException e) {
120             LOG.info(e.getMessage());
121         } catch (InterruptedException e) {
122             LOG.info(e.getMessage());
123         } catch (Exception e) {
124             LOG.info(e.getMessage());
125         }
126 
127         postProcess(job);
128         int recordCount=getOleOrderRecordList().size();
129         return recordCount;
130     }
131 
132     /**
133      * This method forwards to preProcess method if preProcessReq is true else sets the marcXmlContent and ediXmlContent.
134      * @param rawMarcContent
135      * @param rawEdiContent
136      * @param preProcessingReq
137      */
138     private void byPassLogicForPreProcess(String rawMarcContent, String rawEdiContent, boolean preProcessingReq, OLEBatchProcessJobDetailsBo job) {
139         if (preProcessingReq) {
140             preProcess(rawMarcContent, rawEdiContent, job);
141         } else {
142             marcXMLContent = rawMarcContent;
143             ediXMLContent = rawEdiContent;
144         }
145     }
146 
147 
148     public void byPassLogicForPreProcess(String rawMarcContent, boolean preProcessingReq, OLEBatchProcessJobDetailsBo job) {
149         if (preProcessingReq) {
150             preProcess(rawMarcContent, job);
151         } else {
152             marcXMLContent = rawMarcContent;
153         }
154     }
155 
156 
157     private void preProcess(String rawMarcContent, OLEBatchProcessJobDetailsBo job) {
158         marcXMLContent = preProcessMarc(rawMarcContent, job);
159     }
160     /**
161      *   This method forwards to preProcessMarc and preProcessEDI method.
162      * @param rawMarcContent
163      * @param rawEdiContent
164      */
165     private void preProcess(String rawMarcContent, String rawEdiContent , OLEBatchProcessJobDetailsBo job) {
166         marcXMLContent = preProcessMarc(rawMarcContent, job);
167         ediXMLContent = preProcessEDI(rawEdiContent);
168     }
169 
170 
171     /**
172      * This method is for building Order Record values based on ingested marc file/data mapping or constant and default values from profile.
173      * @throws Exception
174      * @param processDef
175      * @param job
176      */
177     public boolean marcProcess(OLEBatchProcessDefinitionDocument processDef, OLEBatchProcessJobDetailsBo job) throws Exception {
178         LOG.info("----Inside marcProcess()------------------------------");
179         OLEBatchProcessProfileBo oleBatchProcessProfileBo = processDef.getOleBatchProcessProfileBo();
180         OrderImportHelperBo orderImportHelperBo = job.getOrderImportHelperBo();
181         orderImportHelperBo.setOrderImportSuccessCount(0);
182         orderImportHelperBo.setOrderImportFailureCount(0);
183         orderImportHelperBo.setOleBatchProcessProfileBo(oleBatchProcessProfileBo);
184         orderImportHelperBo.setFailureReason(new ArrayList<String>());
185         oleOrderRecordService = SpringContext.getBean(OleOrderRecordService.class);
186         BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
187         BibMarcRecords bibMarcRecords = bibMarcRecordProcessor.fromXML(marcXMLContent);
188         List<BibMarcRecord> records = bibMarcRecords.getRecords();
189         OLEBatchProcessProfileBo oleBatchProcessProfileBoForBibImport = getBibImportProfile(oleBatchProcessProfileBo);
190         BatchProcessBibImport batchProcessBibImport = new BatchProcessBibImport(processDef, job);
191         batchProcessBibImport.setOleBatchProcessProfileBo(oleBatchProcessProfileBoForBibImport);
192         int bibMarcRecordsCount = records.size();
193         int noOfProcessedOrderRecords = 0;
194         List<OleTxRecord> oleTxRecords = oleOrderRecordService.getQuantityItemPartsLocation(records, job);
195         if (bibMarcRecordsCount > 0) {
196             noOfProcessedOrderRecords = bibMarcRecordsCount - oleTxRecords.size();
197         }
198         OLEBatchBibImportDataObjects oleBatchBibImportDataObjects = new OLEBatchBibImportDataObjects();
199         List<OrderBibMarcRecord> orderBibMarcRecordList = oleBatchBibImportDataObjects.processBibImport(records,batchProcessBibImport);
200         if (orderBibMarcRecordList != null && orderBibMarcRecordList.size() > 0) {
201             for (int recordCount = 0; recordCount < orderBibMarcRecordList.size(); recordCount++) {
202                 OrderBibMarcRecord orderBibMarcRecord = orderBibMarcRecordList.get(recordCount);
203                 if (orderBibMarcRecord != null && orderBibMarcRecord.getBibId() != null && orderBibMarcRecord.getBibId().getId() != null) {
204                     String bibId = orderBibMarcRecord.getBibId().getId();
205                     OleOrderRecord oleOrderRecord = oleOrderRecordService.fetchOleOrderRecordForMarc(bibId, orderBibMarcRecord.getBibMarcRecord(), recordCount, job);
206                     try{
207                         setBibValues(oleOrderRecord, orderBibMarcRecord.getBibId());
208                         if (oleBatchProcessProfileBoForBibImport.getDataToImport() != null) {
209                             if (oleBatchProcessProfileBoForBibImport.getDataToImport().equals(OLEConstants.BIB_DATA_ONLY)
210                                     || oleBatchProcessProfileBoForBibImport.getDataToImport().equals(OLEConstants.BIB_INS)) {
211                                 oleOrderRecord.setLinkToOrderOption(OLEConstants.ORDER_RECORD_IMPORT_MARC_ONLY_PRINT);
212                             } else if (oleBatchProcessProfileBoForBibImport.getDataToImport().equals(OLEConstants.BIB_EINS)) {
213                                 oleOrderRecord.setLinkToOrderOption(OLEConstants.ORDER_RECORD_IMPORT_MARC_ONLY_ELECTRONIC);
214                             } else if (oleBatchProcessProfileBoForBibImport.getDataToImport().equals(OLEConstants.BIB_INS_EINS)) {
215                                 oleOrderRecord.setLinkToOrderOption(OLEConstants.ORDER_RECORD_IMPORT_MARC_ONLY_PRINT_ELECTRONIC);
216                             }
217                         }
218                         //getValueForVendorProfileCodeFromProfile(getOleBatchProcessProfileBo());
219                         orderImportHelperBo = job.getOrderImportHelperBo();
220                         orderImportHelperBo.setBibMarcRecord(orderBibMarcRecord.getBibMarcRecord());
221                         orderImportHelperBo.setOleBatchProcessProfileBo(oleBatchProcessProfileBo);
222                         orderImportHelperBo.setEdiXMLContent(this.ediXMLContent);
223                         orderImportHelperBo.setAgendaName(ingestRecord.getAgendaName());
224                         if (oleOrderRecord == null) {
225                             ingestRecord.setUpdate(true);
226                             oleOrderRecord = new OleOrderRecord();
227                             oleOrderRecord.setOriginalRecord(orderBibMarcRecord.getBibMarcRecord());
228                             OleTxRecord oleTxRecord = new OleTxRecord();
229                             oleOrderRecord.setOleTxRecord(oleTxRecord);
230                         }
231                         oleOrderRecord.setAgendaName(ingestRecord.getAgendaName());
232                         oleOrderRecord.setOleOriginalBibRecordFileName(ingestRecord.getOriginalMarcFileName());
233                         oleOrderRecord.setOriginalEDIFileName(ingestRecord.getOriginalEdiFileName());
234                         oleOrderRecord.setDescription(ingestRecord.getAgendaDescription());
235                         boolean validBFNFlag = (Boolean) (oleOrderRecord.getMessageMap().get(OLEConstants.IS_VALID_BFN) == null ? true : oleOrderRecord.getMessageMap().get(OLEConstants.IS_VALID_BFN));
236                         oleOrderRecord.addMessageToMap(OLEConstants.IS_VALID_RECORD, true);
237                         oleOrderRecord.addMessageToMap(OLEConstants.IS_VALID_BFN, validBFNFlag);
238                         oleOrderRecordList.add(oleOrderRecord);
239                     }
240                     catch (Exception e){
241                         setOleOrderRecordAsFailureRecord(job);
242                         createErrorLog(job, orderBibMarcRecord.getBibMarcRecord(),OLEConstants.BIB_IMPORT_FAILURE_REASON);
243                     }
244                 } else if (orderBibMarcRecord != null && orderBibMarcRecord.getFailureReason() != null && orderBibMarcRecord.getBibMarcRecord() != null) {
245                     setOleOrderRecordAsFailureRecord(job);
246                     createErrorLog(job, orderBibMarcRecord.getBibMarcRecord(), orderBibMarcRecord.getFailureReason());
247                 } else {
248                     setOleOrderRecordAsFailureRecord(job);
249                     createErrorLog(job, orderBibMarcRecord.getBibMarcRecord());
250                 }
251             }
252             orderImportHelperBo.setCreateBibCount(orderBibMarcRecordList.get(orderBibMarcRecordList.size()-1).getCreateBibCount());
253             orderImportHelperBo.setUpdateBibCount(orderBibMarcRecordList.get(orderBibMarcRecordList.size() - 1).getUpdateBibCount());
254             orderImportHelperBo.setCreateHoldingsCount(orderBibMarcRecordList.get(orderBibMarcRecordList.size() - 1).getCreateHoldingsCount());
255             orderImportHelperBo.setUpdateHoldingsCount(orderBibMarcRecordList.get(orderBibMarcRecordList.size()-1).getUpdateHoldingsCount());
256         }
257         else {
258             for (int count = 0; count < records.size(); count++) {
259                 createErrorLog(job, records.get(count));
260                 setOleOrderRecordAsFailureRecord(job);
261             }
262         }
263         if (noOfProcessedOrderRecords > 0) {
264             for (int i = 0; i < noOfProcessedOrderRecords; i++) {
265                 setOleOrderRecordAsFailureRecord(job);
266             }
267         }
268         job.setUpdateBibCount(0);
269         job.setCreateHoldingsCount(0);
270         LOG.info("----End of marcProcess()------------------------------");
271         return true;
272     }
273 
274     private void setOleOrderRecordAsFailureRecord(OLEBatchProcessJobDetailsBo job) {
275         OrderImportHelperBo orderImportHelperBo = job.getOrderImportHelperBo();
276         OleOrderRecord oleOrderRecord = new OleOrderRecord();
277         //Below fields are set to false when the record is not process to due invalid location/quantity/parts
278         oleOrderRecord.addMessageToMap(OLEConstants.IS_VALID_RECORD, false);
279         oleOrderRecord.addMessageToMap(OLEConstants.IS_VALID_BFN, false);
280         oleOrderRecord.addMessageToMap(OLEConstants.IS_APO_RULE, false);
281         //dataCarrierService.addData(OLEConstants.ORDER_IMPORT_FAILURE_COUNT, (int) dataCarrierService.getData(OLEConstants.ORDER_IMPORT_FAILURE_COUNT) + 1);
282         orderImportHelperBo.setOrderImportFailureCount(orderImportHelperBo.getOrderImportFailureCount() + 1);
283         oleOrderRecordList.add(oleOrderRecord);
284     }
285 
286     /**
287      * This method returns Bib Import Profile based on which we can create the bib.
288      */
289     private OLEBatchProcessProfileBo getBibImportProfile(OLEBatchProcessProfileBo oleBatchProcessProfileBo){
290         String bibImportProfileForOrderRecord = oleBatchProcessProfileBo.getBibImportProfileForOrderRecord();
291         org.kuali.rice.krad.service.BusinessObjectService
292                 businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
293         Map<String,String> bibImportProfileMap = new HashMap<>();
294         bibImportProfileMap.put(OLEConstants.BATCH_PROFILE_NM,bibImportProfileForOrderRecord);
295         List<OLEBatchProcessProfileBo> oleBatchProcessProfileBoList = (List) businessObjectService.findMatching(OLEBatchProcessProfileBo.class, bibImportProfileMap);
296         if(oleBatchProcessProfileBoList != null && oleBatchProcessProfileBoList.size() > 0){
297             return oleBatchProcessProfileBoList.get(0);
298         }
299         return null;
300     }
301 
302     /**
303      * This method populate the value for vendor profile code from profile.
304      * @param oleBatchProcessProfileBo
305      * @return Vendor Profile Code.
306      */
307     private String getValueForVendorProfileCodeFromProfile(OLEBatchProcessProfileBo oleBatchProcessProfileBo){
308         LOG.info("----Inside getValueForVendorProfileCodeFromProfile()------------------------------");
309         List<OLEBatchProcessProfileMappingOptionsBo> oleBatchProcessProfileMappingOptionsBoList = oleBatchProcessProfileBo.getOleBatchProcessProfileMappingOptionsList();
310         for (OLEBatchProcessProfileMappingOptionsBo oleBatchProcessProfileMappingOptionsBo : oleBatchProcessProfileMappingOptionsBoList) {
311             List<OLEBatchProcessProfileDataMappingOptionsBo> oleBatchProcessProfileDataMappingOptionsBoList = oleBatchProcessProfileMappingOptionsBo.getOleBatchProcessProfileDataMappingOptionsBoList();
312             for (int dataMapCount = 0;dataMapCount<oleBatchProcessProfileDataMappingOptionsBoList.size();dataMapCount++) {
313                 String sourceField = oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getSourceField();
314                 String sourceFields[] = sourceField.split("\\$");
315                 if (sourceFields.length == 2) {
316                     if (OLEConstants.OLEBatchProcess.VENDOR_PROFILE_CODE.equals(oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationField())) {
317                         return oleBatchProcessProfileDataMappingOptionsBoList.get(dataMapCount).getDestinationFieldValue();
318                     }
319                 }
320             }
321         }
322         List<OLEBatchProcessProfileConstantsBo> oleBatchProcessProfileConstantsBoList = oleBatchProcessProfileBo.getOleBatchProcessProfileConstantsList();
323         for (OLEBatchProcessProfileConstantsBo oleBatchProcessProfileConstantsBo : oleBatchProcessProfileConstantsBoList) {
324             if (StringUtils.isNotBlank(oleBatchProcessProfileConstantsBo.getDataType()) && OLEConstants.OLEBatchProcess.ORDER_IMPORT.equalsIgnoreCase(oleBatchProcessProfileConstantsBo.getDataType())
325                     && StringUtils.isNotBlank(oleBatchProcessProfileConstantsBo.getAttributeValue()) && StringUtils.isNotBlank(oleBatchProcessProfileConstantsBo.getAttributeName())) {
326                 if (OLEConstants.OLEBatchProcess.CONSTANT.equals(oleBatchProcessProfileConstantsBo.getDefaultValue())) {
327                     if (OLEConstants.OLEBatchProcess.VENDOR_PROFILE_CODE.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
328                         return oleBatchProcessProfileConstantsBo.getAttributeValue();
329                     }
330                 }
331                 else if (OLEConstants.OLEBatchProcess.DEFAULT.equals(oleBatchProcessProfileConstantsBo.getDefaultValue())) {
332                     if (OLEConstants.OLEBatchProcess.VENDOR_PROFILE_CODE.equals(oleBatchProcessProfileConstantsBo.getAttributeName())) {
333                         return oleBatchProcessProfileConstantsBo.getAttributeValue();
334                     }
335                 }
336             }
337         }
338         return null;
339     }
340 
341     /**
342      * This method returns isbn value from the ingested file.
343      * @param lineItemOrder
344      */
345     private String getISBN(LineItemOrder lineItemOrder){
346         if(lineItemOrder.getProductFunction()!= null && lineItemOrder.getProductFunction().size() > 0){
347             if(lineItemOrder.getProductFunction().get(0).getProductArticleNumber() != null && lineItemOrder.getProductFunction().get(0).getProductArticleNumber().size() > 0){
348                 if(lineItemOrder.getProductFunction().get(0).getProductArticleNumber().get(0).getProductIsbn() != null){
349                     return lineItemOrder.getProductFunction().get(0).getProductArticleNumber().get(0).getProductIsbn();
350                 }
351             }
352         }
353         return null;
354     }
355 
356     /**
357      * This method takes the value of isbn and returns corresponding bib id value.
358      * @param isbn
359      * @return
360      * @throws Exception
361      */
362     private String getBibId(String isbn) throws Exception {
363         ISBNUtil isbnUtil = new ISBNUtil();
364         isbn = isbnUtil.normalizeISBN(isbn);
365         SearchParams searchParams = new SearchParams();
366         searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.BIB.getCode(), Bib.ISBN, isbn), "AND"));
367         searchParams.getSearchResultFields().add(searchParams.buildSearchResultField(DocType.BIB.getCode(), "ID"));
368         SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
369         if (searchResponse.getSearchResults().size() > 0) {
370                 SearchResult searchResult = searchResponse.getSearchResults().get(searchResponse.getSearchResults().size()-1);
371                 for(SearchResultField searchResultField : searchResult.getSearchResultFields()) {
372                     if(searchResultField.getFieldName().equalsIgnoreCase("ID") ) {
373                         return searchResultField.getFieldValue();
374                     }
375                 }
376         }
377         return null;
378     }
379 
380     /**
381      * This method sets the value for bib details.
382      * @param oleOrderRecord
383      * @param bibId
384      * @throws Exception
385      */
386     private void setBibValues(OleOrderRecord oleOrderRecord,String bibId) throws Exception{
387         oleOrderRecord.setBibId(bibId);
388         OleBibRecord oleBibRecord = new OleBibRecord();
389         oleBibRecord.setBibUUID(bibId);
390         oleBibRecord.setBib(getDocstoreClientLocator().getDocstoreClient().retrieveBib(bibId));
391         oleOrderRecord.setOleBibRecord(oleBibRecord);
392     }
393 
394     /**
395      * This method sets the value for bib details.
396      * @param oleOrderRecord
397      * @param bibTree
398      * @throws Exception
399      */
400     private void setBibValues(OleOrderRecord oleOrderRecord, BibId bibTree) throws Exception {
401         oleOrderRecord.setBibTree(bibTree);
402         oleOrderRecord.setBibId(bibTree.getId());
403         OleBibRecord oleBibRecord = new OleBibRecord();
404         oleBibRecord.setBibUUID(bibTree.getId());
405         oleBibRecord.setBib(getDocstoreClientLocator().getDocstoreClient().retrieveBib(bibTree.getId()));
406         oleOrderRecord.setOleBibRecord(oleBibRecord);
407     }
408 
409     /**
410      * This method is for building Order Record values based on ingested marc file or Edi files/data mapping or constant and default values from profile.
411      * @throws Exception
412      */
413     public boolean marcEdiprocess(OLEBatchProcessDefinitionDocument processDef, OLEBatchProcessJobDetailsBo job) throws Exception {
414         LOG.info("----Inside marcEdiProcess()------------------------------");
415         OLEBatchProcessProfileBo oleBatchProcessProfileBo = processDef.getOleBatchProcessProfileBo();
416         OLETranscationalRecordGenerator oleTranscationalRecordGenerator = new OLETranscationalRecordGenerator();
417         EDIOrders ediOrders = oleTranscationalRecordGenerator.fromXml(ediXMLContent);
418         EDIOrder ediOrder = ediOrders.getOrders().get(0);
419         BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
420         BibMarcRecords bibMarcRecords = bibMarcRecordProcessor.fromXML(marcXMLContent);
421         List<BibMarcRecord> records = bibMarcRecords.getRecords();
422         OLEBatchProcessProfileBo oleBatchProcessProfileBoForBibImport = getBibImportProfile(oleBatchProcessProfileBo);
423         BatchProcessBibImport batchProcessBibImport = new BatchProcessBibImport(processDef, job);
424         batchProcessBibImport.setOleBatchProcessProfileBo(oleBatchProcessProfileBoForBibImport);
425         //DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
426         //dataCarrierService.addData(OLEConstants.FAILURE_REASON, new ArrayList<>());
427         OrderImportHelperBo orderImportHelperBo = job.getOrderImportHelperBo();
428         orderImportHelperBo.setFailureReason(new ArrayList<String>());
429         /*dataCarrierService.addData(OLEConstants.ORDER_IMPORT_SUCCESS_COUNT, 0);
430         dataCarrierService.addData(OLEConstants.ORDER_IMPORT_FAILURE_COUNT,0);*/
431         orderImportHelperBo.setOrderImportSuccessCount(0);
432         orderImportHelperBo.setOrderImportFailureCount(0);
433         orderImportHelperBo.setOleBatchProcessProfileBo(oleBatchProcessProfileBo);
434         OLEBatchBibImportDataObjects oleBatchBibImportDataObjects = new OLEBatchBibImportDataObjects();
435         List<OrderBibMarcRecord> orderBibMarcRecordList = oleBatchBibImportDataObjects.processBibImport(records, batchProcessBibImport);
436         if (orderBibMarcRecordList != null && orderBibMarcRecordList.size() > 0) {
437             for (int recordCount = 0; recordCount < orderBibMarcRecordList.size(); recordCount++) {
438                 OrderBibMarcRecord orderBibMarcRecord = orderBibMarcRecordList.get(recordCount);
439                 if (orderBibMarcRecord != null && orderBibMarcRecord.getBibId() != null && orderBibMarcRecord.getBibId().getId() != null) {
440                     String bibId = orderBibMarcRecord.getBibId().getId();
441                     oleOrderRecordService = SpringContext.getBean(OleOrderRecordService.class);
442                     OleOrderRecord oleOrderRecord = oleOrderRecordService.fetchOleOrderRecordForMarcEdi(bibId, ediOrder, records.get(recordCount), recordCount, job);
443                     oleOrderRecord.setAgendaName(OLEConstants.PROFILE_AGENDA_NM);
444                     if (oleOrderRecord.getMessageMap().get(OLEConstants.IS_VALID_BFN).toString().equalsIgnoreCase(OLEConstants.TRUE)) {
445                         try {
446                             setBibValues(oleOrderRecord, orderBibMarcRecord.getBibId());
447                             if (oleBatchProcessProfileBoForBibImport.getDataToImport() != null) {
448                                 if (oleBatchProcessProfileBoForBibImport.getDataToImport().equals(OLEConstants.BIB_DATA_ONLY)
449                                         || oleBatchProcessProfileBoForBibImport.getDataToImport().equals(OLEConstants.BIB_INS)) {
450                                     oleOrderRecord.setLinkToOrderOption(OLEConstants.ORDER_RECORD_IMPORT_MARC_EDI);
451                                 } else if (oleBatchProcessProfileBoForBibImport.getDataToImport().equals(OLEConstants.BIB_EINS)) {
452                                     oleOrderRecord.setLinkToOrderOption(OLEConstants.ORDER_RECORD_IMPORT_MARC_EDI_ELECTRONIC);
453                                 } else if (oleBatchProcessProfileBoForBibImport.getDataToImport().equals(OLEConstants.BIB_INS_EINS)) {
454                                     oleOrderRecord.setLinkToOrderOption(OLEConstants.ORDER_RECORD_IMPORT_MARC_EDI_PRINT_ELECTRONIC);
455                                 }
456                             }
457                             populateOrderRecordForValidBFN(job, oleOrderRecord, ediOrder, records, recordCount, oleBatchProcessProfileBo);
458                             oleOrderRecordList.add(oleOrderRecord);
459                         } catch (Exception e) {
460                             setOleOrderRecordAsFailureRecord(job);
461                             createErrorLog(job, orderBibMarcRecord.getBibMarcRecord(), OLEConstants.BIB_IMPORT_FAILURE_REASON);
462                         }
463                     } else {
464                         oleOrderRecord.getMessageMap().put(OLEConstants.IS_VALID_RECORD, true);
465                         oleOrderRecord.getMessageMap().put(OLEConstants.IS_BAD_CTRL_FLD, false);
466                         oleOrderRecord.setOleOriginalBibRecordFileName(ingestRecord.getOriginalMarcFileName());
467                         oleOrderRecord.setOriginalEDIFileName(ingestRecord.getOriginalEdiFileName());
468                         createErrorLog(job, orderBibMarcRecord.getBibMarcRecord(), "Invalid BFN");
469                         orderImportHelperBo.setOrderImportFailureCount(orderImportHelperBo.getOrderImportFailureCount() + 1);
470                         oleOrderRecordList.add(oleOrderRecord);
471                     }
472                 } else if (orderBibMarcRecord != null && orderBibMarcRecord.getFailureReason() != null && orderBibMarcRecord.getBibMarcRecord() != null) {
473                     setOleOrderRecordAsFailureRecord(job);
474                     createErrorLog(job, orderBibMarcRecord.getBibMarcRecord(), orderBibMarcRecord.getFailureReason());
475                 } else {
476                     setOleOrderRecordAsFailureRecord(job);
477                     createErrorLog(job, orderBibMarcRecord.getBibMarcRecord());
478                 }
479             }
480         } else {
481             for (int count = 0; count < records.size(); count++) {
482                 createErrorLog(job, records.get(count));
483                 setOleOrderRecordAsFailureRecord(job);
484             }
485         }
486         /*dataCarrierService.addData(OLEConstants.UPDATE_BIB_CNT, 0);
487         dataCarrierService.addData(OLEConstants.CREATE_HLD_CNT, 0);*/
488         orderImportHelperBo.setUpdateBibCount(0);
489         orderImportHelperBo.setCreateHoldingsCount(0);
490         LOG.info("----End of marcEdiProcess()------------------------------");
491         return true;
492     }
493 
494     /**
495      * This method is for populating order record values for valid BFN value.
496      * @param job
497      * @param oleOrderRecord
498      * @param ediOrder
499      * @param records
500      * @param marcCount
501      */
502     private void populateOrderRecordForValidBFN(OLEBatchProcessJobDetailsBo job,OleOrderRecord oleOrderRecord,EDIOrder ediOrder,List<BibMarcRecord> records,int marcCount, OLEBatchProcessProfileBo oleBatchProcessProfileBo){
503         LOG.info("----Inside populateOrderRecordForValidBFN()------------------------------");
504         OrderImportHelperBo orderImportHelperBo = job.getOrderImportHelperBo();
505         //getValueForVendorProfileCodeFromProfile(getOleBatchProcessProfileBo());
506         LineItemOrderMatcherForBib lineItemOrderMatcherForBib = new LineItemOrderMatcherForBib();
507         String vendorProfileCode = OLEConstants.PROFILE_AGENDA_NM;
508         LineItemOrder lineItemOrder = lineItemOrderMatcherForBib.getLineItemOrder(ediOrder.getLineItemOrder(),records.get(marcCount), vendorProfileCode);
509         /*dataCarrierService.addData(OLEConstants.REQUEST_BIB_RECORD, records.get(marcCount));
510         dataCarrierService.addData(OLEConstants.REQUEST_LINE_ITEM_ORDER_RECORD, lineItemOrder);
511         dataCarrierService.addData(OLEConstants.EDI_ORDER, ediOrder);
512         dataCarrierService.addData(OLEConstants.EDI_XML_CONTENT, this.ediXMLContent);
513         dataCarrierService.addData(OLEConstants.BATCH_PROFILE_BO, getOleBatchProcessProfileBo());*/
514         orderImportHelperBo.setBibMarcRecord(records.get(marcCount));
515         orderImportHelperBo.setLineItemOrder(lineItemOrder);
516         orderImportHelperBo.setEdiOrder(ediOrder);
517         orderImportHelperBo.setEdiXMLContent(this.ediXMLContent);
518         orderImportHelperBo.setOleBatchProcessProfileBo(oleBatchProcessProfileBo);
519         if(oleOrderRecord==null){
520             ingestRecord.setUpdate(true);
521             oleOrderRecord = new OleOrderRecord();
522             oleOrderRecord.setOriginalRecord(records.get(marcCount));
523             oleOrderRecord.setOriginalEdi(ediOrder);
524             OleTxRecord oleTxRecord = new OleTxRecord();
525             oleTxRecord.setVendorItemIdentifier(OleTxRecordBuilder.getInstance().getVendorItemIdentifier(lineItemOrder));
526             oleOrderRecord.setOleTxRecord(oleTxRecord);
527         }
528         oleOrderRecord.setOleOriginalBibRecordFileName(ingestRecord.getOriginalMarcFileName());
529         oleOrderRecord.setOriginalEDIFileName(ingestRecord.getOriginalEdiFileName());
530         oleOrderRecord.addMessageToMap(OLEConstants.IS_VALID_RECORD,true);
531     }
532 
533     /**
534      * This method is for populating order record values for Extra Marc.
535      * @param records
536      * @param marcCount
537      * @param ediOrder
538      * @return OleOrderRecord values
539      */
540     private OleOrderRecord populateOrderRecordForExtraMarc(List<BibMarcRecord> records,int marcCount,EDIOrder ediOrder){
541         LOG.info("----Inside populateOrderRecordForExtraMarc()------------------------------");
542         OleOrderRecord oleOrderRecord=new OleOrderRecord();
543         oleOrderRecord.setAgendaName(ingestRecord.getAgendaName());
544         oleOrderRecord.setOleOriginalBibRecordFileName(ingestRecord.getOriginalMarcFileName());
545         oleOrderRecord.setOriginalEDIFileName(ingestRecord.getOriginalEdiFileName());
546         oleOrderRecord.setOriginalRecord(records.get(marcCount));
547         oleOrderRecord.setDescription(ingestRecord.getAgendaDescription());
548         oleOrderRecord.setOriginalEdi(ediOrder);
549         oleOrderRecord.setOleBibRecord(new OleBibRecord());
550         oleOrderRecord.setOleTxRecord(null);
551         oleOrderRecord.addMessageToMap(OLEConstants.IS_VALID_RECORD,false);
552         oleOrderRecord.addMessageToMap(OLEConstants.IS_BAD_CTRL_FLD,false);
553         List rulesEvaluatedList = new ArrayList();
554         String ruleEvaluated = "ISBN Validation"+ " : " + OLEConstants.TRUE;
555         rulesEvaluatedList.add(ruleEvaluated);
556         oleOrderRecord.addMessageToMap(OLEConstants.RULES_EVAL, rulesEvaluatedList);
557         oleOrderRecord.addMessageToMap(OLEConstants.IS_VALID_BFN,OLEConstants.FALSE);
558         return oleOrderRecord;
559     }
560 
561     /**
562      * This method is for populating order record values for Extra Edi.
563      * @param ediCount
564      * @param ediOrder
565      * @return OleOrderRecord values.
566      */
567     private OleOrderRecord populateOrderRecordForExtraEdi(int ediCount,EDIOrder ediOrder){
568         LOG.info("----Inside populateOrderRecordForExtraEdi()------------------------------");
569         OleOrderRecord oleOrderRecord=new OleOrderRecord();
570         oleOrderRecord.setAgendaName(ingestRecord.getAgendaName());
571         oleOrderRecord.setOleOriginalBibRecordFileName(ingestRecord.getOriginalMarcFileName());
572         oleOrderRecord.setOriginalEDIFileName(ingestRecord.getOriginalEdiFileName());
573         oleOrderRecord.setOriginalRecord(null);
574         oleOrderRecord.setDescription(ingestRecord.getAgendaDescription());
575         oleOrderRecord.setOriginalEdi(ediOrder);
576         oleOrderRecord.setOleBibRecord(new OleBibRecord());
577         OleTxRecord oleTxRecord = null;
578         try {
579             oleTxRecord = getOleTxRecord(ediOrder.getLineItemOrder().get(ediCount), ediOrder);
580         } catch (Exception e) {
581             e.printStackTrace();
582         }
583         oleOrderRecord.setOleTxRecord(oleTxRecord);
584         oleOrderRecord.addMessageToMap(OLEConstants.IS_VALID_RECORD,false);
585         oleOrderRecord.addMessageToMap(OLEConstants.IS_BAD_CTRL_FLD,false);
586         List rulesEvaluatedList = new ArrayList();
587         String ruleEvaluated = "ISBN Validation"+ " : " + OLEConstants.TRUE;
588         rulesEvaluatedList.add(ruleEvaluated);
589         oleOrderRecord.addMessageToMap(OLEConstants.RULES_EVAL, rulesEvaluatedList);
590         oleOrderRecord.addMessageToMap(OLEConstants.IS_VALID_BFN,OLEConstants.FALSE);
591         return oleOrderRecord;
592     }
593 
594     private void createErrorLog(OLEBatchProcessJobDetailsBo job){
595         OrderImportHelperBo orderImportHelperBo = job.getOrderImportHelperBo();
596         //List reasonForFailure = (List) dataCarrierService.getData(OLEConstants.FAILURE_REASON);
597         List reasonForFailure = (List) orderImportHelperBo.getFailureReason();
598         if(reasonForFailure != null){
599             List<String> failureRecords = new ArrayList<>();
600             failureRecords.add("Docstore Exception : Cannot create/update bib , Check Bib Import Profile values");
601             failureRecords.add("Note : Bib Overlay can be performed only if there is an availability of Bib records in the docstore");
602             failureRecords.add("===============================================================================");
603             failureRecords.add("");
604             reasonForFailure.addAll(failureRecords);
605             //dataCarrierService.addData(OLEConstants.FAILURE_REASON, reasonForFailure);
606             orderImportHelperBo.setFailureReason(reasonForFailure);
607         }
608     }
609 
610     private void createErrorLog(OLEBatchProcessJobDetailsBo job, BibMarcRecord bibMarcRecord){
611         OrderImportHelperBo orderImportHelperBo = job.getOrderImportHelperBo();
612         //List reasonForFailure = (List) dataCarrierService.getData(OLEConstants.FAILURE_REASON);
613         List reasonForFailure = (List) orderImportHelperBo.getFailureReason();
614         if(reasonForFailure != null){
615             List<String> failureRecords = new ArrayList<>();
616             String title = null;
617             for (DataField dataField : bibMarcRecord.getDataFields()){
618                 if (dataField.getTag().equals("245")){
619                     for (SubField subfield : dataField.getSubFields()){
620                         if (subfield.getCode().equals("a")){
621                             title = subfield.getValue();
622                         }
623                     }
624                 }
625             }
626             if (title!=null){
627                 failureRecords.add("For Title : "+title);
628             }
629             failureRecords.add("Docstore Exception : Cannot create/update bib , Check Bib Import Profile values");
630             failureRecords.add("Note : Bib Overlay can be performed only if there is an availability of Bib records in the docstore");
631             failureRecords.add("===============================================================================");
632             failureRecords.add("");
633             reasonForFailure.addAll(failureRecords);
634             //dataCarrierService.addData(OLEConstants.FAILURE_REASON, reasonForFailure);
635             orderImportHelperBo.setFailureReason(reasonForFailure);
636         }
637     }
638     private void createErrorLog(OLEBatchProcessJobDetailsBo job, BibMarcRecord bibMarcRecord, String failureReason){
639         OrderImportHelperBo orderImportHelperBo = job.getOrderImportHelperBo();
640         //List reasonForFailure = (List) dataCarrierService.getData(OLEConstants.FAILURE_REASON);
641         List reasonForFailure = (List) orderImportHelperBo.getFailureReason();
642         if(reasonForFailure != null){
643             List<String> failureRecords = new ArrayList<>();
644             String title = null;
645             for (DataField dataField : bibMarcRecord.getDataFields()){
646                 if (dataField.getTag().equals("245")){
647                     for (SubField subfield : dataField.getSubFields()){
648                         if (subfield.getCode().equals("a")){
649                             title = subfield.getValue();
650                         }
651                     }
652                 }
653             }
654             if (title!=null){
655                 failureRecords.add("For Title : "+title);
656             }
657             failureRecords.add(failureReason);
658             failureRecords.add("===============================================================================");
659             reasonForFailure.addAll(failureRecords);
660             //dataCarrierService.addData(OLEConstants.FAILURE_REASON, reasonForFailure);
661             orderImportHelperBo.setFailureReason(reasonForFailure);
662         }
663     }
664 
665     public String getParameter(String name){
666         ParameterKey parameterKey = ParameterKey.create(OLEConstants.APPL_ID,OLEConstants.SELECT_NMSPC,OLEConstants.SELECT_CMPNT,name);
667         Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
668         return parameter!=null?parameter.getValue():null;
669     }
670 
671     private OleTxRecord getOleTxRecord(LineItemOrder lineItemOrder,EDIOrder ediOrder) throws Exception {
672         OleTxRecord oleTxRecord = OleTxRecordBuilder.getInstance().build(lineItemOrder, ediOrder);
673         return oleTxRecord;
674     }
675 
676     /**
677      *  Gets the instance of businessObjectService.
678      *  if businessObjectService is null it will create a new instance else it will return existing businessObjectService.
679      * @return  businessObjectService
680      */
681     public BusinessObjectService getBusinessObjectService() {
682         if (null == businessObjectService) {
683             businessObjectService = KRADServiceLocator.getBusinessObjectService();
684         }
685         return businessObjectService;
686     }
687 
688     /**
689      *  This method returns selector map contains OLE code  and contextName.
690      * @param contextName
691      * @return  selector
692      */
693     protected Map<String, String> getSelectionContext(String contextName) {
694         Map<String, String> selector = new HashMap<String, String>();
695         selector.put(NAMESPACE_CODE_SELECTOR, "OLE");
696         selector.put(NAME_SELECTOR, contextName);
697         return selector;
698     }
699 
700     /**
701      *   This method returns selector map contains agendaName
702      * @param agendaName
703      * @return  selector
704      */
705     protected Map<String, String> getAgendaContext(String agendaName) {
706         Map<String, String> selector = new HashMap<String, String>();
707         selector.put(NAME_SELECTOR, agendaName);
708         return selector;
709     }
710 
711     public abstract String preProcessMarc(String marcFileContent, OLEBatchProcessJobDetailsBo job);
712 
713     public abstract String preProcessEDI(String ediFileContent);
714 
715     public abstract void postProcess(OLEBatchProcessJobDetailsBo job);
716 
717 }