View Javadoc
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   * UpdateItemAction is a action class for Update Item Action
26   */
27  public class UpdateItemAction implements Action {
28  
29      private DocstoreClientLocator docstoreClientLocator;
30  
31      /**
32       *     This method takes the initial request when updating the itemAction.
33       *      @param executionEnvironment
34       */
35      @Override
36      public void execute(ExecutionEnvironment executionEnvironment) {
37          //TODO: Use strings from OleConstants.java; if not present add the string below to the class.
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          /*List responseFromSOLR =  getDiscoveryHelperService().getResponseFromSOLR("DocType=bibliographic and id", bibUUID);
50          String instanceUUID = null;
51          for (Iterator iterator = responseFromSOLR.iterator(); iterator.hasNext(); ) {
52              Map map = (Map) iterator.next();
53              if (map.containsKey(OLEConstants.BIB_INSTANCE_ID)) {
54                  List list =(List)map.get(OLEConstants.BIB_INSTANCE_ID);
55                  instanceUUID =list !=null & list.size()>0 ?(String) list.get(0):null;
56                  break;
57              }
58          }
59  
60          List responseFromSOLR1 =  getDiscoveryHelperService().getResponseFromSOLR("DocType=instance and id", instanceUUID);
61          String itemUUID = null;
62          for (Iterator iterator = responseFromSOLR1.iterator(); iterator.hasNext(); ) {
63              Map map = (Map) iterator.next();
64              if (map.containsKey(OLEConstants.BIB_ITEM_ID)) {
65                  List list = (List)map.get(OLEConstants.BIB_ITEM_ID);
66                  itemUUID = list !=null & list.size()>0 ?(String) list.get(0):null;
67                  break;
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              // items.get(0).setId(itemId);
83              items.get(0).setContent(itemXML);
84              getDocstoreClientLocator().getDocstoreClient().updateItem(items.get(0));
85          }
86          catch (Exception e) {
87              e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
88          }
89          //  getDocstoreHelperService().updateItem(bibliographicRecord, itemUUID);
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      *  Gets the dataCarrierService attribute.
123      * @return  Returns dataCarrierService.
124      */
125     protected DataCarrierService getDataCarrierService() {
126         return GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
127     }
128 
129     /**
130      *   This method simulate the executionEnvironment.
131      * @param executionEnvironment
132      */
133     @Override
134     public void executeSimulation(ExecutionEnvironment executionEnvironment) {
135         execute(executionEnvironment);
136     }
137 
138 
139 
140 }