001package org.kuali.ole.ingest.action;
002
003import org.kuali.ole.DataCarrierService;
004import org.kuali.ole.OLEConstants;
005import org.kuali.ole.OleItemRecordHandler;
006import org.kuali.ole.docstore.common.client.DocstoreClientLocator;
007import org.kuali.ole.docstore.common.document.BibTree;
008import org.kuali.ole.docstore.common.document.Holdings;
009import org.kuali.ole.docstore.common.document.HoldingsTree;
010import org.kuali.ole.docstore.common.document.Item;
011import org.kuali.ole.docstore.common.document.content.bib.marc.BibMarcRecord;
012import org.kuali.ole.docstore.common.document.content.bib.marc.DataField;
013import org.kuali.ole.docstore.common.document.content.bib.marc.SubField;
014import org.kuali.ole.docstore.common.document.content.bib.marc.xstream.BibMarcRecordProcessor;
015import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
016import org.kuali.ole.pojo.ProfileAttribute;
017import org.kuali.ole.sys.context.SpringContext;
018import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
019import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
020import org.kuali.rice.krms.framework.engine.Action;
021
022import java.util.*;
023
024/**
025 * UpdateItemAction is a action class for Update Item Action
026 */
027public class UpdateItemAction implements Action {
028
029    private DocstoreClientLocator docstoreClientLocator;
030
031    /**
032     *     This method takes the initial request when updating the itemAction.
033     *      @param executionEnvironment
034     */
035    @Override
036    public void execute(ExecutionEnvironment executionEnvironment) {
037        //TODO: Use strings from OleConstants.java; if not present add the string below to the class.
038        DataCarrierService dataCarrierService = getDataCarrierService();
039        BibMarcRecord bibMarcRecord = (BibMarcRecord) dataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD);
040        List bibInfoList = (List) dataCarrierService.getData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE);
041        String bibUUID = null;
042        for (Iterator iterator = bibInfoList.iterator(); iterator.hasNext(); ) {
043            Map map = (Map) iterator.next();
044            if (map.containsKey(OLEConstants.BIB_UNIQUE_ID)) {
045                bibUUID = (String) map.get(OLEConstants.BIB_UNIQUE_ID);
046                break;
047            }
048        }
049        /*List responseFromSOLR =  getDiscoveryHelperService().getResponseFromSOLR("DocType=bibliographic and id", bibUUID);
050        String instanceUUID = null;
051        for (Iterator iterator = responseFromSOLR.iterator(); iterator.hasNext(); ) {
052            Map map = (Map) iterator.next();
053            if (map.containsKey(OLEConstants.BIB_INSTANCE_ID)) {
054                List list =(List)map.get(OLEConstants.BIB_INSTANCE_ID);
055                instanceUUID =list !=null & list.size()>0 ?(String) list.get(0):null;
056                break;
057            }
058        }
059
060        List responseFromSOLR1 =  getDiscoveryHelperService().getResponseFromSOLR("DocType=instance and id", instanceUUID);
061        String itemUUID = null;
062        for (Iterator iterator = responseFromSOLR1.iterator(); iterator.hasNext(); ) {
063            Map map = (Map) iterator.next();
064            if (map.containsKey(OLEConstants.BIB_ITEM_ID)) {
065                List list = (List)map.get(OLEConstants.BIB_ITEM_ID);
066                itemUUID = list !=null & list.size()>0 ?(String) list.get(0):null;
067                break;
068            }
069        }*/
070
071        try {
072            BibTree bibTree = getDocstoreClientLocator().getDocstoreClient().retrieveBibTree(bibUUID);
073            List<HoldingsTree> holdingsTree=bibTree.getHoldingsTrees();
074            Holdings holdings = holdingsTree.get(0).getHoldings();
075            String  holdingsId = holdings.getId();
076            List<Item> items=holdingsTree.get(0).getItems();
077            String  itemId=items.get(0).getId();
078            BibMarcRecordProcessor oleItemRecordHandler = new BibMarcRecordProcessor();
079            org.kuali.ole.docstore.common.document.content.instance.Item oleItem = getOleItem(bibMarcRecord);
080            ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
081            String itemXML = itemOlemlRecordProcessor.toXML(oleItem);
082            // items.get(0).setId(itemId);
083            items.get(0).setContent(itemXML);
084            getDocstoreClientLocator().getDocstoreClient().updateItem(items.get(0));
085        }
086        catch (Exception e) {
087            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
088        }
089        //  getDocstoreHelperService().updateItem(bibliographicRecord, itemUUID);
090        executionEnvironment.getEngineResults().setAttribute(OLEConstants.UPDATE_ITEM_FLAG, true);
091    }
092
093    public org.kuali.ole.docstore.common.document.content.instance.Item getOleItem(BibMarcRecord bibMarcRecord) {
094        List<ProfileAttribute>   profileAttributes=new ArrayList<>();
095        for (DataField dataField : bibMarcRecord.getDataFields()) {
096            if (dataField.getTag().equalsIgnoreCase(OLEConstants.DATA_FIELD_985)) {
097                List<SubField> subFieldList = dataField.getSubFields();
098                SubField subField = new SubField();
099                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}