View Javadoc
1   package org.kuali.ole.docstore.document.jcr;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.commons.lang.time.StopWatch;
5   import org.kuali.ole.RepositoryManager;
6   import org.kuali.ole.docstore.OleDocStoreException;
7   import org.kuali.ole.docstore.common.document.content.instance.InstanceCollection;
8   import org.kuali.ole.docstore.common.document.content.instance.Item;
9   import org.kuali.ole.docstore.common.document.content.instance.Items;
10  import org.kuali.ole.docstore.common.document.content.instance.xstream.InstanceOlemlRecordProcessor;
11  import org.kuali.ole.docstore.document.AbstractDocumentManager;
12  import org.kuali.ole.docstore.document.DocumentManager;
13  import org.kuali.ole.docstore.model.enums.DocCategory;
14  import org.kuali.ole.docstore.model.enums.DocFormat;
15  import org.kuali.ole.docstore.model.enums.DocType;
16  import org.kuali.ole.docstore.model.xmlpojo.ingest.*;
17  import org.kuali.ole.docstore.common.document.content.instance.Instance;
18  import org.kuali.ole.docstore.process.ProcessParameters;
19  import org.kuali.ole.docstore.process.batch.BulkProcessRequest;
20  import org.kuali.ole.docstore.repository.CustomNodeManager;
21  import org.kuali.ole.docstore.repository.NodeManager;
22  import org.kuali.ole.docstore.repository.WorkInstanceNodeManager;
23  import org.kuali.ole.docstore.service.BeanLocator;
24  import org.kuali.ole.docstore.service.OleUuidCheckWebService;
25  import org.kuali.ole.docstore.service.OleWebServiceProvider;
26  import org.kuali.ole.docstore.service.ServiceLocator;
27  import org.kuali.ole.docstore.service.impl.OleWebServiceProviderImpl;
28  import org.kuali.ole.docstore.utility.BatchIngestStatistics;
29  import org.kuali.ole.docstore.utility.BulkIngestStatistics;
30  import org.kuali.ole.pojo.OleException;
31  import org.kuali.ole.repository.NodeHandler;
32  import org.kuali.rice.core.api.config.property.ConfigContext;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import javax.jcr.Binary;
37  import javax.jcr.Node;
38  import javax.jcr.RepositoryException;
39  import javax.jcr.Session;
40  import javax.jcr.version.VersionHistory;
41  import javax.jcr.version.VersionIterator;
42  import javax.jcr.version.VersionManager;
43  import java.io.ByteArrayInputStream;
44  import java.io.FileNotFoundException;
45  import java.util.*;
46  
47  import static org.kuali.ole.docstore.process.ProcessParameters.FILE;
48  
49  /**
50   * Provides the common and basic implementation for the DocumentManager interface.
51   *
52   * @author tirumalesh.b
53   * @version %I%, %G%
54   *          Date: 28/8/12 Time: 12:22 PM
55   */
56  @Deprecated
57  public abstract class JcrAbstractDocumentManager
58          extends AbstractDocumentManager {
59      private Logger logger = LoggerFactory.getLogger(this.getClass());
60  
61      public static DocCategory docCategory;
62      public static DocType docType;
63      public static DocFormat docFormat;
64      //    private static final String DELETE_WITH_LINKED_DOCS = "deleteWithLinkedDocs";
65      private static final String BIBLIOGRAPHIC = "bibliographic";
66      private static final String INSTANCE_IDENTIFIER = "instanceIdentifier";
67      private static final String SUCCESS = "Success";
68      private static final String FAILURE = "Failure";
69  
70      protected RepositoryManager repositoryManager;
71      protected NodeManager nodeManager;
72  
73      public JcrAbstractDocumentManager() {
74          try {
75              this.repositoryManager = RepositoryManager.getRepositoryManager();
76          } catch (OleException oe) {
77              //throw new OleDocStoreException(oe);
78              // TODO: log the exception
79          }
80          this.nodeManager = CustomNodeManager.getInstance();
81      }
82  
83      /**
84       * @inheritDoc
85       */
86      @Override
87      public List<ResponseDocument> ingest(List<RequestDocument> requestDocuments, Object object)
88              throws OleDocStoreException {
89          Session session = (Session) object;
90          if (null == session) {
91              throw new OleDocStoreException("Invalid session.");
92          }
93          ResponseDocument respDoc = new ResponseDocument();
94          // Store data in docstore, but do not commit.
95          for (RequestDocument requestDocument : requestDocuments) {
96              ingest(requestDocument, session, respDoc);
97          }
98  
99          List<ResponseDocument> responseDocuments = buildResponseDocuments(
100                 requestDocuments); //buildResponseDocuments(requestDocuments);
101         return responseDocuments;
102     }
103 
104     @Override
105     public ResponseDocument ingest(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
106         Session session = (Session) object;
107         if (null == session) {
108             throw new OleDocStoreException("Invalid session.");
109         }
110         // Store the document of the requestDocument.
111         Node fileNode = storeDocument(requestDocument, session, respDoc);
112 
113         // Store the linked documents of the requestDocument.
114         storeLinkedDocuments(requestDocument, fileNode, session, respDoc);
115         return respDoc;
116     }
117 
118 
119     @Override
120     public List<ResponseDocument> checkout(List<RequestDocument> requestDocuments, Object object) throws OleDocStoreException {
121         // TODO: implement this.
122         return null;
123     }
124 
125     @Override
126     public ResponseDocument checkout(RequestDocument requestDocument, Object object) throws OleDocStoreException {
127         Session session = (Session) object;
128         String content = "";
129         String uuid = requestDocument.getUuid();
130         if (session == null) {
131             throw new OleDocStoreException("Invalid session.");
132         }
133         try {
134             Node nodeByUUID = nodeManager.getNodeByUUID(session, uuid);
135             if (nodeByUUID != null) {
136                 content = checkOutContent(nodeByUUID, requestDocument.getFormat(), requestDocument.getUser());
137             }
138         } catch (Exception e) {
139             logger.error("Error in checking out the file. Please refer to the logs for more details!" + e.getMessage(),
140                     e);
141             throw new OleDocStoreException(e);
142         }
143 
144         ResponseDocument respDoc = new ResponseDocument();
145         Content contentObj = new Content();
146         contentObj.setContent(content);
147         respDoc.setContent(contentObj);
148         String category = requestDocument.getCategory();
149         String type = requestDocument.getType();
150         AdditionalAttributes additionalAttributes = new AdditionalAttributes();
151         Map<String, String> map = additionalAttributes.getAttributeMap();
152         Node node = null;
153         try {
154             node = session.getNodeByIdentifier(requestDocument.getUuid());
155         } catch (RepositoryException e) {
156             logger.info("Failed to get Node:" + e.getMessage(), e);
157         }
158 
159         if (node != null) {
160             try {
161 
162                 Collection<String> attributeKeyCollection = additionalAttributes.getAdditionalAttributeKeyCollection();
163                 for (String key : attributeKeyCollection) {
164                     if (node.hasProperty(key)) {
165                         additionalAttributes.setAttribute(key, node.getProperty(key).getString());
166                     }
167                 }
168 
169                 if (additionalAttributes != null && category.equals(DocCategory.WORK.getDescription()) && type
170                         .equals(DocType.BIB.getDescription())) {
171                     respDoc.setAdditionalAttributes(additionalAttributes);
172                 }
173             } catch (RepositoryException e) {
174                 logger.info("Failed to get node property:" + e.getMessage(), e);
175             }
176         }
177 
178         return respDoc;
179     }
180 
181     protected String checkOutContent(Node nodeByUUID, String format, String user)
182             throws RepositoryException, OleDocStoreException, FileNotFoundException {
183         String content = nodeManager.getData(nodeByUUID);
184         return content;
185     }
186 
187     @Override
188     public List<ResponseDocument> checkin(List<RequestDocument> requestDocuments, Object object) throws OleDocStoreException {
189         // TODO: implement this.
190         return null;
191     }
192 
193     @Override
194     public ResponseDocument checkin(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
195         Session session = (Session) object;
196         String checkInFail = "Check in failed. ";
197         if (requestDocument.getId() != null || requestDocument.getId().trim().length() > 0) {
198             requestDocument.setUuid(requestDocument.getId());
199         }
200 
201         //adding new record to docstore but do not commit
202         addNewRecordsToDocStore(requestDocument, session); // item for instance
203 
204         try {
205             // Store data in docstore and update version if enables .
206             String version = updateDocstore(requestDocument, session);
207         } catch (Exception e) {
208             logger.info(
209                     "Document was updated in indexer but not in docStore, trying to rollback the changes from indexer",
210                     e);
211             /* try {
212                 session.refresh(false);
213                 if (session == null) {
214                     session = repositoryManager.getSession();
215                 }
216                 Node nodeByUUID = nodeManager.getNodeByUUID(session, requestDocument.getId());
217                 RequestDocument prevRequestDoc = new RequestDocument();
218                 prevRequestDoc = requestDocument;
219                 String prevContent = nodeByUUID.getNode("jcr:content").getProperty("jcr:data").getValue().getString();
220                 prevRequestDoc.getContent().setContent(prevContent);
221                 ServiceLocator.getIndexerService().indexDocument(prevRequestDoc);
222             }
223             catch (Exception ex) {
224                 String failMsg = checkInFail + "Unable to Roll back the changes in indexer.  Record UUID "
225                                  + requestDocument.getId() + "got updated in indexer, but not in docstore ";
226                 logger.info(failMsg, ex);
227                 throw new OleDocStoreException(failMsg, ex);
228             }*/
229             logger.info(checkInFail, e);
230             throw new OleDocStoreException(checkInFail, e);
231         }
232         //return buildResponseDocument(requestDocument);
233         buildResponseDocument(requestDocument, session, respDoc);
234         //return buildResponseDocument(requestDocument, session);
235         return respDoc;
236     }
237 
238     protected void addNewRecordsToDocStore(RequestDocument requestDocument, Session session)
239             throws OleDocStoreException {
240     }
241 
242 
243     public String updateDocstore(RequestDocument reqDoc, Session session)
244             throws OleDocStoreException, RepositoryException, FileNotFoundException {
245 
246         Node nodeByUUID = session.getNodeByIdentifier(reqDoc.getUuid());
247         modifyContent(reqDoc, session, nodeByUUID);
248         byte[] documentBytes = convertContentToBytes(reqDoc);
249         modifyAdditionalAttributes(reqDoc, nodeByUUID);
250         updateContentToNode(reqDoc, session, documentBytes, nodeByUUID);
251         //Get the updated version from docstore.
252         String currentVersion = updateVersion(session, nodeByUUID);
253         return currentVersion;
254     }
255 
256     protected void modifyContent(RequestDocument reqDoc, Session session, Node node) throws RepositoryException, FileNotFoundException, OleDocStoreException {
257     }
258 
259     protected String updateVersion(Session session, Node nodeByUUID) throws RepositoryException, OleDocStoreException {
260         session.save();
261         String currentVersion = null;
262         if (isVersioningEnabled()) {
263             VersionManager versionManager = getVersionManager(session);
264             versionManager.checkpoint(nodeByUUID.getPath());
265             VersionHistory versionHistory = versionManager.getVersionHistory(nodeByUUID.getPath());
266             VersionIterator allVersions = versionHistory.getAllVersions();
267             while (allVersions.hasNext()) {
268                 currentVersion = allVersions.nextVersion().getName();
269             }
270             logger.info("Version updated for UUID:" + nodeByUUID.getIdentifier() + "  ====  version:" + currentVersion);
271         }
272         return currentVersion;
273     }
274 
275     protected void updateContentToNode(RequestDocument reqDoc, Session session, byte[] documentBytes, Node nodeByUUID)
276             throws RepositoryException, OleDocStoreException {
277         try {
278             Binary binary = null;
279             if (documentBytes != null) {
280                 binary = session.getValueFactory().createBinary(new ByteArrayInputStream(documentBytes));
281                 nodeByUUID.getNode("jcr:content").setProperty("jcr:data", binary);
282             }
283 
284             AdditionalAttributes additionalAttributes = reqDoc.getAdditionalAttributes();
285             if (additionalAttributes != null) {
286                 Collection<String> attributeNames = additionalAttributes.getAttributeNames();
287                 if (attributeNames != null && attributeNames.size() > 0) {
288                     for (Iterator<String> iterator = attributeNames.iterator(); iterator.hasNext(); ) {
289                         String attributeName = iterator.next();
290                         String attributeValue = additionalAttributes.getAttribute(attributeName);
291                         nodeByUUID.setProperty(attributeName, attributeValue);
292                     }
293                 }
294 
295             }
296             Calendar lastModified = Calendar.getInstance();
297             lastModified.setTimeInMillis(lastModified.getTimeInMillis());
298         } catch (Exception e) {
299             logger.info(e.getMessage());
300             throw new OleDocStoreException(e.getMessage(), e);
301         }
302     }
303 
304     protected byte[] convertContentToBytes(RequestDocument reqDoc) throws OleDocStoreException {
305         String charset = "UTF-8";
306         byte[] documentBytes = null;
307         try {
308             if (reqDoc.getContent().getContent() != null) {
309                 documentBytes = reqDoc.getContent().getContent().getBytes(charset);
310             }
311         } catch (Exception e) {
312             logger.info("Failed to convert input string to byte[] with charset " + charset, e);
313             throw new OleDocStoreException(e.getMessage());
314         }
315         return documentBytes;
316     }
317 
318     protected String updateIndex(RequestDocument requestDocument) throws OleDocStoreException {
319         String result = "failure";
320         result = updateRecord(requestDocument);
321         if (!result.startsWith("success")) {
322             throw new OleDocStoreException("Check in failed. " + result);
323         }
324         return result;
325     }
326 
327     protected String updateRecord(RequestDocument requestDocument) {
328         String result = ServiceLocator.getIndexerService().indexDocument(requestDocument);
329         return result;
330     }
331 
332 
333     @Override
334     public List<ResponseDocument> delete(List<RequestDocument> requestDocuments, Object object) throws OleDocStoreException {
335         // TODO: implement this.
336         return null;
337     }
338 
339     @Override
340     public ResponseDocument delete(RequestDocument requestDocument, Object object) throws Exception {
341         Session session = (Session) object;
342         ResponseDocument rs = new ResponseDocument();
343         try {
344             rs = deleteDoc(requestDocument, session);
345         } catch (Exception e) {
346             throw new OleDocStoreException(e.getMessage(), e);
347         }
348         return rs;
349     }
350 
351     /**
352      * @inheritDoc
353      */
354     @Override
355     public void bulkIngest(BulkProcessRequest bulkProcessRequest, List<RequestDocument> requestDocuments)
356             throws OleDocStoreException {
357         Session session = bulkProcessRequest.getSession();
358         if (session == null) {
359             try {
360                 session = RepositoryManager.getRepositoryManager().getSession(bulkProcessRequest.getUser(),
361                         bulkProcessRequest.getOperation()
362                                 .toString());
363             } catch (Exception e) {
364                 throw new OleDocStoreException(e);
365             }
366             bulkProcessRequest.setSession(session);
367         }
368         if (bulkProcessRequest.getPreviousBatchDocuments() == null) {
369             List<RequestDocument> newList = new ArrayList<RequestDocument>();
370             bulkProcessRequest.setPreviousBatchDocuments(newList);
371         }
372         bulkProcessRequest.getPreviousBatchDocuments().addAll(requestDocuments);
373 
374         batchIngest(bulkProcessRequest, requestDocuments, session);
375 
376         if (session != null) {
377             try {
378                 if (bulkProcessRequest.getBulkIngestStatistics().isLastBatch()) {
379                     RepositoryManager.getRepositoryManager().logout(session);
380                     session = null;
381                 }
382             } catch (OleException e) {
383             }
384         }
385     }
386 
387     private ResponseDocument deleteDoc(RequestDocument document, Session session1) throws Exception {
388         //List<String> uuidsList = new ArrayList<String>();
389         String status = null;
390         String category = null;
391         Response response = new Response();
392         List<String> respositoryUuidList = new ArrayList<String>();
393         category = document.getCategory();
394         String uuid = document.getUuid();
395         String operation = document.getOperation();
396         respositoryUuidList = getLinkedDocsFromRepository(uuid, session1, respositoryUuidList, operation);
397         logger.info("respository UuidList size-->" + respositoryUuidList);
398         deleteFromRepository(respositoryUuidList, session1);
399         String statusValue = ServiceLocator.getIndexerService().deleteDocuments(category, respositoryUuidList);
400         return prepareResponseDocument(document);
401 
402     }
403 
404     private ResponseDocument prepareResponseDocument(RequestDocument requestDocument) {
405         ResponseDocument responseDocument = new ResponseDocument();
406         responseDocument.setStatus("success");
407         responseDocument.setStatusMessage("Success");
408         responseDocument.setUuid(requestDocument.getUuid());
409         return responseDocument;
410     }
411 
412     private List<String> getLinkedDocsFromRepository(String uuid, Session session, List<String> respositoryUuidList,
413                                                      String operation) throws Exception {
414         if (operation.equalsIgnoreCase(Request.Operation.deleteWithLinkedDocs.toString())) {
415             Node node = session.getNodeByIdentifier(uuid);
416             if (node.getPath().contains(BIBLIOGRAPHIC)) {
417                 String instanceId = node.getProperty(INSTANCE_IDENTIFIER).getString();
418                 respositoryUuidList.add(instanceId);
419             }
420         }
421         respositoryUuidList.add(uuid);
422         return respositoryUuidList;
423     }
424 
425     protected void deleteFromRepository(List<String> uuidsList, Session session) throws Exception {
426         if (uuidsList != null && uuidsList.size() > 0) {
427             for (int i = 0; i < uuidsList.size(); i++) {
428                 Node deleteNode = new NodeHandler().getNodeByUUID(session, uuidsList.get(i));
429                 if (deleteNode != null) {
430                     deleteNode.remove();
431                 }
432             }
433         }
434     }
435 
436     public List<String> batchIngest(BulkProcessRequest bulkProcessRequest, List<RequestDocument> requestDocuments,
437                                     Session session) {
438         //RequestDocument requestDocument = request.getRequestDocuments().get(0);
439         //DocumentManager documentManager = BeanLocator.getDocumentManagerFactory().getDocumentManager(requestDocument);
440         BulkIngestStatistics bulkIngestStatistics = bulkProcessRequest.getBulkIngestStatistics();
441         BatchIngestStatistics batchStatistics = bulkIngestStatistics.getCurrentBatch();
442         long commitSize = ProcessParameters.BULK_INGEST_COMMIT_SIZE;
443         logger.debug("commitSize = " + commitSize);
444         logger.debug("bulkIngestNIndex(" + requestDocuments.size() + ") START");
445         logger.debug("BULK_INGEST_IS_LINKING_ENABLED=" + ProcessParameters.BULK_INGEST_IS_LINKING_ENABLED);
446         //Session session = null;
447         List<String> docUUIDs = new ArrayList<String>();
448         StopWatch ingestTimer = new StopWatch();
449         StopWatch indexTimer = new StopWatch();
450         StopWatch totalTimer = new StopWatch();
451         StopWatch createNodesTimer = new StopWatch();
452         StopWatch sessionSaveTimer = new StopWatch();
453         StopWatch solrOptimizeTimer = new StopWatch();
454         long recCount = requestDocuments.size();
455         boolean isCommit = false;
456         totalTimer.start();
457         try {
458             ingestTimer.start();
459             createNodesTimer.start();
460             //session = RepositoryManager.getRepositoryManager().getSession(request.getUser(), request.getOperation());
461             //List<RequestDocument> reqDocs = requestDocuments;
462             //docUUIDs.addAll(documentIngester.ingestRequestDocumentsForBulk(requestDocuments, session));
463             //docUUIDs.addAll(documentIngester.ingestRequestDocumentsForBulkUsingBTreeMgr(reqDocs, session));
464             store(requestDocuments, session);
465             createNodesTimer.stop();
466             try {
467                 ingestTimer.suspend();
468                 indexTimer.start();
469             } catch (Exception e2) {
470                 logger.info("Exception :" + e2);
471             }
472             bulkIngestStatistics.setCommitRecCount(bulkIngestStatistics.getCommitRecCount() + recCount);
473             if (bulkIngestStatistics.getCommitRecCount() == commitSize || bulkIngestStatistics.isLastBatch()) {
474                 isCommit = true;
475             }
476             //documentIndexer.indexDocumentsForBulk(requestDocuments, isCommit);
477             index(requestDocuments, isCommit);
478             try {
479                 indexTimer.suspend();
480                 ingestTimer.resume();
481             } catch (Exception e2) {
482                 logger.info("Exception :" + e2);
483             }
484             if (isCommit) {
485                 sessionSaveTimer.start();
486                 logger.info("Bulk ingest: Repository commit started. Number of records being committed : "
487                         + bulkIngestStatistics.getCommitRecCount());
488                 session.save();
489                 bulkIngestStatistics.setCommitRecCount(0);
490                 bulkProcessRequest.setPreviousBatchDocuments(null);
491                 sessionSaveTimer.stop();
492             }
493 
494             try {
495                 ingestTimer.stop();
496             } catch (Exception e2) {
497                 logger.info("Exception :" + e2);
498             }
499             // Documents processed can be different from records processed as in the case of Instance data.
500             logger.debug("Documents processed:" + recCount);
501             bulkIngestStatistics.setFileRecCount(bulkIngestStatistics.getFileRecCount() + recCount);
502             logger.info(
503                     "Bulk ingest: Records processed in the current file :" + bulkIngestStatistics.getFileRecCount());
504         } catch (Exception e) {
505             logger.info("Exception :" + e);
506             bulkIngestStatistics.setCommitRecCount(0);
507             try {
508                 ingestTimer.resume();
509             } catch (Exception e2) {
510                 logger.info("Exception :" + e2);
511             }
512             //documentIngester.rollbackDocStoreIngestedData(session, request.getRequestDocuments());
513             //documentIngester.rollbackDocStoreIngestedData(session, prevRequestDocs);
514             delete(bulkProcessRequest.getPreviousBatchDocuments(), session);
515             ingestTimer.stop();
516             try {
517                 indexTimer.resume();
518             } catch (Exception e2) {
519                 logger.info("Exception :" + e2);
520             }
521             //documentIndexer.rollbackIndexedData(request.getRequestDocuments());
522             //prevRequestDocs = prevRequestDocs.subList(0, prevRequestDocs.size() - request.getRequestDocuments().size());
523             //logger.info("prevRequestDocs before remove INDEXES = " + prevRequestDocs.size());
524             //documentIndexer.rollbackIndexedData(prevRequestDocs);
525             try {
526                 deleteIndex(bulkProcessRequest.getPreviousBatchDocuments());
527                 indexTimer.stop();
528             } catch (Exception e2) {
529                 logger.info("Exception :" + e2);
530             }
531             bulkProcessRequest.setPreviousBatchDocuments(null);
532             logger.error("Document Ingest & Index Failed, Cause: " + e.getMessage(), e);
533             try {
534                 totalTimer.stop();
535             } catch (Exception e2) {
536                 logger.info("Exception :" + e2);
537             }
538             logger.debug("Time Consumptions...:\tcreatingNodes(" + docUUIDs.size() + "):" + createNodesTimer
539                     + "\tSessionSave(" + docUUIDs.size() + "):" + sessionSaveTimer + "\tIngest(" + docUUIDs.size()
540                     + "):" + ingestTimer + "\tIndexing(" + docUUIDs.size() + "):" + indexTimer + "\tTotal Time: "
541                     + totalTimer);
542             docUUIDs.clear();
543         } finally {
544             /*if (session != null) {
545                 try {
546                     RepositoryManager.getRepositoryManager().logout(session);
547                 } catch (OleException e) {
548                 }
549             } */
550         }
551         try {
552             totalTimer.stop();
553         } catch (Exception exe) {
554             logger.info("Exception :" + exe);
555         }
556         logger.debug(
557                 "Time Consumptions...:\tcreatingNodes(" + docUUIDs.size() + "):" + createNodesTimer + "\tSessionSave("
558                         + docUUIDs.size() + "):" + sessionSaveTimer + "\tIngest(" + docUUIDs.size() + "):" + ingestTimer
559                         + "\tIndexing(" + docUUIDs.size() + "):" + indexTimer + "\tTotal Time: " + totalTimer);
560         logger.debug("bulkIngestNIndex(" + requestDocuments.size() + ") END");
561         batchStatistics.setTimeToCreateNodesInJcr(createNodesTimer.getTime());
562         batchStatistics.setTimeToSaveJcrSession(sessionSaveTimer.getTime());
563         batchStatistics.setIngestingTime(ingestTimer.getTime());
564         batchStatistics.setIndexingTime(indexTimer.getTime());
565         batchStatistics.setIngestNIndexTotalTime(totalTimer.getTime());
566         //updateProcessTimer(docUUIDs.size(), ingestTimer, indexTimer, totalTimer);
567         solrOptimizeTimer.start();
568         //optimizeSolr(docUUIDs.size());
569         solrOptimizeTimer.stop();
570         batchStatistics.setTimeToSolrOptimize(solrOptimizeTimer.getTime());
571         return docUUIDs;
572     }
573 
574 
575     /**
576      * @inheritDoc
577      */
578     //@Override
579     public void store(List<RequestDocument> requestDocuments, Session session) throws OleDocStoreException {
580         for (RequestDocument requestDocument : requestDocuments) {
581             store(requestDocument, session);
582         }
583     }
584 
585     /**
586      * @inheritDoc
587      */
588     @Override
589     public void index(List<RequestDocument> requestDocuments, boolean commit) throws OleDocStoreException {
590         if (commit == true) {
591             String result = ServiceLocator.getIndexerService().indexDocuments(requestDocuments);
592             if (!result.startsWith("success")) {
593                 throw new OleDocStoreException(result);
594             }
595         } else {
596             String result = ServiceLocator.getIndexerService().bulkIndexDocuments(requestDocuments, commit);
597             if (!result.startsWith("success")) {
598                 throw new OleDocStoreException(result);
599             }
600         }
601     }
602 
603     protected void delete(List<RequestDocument> requestDocuments, Session session) {
604         // TODO: implement this.
605     }
606 
607     /**
608      * @inheritDoc
609      */
610     //@Override
611     public void deleteIndex(List<RequestDocument> requestDocuments) throws OleDocStoreException {
612         try {
613             Map<String, List<String>> uuids = new HashMap<String, List<String>>();
614             for (RequestDocument document : requestDocuments) {
615                 for (RequestDocument linkedDoc : document.getLinkedRequestDocuments()) {
616                     if (uuids.get(linkedDoc.getCategory()) == null) {
617                         uuids.put(linkedDoc.getCategory(), new ArrayList<String>());
618                     }
619                     uuids.get(linkedDoc.getCategory()).add(linkedDoc.getUuid());
620                 }
621                 if (uuids.get(document.getCategory()) == null) {
622                     uuids.put(document.getCategory(), new ArrayList<String>());
623                 }
624                 uuids.get(document.getCategory()).add(document.getUuid());
625             }
626             for (String category : uuids.keySet()) {
627                 ServiceLocator.getIndexerService().deleteDocuments(category, uuids.get(category));
628             }
629         } catch (Exception e) {
630             throw new OleDocStoreException(e);
631         }
632 
633     }
634 
635     public void store(RequestDocument requestDocument, Session session) throws OleDocStoreException {
636         // Store the document of the requestDocument.
637         ResponseDocument responseDocument = new ResponseDocument();
638         Node fileNode = storeDocument(requestDocument, session, responseDocument);
639 
640         // Store the linked documents of the requestDocument.
641         storeLinkedDocuments(requestDocument, fileNode, session, responseDocument);
642     }
643 
644     public Node storeDocument(RequestDocument requestDocument, Object object, ResponseDocument responseDocument) throws OleDocStoreException {
645         Session session = (Session) object;
646         Node fileNode = null;
647         String validationMsg = null;
648         try {
649             AdditionalAttributes additionalAttributes = requestDocument.getAdditionalAttributes();
650             modifyAdditionalAttributes(requestDocument, null);
651 
652             // get the parent node
653             Node parentNode = nodeManager.getParentNode(requestDocument, session);
654             String fileName = requestDocument.getFormat() + FILE;
655 
656             // create the file node
657             fileNode = nodeManager.createFileNode(requestDocument, fileName, parentNode, session);
658 
659             if (isVersioningEnabled()) {
660                 nodeManager.enableVersioning(fileNode);
661             }
662 
663             // modify document content with node identifier information
664             String parentNodeIdentifier = parentNode.getIdentifier();
665             String nodeIdentifier = fileNode.getIdentifier();
666             modifyDocumentContent(requestDocument, nodeIdentifier, parentNodeIdentifier);
667 
668             // create content node for the document
669             Node contentNode = nodeManager.createContentNode(fileNode, requestDocument, parentNode, session);
670             buildResponseDocument(requestDocument, session, responseDocument);
671             if (validationMsg != null) {
672                 responseDocument.setStatusMessage(validationMsg);
673                 responseDocument.setStatus(ResponseStatus.INVALID_DATA.toString());
674             }
675         } catch (Exception e) {
676             throw new OleDocStoreException(e);
677         }
678         return fileNode;
679     }
680 
681     protected void storeLinkedDocuments(RequestDocument requestDocument, Node node, Session session, ResponseDocument responseDocument)
682             throws OleDocStoreException {
683         // store the linked documents if any.
684         List<ResponseDocument> linkedResponseDocumentList = new ArrayList<ResponseDocument>();
685         if (responseDocument.getLinkedDocuments() != null && responseDocument.getLinkedDocuments().size() > 0) {
686             linkedResponseDocumentList.addAll(responseDocument.getLinkedDocuments());
687         }
688         responseDocument.setLinkedDocuments(linkedResponseDocumentList);
689         for (RequestDocument linkedRequestDocument : requestDocument.getLinkedRequestDocuments()) {
690             ResponseDocument linkedResponseDocument = new ResponseDocument();
691             linkedResponseDocumentList.add(linkedResponseDocument);
692             DocumentManager documentManager = BeanLocator.getDocstoreFactory()
693                     .getDocumentManager(linkedRequestDocument.getCategory(), linkedRequestDocument.getType(), linkedRequestDocument.getFormat());
694             if (linkedRequestDocument.getContent().getContentObject() instanceof InstanceCollection) {
695                 InstanceCollection instCol = (InstanceCollection) linkedRequestDocument.getContent().getContentObject();
696                 if (instCol != null) {
697                     for (Instance inst : instCol.getInstance()) {
698                         List<String> resIdList = new ArrayList<String>();
699                         resIdList.addAll(inst.getResourceIdentifier());
700                         for (String resId : inst.getResourceIdentifier()) {
701                             try {
702                                 nodeManager.getNodeByUUID(session, resId);
703                             } catch (Exception e) {
704                                 resIdList.remove(resId);
705                             }
706                         }
707                         inst.setResourceIdentifier(resIdList);
708                     }
709                 }
710             }
711             Node linkedDocumentNode = documentManager.storeDocument(linkedRequestDocument, session, linkedResponseDocument);
712             nodeManager.linkNodes(node, linkedDocumentNode, session);
713             //buildResponseDocument(linkedRequestDocument,session,linkedResponseDocument);
714         }
715 
716     }
717 
718 /*    protected void validateInput(List<RequestDocument> requestDocuments) throws OleDocStoreException, RepositoryException, FileNotFoundException {
719         if ((null == requestDocuments) || (requestDocuments.size() == 0)) {
720             throw new OleDocStoreException("RequestDocuments are not specified.");
721         }
722         for (RequestDocument requestDocument : requestDocuments) {
723             validateInput(requestDocument );
724         }
725     }*/
726 
727 
728     public List<ResponseDocument> buildResponseDocuments(List<RequestDocument> requestDocuments) {
729         List<ResponseDocument> responseDocumentList = new ArrayList<ResponseDocument>();
730         for (int i = 0; i < requestDocuments.size(); i++) {
731             responseDocumentList.add(buildResponseDocument(requestDocuments.get(i)));
732         }
733         return responseDocumentList;
734     }
735 
736     public ResponseDocument buildResponseDocument(RequestDocument requestDocument) {
737         ResponseDocument responseDocument = new ResponseDocument();
738         responseDocument.setId(requestDocument.getId());
739         responseDocument.setCategory(requestDocument.getCategory());
740         responseDocument.setType(requestDocument.getType());
741         responseDocument.setFormat(requestDocument.getFormat());
742         responseDocument.setUuid(requestDocument.getUuid());
743         buildLinkedResponseDocuments(requestDocument, responseDocument);
744         return responseDocument;
745     }
746 
747     public void buildResponseDocument(RequestDocument requestDocument, Session session, ResponseDocument responseDocument) {
748         responseDocument.setId(requestDocument.getId());
749         responseDocument.setCategory(requestDocument.getCategory());
750         responseDocument.setType(requestDocument.getType());
751         responseDocument.setFormat(requestDocument.getFormat());
752         responseDocument.setUuid(requestDocument.getUuid());
753         String category = requestDocument.getCategory();
754         String type = requestDocument.getType();
755         Node node = null;
756         try {
757             node = session.getNodeByIdentifier(requestDocument.getUuid());
758         } catch (RepositoryException e) {
759             logger.info("Failed to get node:" + e.getMessage(), e);
760         }
761 
762         if (node != null) {
763             try {
764                 AdditionalAttributes additionalAttributes = requestDocument.getAdditionalAttributes();
765                 if (additionalAttributes != null && category.equals(DocCategory.WORK.getDescription()) && type
766                         .equals(DocType.BIB.getDescription())) {
767                     Collection<String> attributeNames = additionalAttributes.getAttributeNames();
768                     if (attributeNames != null && attributeNames.size() > 0) {
769                         for (Iterator<String> iterator = attributeNames.iterator(); iterator.hasNext(); ) {
770                             String attributeName = iterator.next();
771                             if (node.hasProperty(attributeName)) {
772                                 additionalAttributes
773                                         .setAttribute(attributeName, node.getProperty(attributeName).getString());
774                             }
775                         }
776                     }
777                     responseDocument.setAdditionalAttributes(additionalAttributes);
778                 }
779             } catch (RepositoryException e) {
780                 logger.info("Failed to get node property:" + e.getMessage(), e);
781             }
782         }
783 
784         if (requestDocument.getType().equalsIgnoreCase(DocType.INSTANCE.getCode())) {
785             buildLinkedResponseDocuments(requestDocument, responseDocument);
786         }
787         // buildLinkedResponseDocuments(requestDocument, responseDocument);
788         //  return responseDocument;
789     }
790 
791     protected void buildLinkedResponseDocuments(RequestDocument requestDocument, ResponseDocument responseDocument) {
792 
793     }
794 
795     protected void setResponseParameters(ResponseDocument responseDocument, RequestDocument docStoreDocument) {
796         responseDocument.setId(docStoreDocument.getId());
797         responseDocument.setCategory(docStoreDocument.getCategory());
798         responseDocument.setType(docStoreDocument.getType());
799         responseDocument.setFormat(docStoreDocument.getFormat());
800         responseDocument.setContent(docStoreDocument.getContent());
801         responseDocument.setUuid(docStoreDocument.getUuid());
802     }
803 
804 
805     protected void modifyAdditionalAttributes(RequestDocument requestDocument, Node node) {
806     }
807 
808     @Override
809     public ResponseDocument bind(RequestDocument requestDocument, Object object, String operation) throws OleDocStoreException, RepositoryException, OleException, FileNotFoundException {
810         Session session = (Session) object;
811         return new JcrWorkInstanceDocumentManager().bind(requestDocument, session, operation);
812     }
813 
814     @Override
815     public ResponseDocument unbind(RequestDocument requestDocument, Object object, String operation) throws OleDocStoreException, RepositoryException, OleException, FileNotFoundException {
816         Session session = (Session) object;
817         return new JcrWorkInstanceDocumentManager().unbind(requestDocument, session, operation);
818     }
819 
820     protected void modifyDocumentContent(RequestDocument requestDocument, String nodeIdentifier,
821                                          String parentNodeIdentifier) {
822     }
823 
824     public boolean isVersioningEnabled() {
825         return false;
826     }
827 
828     public VersionManager getVersionManager(Session session) throws OleDocStoreException, RepositoryException {
829         return session.getWorkspace().getVersionManager();
830     }
831 
832 //    public boolean checkItemsExists( List<Item> itemIdentifierList,Session session) throws Exception{
833 //        for (Item item : itemIdentifierList) {
834 //           //TODO
835 //       }
836 //        return false;
837 //    }
838 
839     public boolean checkItemsExists() throws Exception {
840 
841         //TODO
842 
843         return false;
844     }
845 
846 
847     public boolean checkInstancesOrItemsExistsInOLE(List<String> uuidsList) {
848         String uuidsNotInOle = null;
849         String serviceURL = ConfigContext.getCurrentContextConfig().getProperty("uuidCheckServiceURL");
850         OleWebServiceProvider oleWebServiceProvider = new OleWebServiceProviderImpl();
851         OleUuidCheckWebService oleUuidCheckWebService = (OleUuidCheckWebService) oleWebServiceProvider
852                 .getService("org.kuali.ole.docstore.service.OleUuidCheckWebService", "oleUuidCheckWebService", serviceURL);
853         StringBuilder uuidsSB = new StringBuilder();
854         for (String uuid : uuidsList) {
855             //uuidsSB.append(requestDocument.getId()).append(",");
856             uuidsSB.append(uuid).append(",");
857         }
858         logger.debug("JcrAbstractDocumentManager checkInstancesOrItemsExistsInOLE :uuidsSB " + uuidsSB.toString());
859         uuidsNotInOle = oleUuidCheckWebService.checkUuidExsistence(uuidsSB.substring(0, uuidsSB.length() - 1));
860         logger.debug("JcrAbstractDocumentManager checkInstancesOrItemsExistsInOLE :uuidsNotInOle " + uuidsNotInOle);
861         String[] uuids = StringUtils.split(uuidsNotInOle, ",");
862         if (uuids.length == uuidsList.size()) {
863             return false;
864         } else {
865             return true;
866         }
867         //false means not exists
868     }
869 
870     //checks whether instance and its corresponding item identifiers are exists in ole
871     public boolean checkInstancesOrItemsExistsInOLE(String instanceIdentifier, Session session) throws Exception {
872         String uuidsNotInOle = null;
873         List<String> instanceOrItemIdentifiersList = new ArrayList<String>();
874         Node instanceNode = session.getNodeByIdentifier(instanceIdentifier);
875         String instanceXML = WorkInstanceNodeManager.getInstance().getInstanceData(instanceNode);
876         InstanceCollection instanceCollection = new InstanceOlemlRecordProcessor().fromXML(instanceXML);
877         Items items = instanceCollection.getInstance().get(0).getItems();
878         List<Item> itemIdentifierList = items.getItem();
879         for (Item item : itemIdentifierList) {
880             instanceOrItemIdentifiersList.add(item.getItemIdentifier());
881         }
882         instanceOrItemIdentifiersList.add(instanceIdentifier);
883         return checkInstancesOrItemsExistsInOLE(instanceOrItemIdentifiersList);
884     }
885 
886     public boolean checkInstanceForBoundsWith(String instanceIdentifier, RequestDocument requestDocument,
887                                               Session session, ResponseDocument responseDocument) throws Exception {
888 
889         Node instanceNode = session.getNodeByIdentifier(instanceIdentifier);
890         String bibIdentifier = instanceNode.getProperty("bibIdentifier").getString();
891         String[] bibIds = bibIdentifier.split(",");
892         logger.debug("JcrAbstractDocumentManager : checkInstanceForBoundsWith bibIds length " + bibIds.length);
893         if (bibIds.length > 1) {
894             responseDocument.setCategory(requestDocument.getCategory());
895             responseDocument.setType(requestDocument.getType());
896             responseDocument.setFormat(requestDocument.getFormat());
897             responseDocument.setUuid(requestDocument.getUuid());
898             responseDocument.setStatus("failure'");
899             responseDocument.setStatusMessage("Instance is bound with more than one bid. So deletion cannot be done");
900             return true;
901         } else {
902             return false;
903         }
904     }
905 
906     public RequestDocument prepareRequestDocument(ResponseDocument responseDocument) {
907         RequestDocument requestDocument = new RequestDocument();
908         requestDocument.setCategory(responseDocument.getCategory());
909         requestDocument.setFormat(responseDocument.getFormat());
910         requestDocument.setType(responseDocument.getType());
911         requestDocument.setUuid(responseDocument.getUuid());
912         return requestDocument;
913 
914     }
915 }