001package org.kuali.ole.ingest.action;
002
003import org.kuali.ole.DataCarrierService;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
006import org.kuali.ole.docstore.common.document.Bib;
007import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
008import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecords;
009import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
010import org.kuali.ole.docstore.common.document.content.enums.DocType;
011import org.kuali.ole.docstore.common.search.SearchParams;
012import org.kuali.ole.docstore.common.search.SearchResponse;
013import org.kuali.ole.docstore.common.search.SearchResult;
014import org.kuali.ole.docstore.common.search.SearchResultField;
015import org.kuali.ole.ingest.pojo.OverlayOption;
016import org.kuali.ole.pojo.edi.LineItemOrder;
017import org.kuali.ole.service.OleOverlayActionService;
018import org.kuali.ole.service.OverlayHelperService;
019import org.kuali.ole.service.OverlayRetrivalService;
020import org.kuali.ole.sys.context.SpringContext;
021import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
022import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
023import org.kuali.rice.krms.framework.engine.Action;
024
025import java.util.*;
026
027/**
028 * Created by IntelliJ IDEA.
029 * User: premkb
030 * Date: 12/11/12
031 * Time: 3:14 PM
032 * To change this template use File | Settings | File Templates.
033 */
034public class UpdateBibExcludingGPFieldAction implements Action {
035    private OverlayHelperService overlayHelperService;
036    private OverlayRetrivalService overlayRetrivalService;
037    private DataCarrierService dataCarrierService;
038    private OleOverlayActionService oleOverlayActionService;
039
040    private DocstoreClientLocator docstoreClientLocator;
041
042    /**
043     *  This will update the existing Bib and Instance record ignoring the Globally Protected field.
044     * @param executionEnvironment
045     */
046    @Override
047    public void execute(ExecutionEnvironment executionEnvironment) {
048        dataCarrierService = getDataCarrierService();
049        overlayHelperService = getOverlayHelperService();
050        overlayRetrivalService = getOverlayRetrivalService();
051        oleOverlayActionService = getOleOverlayActionService();
052        List<OverlayOption> overlayOptionList = (List<OverlayOption>) dataCarrierService.getData(OLEConstants.OVERLAY_OPTION_LIST);
053       // List<OverlayLookupAction> overlayLookupActionList = (List<OverlayLookupAction>) dataCarrierService.getData(OLEConstants.OVERLAY_LOOKUP_ACTION_LIST);
054        LineItemOrder lineItemOrder = (LineItemOrder) dataCarrierService.getData(OLEConstants.REQUEST_LINE_ITEM_ORDER_RECORD);
055        String profileName = (String)dataCarrierService.getData(OLEConstants.PROFILE_NM);
056        HashMap<String,String> uuids = new HashMap<String, String>();
057        BibMarcRecord newBibMarcRecord = (BibMarcRecord) dataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD);
058        try {
059            List bibInfoList = (List) dataCarrierService.getData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE);
060            String existingBibUUID = null;
061            for (Iterator iterator = bibInfoList.iterator(); iterator.hasNext(); ) {
062                Map map = (Map) iterator.next();
063                if (map.containsKey(OLEConstants.BIB_UNIQUE_ID)) {
064                    existingBibUUID = (String) map.get(OLEConstants.BIB_UNIQUE_ID);
065                    uuids.put(OLEConstants.BIBlIOGRAPHICUUID,existingBibUUID);
066                    break;
067                }
068            }
069            SearchParams searchParams=new  SearchParams();
070            searchParams.getSearchConditions().add(searchParams.buildSearchCondition("", searchParams.buildSearchField(DocType.BIB.getCode(),"bibIdentifier",existingBibUUID), "AND"));
071            searchParams.getSearchResultFields().add(searchParams.buildSearchResultField("holdings", "holdingsIdentifier"));
072            try {
073                SearchResponse searchResponse = getDocstoreClientLocator().getDocstoreClient().search(searchParams);
074                List<SearchResult>  searchResults=searchResponse.getSearchResults();
075                 if (searchResults.size()>0 ){
076                     for(SearchResultField searchResultField:searchResults.get(0).getSearchResultFields()){
077                         if(searchResultField.getFieldName()!=null && !searchResultField.getFieldName().isEmpty() && searchResultField.getFieldName().equalsIgnoreCase("holdingsIdentifier") ){
078                             uuids.put(OLEConstants.OVERLAY_HOLDINGUUID, searchResultField.getFieldValue());
079                         }
080                       break;
081                     }
082                 }
083            }
084                catch(Exception ex){
085                    throw new RuntimeException(ex);
086                }
087            /* newly modified   */
088            Bib bib = getDocstoreClientLocator().getDocstoreClient().retrieveBib(existingBibUUID);
089            String bibContent = bib.getContent();
090            BibMarcRecordProcessor recordProcessor = new BibMarcRecordProcessor();
091            BibMarcRecords bibMarcRecords = recordProcessor.fromXML(bibContent);
092            BibMarcRecord oldBibMarcRecord=bibMarcRecords.getRecords().get(0);
093            List<String> gpfFieldList = overlayRetrivalService.getGloballyProtectedFieldsList();
094            oleOverlayActionService.updateRecordExcludingGPF(uuids, oldBibMarcRecord, newBibMarcRecord,lineItemOrder, gpfFieldList, overlayOptionList,profileName);
095            executionEnvironment.getEngineResults().setAttribute(OLEConstants.UPDATE_BIB_EXCLUDING_GPF, true);
096        } catch (Exception e) {
097            e.printStackTrace();
098            executionEnvironment.getEngineResults().setAttribute(OLEConstants.UPDATE_BIB_EXCLUDING_GPF, null);
099        }
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}