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.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.pojo.edi.LineItemOrder;
17  import org.kuali.ole.service.OleOverlayActionService;
18  import org.kuali.ole.service.OverlayHelperService;
19  import org.kuali.ole.service.OverlayRetrivalService;
20  import org.kuali.ole.sys.context.SpringContext;
21  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
23  import org.kuali.rice.krms.framework.engine.Action;
24  
25  import java.util.*;
26  
27  /**
28   * Created by IntelliJ IDEA.
29   * User: premkb
30   * Date: 12/11/12
31   * Time: 3:14 PM
32   * To change this template use File | Settings | File Templates.
33   */
34  public class UpdateBibExcludingGPFieldAction implements Action {
35      private OverlayHelperService overlayHelperService;
36      private OverlayRetrivalService overlayRetrivalService;
37      private DataCarrierService dataCarrierService;
38      private OleOverlayActionService oleOverlayActionService;
39  
40      private DocstoreClientLocator docstoreClientLocator;
41  
42      /**
43       *  This will update the existing Bib and Instance record ignoring the Globally Protected field.
44       * @param executionEnvironment
45       */
46      @Override
47      public void execute(ExecutionEnvironment executionEnvironment) {
48          dataCarrierService = getDataCarrierService();
49          overlayHelperService = getOverlayHelperService();
50          overlayRetrivalService = getOverlayRetrivalService();
51          oleOverlayActionService = getOleOverlayActionService();
52          List<OverlayOption> overlayOptionList = (List<OverlayOption>) dataCarrierService.getData(OLEConstants.OVERLAY_OPTION_LIST);
53         // List<OverlayLookupAction> overlayLookupActionList = (List<OverlayLookupAction>) dataCarrierService.getData(OLEConstants.OVERLAY_LOOKUP_ACTION_LIST);
54          LineItemOrder lineItemOrder = (LineItemOrder) dataCarrierService.getData(OLEConstants.REQUEST_LINE_ITEM_ORDER_RECORD);
55          String profileName = (String)dataCarrierService.getData(OLEConstants.PROFILE_NM);
56          HashMap<String,String> uuids = new HashMap<String, String>();
57          BibMarcRecord newBibMarcRecord = (BibMarcRecord) dataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD);
58          try {
59              List bibInfoList = (List) dataCarrierService.getData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE);
60              String existingBibUUID = null;
61              for (Iterator iterator = bibInfoList.iterator(); iterator.hasNext(); ) {
62                  Map map = (Map) iterator.next();
63                  if (map.containsKey(OLEConstants.BIB_UNIQUE_ID)) {
64                      existingBibUUID = (String) map.get(OLEConstants.BIB_UNIQUE_ID);
65                      uuids.put(OLEConstants.BIBlIOGRAPHICUUID,existingBibUUID);
66                      break;
67                  }
68              }
69              SearchParams searchParams=new  SearchParams();
70              searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.BIB.getCode(),"bibIdentifier",existingBibUUID), "AND"));
71              searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("holdings", "holdingsIdentifier"));
72              try {
73                  SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
74                  List<SearchResult>  searchResults=searchResponse.getSearchResults();
75                   if (searchResults.size()>0 ){
76                       for(SearchResultField searchResultField:searchResults.get(0).getSearchResultFields()){
77                           if(searchResultField.getFieldName()!=null && !searchResultField.getFieldName().isEmpty() && searchResultField.getFieldName().equalsIgnoreCase("holdingsIdentifier") ){
78                               uuids.put(OLEConstants.OVERLAY_HOLDINGUUID, searchResultField.getFieldValue());
79                           }
80                         break;
81                       }
82                   }
83              }
84                  catch(Exception ex){
85                      throw new RuntimeException(ex);
86                  }
87              /* newly modified   */
88              Bib bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(existingBibUUID);
89              String bibContent = bib.getContent();
90              BibMarcRecordProcessor recordProcessor = new BibMarcRecordProcessor();
91              BibMarcRecords bibMarcRecords = recordProcessor.fromXML(bibContent);
92              BibMarcRecord oldBibMarcRecord=bibMarcRecords.getRecords().get(0);
93              List<String> gpfFieldList = overlayRetrivalService.getGloballyProtectedFieldsList();
94              oleOverlayActionService.updateRecordExcludingGPF(uuids, oldBibMarcRecord, newBibMarcRecord,lineItemOrder, gpfFieldList, overlayOptionList,profileName);
95              executionEnvironment.getEngineResults().setAttribute(OLEConstants.UPDATE_BIB_EXCLUDING_GPF, true);
96          } catch (Exception e) {
97              e.printStackTrace();
98              executionEnvironment.getEngineResults().setAttribute(OLEConstants.UPDATE_BIB_EXCLUDING_GPF, null);
99          }
100     }
101 
102     /**
103      *   This method simulate the executionEnvironment.
104      * @param executionEnvironment
105      */
106 
107     @Override
108     public void executeSimulation(ExecutionEnvironment executionEnvironment) {
109         execute(executionEnvironment);
110     }
111 
112     /**
113      *  Gets the dataCarrierService attribute.
114      * @return  Returns dataCarrierService.
115      */
116     protected DataCarrierService getDataCarrierService() {
117         if(dataCarrierService == null){
118             return GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
119         }
120         return dataCarrierService;
121     }
122 
123     public OverlayHelperService getOverlayHelperService() {
124         if(overlayHelperService==null){
125             overlayHelperService = GlobalResourceLoader.getService(OLEConstants.OVERLAY_HELPER_SERVICE);
126         }
127         return overlayHelperService;
128     }
129 
130     public OverlayRetrivalService getOverlayRetrivalService() {
131         if(overlayRetrivalService == null){
132             overlayRetrivalService = GlobalResourceLoader.getService(OLEConstants.OVERLAY_RETRIVAL_SERVICE);
133         }
134         return overlayRetrivalService;
135     }
136 
137 
138     public OleOverlayActionService getOleOverlayActionService() {
139         if(null==oleOverlayActionService){
140             oleOverlayActionService = GlobalResourceLoader.getService(OLEConstants.OVERLAY_ACTION_SERVICE);
141         }
142         return oleOverlayActionService;
143     }
144 
145     public DocstoreClientLocator getDocstoreClientLocator() {
146         if (null == docstoreClientLocator) {
147             return  SpringContext.getBean(DocstoreClientLocator.class);
148         }
149         return docstoreClientLocator;
150     }
151 
152 
153 }