View Javadoc
1   package org.kuali.ole.docstore.document.rdbms;
2   
3   
4   import org.apache.commons.lang.StringUtils;
5   import org.kuali.ole.DocumentUniqueIDPrefix;
6   import org.kuali.ole.docstore.OleDocStoreException;
7   import org.kuali.ole.docstore.document.AbstractDocumentManager;
8   import org.kuali.ole.docstore.document.DocumentManager;
9   
10  import org.kuali.ole.docstore.model.rdbms.bo.*;
11  import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
12  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
13  import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
14  import org.kuali.ole.docstore.process.batch.BulkProcessRequest;
15  import org.kuali.ole.docstore.service.BeanLocator;
16  import org.kuali.ole.docstore.service.OleUuidCheckWebService;
17  import org.kuali.ole.docstore.service.OleWebServiceProvider;
18  import org.kuali.ole.docstore.service.impl.OleWebServiceProviderImpl;
19  import org.kuali.ole.pojo.OleException;
20  import org.kuali.rice.core.api.config.property.ConfigContext;
21  import org.kuali.rice.krad.service.BusinessObjectService;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  
24  import javax.jcr.RepositoryException;
25  import java.io.FileNotFoundException;
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * Created with IntelliJ IDEA.
33   * User: mjagan
34   * Date: 6/26/13
35   * Time: 3:17 PM
36   * To change this template use File | Settings | File Templates.
37   */
38  @Deprecated
39  public abstract class RdbmsAbstarctDocumentManager extends AbstractDocumentManager {
40  
41      private BusinessObjectService businessObjectService;
42  
43      @Override
44      public List<ResponseDocument> ingest(List<RequestDocument> requestDocuments, Object object) throws OleDocStoreException {
45  
46          List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
47          for (RequestDocument requestDocument : requestDocuments) {
48              ResponseDocument responseDocument = new ResponseDocument();
49              this.ingest(requestDocument, object, responseDocument);
50              responseDocuments.add(responseDocument);
51          }
52          return responseDocuments;
53      }
54  
55      public BusinessObjectService getBusinessObjectService() {
56          if (null == businessObjectService) {
57              businessObjectService = KRADServiceLocator.getBusinessObjectService();
58          }
59          return businessObjectService;
60      }
61  
62      @Override
63      public ResponseDocument ingest(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
64  
65          BusinessObjectService businessObjectService = (BusinessObjectService) object;
66          storeDocument(requestDocument, object, respDoc);
67          storeLinkDocuments(requestDocument, object, respDoc);
68  
69          return respDoc;
70      }
71  
72  
73      public void storeLinkDocuments(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
74  
75          List<ResponseDocument> linkedResponseDocumentList = new ArrayList<ResponseDocument>();
76          if (respDoc.getLinkedDocuments() != null && respDoc.getLinkedDocuments().size() > 0) {
77              linkedResponseDocumentList.addAll(respDoc.getLinkedDocuments());
78          }
79          respDoc.setLinkedDocuments(linkedResponseDocumentList);
80          for (RequestDocument linkedRequestDocument : requestDocument.getLinkedRequestDocuments()) {
81              ResponseDocument linkedResponseDocument = new ResponseDocument();
82              linkedResponseDocument.setUuid(respDoc.getUuid());
83              linkedResponseDocumentList.add(linkedResponseDocument);
84              DocumentManager documentManager = BeanLocator.getDocstoreFactory()
85                      .getDocumentManager(linkedRequestDocument.getCategory(), linkedRequestDocument.getType(), linkedRequestDocument.getFormat());
86              documentManager.addResourceId(linkedRequestDocument, respDoc);
87              String createdBy = requestDocument.getAdditionalAttributes().getAttribute(AdditionalAttributes.CREATED_BY);
88              String createdDate = requestDocument.getAdditionalAttributes().getAttribute(AdditionalAttributes.DATE_ENTERED);
89              if (linkedRequestDocument != null && linkedRequestDocument.getAdditionalAttributes() != null) {
90                  linkedRequestDocument.getAdditionalAttributes().setAttribute(AdditionalAttributes.HOLDINGS_CREATED_BY, createdBy);
91                  linkedRequestDocument.getAdditionalAttributes().setAttribute(AdditionalAttributes.HOLDINGS_DATE_ENTERED, createdDate);
92              } else {
93                  AdditionalAttributes additionalAttributes = new AdditionalAttributes();
94                  additionalAttributes.setAttribute(AdditionalAttributes.HOLDINGS_CREATED_BY, createdBy);
95                  additionalAttributes.setAttribute(AdditionalAttributes.HOLDINGS_DATE_ENTERED, createdDate);
96                  linkedRequestDocument.setAdditionalAttributes(additionalAttributes);
97              }
98  
99              //linkedRequestDocument.setAdditionalAttributes(additionalAttributes);
100 
101             documentManager.storeDocument(linkedRequestDocument, object, linkedResponseDocument);
102         }
103     }
104 
105     @Override
106     public List<ResponseDocument> delete(List<RequestDocument> requestDocuments, Object object) throws Exception {
107         List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
108         for (RequestDocument requestDocument : requestDocuments) {
109             ResponseDocument responseDocument = this.delete(requestDocument, object);
110             responseDocuments.add(responseDocument);
111         }
112         return responseDocuments;
113     }
114 
115     @Override
116     public ResponseDocument delete(RequestDocument requestDocument, Object object) throws Exception {
117         ResponseDocument responseDocument = new ResponseDocument();
118         BusinessObjectService businessObjectService = (BusinessObjectService) object;
119         responseDocument = deleteVerify(requestDocument , object);
120         if("success".equals(responseDocument.getStatus())) {
121             deleteDocs(requestDocument, object);
122         }
123         return responseDocument;
124     }
125 
126 
127     public abstract void deleteDocs(RequestDocument requestDocument, Object object);
128 
129     @Override
130     public List<ResponseDocument> checkout(List<RequestDocument> requestDocuments, Object object) throws OleDocStoreException {
131         List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
132         for (RequestDocument requestDocument : requestDocuments) {
133             ResponseDocument responseDocument = new ResponseDocument();
134             this.checkout(requestDocument, object);
135             responseDocuments.add(responseDocument);
136         }
137         return responseDocuments;
138     }
139 
140     @Override
141     public ResponseDocument checkout(RequestDocument requestDocument, Object object) throws OleDocStoreException {
142 
143         BusinessObjectService businessObjectService = (BusinessObjectService) object;
144         ResponseDocument respDoc = checkoutContent(requestDocument, object);
145 
146         return respDoc;
147     }
148 
149     public abstract ResponseDocument checkoutContent(RequestDocument requestDocument, Object object);
150 
151     @Override
152     public List<ResponseDocument> checkin(List<RequestDocument> requestDocuments, Object object) throws OleDocStoreException {
153         List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
154         for (RequestDocument requestDocument : requestDocuments) {
155             ResponseDocument responseDocument = new ResponseDocument();
156             this.checkin(requestDocument, object, responseDocument);
157             responseDocuments.add(responseDocument);
158         }
159         return responseDocuments;
160     }
161 
162     @Override
163     public ResponseDocument checkin(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
164 
165         BusinessObjectService businessObjectService = (BusinessObjectService) object;
166         if (requestDocument.getId() != null || requestDocument.getId().trim().length() > 0) {
167             requestDocument.setUuid(requestDocument.getId());
168         }
169         checkInContent(requestDocument, object, respDoc);
170 //        AdditionalAttributes attributes = requestDocument.getAdditionalAttributes();
171 //        respDoc.setAdditionalAttributes(attributes);
172         return respDoc;
173     }
174 
175     public abstract void checkInContent(RequestDocument requestDocument, Object object, ResponseDocument responseDocument) throws OleDocStoreException;
176 
177 
178     @Override
179     public ResponseDocument buildResponseDocument(RequestDocument requestDocument) {
180         return null;  //To change body of implemented methods use File | Settings | File Templates.
181     }
182 
183     @Override
184     public void validateInput(RequestDocument requestDocument, Object object, List<String> valuesList) throws OleDocStoreException {
185         //To change body of implemented methods use File | Settings | File Templates.
186     }
187 
188     @Override
189     public void bulkIngest(BulkProcessRequest bulkProcessRequest, List<RequestDocument> requestDocuments) throws OleDocStoreException {
190         //To change body of implemented methods use File | Settings | File Templates.
191     }
192 
193     @Override
194     public void index(List<RequestDocument> requestDocuments, boolean commit) throws OleDocStoreException {
195         //To change body of implemented methods use File | Settings | File Templates.
196     }
197 
198     @Override
199     public ResponseDocument bind(RequestDocument requestDocument, Object object, String operation) throws OleDocStoreException, RepositoryException, OleException, FileNotFoundException {
200         return new RdbmsWorkInstanceDocumentManager().bind(requestDocument, object, operation);
201     }
202 
203     @Override
204     public ResponseDocument unbind(RequestDocument requestDocument, Object object, String operation) throws OleDocStoreException, RepositoryException, OleException, FileNotFoundException {
205         return null;  //To change body of implemented methods use File | Settings | File Templates.
206     }
207 
208     @Override
209     public List<ResponseDocument> deleteVerify(List<RequestDocument> requestDocument, Object object) {
210         return null;  //To change body of implemented methods use File | Settings | File Templates.
211     }
212 
213     @Override
214     public ResponseDocument deleteVerify(RequestDocument requestDocument, Object object) throws Exception {
215         return null;  //To change body of implemented methods use File | Settings | File Templates.
216     }
217 
218     protected boolean checkInstancesOrItemsExistsInOLE(String instanceIdentifier, Object object) throws Exception {
219         String uuidsNotInOle = null;
220         List<String> instanceOrItemIdentifiersList = new ArrayList<String>();
221         BusinessObjectService businessObjectService = (BusinessObjectService) object;
222         Map instanceMap = new HashMap();
223         instanceMap.put("instanceId", DocumentUniqueIDPrefix.getDocumentId(instanceIdentifier));
224         InstanceRecord oleInstanceRecord = getBusinessObjectService().findByPrimaryKey(InstanceRecord.class, instanceMap);
225         List<ItemRecord> oleItemRecords = null;
226         if (oleInstanceRecord != null && oleInstanceRecord.getItemRecords() != null) {
227             oleItemRecords = oleInstanceRecord.getItemRecords();
228 
229             for (ItemRecord oleItemRecord : oleItemRecords) {
230                 instanceOrItemIdentifiersList.add(oleItemRecord.getItemId());
231             }
232         }
233         instanceOrItemIdentifiersList.add(instanceIdentifier);
234         return checkInstancesOrItemsExistsInOLE(instanceOrItemIdentifiersList);
235     }
236 
237     protected boolean checkInstancesOrItemsExistsInOLE(List<String> uuidsList) {
238         String uuidsNotInOle = null;
239         String serviceURL = ConfigContext.getCurrentContextConfig().getProperty("uuidCheckServiceURL");
240         OleWebServiceProvider oleWebServiceProvider = new OleWebServiceProviderImpl();
241         OleUuidCheckWebService oleUuidCheckWebService = (OleUuidCheckWebService) oleWebServiceProvider
242                 .getService("org.kuali.ole.docstore.service.OleUuidCheckWebService", "oleUuidCheckWebService",
243                         serviceURL);
244         StringBuilder uuidsSB = new StringBuilder();
245         for (String uuid : uuidsList) {
246             //uuidsSB.append(requestDocument.getId()).append(",");
247             uuidsSB.append(uuid).append(",");
248         }
249         uuidsNotInOle = oleUuidCheckWebService.checkUuidExsistence(uuidsSB.substring(0, uuidsSB.length() - 1));
250         String[] uuids = StringUtils.split(uuidsNotInOle, ",");
251         if (uuids.length == uuidsList.size()) {
252             return false;
253         } else {
254             return true;
255         }
256     }
257 }