1 package org.kuali.ole.ingest.action;
2
3 import org.kuali.ole.DataCarrierService;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.OleHoldingsRecordHandler;
6 import org.kuali.ole.OleItemRecordHandler;
7 import org.kuali.ole.batch.bo.OLEBatchProcessProfileBo;
8 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
9 import org.kuali.ole.docstore.common.document.*;
10 import org.kuali.ole.docstore.common.document.HoldingsTree;
11 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
12 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
13 import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
14 import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
15 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
16 import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
17 import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
18 import org.kuali.ole.docstore.common.document.content.enums.DocType;
19 import org.kuali.ole.docstore.common.document.content.instance.*;
20 import org.kuali.ole.docstore.common.document.content.instance.Item;
21 import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
22 import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
23 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
24 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
25 import org.kuali.ole.describe.service.DiscoveryHelperService;
26 import org.kuali.ole.ingest.pojo.ProfileAttributeBo;
27 import org.kuali.ole.pojo.OleBibRecord;
28 import org.kuali.ole.pojo.OleOrderRecord;
29 import org.kuali.ole.pojo.ProfileAttribute;
30 import org.kuali.ole.pojo.edi.EDIOrder;
31 import org.kuali.ole.pojo.edi.LineItemOrder;
32 import org.kuali.ole.service.OleOrderRecordService;
33 import org.kuali.ole.service.OleOverlayActionService;
34 import org.kuali.ole.sys.context.SpringContext;
35 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
36 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
37 import org.kuali.rice.krms.framework.engine.Action;
38
39 import java.text.DateFormat;
40 import java.text.SimpleDateFormat;
41 import java.util.*;
42
43
44
45
46
47 public class CreateBibAction implements Action {
48 private DiscoveryHelperService discoveryHelperService;
49 private OleOrderRecordService oleOrderRecordService;
50 private OleOverlayActionService oleOverlayActionService;
51 private OLEBatchProcessProfileBo oleBatchProcessProfileBo;
52
53 private DocstoreClientLocator docstoreClientLocator;
54
55 public DocstoreClientLocator getDocstoreClientLocator() {
56
57 if (docstoreClientLocator == null) {
58 docstoreClientLocator = SpringContext.getBean(DocstoreClientLocator.class);
59
60 }
61 return docstoreClientLocator;
62 }
63
64 private List<ProfileAttribute> attributes = new ArrayList<ProfileAttribute>();
65
66 public OLEBatchProcessProfileBo getOleBatchProcessProfileBo() {
67 return oleBatchProcessProfileBo;
68 }
69
70 public void setOleBatchProcessProfileBo(OLEBatchProcessProfileBo oleBatchProcessProfileBo) {
71 this.oleBatchProcessProfileBo = oleBatchProcessProfileBo;
72 }
73
74
75
76
77
78
79 @Override
80 public void execute(ExecutionEnvironment executionEnvironment) {
81 DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
82 String ediXMLContent = (String) dataCarrierService.getData("ediXMLContent");
83 if (ediXMLContent != null) {
84 executeForMarcEdi(executionEnvironment);
85 } else {
86 executeForMarc(executionEnvironment);
87 }
88 }
89
90
91 private List getBibInfo(String instanceUUID) {
92 return getDiscoveryHelperService().getBibInformationFromInsatnceId(instanceUUID);
93 }
94
95 public void executeForMarcEdi(ExecutionEnvironment executionEnvironment) {
96 DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
97 List<ProfileAttributeBo> profileAttributes = (List<ProfileAttributeBo>) dataCarrierService.getData(OLEConstants.PROFILE_ATTRIBUTE_LIST);
98 BibMarcRecord bibMarcRecord = (BibMarcRecord) dataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD);
99 LineItemOrder lineItemOrder = (LineItemOrder) dataCarrierService.getData(OLEConstants.REQUEST_LINE_ITEM_ORDER_RECORD);
100 EDIOrder ediOrder = (EDIOrder) dataCarrierService.getData(OLEConstants.EDI_ORDER);
101 String profileName = (String) dataCarrierService.getData(OLEConstants.PROFILE_NM);
102 oleBatchProcessProfileBo = (OLEBatchProcessProfileBo) dataCarrierService.getData(OLEConstants.BATCH_PROFILE_BO);
103 try {
104
105
106
107
108
109 List<BibMarcRecord> bibMarcRecordList = new ArrayList<>();
110 bibMarcRecordList.add(bibMarcRecord);
111 BibMarcRecords bibMarcRecords = new BibMarcRecords();
112 bibMarcRecords.setRecords(bibMarcRecordList);
113 BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
114 Bib bib = new BibMarc();
115 bib.setCategory(DocCategory.WORK.getCode());
116 bib.setType(DocType.BIB.getCode());
117 bib.setFormat(DocFormat.MARC.getCode());
118 bib.setContent(bibMarcRecordProcessor.toXml(bibMarcRecords));
119 List<Item> oleItemList = new ArrayList<Item>();
120 oleItemList.add(getOleItem(lineItemOrder, bibMarcRecord));
121 List<org.kuali.ole.docstore.common.document.Item> itemXml = new ArrayList<org.kuali.ole.docstore.common.document.Item>();
122 ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
123 for (Item item : oleItemList) {
124 org.kuali.ole.docstore.common.document.Item itemXmlContent = new ItemOleml();
125 itemXmlContent.setContent(itemOlemlRecordProcessor.toXML(item));
126 itemXml.add(itemXmlContent);
127
128 }
129 this.attributes = buildListOfProfileAttributes(profileAttributes);
130 OleHoldings oleHoldings = new OleHoldingsRecordHandler().getOleHoldings(bibMarcRecord, attributes);
131 Holdings holdings = new PHoldings();
132 DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
133 holdings.setCreatedOn(String.valueOf(dateFormat.format(new Date())));
134 holdings.setLastUpdated(String.valueOf(dateFormat.format(new Date())));
135 holdings.setPublic(false);
136 holdings.setFastAdd(false);
137
138 holdings.setStatus("n");
139
140
141 Uri uri = new Uri();
142 uri.setValue("");
143 uri.setResolvable(null);
144 oleHoldings.getUri().add(uri);
145 Note note = new Note();
146 note.setType(OLEConstants.NOTE_TYPE);
147 oleHoldings.getNote().add(note);
148 CallNumber callNumber = new CallNumber();
149 ShelvingScheme shelvingScheme = new ShelvingScheme();
150 ShelvingOrder shelvingOrder = new ShelvingOrder();
151 callNumber.setType("");
152 callNumber.setPrefix("");
153 callNumber.setNumber("");
154 shelvingScheme.setCodeValue("");
155 shelvingOrder.setCodeValue("");
156 callNumber.setShelvingScheme(shelvingScheme);
157 callNumber.setShelvingOrder(shelvingOrder);
158 oleHoldings.setCallNumber(callNumber);
159 if (oleHoldings.getLocation() == null) {
160 LocationLevel locationLevel = new LocationLevel();
161 Location location = new Location();
162 locationLevel.setLevel("");
163 locationLevel.setName("");
164 location.setPrimary(OLEConstants.TRUE);
165 location.setStatus(OLEConstants.PERMANENT);
166 location.setLocationLevel(locationLevel);
167 oleHoldings.setLocation(location);
168 }
169
170 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
171 holdings.setContent(holdingOlemlRecordProcessor.toXML(oleHoldings));
172 HoldingsTree holdingsTree = new HoldingsTree();
173 holdingsTree.setHoldings(holdings);
174 holdingsTree.getItems().addAll(itemXml);
175 BibTree bibTree = new BibTree();
176 bibTree.setBib(bib);
177 bibTree.getHoldingsTrees().add(holdingsTree);
178 try {
179 getDocstoreClientLocator().getDocstoreClient().createBibTree(bibTree);
180 } catch (Exception ex) {
181 ex.printStackTrace();
182 }
183
184 HashMap<String, Object> objects = new HashMap<String, Object>();
185 objects.put(OLEConstants.MRC, bibMarcRecord);
186 objects.put(OLEConstants.EDI, lineItemOrder);
187
188
189 BibTree bib_Tree=getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibTree.getBib().getId());
190 if (bib_Tree==null) {
191 Thread.sleep(2000);
192 bib_Tree=getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibTree.getBib().getId());
193 }
194 String holdingsUUID = bib_Tree.getHoldingsTrees().get(0).getHoldings().getId();
195
196 if (null == holdingsUUID) {
197 throw new Exception("instance id returned from docstore is null");
198 }
199
200
201
202
203
204 OleBibRecord oleBibRecord = new OleBibRecord();
205
206
207 oleBibRecord.setBib(bib_Tree.getBib());
208 oleBibRecord.setLinkedInstanceId(holdingsUUID);
209
210 oleBibRecord.setBibUUID(bibTree.getBib().getId());
211 executionEnvironment.getEngineResults().setAttribute(OLEConstants.BIB_CREATION_FLAG, true);
212 OleOrderRecord oleOrderRecord = oleOrderRecordService.fetchOleOrderRecordForMarcEdi(null, ediOrder, bibMarcRecord,0, null);
213 oleOverlayActionService.performOverlayLookupAction(profileName, objects, holdingsUUID, oleOrderRecord);
214 executionEnvironment.getEngineResults().setAttribute(OLEConstants.OLE_ORDER_RECORD, oleOrderRecord);
215 } catch (Exception e) {
216 e.printStackTrace();
217 executionEnvironment.getEngineResults().setAttribute(OLEConstants.OLE_ORDER_RECORD, null);
218 }
219 }
220
221
222 public void executeForMarc(ExecutionEnvironment executionEnvironment) {
223 DataCarrierService dataCarrierService = GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
224 List<ProfileAttributeBo> profileAttributes = (List<ProfileAttributeBo>) dataCarrierService.getData(OLEConstants.PROFILE_ATTRIBUTE_LIST);
225 BibMarcRecord bibMarcRecord = (BibMarcRecord) dataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD);
226
227
228 String profileName = (String) dataCarrierService.getData(OLEConstants.PROFILE_NM);
229 oleBatchProcessProfileBo = (OLEBatchProcessProfileBo) dataCarrierService.getData(OLEConstants.BATCH_PROFILE_BO);
230
231 try {
232
233 List<BibMarcRecord> bibMarcRecordList = new ArrayList<>();
234 bibMarcRecordList.add(bibMarcRecord);
235 BibMarcRecords bibMarcRecords = new BibMarcRecords();
236 bibMarcRecords.setRecords(bibMarcRecordList);
237 BibMarcRecordProcessor bibMarcRecordProcessor = new BibMarcRecordProcessor();
238
239
240 Bib bib = new BibMarc();
241 bib.setCategory(DocCategory.WORK.getCode());
242 bib.setType(DocType.BIB.getCode());
243 bib.setFormat(DocFormat.MARC.getCode());
244 bib.setContent(bibMarcRecordProcessor.toXml(bibMarcRecords));
245
246 List<Item> oleItemList = new ArrayList<Item>();
247 oleItemList.add(getOleItem(bibMarcRecord));
248 List<org.kuali.ole.docstore.common.document.Item> itemXml = new ArrayList<org.kuali.ole.docstore.common.document.Item>();
249 ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
250 for (Item item : oleItemList) {
251 org.kuali.ole.docstore.common.document.Item itemXmlContent = new ItemOleml();
252 itemXmlContent.setContent(itemOlemlRecordProcessor.toXML(item));
253 itemXml.add(itemXmlContent);
254
255 }
256
257 this.attributes = buildListOfProfileAttributes(profileAttributes);
258
259 OleHoldings oleHoldings = new OleHoldingsRecordHandler().getOleHoldings(bibMarcRecord, attributes);
260 Holdings holdings = new PHoldings();
261
262 DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
263 holdings.setCreatedOn(String.valueOf(dateFormat.format(new Date())));
264 holdings.setLastUpdated(String.valueOf(dateFormat.format(new Date())));
265 holdings.setPublic(false);
266 holdings.setFastAdd(false);
267
268 holdings.setStatus("n");
269
270
271
272
273 oleHoldings.setPrimary(OLEConstants.TRUE);
274
275 Uri uri = new Uri();
276 uri.setValue("");
277 uri.setResolvable(null);
278 oleHoldings.getUri().add(uri);
279 Note note = new Note();
280 note.setType(OLEConstants.NOTE_TYPE);
281 oleHoldings.getNote().add(note);
282 CallNumber callNumber = new CallNumber();
283 ShelvingScheme shelvingScheme = new ShelvingScheme();
284 ShelvingOrder shelvingOrder = new ShelvingOrder();
285 callNumber.setType("");
286 callNumber.setPrefix("");
287 callNumber.setNumber("");
288 shelvingScheme.setCodeValue("");
289 shelvingOrder.setCodeValue("");
290 callNumber.setShelvingScheme(shelvingScheme);
291 callNumber.setShelvingOrder(shelvingOrder);
292 oleHoldings.setCallNumber(callNumber);
293
294
295 if (oleHoldings.getLocation() == null) {
296 LocationLevel locationLevel = new LocationLevel();
297 Location location = new Location();
298 locationLevel.setLevel("");
299 locationLevel.setName("");
300 location.setPrimary(OLEConstants.TRUE);
301 location.setStatus(OLEConstants.PERMANENT);
302 location.setLocationLevel(locationLevel);
303 oleHoldings.setLocation(location);
304 }
305
306
307 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
308 holdings.setContent(holdingOlemlRecordProcessor.toXML(oleHoldings));
309
310 HoldingsTree holdingsTree = new HoldingsTree();
311
312 holdingsTree.setHoldings(holdings);
313 holdingsTree.getItems().addAll(itemXml);
314
315
316 BibTree bibTree = new BibTree();
317 bibTree.setBib(bib);
318 bibTree.getHoldingsTrees().add(holdingsTree);
319
320
321 try {
322 getDocstoreClientLocator().getDocstoreClient().createBibTree(bibTree);
323 } catch (Exception ex) {
324 ex.printStackTrace();
325 }
326
327
328 HashMap<String, Object> objects = new HashMap<String, Object>();
329 objects.put(OLEConstants.MRC, bibMarcRecord);
330
331
332
333
334 BibTree bib_Tree=getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibTree.getBib().getId());
335 if (bib_Tree == null) {
336 Thread.sleep(2000);
337 bib_Tree=getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibTree.getBib().getId());
338 }
339 String holdingsUUID = bib_Tree.getHoldingsTrees().get(0).getHoldings().getId();
340
341 if (null == holdingsUUID) {
342 throw new Exception("instance id returned from docstore is null");
343 }
344
345
346
347
348
349 OleBibRecord oleBibRecord = new OleBibRecord();
350
351
352 oleBibRecord.setBib(bib_Tree.getBib());
353 oleBibRecord.setLinkedInstanceId(holdingsUUID);
354 oleBibRecord.setBibUUID(bibTree.getBib().getId());
355 executionEnvironment.getEngineResults().setAttribute(OLEConstants.BIB_CREATION_FLAG, true);
356 OleOrderRecord oleOrderRecord = oleOrderRecordService.fetchOleOrderRecordForMarc(null,bibMarcRecord,0, null);
357 boolean validBFNFlag = (Boolean) (oleOrderRecord.getMessageMap().get("isValidBFN") == null ? true : oleOrderRecord.getMessageMap().get("isValidBFN"));
358 if (validBFNFlag) {
359 oleOverlayActionService.performOverlayLookupAction(profileName, objects, holdingsUUID, oleOrderRecord);
360 }
361 executionEnvironment.getEngineResults().setAttribute(OLEConstants.OLE_ORDER_RECORD, oleOrderRecord);
362 } catch (Exception e) {
363 e.printStackTrace();
364 executionEnvironment.getEngineResults().setAttribute(OLEConstants.OLE_ORDER_RECORD, null);
365 }
366 }
367
368
369
370
371
372
373
374 @Override
375 public void executeSimulation(ExecutionEnvironment executionEnvironment) {
376 execute(executionEnvironment);
377 }
378
379
380
381
382
383
384
385
386 private String getUUID(Response response, String docType) {
387 List<ResponseDocument> documents = response.getDocuments();
388 return getUUID(documents, docType);
389 }
390
391
392
393
394
395
396
397
398 private String getUUID(List<ResponseDocument> documents, String docType) {
399 for (Iterator<ResponseDocument> iterator = documents.iterator(); iterator.hasNext(); ) {
400 ResponseDocument responseDocument = iterator.next();
401 if (responseDocument.getType().equals(docType)) {
402 return responseDocument.getUuid();
403 } else {
404 return getUUID(responseDocument.getLinkedDocuments(), docType);
405 }
406 }
407 return null;
408 }
409
410
411
412
413
414
415
416 public void setDiscoveryHelperService(DiscoveryHelperService discoveryHelperService) {
417 this.discoveryHelperService = discoveryHelperService;
418 }
419
420
421
422
423
424
425 public DiscoveryHelperService getDiscoveryHelperService() {
426 return discoveryHelperService;
427 }
428
429 public OleOrderRecordService getOleOrderRecordService() {
430 return oleOrderRecordService;
431 }
432
433 public void setOleOrderRecordService(OleOrderRecordService oleOrderRecordService) {
434 this.oleOrderRecordService = oleOrderRecordService;
435 }
436
437 public OleOverlayActionService getOleOverlayActionService() {
438 return oleOverlayActionService;
439 }
440
441 public void setOleOverlayActionService(OleOverlayActionService oleOverlayActionService) {
442 this.oleOverlayActionService = oleOverlayActionService;
443 }
444
445 private List<ProfileAttribute> buildListOfProfileAttributes(List<ProfileAttributeBo> profileAttributes) {
446 for (Iterator iterator = profileAttributes.iterator(); iterator.hasNext(); ) {
447 ProfileAttributeBo profileAttributeBo = (ProfileAttributeBo) iterator.next();
448 ProfileAttribute profileAttribute = new ProfileAttribute();
449 profileAttribute.setAgendaName(profileAttributeBo.getAgendaName());
450 profileAttribute.setAttributeName(profileAttributeBo.getAttributeName());
451 profileAttribute.setAttributeValue(profileAttributeBo.getAttributeValue());
452 profileAttribute.setSystemValue(profileAttributeBo.getSystemValue());
453 attributes.add(profileAttribute);
454 }
455 return attributes;
456 }
457
458 public Item getOleItem(LineItemOrder lineItemOrder, BibMarcRecord bibliographicRecord) {
459 for (DataField dataField : bibliographicRecord.getDataFields()) {
460 if (dataField.getTag().equalsIgnoreCase(OLEConstants.DATA_FIELD_985)) {
461 List<SubField> subFieldList = dataField.getSubFields();
462 SubField subField = new SubField();
463 subField.setCode(OLEConstants.SUB_FIELD_A);
464 subField.setValue(OLEConstants.DEFAULT_LOCATION_LEVEL_INSTITUTION);
465 subFieldList.add(subField);
466 dataField.setSubFields(subFieldList);
467 }
468 }
469 return new OleItemRecordHandler().getOleItem(lineItemOrder, bibliographicRecord, attributes);
470 }
471
472 public Item getOleItem(BibMarcRecord bibliographicRecord) {
473 for (DataField dataField : bibliographicRecord.getDataFields()) {
474 if (dataField.getTag().equalsIgnoreCase(OLEConstants.DATA_FIELD_985)) {
475 List<SubField> subFieldList = dataField.getSubFields();
476 SubField subField = new SubField();
477 subField.setCode(OLEConstants.SUB_FIELD_A);
478 subField.setValue(OLEConstants.DEFAULT_LOCATION_LEVEL_INSTITUTION);
479 subFieldList.add(subField);
480 dataField.setSubFields(subFieldList);
481 }
482 }
483 return new OleItemRecordHandler().getOleItem(bibliographicRecord, attributes);
484 }
485
486 }