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
33
34
35
36
37
38 public abstract class RdbmsAbstarctDocumentManager extends AbstractDocumentManager {
39
40 private BusinessObjectService businessObjectService;
41
42 @Override
43 public List<ResponseDocument> ingest(List<RequestDocument> requestDocuments, Object object) throws OleDocStoreException {
44
45 List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
46 for (RequestDocument requestDocument : requestDocuments) {
47 ResponseDocument responseDocument = new ResponseDocument();
48 this.ingest(requestDocument, object, responseDocument);
49 responseDocuments.add(responseDocument);
50 }
51 return responseDocuments;
52 }
53
54 public BusinessObjectService getBusinessObjectService() {
55 if (null == businessObjectService) {
56 businessObjectService = KRADServiceLocator.getBusinessObjectService();
57 }
58 return businessObjectService;
59 }
60
61 @Override
62 public ResponseDocument ingest(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
63
64 BusinessObjectService businessObjectService = (BusinessObjectService) object;
65 storeDocument(requestDocument, object, respDoc);
66 storeLinkDocuments(requestDocument, object, respDoc);
67
68 return respDoc;
69 }
70
71
72 public void storeLinkDocuments(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
73
74 List<ResponseDocument> linkedResponseDocumentList = new ArrayList<ResponseDocument>();
75 if (respDoc.getLinkedDocuments() != null && respDoc.getLinkedDocuments().size() > 0) {
76 linkedResponseDocumentList.addAll(respDoc.getLinkedDocuments());
77 }
78 respDoc.setLinkedDocuments(linkedResponseDocumentList);
79 for (RequestDocument linkedRequestDocument : requestDocument.getLinkedRequestDocuments()) {
80 ResponseDocument linkedResponseDocument = new ResponseDocument();
81 linkedResponseDocument.setUuid(respDoc.getUuid());
82 linkedResponseDocumentList.add(linkedResponseDocument);
83 DocumentManager documentManager = BeanLocator.getDocstoreFactory()
84 .getDocumentManager(linkedRequestDocument.getCategory(), linkedRequestDocument.getType(), linkedRequestDocument.getFormat());
85 documentManager.addResourceId(linkedRequestDocument, respDoc);
86 String createdBy = requestDocument.getAdditionalAttributes().getAttribute(AdditionalAttributes.CREATED_BY);
87 String createdDate = requestDocument.getAdditionalAttributes().getAttribute(AdditionalAttributes.DATE_ENTERED);
88 if (linkedRequestDocument != null && linkedRequestDocument.getAdditionalAttributes() != null) {
89 linkedRequestDocument.getAdditionalAttributes().setAttribute(AdditionalAttributes.HOLDINGS_CREATED_BY, createdBy);
90 linkedRequestDocument.getAdditionalAttributes().setAttribute(AdditionalAttributes.HOLDINGS_DATE_ENTERED, createdDate);
91 } else {
92 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
93 additionalAttributes.setAttribute(AdditionalAttributes.HOLDINGS_CREATED_BY, createdBy);
94 additionalAttributes.setAttribute(AdditionalAttributes.HOLDINGS_DATE_ENTERED, createdDate);
95 linkedRequestDocument.setAdditionalAttributes(additionalAttributes);
96 }
97
98
99
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
170
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;
180 }
181
182 @Override
183 public void validateInput(RequestDocument requestDocument, Object object, List<String> valuesList) throws OleDocStoreException {
184
185 }
186
187 @Override
188 public void bulkIngest(BulkProcessRequest bulkProcessRequest, List<RequestDocument> requestDocuments) throws OleDocStoreException {
189
190 }
191
192 @Override
193 public void index(List<RequestDocument> requestDocuments, boolean commit) throws OleDocStoreException {
194
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;
205 }
206
207 @Override
208 public List<ResponseDocument> deleteVerify(List<RequestDocument> requestDocument, Object object) {
209 return null;
210 }
211
212 @Override
213 public ResponseDocument deleteVerify(RequestDocument requestDocument, Object object) throws Exception {
214 return null;
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
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 }