1 package org.kuali.ole.docstore.transaction;
2
3 import org.apache.commons.lang.time.StopWatch;
4 import org.kuali.ole.docstore.OleDocStoreException;
5 import org.kuali.ole.docstore.document.DocumentManager;
6 import org.kuali.ole.docstore.document.jcr.JcrWorkInstanceDocumentManager;
7 import org.kuali.ole.docstore.document.rdbms.RdbmsWorkInstanceDocumentManager;
8 import org.kuali.ole.docstore.factory.DocstoreFactory;
9 import org.kuali.ole.docstore.indexer.solr.IndexerService;
10 import org.kuali.ole.docstore.indexer.solr.WorkBibDocumentIndexer;
11 import org.kuali.ole.docstore.indexer.solr.WorkInstanceDocumentIndexer;
12 import org.kuali.ole.docstore.indexer.solr.WorkItemDocumentIndexer;
13 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
14 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
15 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseStatus;
16 import org.kuali.ole.docstore.process.ProcessParameters;
17 import org.kuali.ole.docstore.process.batch.BulkProcessRequest;
18 import org.kuali.ole.docstore.service.BeanLocator;
19 import org.kuali.ole.docstore.utility.BatchIngestStatistics;
20 import org.kuali.ole.docstore.utility.BulkIngestStatistics;
21 import org.kuali.rice.krad.service.BusinessObjectService;
22 import org.kuali.rice.krad.service.KRADServiceLocator;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.util.ArrayList;
27 import java.util.List;
28
29
30
31
32
33
34
35
36 @Deprecated
37 public class RdbmsTransactionManager extends AbstractTransactionManager {
38
39 private static final Logger logger = LoggerFactory.getLogger(RdbmsTransactionManager.class);
40 private BusinessObjectService businessObjectService;
41 private IndexerService indexerService;
42 private TransactionState transactionState = TransactionState.IDLE;
43 private List<RequestDocument> newStateRequestDocuments = null;
44 private List<RequestDocument> oldStateRequestDocuments = new ArrayList<RequestDocument>();
45
46 @Override
47 public void startTransaction(String user, String operation) throws Exception {
48 try {
49 if (null == businessObjectService) {
50 businessObjectService = KRADServiceLocator.getBusinessObjectService();
51 }
52
53
54
55 } catch (Exception e) {
56 logger.error(e.getMessage() , e );
57
58 }
59 }
60
61
62 @Override
63 public List<ResponseDocument> ingest(List<RequestDocument> requestDocuments) throws Exception {
64
65 DocstoreFactory docstoreFactory = BeanLocator.getDocstoreFactory();
66
67 List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
68 List<String> valuesList = new ArrayList<String>();
69 for (RequestDocument requestDocument : requestDocuments) {
70
71 DocumentManager documentManager = docstoreFactory.getDocumentManager(requestDocument.getCategory(), requestDocument.getType(), requestDocument.getFormat());
72 ResponseDocument respDoc = new ResponseDocument();
73 try {
74 documentManager.validateInput(requestDocument, businessObjectService, valuesList);
75 } catch (OleDocStoreException e) {
76 respDoc.setStatus(ResponseStatus.INVALID_DATA.toString());
77 logger.debug("validationMessage-->" + e.getMessage(), e);
78 throw new OleDocStoreException(e.getMessage(), e);
79 }
80
81 documentManager.ingest(requestDocument, businessObjectService, respDoc);
82 responseDocuments.add(respDoc);
83 }
84
85 for (RequestDocument requestDocument : requestDocuments) {
86 String result = getIndexerService(requestDocument).indexDocument(requestDocument, false);
87 if (!result.startsWith("success")) {
88 throw new OleDocStoreException(result);
89 }
90 }
91
92 return responseDocuments;
93 }
94
95 @Override
96 public List<ResponseDocument> checkIn(List<RequestDocument> requestDocuments) throws Exception {
97 DocstoreFactory docstoreFactory = BeanLocator.getDocstoreFactory();
98
99 List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
100 List<String> valuesList = new ArrayList<String>();
101 for (RequestDocument requestDocument : requestDocuments) {
102
103 DocumentManager documentManager = BeanLocator.getDocstoreFactory().getDocumentManager(requestDocument.getCategory(), requestDocument.getType(), requestDocument.getFormat());
104 if (requestDocument.getId() != null || requestDocument.getId().trim().length() > 0) {
105 requestDocument.setUuid(requestDocument.getId());
106 }
107
108
109
110 ResponseDocument respDoc = new ResponseDocument();
111
112 try {
113 documentManager.validateInput(requestDocument, businessObjectService, valuesList);
114 } catch (OleDocStoreException e) {
115 respDoc.setStatus(ResponseStatus.INVALID_DATA.toString());
116 logger.debug("validationMessage-->" + e.getMessage(), e);
117 throw new OleDocStoreException(e.getMessage());
118 }
119
120
121
122
123 documentManager.checkin(requestDocument, businessObjectService, respDoc);
124 responseDocuments.add(respDoc);
125 }
126
127 for (RequestDocument requestDocument : requestDocuments) {
128 String result = getIndexerService(requestDocument).indexDocument(requestDocument, false);
129 if (!result.startsWith("success")) {
130 throw new OleDocStoreException(result);
131 }
132 }
133
134 return responseDocuments;
135 }
136
137 @Override
138 public List<ResponseDocument> checkOut(List<RequestDocument> requestDocuments, Object object) throws Exception {
139 DocstoreFactory docstoreFactory = BeanLocator.getDocstoreFactory();
140
141 List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
142 List<String> valuesList = new ArrayList<String>();
143 for (RequestDocument requestDocument : requestDocuments) {
144
145 DocumentManager documentManager = docstoreFactory.getDocumentManager(requestDocument.getCategory(), requestDocument.getType(), requestDocument.getFormat());
146 responseDocuments.add(documentManager.checkout(requestDocument, businessObjectService));
147 }
148 return responseDocuments;
149 }
150
151 @Override
152 public List<ResponseDocument> bind(List<RequestDocument> requestDocuments, String operation) throws Exception {
153 DocumentManager documentManager = null;
154 List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
155 for (RequestDocument requestDocument : requestDocuments) {
156 documentManager = BeanLocator.getDocstoreFactory()
157 .getDocumentManager(requestDocument.getCategory(), requestDocument.getType(),
158 requestDocument.getFormat());
159 responseDocuments.add(documentManager.bind(requestDocument, businessObjectService, operation));
160 }
161
162
163 for (RequestDocument requestDocument : requestDocuments) {
164 String result = getIndexerService(requestDocument).bind(requestDocument);
165 if (!result.startsWith("success")) {
166 throw new OleDocStoreException(result);
167 }
168
169 }
170 return responseDocuments;
171 }
172
173 @Override
174 public List<ResponseDocument> unbind(List<RequestDocument> requestDocuments, String operation) throws Exception {
175 return null;
176 }
177
178 @Override
179 public List<ResponseDocument> delete(List<RequestDocument> requestDocuments) throws Exception {
180 DocumentManager documentManager = null;
181
182 List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
183 for (RequestDocument requestDocument : requestDocuments) {
184
185 documentManager = BeanLocator.getDocstoreFactory()
186 .getDocumentManager(requestDocument.getCategory(), requestDocument.getType(),
187 requestDocument.getFormat());
188
189 responseDocuments.add(documentManager.delete(requestDocument, businessObjectService));
190 }
191
192 for (int index = 0; index < requestDocuments.size(); index++) {
193 ResponseDocument responseDocument = responseDocuments.get(index);
194 if ("success".equals(responseDocument.getStatus())) {
195 RequestDocument requestDocument = requestDocuments.get(index);
196 String result = getIndexerService(requestDocument).delete(requestDocument);
197 if (!result.startsWith("success")) {
198 throw new OleDocStoreException(result);
199 }
200 }
201 }
202 return responseDocuments;
203 }
204
205 @Override
206 public List<ResponseDocument> deleteVerify(List<RequestDocument> requestDocuments) throws Exception {
207 DocumentManager documentManager = null;
208 List<ResponseDocument> responseDocuments = new ArrayList<ResponseDocument>();
209 for (RequestDocument requestDocument : requestDocuments) {
210 documentManager = BeanLocator.getDocstoreFactory().getDocumentManager(requestDocument.getCategory(), requestDocument.getType(), requestDocument.getFormat());
211 responseDocuments.add(documentManager.deleteVerify(requestDocument, businessObjectService));
212 }
213 return responseDocuments;
214 }
215
216 @Override
217 public void transferInstances(List<RequestDocument> requestDocuments) throws Exception {
218 logger.debug("TransactionManager transferInstancessssssssssssssssss");
219 RdbmsWorkInstanceDocumentManager.getInstance().transferInstances(requestDocuments, businessObjectService);
220 WorkInstanceDocumentIndexer.getInstance().transferInstances(requestDocuments);
221 }
222
223 @Override
224 public void transferItems(List<RequestDocument> requestDocuments) throws Exception {
225 logger.debug("TransactionManager transferItemsssssssssss");
226 RdbmsWorkInstanceDocumentManager.getInstance().transferItems(requestDocuments, businessObjectService);
227 WorkItemDocumentIndexer.getInstance().transferItems(requestDocuments);
228 }
229
230 @Override
231 public void batchIngest(BulkProcessRequest bulkProcessRequest, List<RequestDocument> requestDocuments)
232 throws Exception {
233 BulkIngestStatistics bulkIngestStatistics = bulkProcessRequest.getBulkIngestStatistics();
234 BatchIngestStatistics batchStatistics = bulkIngestStatistics.getCurrentBatch();
235 long commitSize = ProcessParameters.BULK_INGEST_COMMIT_SIZE;
236 newStateRequestDocuments = new ArrayList<RequestDocument>();
237
238 newStateRequestDocuments.addAll(requestDocuments);
239 DocumentManager documentManager = BeanLocator.getDocstoreFactory()
240 .getDocumentManager(requestDocuments.get(0).getCategory(),
241 requestDocuments.get(0).getType(),
242 requestDocuments.get(0).getFormat());
243
244
245 StopWatch createNodesTimer = new StopWatch();
246 createNodesTimer.start();
247 documentManager.ingest(requestDocuments, businessObjectService);
248 createNodesTimer.stop();
249 batchStatistics.setTimeToCreateNodesInJcr(createNodesTimer.getTime());
250 batchStatistics.setCommitSize(commitSize);
251
252 StopWatch indexSolrDocsTime = new StopWatch();
253 indexSolrDocsTime.start();
254
255
256 String result = getIndexerService(requestDocuments.get(0)).bulkIndexDocuments(requestDocuments, false);
257
258
259
260
261
262
263 indexSolrDocsTime.stop();
264 batchStatistics.setTimeToIndexSolrInputDocs(indexSolrDocsTime.getTime());
265
266 if (!result.startsWith("success")) {
267 throw new OleDocStoreException(result);
268 }
269 }
270
271 @Override
272 public void startSession(String user, String operation) throws Exception {
273
274 }
275
276 @Override
277 public void commit() throws Exception {
278 commit(null);
279 }
280
281 @Override
282 public void closeSession() {
283
284 }
285
286 @Override
287 public void abort() {
288
289 }
290
291 public void commit(BatchIngestStatistics batchIngestStatistics) throws OleDocStoreException {
292 logger.info("Commit: Saving changes to index...");
293 StopWatch solrCommitTimer = new StopWatch();
294 try {
295 if (null != batchIngestStatistics) {
296 solrCommitTimer = new StopWatch();
297 solrCommitTimer.start();
298 }
299 WorkBibDocumentIndexer.getInstance().commit();
300 if (null != batchIngestStatistics) {
301 solrCommitTimer.stop();
302 batchIngestStatistics.setTimeToSolrCommit(solrCommitTimer.getTime());
303 }
304 } catch (Exception e) {
305 transactionState = TransactionState.FAILED;
306 logger.error("Exception during commit: Unable to save changes to index. :", e);
307
308 throw new OleDocStoreException("Commit failed.", e);
309 }
310
311 logger.info("Commit: Saving changes to docstore...");
312 StopWatch sessionSaveTimer = null;
313 try {
314 if (null != batchIngestStatistics) {
315 sessionSaveTimer = new StopWatch();
316 sessionSaveTimer.start();
317 }
318 if (null != batchIngestStatistics) {
319 sessionSaveTimer.stop();
320 batchIngestStatistics.setTimeToSaveJcrSession(sessionSaveTimer.getTime());
321 }
322 } catch (Exception e) {
323 transactionState = TransactionState.FAILED;
324 logger.error("Exception during commit. Unable to save changes to docstore. :", e);
325 logger.info("Commit: Reverting changes to index...");
326
327 try {
328 rollBackDataInIndexer(oldStateRequestDocuments);
329
330 } catch (Exception ex) {
331 logger.error("error while performing roll back in Indexer" , ex);
332 }
333 throw new OleDocStoreException("Commit failed.", e);
334 }
335
336
337
338 transactionState = TransactionState.COMMITTED;
339 newStateRequestDocuments = null;
340 oldStateRequestDocuments = null;
341 }
342
343 private void rollBackDataInIndexer(List<RequestDocument> oldStateRequestDocuments) throws OleDocStoreException {
344 if (oldStateRequestDocuments != null && oldStateRequestDocuments.size() > 0) {
345 for (RequestDocument requestDocument : oldStateRequestDocuments) {
346 String result = getIndexerService(requestDocument).indexDocument(requestDocument, false);
347 if (!result.startsWith("success")) {
348 throw new OleDocStoreException(result);
349 }
350 }
351 }
352 }
353
354 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
355 this.businessObjectService = businessObjectService;
356 }
357 }