1 package org.kuali.ole.ingest;
2
3 import org.kuali.ole.BibliographicRecordHandler;
4 import org.kuali.ole.DataCarrierService;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.OLETranscationalRecordGenerator;
7 import org.kuali.ole.ingest.pojo.*;
8 import org.kuali.ole.editor.service.DocstoreHelperService;
9 import org.kuali.ole.ingest.pojo.IngestRecord;
10 import org.kuali.ole.ingest.pojo.MatchBo;
11 import org.kuali.ole.ingest.pojo.ProfileAttributeBo;
12 import org.kuali.ole.ingest.pojo.ProfileTerm;
13 import org.kuali.ole.pojo.OleBibRecord;
14 import org.kuali.ole.pojo.OleOrderRecord;
15 import org.kuali.ole.pojo.OleTxRecord;
16 import org.kuali.ole.pojo.bib.BibliographicRecord;
17 import org.kuali.ole.pojo.bib.Collection;
18 import org.kuali.ole.pojo.edi.*;
19 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20 import org.kuali.rice.krad.service.BusinessObjectService;
21 import org.kuali.rice.krad.service.KRADServiceLocator;
22 import org.kuali.rice.krms.api.KrmsApiServiceLocator;
23 import org.kuali.rice.krms.api.engine.*;
24 import org.kuali.rice.krms.framework.engine.EngineResultsImpl;
25 import org.kuali.rice.krms.impl.repository.AgendaBo;
26 import org.xml.sax.SAXException;
27
28 import javax.xml.parsers.ParserConfigurationException;
29 import javax.xml.xpath.XPathExpressionException;
30 import java.io.IOException;
31 import java.net.URISyntaxException;
32 import java.util.*;
33
34
35
36
37 public abstract class AbstractIngestProcessor {
38 private static final String NAMESPACE_CODE_SELECTOR = "namespaceCode";
39 private static final String NAME_SELECTOR = "name";
40 private BusinessObjectService businessObjectService;
41 private List<EngineResults> engineResults = new ArrayList<EngineResults>();
42
43 String marcXMLContent;
44 String ediXMLContent;
45 private IngestRecord ingestRecord;
46 private DocstoreHelperService docstoreHelperService;
47
48
49
50
51
52
53 public List<EngineResults> getEngineResults() {
54 return engineResults;
55 }
56
57 public DocstoreHelperService getDocstoreHelperService() {
58 if(docstoreHelperService==null)
59 docstoreHelperService=new DocstoreHelperService();
60 return docstoreHelperService;
61 }
62
63
64
65
66
67
68 public boolean start(IngestRecord ingestRecord) {
69 this.ingestRecord = ingestRecord;
70 boolean all_records_failure_falg=true;
71
72 byPassLogicForPreProcess(ingestRecord.getMarcFileContent(), ingestRecord.getEdiFileContent(), ingestRecord.getByPassPreProcessing());
73 try {
74 all_records_failure_falg=process();
75 } catch (URISyntaxException e) {
76 System.out.println(e.getMessage());
77 } catch (IOException e) {
78 System.out.println(e.getMessage());
79 } catch (SAXException e) {
80 System.out.println(e.getMessage());
81 } catch (XPathExpressionException e) {
82 System.out.println(e.getMessage());
83 } catch (ParserConfigurationException e) {
84 System.out.println(e.getMessage());
85 } catch (InterruptedException e) {
86 System.out.println(e.getMessage());
87 } catch (Exception e) {
88 System.out.println(e.getMessage());
89 }
90
91 postProcess();
92 return all_records_failure_falg;
93 }
94
95
96
97
98
99
100
101 private void byPassLogicForPreProcess(String rawMarcContent, String rawEdiContent, boolean preProcessingReq) {
102 if (preProcessingReq) {
103 preProcess(rawMarcContent, rawEdiContent);
104 } else {
105 marcXMLContent = rawMarcContent;
106 ediXMLContent = rawEdiContent;
107 }
108 }
109
110
111
112
113
114
115 private void preProcess(String rawMarcContent, String rawEdiContent) {
116 marcXMLContent = preProcessMarc(rawMarcContent);
117 ediXMLContent = preProcessEDI(rawEdiContent);
118 }
119
120
121
122
123
124
125
126
127
128
129 public boolean process() throws URISyntaxException, IOException, SAXException, XPathExpressionException, ParserConfigurationException, InterruptedException, Exception {
130
131 Engine engine = KrmsApiServiceLocator.getEngine();
132
133 HashMap<String, String> map = new HashMap<String, String>();
134 map.put("AGENDA_NAME", ingestRecord.getAgendaName());
135 List<MatchBo> matchBos = (List<MatchBo>) getBusinessObjectService().findMatching(MatchBo.class, map);
136
137 HashMap<String, Object> agendaValue = new HashMap<String, Object>();
138 agendaValue.put("nm", ingestRecord.getAgendaName());
139 List<AgendaBo> agendaBos = (List<AgendaBo>) getBusinessObjectService().findMatching(AgendaBo.class, agendaValue);
140 AgendaBo agendaBo = agendaBos.get(0);
141
142 SelectionCriteria selectionCriteria =
143 SelectionCriteria.createCriteria(null, getSelectionContext(agendaBo.getContext().getName()), getAgendaContext(ingestRecord.getAgendaName()));
144
145 ExecutionOptions executionOptions = new ExecutionOptions();
146 executionOptions.setFlag(ExecutionFlag.LOG_EXECUTION, true);
147
148 OLETranscationalRecordGenerator oleTranscationalRecordGenerator = new OLETranscationalRecordGenerator();
149 EDIOrders ediOrders = oleTranscationalRecordGenerator.fromXml(ediXMLContent);
150
151 BibliographicRecordHandler bibliographicRecordHandler = new BibliographicRecordHandler();
152 Collection bibliographicRecords = bibliographicRecordHandler.fromXML(marcXMLContent);
153
154 List<BibliographicRecord> records = bibliographicRecords.getRecords();
155
156 List<ProfileAttributeBo> profileAttributeBos = getProfileAttributeBos(ingestRecord.getAgendaName());
157 List<OverlayOption> overlayOption = getOverlayOption(ingestRecord.getAgendaName());
158 List<OverlayLookupAction> overlayLookupActionList = getOverlayLookupAction(ingestRecord.getAgendaName());
159
160 EDIOrder ediOrder = ediOrders.getOrders().get(0);
161
162
163
164
165 int matchBibEdiRecord=0;
166 for (Iterator<BibliographicRecord> iterator = records.iterator(); iterator.hasNext();matchBibEdiRecord++ ) {
167 BibliographicRecord bibliographicRecord = iterator.next();
168 if(matchBibEdiRecord<records.size() && matchBibEdiRecord<ediOrder.getLineItemOrder().size()) {
169 if(records.get(matchBibEdiRecord)!=null && ediOrder.getLineItemOrder().get(matchBibEdiRecord)!=null){
170 String singleMarcXML = bibliographicRecordHandler.generateXML(bibliographicRecord);
171 Map<String, String> valuesMap = bibliographicRecordHandler.valuesMapFromXML(singleMarcXML);
172 valuesMap.putAll(getvendorLineItemReferenceNumber(ediOrder));
173 Facts.Builder factBuilder = Facts.Builder.create();
174 String termIncominField = null;
175 String termExistingField = null;
176 for (Iterator<MatchBo> matchBoIterator = matchBos.iterator(); matchBoIterator.hasNext(); ) {
177 MatchBo matchBo = matchBoIterator.next();
178 ProfileTerm term = new ProfileTerm();
179 String[] matchTermNames = matchBo.getTermName().split("-");
180 if(matchTermNames[0].equalsIgnoreCase(OLEConstants.INCOMING_FIELD)) {
181 term.setIncomingField(valuesMap.get(matchBo.getIncomingField()));
182 termIncominField = term.getIncomingField();
183
184 factBuilder.addFact(matchBo.getTermName(),termIncominField);
185 }
186 else if(matchTermNames[0].equalsIgnoreCase(OLEConstants.EXISTING_FIELD)){
187 term.setExistingField(matchBo.getExistingField());
188 term.setIncomingField(termIncominField);
189 termExistingField = term.getExistingField();
190
191 factBuilder.addFact(matchBo.getTermName(),termExistingField);
192 }
193 else {
194 factBuilder.addFact(matchBo.getTermName(),null);
195 }
196 }
197 LineItemOrderMatcherForBib lineItemOrderMatcherForBib = new LineItemOrderMatcherForBib();
198 LineItemOrder lineItemOrder = lineItemOrderMatcherForBib.getLineItemOrder(ediOrder.getLineItemOrder(), bibliographicRecord, getVendorProfileCode(profileAttributeBos));
199
200 DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
201 dataCarrierService.addData(OLEConstants.REQUEST_BIB_RECORD, bibliographicRecord);
202 dataCarrierService.addData(OLEConstants.PROFILE_ATTRIBUTE_LIST, profileAttributeBos);
203 dataCarrierService.addData(OLEConstants.OVERLAY_OPTION_LIST, overlayOption);
204 dataCarrierService.addData(OLEConstants.OVERLAY_LOOKUP_ACTION_LIST, overlayLookupActionList);
205 dataCarrierService.addData(OLEConstants.REQUEST_LINE_ITEM_ORDER_RECORD,lineItemOrder);
206
207 EngineResults engineResult=engine.execute(selectionCriteria, factBuilder.build(), executionOptions);
208 OleBibRecord oleBibRecord = (OleBibRecord) engineResult.getAttribute(OLEConstants.OLE_BIB_RECORD);
209 OleOrderRecord oleOrderRecord = new OleOrderRecord();
210 oleOrderRecord.setAgendaName(ingestRecord.getAgendaName());
211 oleOrderRecord.setOleOriginalBibRecordFileName(ingestRecord.getOriginalMarcFileName());
212 oleOrderRecord.setOriginalEDIFileName(ingestRecord.getOriginalEdiFileName());
213 oleOrderRecord.setOriginalRecord(bibliographicRecord);
214 oleOrderRecord.setDescription(ingestRecord.getAgendaDescription());
215 oleOrderRecord.setOriginalEdi(ediOrder);
216
217 boolean validBFNFlag=isValidBFN(ediOrder, bibliographicRecord, profileAttributeBos);
218 oleOrderRecord.addMessageToMap("isValidRecord",true);
219 if (validBFNFlag) {
220 oleOrderRecord.setOleBibRecord(oleBibRecord);
221 OleTxRecord oleTxRecord = getMatchingTxRecord(ediOrder, bibliographicRecord, profileAttributeBos);
222 oleOrderRecord.setOleTxRecord(oleTxRecord);
223 oleOrderRecord.addMessageToMap("isValidBFN",validBFNFlag);
224 } else {
225 oleOrderRecord.addMessageToMap("isValidBFN","false");
226 if(oleBibRecord!=null)
227 getDocstoreHelperService().deleteDocstoreRecord(oleBibRecord.getBibUUID());
228 OleTxRecord oleTxRecord = getMatchingTxRecord(ediOrder, bibliographicRecord, profileAttributeBos);
229 oleOrderRecord.setOleTxRecord(oleTxRecord);
230 }
231 List<ResultEvent> allResults = engineResult.getAllResults();
232 for (Iterator<ResultEvent> resultEventIterator = allResults.iterator(); resultEventIterator.hasNext(); ) {
233 ResultEvent resultEvent = resultEventIterator.next();
234 String ruleEvaluated = null;
235 if (resultEvent.getType().equals("Rule Evaluated")) {
236 if(validBFNFlag)
237 ruleEvaluated = resultEvent.getSource() + " : " + resultEvent.getResult();
238 else
239 ruleEvaluated = resultEvent.getSource() + " : " + "false";
240 List rulesEvaluatedList = (List) oleOrderRecord.getMessageMap().get("rulesEvaluated");
241 if (null == rulesEvaluatedList) {
242 rulesEvaluatedList = new ArrayList();
243 }
244 rulesEvaluatedList.add(ruleEvaluated);
245 oleOrderRecord.addMessageToMap("rulesEvaluated", rulesEvaluatedList);
246 }
247 }
248 if("false".equals((((List)oleOrderRecord.getMessageMap().get("rulesEvaluated")).get(0)).toString().split(":")[1].trim()) && oleOrderRecord.getOleBibRecord()==null)
249 oleOrderRecord.addMessageToMap("isBadControlField",true);
250 else
251 oleOrderRecord.addMessageToMap("isBadControlField",false);
252 engineResult.setAttribute(OLEConstants.OLE_ORDER_RECORD, oleOrderRecord);
253 engineResults.add(engineResult);
254 }
255 }
256 }
257 if(records.size()>ediOrder.getLineItemOrder().size()){
258 for(int i=ediOrder.getLineItemOrder().size();i<records.size();i++){
259 OleOrderRecord oleOrderRecord=new OleOrderRecord();
260 oleOrderRecord.setAgendaName(ingestRecord.getAgendaName());
261 oleOrderRecord.setOleOriginalBibRecordFileName(ingestRecord.getOriginalMarcFileName());
262 oleOrderRecord.setOriginalEDIFileName(ingestRecord.getOriginalEdiFileName());
263 oleOrderRecord.setOriginalRecord(records.get(i));
264 oleOrderRecord.setDescription(ingestRecord.getAgendaDescription());
265 oleOrderRecord.setOriginalEdi(ediOrder);
266 oleOrderRecord.setOleBibRecord(new OleBibRecord());
267
268 oleOrderRecord.setOleTxRecord(null);
269 oleOrderRecord.addMessageToMap("isValidRecord",false);
270 oleOrderRecord.addMessageToMap("isBadControlField",false);
271 Facts.Builder factBuilder = Facts.Builder.create();
272 EngineResults engineResult=new EngineResultsImpl();
273 engineResult.setAttribute(OLEConstants.OLE_ORDER_RECORD, oleOrderRecord);
274 List rulesEvaluatedList = new ArrayList();
275 String ruleEvaluated = "ISBN Validation"+ " : " + "true";
276 rulesEvaluatedList.add(ruleEvaluated);
277 oleOrderRecord.addMessageToMap("rulesEvaluated", rulesEvaluatedList);
278 oleOrderRecord.addMessageToMap("isValidBFN","false");
279 getEngineResults().add(engineResult);
280
281 }
282 }
283 else if(records.size()<ediOrder.getLineItemOrder().size()){
284 for(int i=records.size();i<ediOrder.getLineItemOrder().size();i++){
285 OleOrderRecord oleOrderRecord=new OleOrderRecord();
286 oleOrderRecord.setAgendaName(ingestRecord.getAgendaName());
287 oleOrderRecord.setOleOriginalBibRecordFileName(ingestRecord.getOriginalMarcFileName());
288 oleOrderRecord.setOriginalEDIFileName(ingestRecord.getOriginalEdiFileName());
289 oleOrderRecord.setOriginalRecord(null);
290 oleOrderRecord.setDescription(ingestRecord.getAgendaDescription());
291 oleOrderRecord.setOriginalEdi(ediOrder);
292 oleOrderRecord.setOleBibRecord(new OleBibRecord());
293 OleTxRecord oleTxRecord = getOleTxRecord(ediOrder.getLineItemOrder().get(i), ediOrder, profileAttributeBos);
294 oleOrderRecord.setOleTxRecord(oleTxRecord);
295 oleOrderRecord.addMessageToMap("isValidRecord",false);
296 oleOrderRecord.addMessageToMap("isBadControlField",false);
297 Facts.Builder factBuilder = Facts.Builder.create();
298 EngineResults engineResult=new EngineResultsImpl();
299 engineResult.setAttribute(OLEConstants.OLE_ORDER_RECORD, oleOrderRecord);
300 List rulesEvaluatedList = new ArrayList();
301 String ruleEvaluated = "ISBN Validation"+ " : " + "true";
302 rulesEvaluatedList.add(ruleEvaluated);
303 oleOrderRecord.addMessageToMap("rulesEvaluated", rulesEvaluatedList);
304 oleOrderRecord.addMessageToMap("isValidBFN","false");
305 getEngineResults().add(engineResult);
306
307 }
308 }
309
310 boolean all_records_failure_falg=true;
311
312 List<EngineResults> engineResults = getEngineResults();
313 for (Iterator<EngineResults> iterator = engineResults.iterator(); iterator.hasNext(); ) {
314 EngineResults results = iterator.next();
315 OleOrderRecord oleOrderRecord = (OleOrderRecord) results.getAttribute(OLEConstants.OLE_ORDER_RECORD);
316
317 if("true".equals((oleOrderRecord.getMessageMap().get("isValidRecord")).toString()) &&
318 "true".equals((oleOrderRecord.getMessageMap().get("isValidBFN")).toString()) &&
319 "false".equals((((List)oleOrderRecord.getMessageMap().get("rulesEvaluated")).get(0)).toString().split(":")[1].trim())) {
320 all_records_failure_falg=false;
321 break;
322 }
323 }
324
325
326
327
328
329
330
331 return all_records_failure_falg;
332 }
333
334
335
336
337
338
339
340 private boolean isNumMarcMatchingNumEdiRecords(EDIOrder ediOrder, List<BibliographicRecord> records) {
341 return records.size() == ediOrder.getLineItemOrder().size();
342 }
343
344
345
346
347
348
349 private String getVendorProfileCode(List<ProfileAttributeBo> profileAttributeBo) {
350 for (Iterator<ProfileAttributeBo> iterator = profileAttributeBo.iterator(); iterator.hasNext(); ) {
351 ProfileAttributeBo attributeBo = iterator.next();
352 if (attributeBo.getAttributeName().equals(OLEConstants.VENDOR_PROFILE_CODE)) {
353 return attributeBo.getAttributeValue();
354 }
355 }
356 return null;
357 }
358
359
360
361
362
363
364 public List<OverlayLookupAction> getOverlayLookupAction(String agendaName) {
365 HashMap map = new HashMap();
366 map.put("agenda_name", agendaName);
367 List<OverlayLookupAction> matching = (List<OverlayLookupAction>) getBusinessObjectService().findMatching(OverlayLookupAction.class, map);
368 return matching;
369 }
370
371
372
373
374
375
376 public List<OverlayOption> getOverlayOption(String agendaName) {
377 HashMap map = new HashMap();
378 map.put("agenda_name", agendaName);
379 List<OverlayOption> matching = (List<OverlayOption>) getBusinessObjectService().findMatching(OverlayOption.class, map);
380 return matching;
381 }
382
383
384
385
386
387
388 public List<ProfileAttributeBo> getProfileAttributeBos(String agendaName) {
389 HashMap map = new HashMap();
390 map.put("agenda_name", agendaName);
391 List<ProfileAttributeBo> matching = (List<ProfileAttributeBo>) getBusinessObjectService().findMatching(ProfileAttributeBo.class, map);
392 return matching;
393 }
394
395
396
397
398
399
400
401 private OleTxRecord getMatchingTxRecord(EDIOrder ediOrder, BibliographicRecord bibliographicRecord, List<ProfileAttributeBo> profileAttributeBos) throws Exception {
402 LineItemOrderMatcherForBib lineItemOrderMatcherForBib = new LineItemOrderMatcherForBib();
403 LineItemOrder lineItemOrder = lineItemOrderMatcherForBib.getLineItemOrder(ediOrder.getLineItemOrder(), bibliographicRecord, getVendorProfileCode(profileAttributeBos));
404 OleTxRecord oleTxRecord = OleTxRecordBuilder.getInstance().build(lineItemOrder, profileAttributeBos, ediOrder);
405 return oleTxRecord;
406 }
407
408 private OleTxRecord getOleTxRecord(LineItemOrder lineItemOrder,EDIOrder ediOrder,List<ProfileAttributeBo> profileAttributeBos) throws Exception {
409 LineItemOrderMatcherForBib lineItemOrderMatcherForBib = new LineItemOrderMatcherForBib();
410 OleTxRecord oleTxRecord = OleTxRecordBuilder.getInstance().build(lineItemOrder, profileAttributeBos, ediOrder);
411 return oleTxRecord;
412 }
413
414 private boolean isValidBFN(EDIOrder ediOrder, BibliographicRecord bibliographicRecord, List<ProfileAttributeBo> profileAttributeBos) throws Exception {
415 LineItemOrderMatcherForBib lineItemOrderMatcherForBib = new LineItemOrderMatcherForBib();
416 LineItemOrder lineItemOrder = lineItemOrderMatcherForBib.getLineItemOrder(ediOrder.getLineItemOrder(), bibliographicRecord, getVendorProfileCode(profileAttributeBos));
417 String bfn=null;
418 if(lineItemOrder!=null){
419 Map<String, String> accountInfo = OleTxRecordBuilder.getInstance().getAccountInfo(lineItemOrder);
420 bfn = accountInfo!=null?"1":null;
421 }
422 if(bfn!=null)
423 return true;
424 return false;
425 }
426
427
428
429
430
431
432
433
434 private BusinessObjectService getBusinessObjectService() {
435 if (null == businessObjectService) {
436 businessObjectService = KRADServiceLocator.getBusinessObjectService();
437 }
438 return businessObjectService;
439 }
440
441
442
443
444
445
446 protected Map<String, String> getSelectionContext(String contextName) {
447 Map<String, String> selector = new HashMap<String, String>();
448 selector.put(NAMESPACE_CODE_SELECTOR, "OLE");
449 selector.put(NAME_SELECTOR, contextName);
450 return selector;
451 }
452
453
454
455
456
457
458 protected Map<String, String> getAgendaContext(String agendaName) {
459 Map<String, String> selector = new HashMap<String, String>();
460 selector.put(NAME_SELECTOR, agendaName);
461 return selector;
462 }
463
464 private Map<String, String> getvendorLineItemReferenceNumber(EDIOrder ediOrder){
465 Map<String, String> referenceNumberMap = new HashMap<String, String>();
466
467 for(LineItemOrder lineItemOrder : ediOrder.getLineItemOrder()){
468 for(BuyerReferenceInformation buyerReferenceInformation : lineItemOrder.getBuyerReferenceInformation()){
469 for(BuyerLineItemReference buyerLineItemReference : buyerReferenceInformation.getBuyerLineItemReference()){
470 referenceNumberMap.put(buyerLineItemReference.getBuyersOrderLine(),buyerLineItemReference.getOrderLineNumber());
471 }
472 }
473 }
474 return referenceNumberMap;
475 }
476
477 public abstract String preProcessMarc(String marcFileContent);
478
479 public abstract String preProcessEDI(String ediFileContent);
480
481 public abstract void postProcess();
482
483 }