001package org.kuali.ole.docstore.document.rdbms;
002
003
004import org.apache.commons.lang.StringUtils;
005import org.kuali.ole.DocumentUniqueIDPrefix;
006import org.kuali.ole.docstore.OleDocStoreException;
007import org.kuali.ole.docstore.document.AbstractDocumentManager;
008import org.kuali.ole.docstore.document.DocumentManager;
009
010import org.kuali.ole.docstore.model.rdbms.bo.*;
011import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
012import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
013import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
014import org.kuali.ole.docstore.process.batch.BulkProcessRequest;
015import org.kuali.ole.docstore.service.BeanLocator;
016import org.kuali.ole.docstore.service.OleUuidCheckWebService;
017import org.kuali.ole.docstore.service.OleWebServiceProvider;
018import org.kuali.ole.docstore.service.impl.OleWebServiceProviderImpl;
019import org.kuali.ole.pojo.OleException;
020import org.kuali.rice.core.api.config.property.ConfigContext;
021import org.kuali.rice.krad.service.BusinessObjectService;
022import org.kuali.rice.krad.service.KRADServiceLocator;
023
024import javax.jcr.RepositoryException;
025import java.io.FileNotFoundException;
026import java.util.ArrayList;
027import java.util.HashMap;
028import java.util.List;
029import java.util.Map;
030
031/**
032 * Created with IntelliJ IDEA.
033 * User: mjagan
034 * Date: 6/26/13
035 * Time: 3:17 PM
036 * To change this template use File | Settings | File Templates.
037 */
038public abstract class RdbmsAbstarctDocumentManager extends AbstractDocumentManager {
039
040    private BusinessObjectService businessObjectService;
041
042    @Override
043    public List<ResponseDocument> ingest(List<RequestDocument> requestDocuments, Object object) throws OleDocStoreException {
044
045        List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
046        for (RequestDocument requestDocument : requestDocuments) {
047            ResponseDocument responseDocument = new ResponseDocument();
048            this.ingest(requestDocument, object, responseDocument);
049            responseDocuments.add(responseDocument);
050        }
051        return responseDocuments;
052    }
053
054    public BusinessObjectService getBusinessObjectService() {
055        if (null == businessObjectService) {
056            businessObjectService = KRADServiceLocator.getBusinessObjectService();
057        }
058        return businessObjectService;
059    }
060
061    @Override
062    public ResponseDocument ingest(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
063
064        BusinessObjectService businessObjectService = (BusinessObjectService) object;
065        storeDocument(requestDocument, object, respDoc);
066        storeLinkDocuments(requestDocument, object, respDoc);
067
068        return respDoc;
069    }
070
071
072    public void storeLinkDocuments(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
073
074        List<ResponseDocument> linkedResponseDocumentList = new ArrayList<ResponseDocument>();
075        if (respDoc.getLinkedDocuments() != null && respDoc.getLinkedDocuments().size() > 0) {
076            linkedResponseDocumentList.addAll(respDoc.getLinkedDocuments());
077        }
078        respDoc.setLinkedDocuments(linkedResponseDocumentList);
079        for (RequestDocument linkedRequestDocument : requestDocument.getLinkedRequestDocuments()) {
080            ResponseDocument linkedResponseDocument = new ResponseDocument();
081            linkedResponseDocument.setUuid(respDoc.getUuid());
082            linkedResponseDocumentList.add(linkedResponseDocument);
083            DocumentManager documentManager = BeanLocator.getDocstoreFactory()
084                    .getDocumentManager(linkedRequestDocument.getCategory(), linkedRequestDocument.getType(), linkedRequestDocument.getFormat());
085            documentManager.addResourceId(linkedRequestDocument, respDoc);
086            String createdBy = requestDocument.getAdditionalAttributes().getAttribute(AdditionalAttributes.CREATED_BY);
087            String createdDate = requestDocument.getAdditionalAttributes().getAttribute(AdditionalAttributes.DATE_ENTERED);
088            if (linkedRequestDocument != null && linkedRequestDocument.getAdditionalAttributes() != null) {
089                linkedRequestDocument.getAdditionalAttributes().setAttribute(AdditionalAttributes.HOLDINGS_CREATED_BY, createdBy);
090                linkedRequestDocument.getAdditionalAttributes().setAttribute(AdditionalAttributes.HOLDINGS_DATE_ENTERED, createdDate);
091            } else {
092                AdditionalAttributes additionalAttributes = new AdditionalAttributes();
093                additionalAttributes.setAttribute(AdditionalAttributes.HOLDINGS_CREATED_BY, createdBy);
094                additionalAttributes.setAttribute(AdditionalAttributes.HOLDINGS_DATE_ENTERED, createdDate);
095                linkedRequestDocument.setAdditionalAttributes(additionalAttributes);
096            }
097
098            //linkedRequestDocument.setAdditionalAttributes(additionalAttributes);
099
100            documentManager.storeDocument(linkedRequestDocument, object, linkedResponseDocument);
101        }
102    }
103
104    @Override
105    public List<ResponseDocument> delete(List<RequestDocument> requestDocuments, Object object) throws Exception {
106        List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
107        for (RequestDocument requestDocument : requestDocuments) {
108            ResponseDocument responseDocument = this.delete(requestDocument, object);
109            responseDocuments.add(responseDocument);
110        }
111        return responseDocuments;
112    }
113
114    @Override
115    public ResponseDocument delete(RequestDocument requestDocument, Object object) throws Exception {
116        ResponseDocument responseDocument = new ResponseDocument();
117        BusinessObjectService businessObjectService = (BusinessObjectService) object;
118        responseDocument = deleteVerify(requestDocument , object);
119        if("success".equals(responseDocument.getStatus())) {
120            deleteDocs(requestDocument, object);
121        }
122        return responseDocument;
123    }
124
125
126    public abstract void deleteDocs(RequestDocument requestDocument, Object object);
127
128    @Override
129    public List<ResponseDocument> checkout(List<RequestDocument> requestDocuments, Object object) throws OleDocStoreException {
130        List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
131        for (RequestDocument requestDocument : requestDocuments) {
132            ResponseDocument responseDocument = new ResponseDocument();
133            this.checkout(requestDocument, object);
134            responseDocuments.add(responseDocument);
135        }
136        return responseDocuments;
137    }
138
139    @Override
140    public ResponseDocument checkout(RequestDocument requestDocument, Object object) throws OleDocStoreException {
141
142        BusinessObjectService businessObjectService = (BusinessObjectService) object;
143        ResponseDocument respDoc = checkoutContent(requestDocument, object);
144
145        return respDoc;
146    }
147
148    public abstract ResponseDocument checkoutContent(RequestDocument requestDocument, Object object);
149
150    @Override
151    public List<ResponseDocument> checkin(List<RequestDocument> requestDocuments, Object object) throws OleDocStoreException {
152        List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
153        for (RequestDocument requestDocument : requestDocuments) {
154            ResponseDocument responseDocument = new ResponseDocument();
155            this.checkin(requestDocument, object, responseDocument);
156            responseDocuments.add(responseDocument);
157        }
158        return responseDocuments;
159    }
160
161    @Override
162    public ResponseDocument checkin(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
163
164        BusinessObjectService businessObjectService = (BusinessObjectService) object;
165        if (requestDocument.getId() != null || requestDocument.getId().trim().length() > 0) {
166            requestDocument.setUuid(requestDocument.getId());
167        }
168        checkInContent(requestDocument, object, respDoc);
169//        AdditionalAttributes attributes = requestDocument.getAdditionalAttributes();
170//        respDoc.setAdditionalAttributes(attributes);
171        return respDoc;
172    }
173
174    public abstract void checkInContent(RequestDocument requestDocument, Object object, ResponseDocument responseDocument) throws OleDocStoreException;
175
176
177    @Override
178    public ResponseDocument buildResponseDocument(RequestDocument requestDocument) {
179        return null;  //To change body of implemented methods use File | Settings | File Templates.
180    }
181
182    @Override
183    public void validateInput(RequestDocument requestDocument, Object object, List<String> valuesList) throws OleDocStoreException {
184        //To change body of implemented methods use File | Settings | File Templates.
185    }
186
187    @Override
188    public void bulkIngest(BulkProcessRequest bulkProcessRequest, List<RequestDocument> requestDocuments) throws OleDocStoreException {
189        //To change body of implemented methods use File | Settings | File Templates.
190    }
191
192    @Override
193    public void index(List<RequestDocument> requestDocuments, boolean commit) throws OleDocStoreException {
194        //To change body of implemented methods use File | Settings | File Templates.
195    }
196
197    @Override
198    public ResponseDocument bind(RequestDocument requestDocument, Object object, String operation) throws OleDocStoreException, RepositoryException, OleException, FileNotFoundException {
199        return new RdbmsWorkInstanceDocumentManager().bind(requestDocument, object, operation);
200    }
201
202    @Override
203    public ResponseDocument unbind(RequestDocument requestDocument, Object object, String operation) throws OleDocStoreException, RepositoryException, OleException, FileNotFoundException {
204        return null;  //To change body of implemented methods use File | Settings | File Templates.
205    }
206
207    @Override
208    public List<ResponseDocument> deleteVerify(List<RequestDocument> requestDocument, Object object) {
209        return null;  //To change body of implemented methods use File | Settings | File Templates.
210    }
211
212    @Override
213    public ResponseDocument deleteVerify(RequestDocument requestDocument, Object object) throws Exception {
214        return null;  //To change body of implemented methods use File | Settings | File Templates.
215    }
216
217    protected boolean checkInstancesOrItemsExistsInOLE(String instanceIdentifier, Object object) throws Exception {
218        String uuidsNotInOle = null;
219        List<String> instanceOrItemIdentifiersList = new ArrayList<String>();
220        BusinessObjectService businessObjectService = (BusinessObjectService) object;
221        Map instanceMap = new HashMap();
222        instanceMap.put("instanceId", DocumentUniqueIDPrefix.getDocumentId(instanceIdentifier));
223        InstanceRecord oleInstanceRecord = getBusinessObjectService().findByPrimaryKey(InstanceRecord.class, instanceMap);
224        List<ItemRecord> oleItemRecords = null;
225        if (oleInstanceRecord != null && oleInstanceRecord.getItemRecords() != null) {
226            oleItemRecords = oleInstanceRecord.getItemRecords();
227
228            for (ItemRecord oleItemRecord : oleItemRecords) {
229                instanceOrItemIdentifiersList.add(oleItemRecord.getItemId());
230            }
231        }
232        instanceOrItemIdentifiersList.add(instanceIdentifier);
233        return checkInstancesOrItemsExistsInOLE(instanceOrItemIdentifiersList);
234    }
235
236    protected boolean checkInstancesOrItemsExistsInOLE(List<String> uuidsList) {
237        String uuidsNotInOle = null;
238        String serviceURL = ConfigContext.getCurrentContextConfig().getProperty("uuidCheckServiceURL");
239        OleWebServiceProvider oleWebServiceProvider = new OleWebServiceProviderImpl();
240        OleUuidCheckWebService oleUuidCheckWebService = (OleUuidCheckWebService) oleWebServiceProvider
241                .getService("org.kuali.ole.docstore.service.OleUuidCheckWebService", "oleUuidCheckWebService",
242                        serviceURL);
243        StringBuilder uuidsSB = new StringBuilder();
244        for (String uuid : uuidsList) {
245            //uuidsSB.append(requestDocument.getId()).append(",");
246            uuidsSB.append(uuid).append(",");
247        }
248        uuidsNotInOle = oleUuidCheckWebService.checkUuidExsistence(uuidsSB.substring(0, uuidsSB.length() - 1));
249        String[] uuids = StringUtils.split(uuidsNotInOle, ",");
250        if (uuids.length == uuidsList.size()) {
251            return false;
252        } else {
253            return true;
254        }
255    }
256}