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.OleItemRecordHandler;
6 import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
7 import org.kuali.ole.docstore.common.document.BibTree;
8 import org.kuali.ole.docstore.common.document.Holdings;
9 import org.kuali.ole.docstore.common.document.HoldingsTree;
10 import org.kuali.ole.docstore.common.document.Item;
11 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
12 import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
13 import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
14 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
15 import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
16 import org.kuali.ole.pojo.ProfileAttribute;
17 import org.kuali.ole.sys.context.SpringContext;
18 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
19 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
20 import org.kuali.rice.krms.framework.engine.Action;
21
22 import java.util.*;
23
24
25
26
27 public class UpdateItemAction implements Action {
28
29 private DocstoreClientLocator docstoreClientLocator;
30
31
32
33
34
35 @Override
36 public void execute(ExecutionEnvironment executionEnvironment) {
37
38 DataCarrierService dataCarrierService = getDataCarrierService();
39 BibMarcRecord bibMarcRecord = (BibMarcRecord) dataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD);
40 List bibInfoList = (List) dataCarrierService.getData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE);
41 String bibUUID = null;
42 for (Iterator iterator = bibInfoList.iterator(); iterator.hasNext(); ) {
43 Map map = (Map) iterator.next();
44 if (map.containsKey(OLEConstants.BIB_UNIQUE_ID)) {
45 bibUUID = (String) map.get(OLEConstants.BIB_UNIQUE_ID);
46 break;
47 }
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 try {
72 BibTree bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibUUID);
73 List<HoldingsTree> holdingsTree=bibTree.getHoldingsTrees();
74 Holdings holdings = holdingsTree.get(0).getHoldings();
75 String holdingsId = holdings.getId();
76 List<Item> items=holdingsTree.get(0).getItems();
77 String itemId=items.get(0).getId();
78 BibMarcRecordProcessor oleItemRecordHandler = new BibMarcRecordProcessor();
79 org.kuali.ole.docstore.common.document.content.instance.Item oleItem = getOleItem(bibMarcRecord);
80 ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
81 String itemXML = itemOlemlRecordProcessor.toXML(oleItem);
82
83 items.get(0).setContent(itemXML);
84 getDocstoreClientLocator().getDocstoreClient().updateItem(items.get(0));
85 }
86 catch (Exception e) {
87 e.printStackTrace();
88 }
89
90 executionEnvironment.getEngineResults().setAttribute(OLEConstants.UPDATE_ITEM_FLAG, true);
91 }
92
93 public org.kuali.ole.docstore.common.document.content.instance.Item getOleItem(BibMarcRecord bibMarcRecord) {
94 List<ProfileAttribute> profileAttributes=new ArrayList<>();
95 for (DataField dataField : bibMarcRecord.getDataFields()) {
96 if (dataField.getTag().equalsIgnoreCase(OLEConstants.DATA_FIELD_985)) {
97 List<SubField> subFieldList = dataField.getSubFields();
98 SubField subField = new SubField();
99 subField.setCode(OLEConstants.SUB_FIELD_A);
100 subField.setValue(OLEConstants.DEFAULT_LOCATION_LEVEL_INSTITUTION);
101 subFieldList.add(subField);
102 dataField.setSubFields(subFieldList);
103 }
104 }
105 return new OleItemRecordHandler().getOleItem(bibMarcRecord, profileAttributes);
106 }
107
108
109 public DocstoreClientLocator getDocstoreClientLocator() {
110 if (null == docstoreClientLocator) {
111 return SpringContext.getBean(DocstoreClientLocator.class);
112 }
113 return docstoreClientLocator;
114 }
115
116 public void setDocstoreClientLocator(DocstoreClientLocator docstoreClientLocator) {
117 this.docstoreClientLocator = docstoreClientLocator;
118 }
119
120
121
122
123
124
125 protected DataCarrierService getDataCarrierService() {
126 return GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
127 }
128
129
130
131
132
133 @Override
134 public void executeSimulation(ExecutionEnvironment executionEnvironment) {
135 execute(executionEnvironment);
136 }
137
138
139
140 }