001    package org.kuali.ole.ingest.action;
002    
003    import org.kuali.ole.DataCarrierService;
004    import org.kuali.ole.OLEConstants;
005    import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
006    import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
007    import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
008    import org.kuali.ole.editor.service.DiscoveryHelperService;
009    import org.kuali.ole.editor.service.DocstoreHelperService;
010    import org.kuali.ole.ingest.pojo.ProfileAttributeBo;
011    import org.kuali.ole.pojo.OleBibRecord;
012    import org.kuali.ole.pojo.bib.BibliographicRecord;
013    import org.kuali.ole.service.OverlayHelperService;
014    import org.kuali.ole.service.impl.OverlayHelperServiceImpl;
015    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
016    import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
017    import org.kuali.rice.krms.framework.engine.Action;
018    
019    import java.util.Iterator;
020    import java.util.List;
021    import java.util.Map;
022    
023    /**
024     * CreateBibAction is the action class for BatchIngest(Staff upload screen) which creates a bibliographic
025     * record in Docstore.
026     */
027    public class DeleteReplaceAction implements Action {
028        private DocstoreHelperService docstoreHelperService;
029        private DiscoveryHelperService discoveryHelperService;
030        private OverlayHelperService overlayHelperService;
031    
032        /**
033         *  This method will delete the existing bib and instance record and replace with the new record.
034         * @param executionEnvironment
035         */
036        @Override
037        public void execute(ExecutionEnvironment executionEnvironment) {
038            DataCarrierService dataCarrierService = getDataCarrierService();
039            List<ProfileAttributeBo> profileAttributes = (List<ProfileAttributeBo>) dataCarrierService.getData(OLEConstants.PROFILE_ATTRIBUTE_LIST);
040            BibliographicRecord bibliographicRecord = (BibliographicRecord) dataCarrierService.getData(OLEConstants.REQUEST_BIB_RECORD);
041            try {
042                List bibInfoList = (List) dataCarrierService.getData(OLEConstants.BIB_INFO_LIST_FROM_SOLR_RESPONSE);
043                String bibUUID = null;
044                for (Iterator iterator = bibInfoList.iterator(); iterator.hasNext(); ) {
045                    Map map = (Map) iterator.next();
046                    if (map.containsKey(OLEConstants.BIB_UNIQUE_ID)) {
047                        bibUUID = (String) map.get(OLEConstants.BIB_UNIQUE_ID);
048                        break;
049                    }
050                }
051    
052                List responseFromSOLR =  getDiscoveryHelperService().getResponseFromSOLR("DocType=bibliographic and id", bibUUID);
053                String instanceUUID = null;
054                for (Iterator iterator = responseFromSOLR.iterator(); iterator.hasNext(); ) {
055                    Map map = (Map) iterator.next();
056                    if (map.containsKey(OLEConstants.BIB_INSTANCE_ID)) {
057                        List list =(List)map.get(OLEConstants.BIB_INSTANCE_ID);
058                        instanceUUID =list !=null & list.size()>0 ?(String) list.get(0):null;
059                        break;
060                    }
061                }
062    
063                String deleteBibResponseXMLString = getDocstoreHelperService().deleteDocstoreRecord(bibUUID);
064                String deleteInstanceResponseXMLString = docstoreHelperService.deleteDocstoreRecord(instanceUUID);
065    
066                String responseFromDocstore = getDocstoreHelperService().persistNewToDocstoreForIngest(bibliographicRecord, profileAttributes);
067                ResponseHandler responseHander = new ResponseHandler();
068                Response response = responseHander.toObject(responseFromDocstore);
069                instanceUUID = getOverlayHelperService().getUUID(response, OLEConstants.INSTANCE_DOC_TYPE);
070    
071                List bibInfo = getDiscoveryHelperService().getBibInformationFromInsatnceId(instanceUUID);
072    
073                OleBibRecord oleBibRecord = new OleBibRecord();
074                Map<String, List> bibFieldValues = (Map<String, List>) bibInfo.get(0);
075                oleBibRecord.setBibAssociatedFieldsValueMap(bibFieldValues);
076                oleBibRecord.setLinkedInstanceId(instanceUUID);
077                oleBibRecord.setBibUUID(getOverlayHelperService().getUUID(response, OLEConstants.BIB_DOC_TYPE));
078                executionEnvironment.getEngineResults().setAttribute(OLEConstants.OLE_BIB_RECORD, oleBibRecord);
079                executionEnvironment.getEngineResults().setAttribute(OLEConstants.OVERLAY_DELETE_REPLACE, true);
080            } catch (Exception e) {
081                executionEnvironment.getEngineResults().setAttribute(OLEConstants.OVERLAY_DELETE_REPLACE, null);
082            }
083        }
084    
085        /**
086         *   This method simulate the executionEnvironment.
087         * @param executionEnvironment
088         */
089    
090        @Override
091        public void executeSimulation(ExecutionEnvironment executionEnvironment) {
092            execute(executionEnvironment);
093        }
094    
095        /**
096         *     Gets the docstoreHelperService attribute.
097         * @return  Returns the docstoreHelperService.
098         */
099        public DocstoreHelperService getDocstoreHelperService() {
100            if(docstoreHelperService == null){
101                docstoreHelperService = new DocstoreHelperService();
102            }
103            return docstoreHelperService;
104        }
105    
106        /**
107         * Sets the docstoreHelperService attribute value.
108         * @param docstoreHelperService .The docstoreHelperService to set.
109         */
110        public void setDocstoreHelperService(DocstoreHelperService docstoreHelperService) {
111            this.docstoreHelperService = docstoreHelperService;
112        }
113    
114        /**
115         *   Sets the discoveryHelperService attribute value.
116         * @param discoveryHelperService .The discoveryHelperService to set.
117         */
118        public void setDiscoveryHelperService(DiscoveryHelperService discoveryHelperService) {
119            this.discoveryHelperService = discoveryHelperService;
120        }
121    
122        /**
123         * Gets the discoveryHelperService attribute.
124         * @return  Returns discoveryHelperService.
125         */
126        public DiscoveryHelperService getDiscoveryHelperService() {
127            if(discoveryHelperService == null){
128                discoveryHelperService = new DiscoveryHelperService();
129            }
130            return discoveryHelperService;
131        }
132    
133        /**
134         *  Gets the dataCarrierService attribute.
135         * @return  Returns dataCarrierService.
136         */
137        protected DataCarrierService getDataCarrierService() {
138            return GlobalResourceLoader.getService(OLEConstants.DATA_CARRIER_SERVICE);
139        }
140    
141        public OverlayHelperService getOverlayHelperService() {
142            if(overlayHelperService == null){
143                overlayHelperService = new OverlayHelperServiceImpl();
144            }
145            return overlayHelperService;
146        }
147    }