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.docstore.common.client.DocstoreClientLocator;
6 import org.kuali.ole.docstore.common.document.Bib;
7 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
8 import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
9 import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
10 import org.kuali.ole.docstore.common.document.content.enums.DocType;
11 import org.kuali.ole.docstore.common.search.SearchParams;
12 import org.kuali.ole.docstore.common.search.SearchResponse;
13 import org.kuali.ole.docstore.common.search.SearchResult;
14 import org.kuali.ole.docstore.common.search.SearchResultField;
15 import org.kuali.ole.ingest.pojo.OverlayOption;
16 import org.kuali.ole.ingest.pojo.ProfileAttributeBo;
17 import org.kuali.ole.pojo.edi.LineItemOrder;
18 import org.kuali.ole.service.OleOverlayActionService;
19 import org.kuali.ole.sys.context.SpringContext;
20 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
21 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
22 import org.kuali.rice.krms.framework.engine.Action;
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28
29
30
31
32
33
34
35
36
37 public class UpdateBibIncludingGPFieldAction implements Action {
38 private DataCarrierService dataCarrierService;
39
40
41 private OleOverlayActionService oleOverlayActionService;
42
43
44 private DocstoreClientLocator docstoreClientLocator;
45
46
47
48
49
50 @Override
51 public void execute(ExecutionEnvironment executionEnvironment) {
52 dataCarrierService = getDataCarrierService();
53 oleOverlayActionService = getOleOverlayActionService();
54 List<ProfileAttributeBo> profileAttributesList = (List<ProfileAttributeBo>) dataCarrierService.getData(OLEConstants.PROFILE_ATTRIBUTE_LIST);
55 List<OverlayOption> overlayOptionList = (List<OverlayOption>) dataCarrierService.getData(OLEConstants.OVERLAY_OPTION_LIST);
56
57 LineItemOrder lineItemOrder = (LineItemOrder) dataCarrierService.getData(OLEConstants.REQUEST_LINE_ITEM_ORDER_RECORD);
58 String profileName = (String)dataCarrierService.getData(OLEConstants.PROFILE_NM);
59 HashMap<String,String> uuids = new HashMap<String, String>();
60 BibMarcRecord newBibMarcRecord = (BibMarcRecord) dataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD);
61 try {
62 List bibInfoList = (List) dataCarrierService.getData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE);
63 String existingBibUUID = null;
64 for (Iterator iterator = bibInfoList.iterator(); iterator.hasNext(); ) {
65 Map map = (Map) iterator.next();
66 if (map.containsKey(OLEConstants.BIB_UNIQUE_ID)) {
67 existingBibUUID = (String) map.get(OLEConstants.BIB_UNIQUE_ID);
68 uuids.put(OLEConstants.BIBlIOGRAPHICUUID,existingBibUUID);
69 break;
70 }
71 }
72
73 SearchParams searchParams=new SearchParams();
74 searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.BIB.getCode(),"id",existingBibUUID), "AND"));
75
76 searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("holdings", "holdingsIdentifier"));
77 try {
78 SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
79 List<SearchResult> searchResults=searchResponse.getSearchResults();
80 if (searchResults.size()>0 ){
81 for(SearchResultField searchResultField:searchResults.get(0).getSearchResultFields()){
82 if(searchResultField.getFieldName()!=null && !searchResultField.getFieldName().isEmpty() && searchResultField.getFieldName().equalsIgnoreCase("holdingsIdentifier") ){
83 uuids.put(OLEConstants.OVERLAY_HOLDINGUUID, searchResultField.getFieldValue());
84 }
85 break;
86 }
87 }
88 }
89 catch(Exception ex){
90 throw new RuntimeException(ex);
91 }
92 Bib bib=getDocstoreClientLocator().getDocstoreClient().retrieveBib(existingBibUUID);
93 String bibContent=bib.getContent();
94 BibMarcRecordProcessor recordProcessor = new BibMarcRecordProcessor();
95 BibMarcRecords bibMarcRecords = recordProcessor.fromXML(bibContent);
96 BibMarcRecord oldBibMarcRecord=bibMarcRecords.getRecords().get(0);
97 oleOverlayActionService.updateRecordIncludingGPF(uuids, oldBibMarcRecord, newBibMarcRecord, lineItemOrder,null, overlayOptionList,profileName);
98 executionEnvironment.getEngineResults().setAttribute(OLEConstants.UPDATE_BIB_INCLUDING_GPF, true);
99 } catch (Exception e) {
100 e.printStackTrace();
101 executionEnvironment.getEngineResults().setAttribute(OLEConstants.UPDATE_BIB_INCLUDING_GPF, null);
102 }
103 }
104
105
106
107
108
109 @Override
110 public void executeSimulation(ExecutionEnvironment executionEnvironment) {
111 execute(executionEnvironment);
112 }
113
114 protected DataCarrierService getDataCarrierService() {
115 if(dataCarrierService == null){
116 return GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
117 }
118 return dataCarrierService;
119 }
120
121
122 public OleOverlayActionService getOleOverlayActionService() {
123 if(null==oleOverlayActionService){
124 oleOverlayActionService = GlobalResourceLoader.getService(OLEConstants.OVERLAY_ACTION_SERVICE);
125 }
126 return oleOverlayActionService;
127 }
128 public DocstoreClientLocator getDocstoreClientLocator() {
129 if (null == docstoreClientLocator) {
130 return SpringContext.getBean(DocstoreClientLocator.class);
131 }
132 return docstoreClientLocator;
133 }
134
135
136 }