View Javadoc

1   package org.kuali.ole.docstore.process;
2   
3   import org.apache.commons.lang.time.StopWatch;
4   import org.apache.solr.common.SolrDocument;
5   import org.apache.solr.common.SolrInputDocument;
6   import org.kuali.ole.DocumentUniqueIDPrefix;
7   import org.kuali.ole.RepositoryBrowser;
8   import org.kuali.ole.RepositoryManager;
9   import org.kuali.ole.docstore.discovery.solr.work.bib.marc.WorkBibMarcDocBuilder;
10  import org.kuali.ole.docstore.document.rdbms.RdbmsWorkBibMarcDocumentManager;
11  import org.kuali.ole.docstore.document.rdbms.RdbmsWorkEInstanceDocumentManager;
12  import org.kuali.ole.docstore.document.rdbms.RdbmsWorkInstanceDocumentManager;
13  import org.kuali.ole.docstore.indexer.solr.IndexerService;
14  import org.kuali.ole.docstore.metrics.reindex.ReIndexingBatchStatus;
15  import org.kuali.ole.docstore.metrics.reindex.ReIndexingStatus;
16  import org.kuali.ole.docstore.model.enums.DocCategory;
17  import org.kuali.ole.docstore.model.enums.DocFormat;
18  import org.kuali.ole.docstore.model.enums.DocType;
19  import org.kuali.ole.docstore.model.rdbms.bo.BibRecord;
20  import org.kuali.ole.docstore.model.rdbms.bo.EInstanceRecord;
21  import org.kuali.ole.docstore.model.rdbms.bo.InstanceRecord;
22  import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
23  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
24  import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
25  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.FormerIdentifier;
26  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Identifier;
27  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Instance;
28  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.InstanceCollection;
29  import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkInstanceOlemlRecordProcessor;
30  import org.kuali.ole.docstore.model.xstream.work.oleml.WorkEInstanceOlemlRecordProcessor;
31  import org.kuali.ole.docstore.service.BeanLocator;
32  import org.kuali.ole.docstore.service.DocumentIngester;
33  import org.kuali.ole.docstore.service.ServiceLocator;
34  import org.kuali.ole.pojo.OleException;
35  import org.kuali.ole.repository.CheckoutManager;
36  import org.kuali.ole.repository.NodeHandler;
37  import org.kuali.rice.krad.service.BusinessObjectService;
38  import org.kuali.rice.krad.service.KRADServiceLocator;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  import javax.jcr.Binary;
43  import javax.jcr.Node;
44  import javax.jcr.NodeIterator;
45  import javax.jcr.Session;
46  import java.io.ByteArrayInputStream;
47  import java.text.DateFormat;
48  import java.text.SimpleDateFormat;
49  import java.util.*;
50  
51  /**
52   * Class to Rebuild Indexes.
53   *
54   * @author Rajesh Chowdary K
55   * @created May 2, 2012
56   */
57  public class RebuildIndexesHandler
58          implements Runnable {
59  
60      private static RebuildIndexesHandler reBuilder = null;
61      private boolean isRunning = false;
62      private boolean isStop = false;
63      private static final Logger logger = LoggerFactory.getLogger(RebuildIndexesHandler.class);
64      private String docCategory;
65      private String docType;
66      private String docFormat;
67      private CheckoutManager checkoutManager;
68      //    private ReIndexingStatus reIndexingStatus;
69  
70  
71      public synchronized void setRunning(boolean running) {
72          isRunning = running;
73      }
74  
75      public synchronized void setStop(boolean stop) {
76          isStop = stop;
77      }
78  
79      private RebuildIndexesHandler() {
80          checkoutManager = new CheckoutManager();
81      }
82  
83      public static RebuildIndexesHandler getInstance() {
84          if (reBuilder == null) {
85              reBuilder = new RebuildIndexesHandler();
86          }
87          return reBuilder;
88      }
89  
90      /**
91       * Method to get running status.
92       *
93       * @return
94       */
95      public synchronized boolean isRunning() {
96          return isRunning;
97      }
98  
99      public synchronized boolean isStop() {
100         return isStop;
101     }
102 
103     /**
104      * Method to startProcess
105      */
106     public String startProcess(String docCategory, String docType, String docFormat) throws InterruptedException {
107         String status = null;
108         if (isRunning()) {
109             status = "ReIndexing process is already running. Click 'Show Status' button to know the status. ";
110         } else {
111             setRunning(true);
112             setStop(false);
113             status = "ReIndexing process has started. Click 'Show Status' button to know the status. ";
114             ReIndexingStatus reIndexingStatus = ReIndexingStatus.getInstance();
115             reIndexingStatus.reset();
116             if (docCategory == null || docCategory.equals("")) {
117                 docCategory = "all";
118             }
119             if (docType == null || docType.equals("")) {
120                 docType = "all";
121             }
122             if (docFormat == null || docType.equals("")) {
123                 docFormat = "all";
124             }
125             this.docCategory = docCategory;
126             this.docType = docType;
127             this.docFormat = docFormat;
128             Thread reBuilderThread = new Thread(this);
129             reBuilderThread.start();
130             //            reBuilderThread.join();
131             setRunning(false);
132         }
133         return status;
134     }
135 
136     public String stopProcess() throws Exception {
137         String status = null;
138         if (isRunning()) {
139             status = "ReIndexing process is running. ReIndexing will stop after current batch. ";
140             setStop(true);
141             setRunning(false);
142         } else {
143             status = "ReIndexing process is not running.";
144         }
145         return status;
146 
147     }
148 
149     public void run() {
150         DocCategoryTypeFormat docCategoryTypeFormat = new DocCategoryTypeFormat();
151         List<String> categoryList = docCategoryTypeFormat.getCategories();
152         List<String> typeList = null;
153         List<String> formatList = null;
154         for (String docCategoryCurr : categoryList) {
155             if (docCategory.equals("all") || docCategory.equals(docCategoryCurr)) {
156                 typeList = docCategoryTypeFormat.getDocTypes(docCategoryCurr);
157                 for (String docTypeCurr : typeList) {
158                     if (docType.equals("all") || docType.equals(docTypeCurr)) {
159                         formatList = docCategoryTypeFormat.getDocFormats(docCategoryCurr, docTypeCurr);
160                         for (String docFormatCurr : formatList) {
161                             if (docFormat.equals("all") || docFormat.equals(docFormatCurr)) {
162                                 if (!isStop()) {
163                                     ReIndexingStatus.getInstance()
164                                             .startDocType(docCategoryCurr, docTypeCurr, docFormatCurr);
165                                     reIndex(docCategoryCurr, docTypeCurr, docFormatCurr);
166                                 } else {
167                                     return;
168                                 }
169                             }
170                         }
171                     }
172                 }
173             }
174         }
175         setRunning(false);
176     }
177 
178     private void reIndex(String docCategory, String docType, String docFormat) {
179         Session session = null;
180         setRunning(true);
181         logger.info("Rebuild Indexes Run(" + docCategory + " : " + docType + " : " + docFormat + "): ");
182         try {
183             if (docCategory.equals(DocCategory.WORK.getCode())) {
184                 if (docType.equals(DocType.BIB.getDescription())) {
185                     if (docFormat.equals(DocFormat.MARC.getCode()) || docFormat.equals(DocFormat.DUBLIN_CORE.getCode())
186                             || docFormat.equals(DocFormat.DUBLIN_UNQUALIFIED.getCode())) {
187                         workBibMarcAndDublinAll(docCategory, docType, docFormat);
188                     } else {
189                         logger.info(
190                                 "Rebuild Indexes Run(" + docCategory + " : " + docType + " : " + docFormat + "): FAIL");
191                     }
192                 } else if (docType.equals(DocType.INSTANCE.getDescription())) {
193                     if (docFormat.equals(DocFormat.OLEML.getCode())) {
194                         workInstanceOLEML(docCategory, docType, docFormat);
195                     } else {
196                         logger.info(
197                                 "Rebuild Indexes Run(" + docCategory + " : " + docType + " : " + docFormat + "): FAIL");
198                     }
199                 } else if (docType.equals(DocType.LICENSE.getDescription())) {
200                     if (docFormat.equals(DocFormat.ONIXPL.getCode()) || docFormat.equals(DocFormat.PDF.getCode())
201                             || docFormat.equals(DocFormat.DOC.getCode())) {
202                         workLicense(docCategory, docType, docFormat);
203                     } else {
204                         logger.info(
205                                 "Rebuild Indexes Run(" + docCategory + " : " + docType + " : " + docFormat + "): FAIL");
206                     }
207                 } else if (docType.equals(DocType.EINSTANCE.getCode())) {
208                     if (docFormat.equals(DocFormat.OLEML.getCode())) {
209                         workEInstanceOLEML(docCategory, docType, docFormat);
210                     } else {
211                         logger.info(
212                                 "Rebuild Indexes Run(" + docCategory + " : " + docType + " : " + docFormat + "): FAIL");
213                     }
214                 }
215             }
216         } catch (Exception e) {
217             logger.info(e.getMessage(), e);
218         } finally {
219             try {
220                 if (isStop) {
221                     ReIndexingStatus.getInstance().getDocTypeList().setStatus("Stopped");
222                 } else {
223                     ReIndexingStatus.getInstance().getDocTypeList().setStatus("Done");
224                 }
225                 RepositoryManager.getRepositoryManager().logout(session);
226             } catch (OleException e) {
227                 logger.error(e.getMessage(), e);
228             }
229         }
230 
231     }
232 
233     private void workEInstanceOLEML(String docCategory, String docType, String docFormat) {
234         long totalCount = 0;
235         long nodeCount = 0;
236         List<RequestDocument> docs = new ArrayList<RequestDocument>();
237         WorkEInstanceOlemlRecordProcessor workEInstanceOlemlRecordProcessor = new WorkEInstanceOlemlRecordProcessor();
238         try {
239             RequestDocument rd = new RequestDocument();
240             rd.setCategory(docCategory);
241             rd.setType(docType);
242             rd.setFormat(docFormat);
243             List<ReIndexingBatchStatus> batchStatusList = new ArrayList<ReIndexingBatchStatus>();
244             BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
245             List<EInstanceRecord> instanceRecords = (List<EInstanceRecord>) businessObjectService.findAll(EInstanceRecord.class);
246             StopWatch loadTimer = new StopWatch();
247             StopWatch batchTimer = new StopWatch();
248             loadTimer.start();
249             batchTimer.start();
250             for (int i = 0; i < instanceRecords.size(); i++) {
251                 if (docs.size() == ProcessParameters.BULK_PROCESSOR_SPLIT_SIZE) {
252                     if (!isStop()) {
253                         ReIndexingBatchStatus reIndexingBatchStatus = indexBeforeParams(loadTimer);
254                         indexDocs(docs, totalCount, nodeCount, batchStatusList, reIndexingBatchStatus);
255                         indexAfterParams(batchTimer, reIndexingBatchStatus, batchStatusList);
256                         resetTimers(batchTimer, loadTimer);
257                         totalCount = 0;
258                         logger.info("Rebuild");
259                     } else {
260                         return;
261                     }
262                 } else {
263                     EInstanceRecord instanceRecord = instanceRecords.get(i);
264                     String uuid = DocumentUniqueIDPrefix.getPrefixedId(instanceRecord.getUniqueIdPrefix(), instanceRecord.geteInstanceIdentifier());
265                     RequestDocument requestDocument = buildRequestDocumentForCheckout(docCategory, docType, docFormat, uuid);
266                     ResponseDocument responseDocument = RdbmsWorkEInstanceDocumentManager.getInstance().checkoutContent(requestDocument, businessObjectService);
267                     String content = responseDocument.getContent().getContent();
268                     RequestDocument requestDocumentForIndex = (RequestDocument) rd.clone();
269                     requestDocumentForIndex.setAdditionalAttributes(responseDocument.getAdditionalAttributes());
270                     requestDocumentForIndex.setId(uuid);
271                     requestDocumentForIndex.setUuid(uuid);
272                     org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.InstanceCollection instanceCollection = workEInstanceOlemlRecordProcessor.fromXML(content);
273                     content = workEInstanceOlemlRecordProcessor.toXML(instanceCollection);
274                     Content contentObj = new Content();
275                     contentObj.setContent(content);
276                     contentObj.setContentObject(instanceCollection);
277                     requestDocumentForIndex.setContent(contentObj);
278                     docs.add(requestDocumentForIndex);
279                     totalCount++;
280                 }
281             }
282             if (docs.size() > 0 && !isStop()) {
283                 ReIndexingBatchStatus reIndexingBatchStatus = indexBeforeParams(loadTimer);
284                 indexDocs(docs, totalCount, nodeCount, batchStatusList, reIndexingBatchStatus);
285                 indexAfterParams(batchTimer, reIndexingBatchStatus, batchStatusList);
286             }
287         } catch (Exception e) {
288             logger.error(
289                     "Rebuild Indexes Process(" + docCategory + " : " + docType + " : " + docFormat + ") Processed(" + (
290                             totalCount - docs.size()) + "), Failed @ batch(" + docs.size() + "): Cause: " + e, e);
291         } finally {
292             if (isStop) {
293                 ReIndexingStatus.getInstance().getDocTypeList().setStatus("Stopped");
294             } else {
295                 ReIndexingStatus.getInstance().getDocTypeList().setStatus("Done");
296             }
297         }
298 
299     }
300 
301 
302     private void indexDocs(List<RequestDocument> docs, long records, long recCount,
303                            List<ReIndexingBatchStatus> batchStatusList, ReIndexingBatchStatus reIndexingBatchStatus) {
304         try {
305             StopWatch indexTimer = new StopWatch();
306             DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
307             Date startDate = new Date();
308             reIndexingBatchStatus.setBatchStartTime(dateFormat.format(startDate));
309             indexTimer.start();
310             reIndexingBatchStatus.setStatus("Indexing");
311             reIndexingBatchStatus.setBatchIndexingTime(indexTimer.toString());
312             reIndexingBatchStatus.setRecordsProcessed(records);
313             reIndexingBatchStatus.setBatchEndTime(" ");
314             batchStatusList.add(reIndexingBatchStatus);
315             ReIndexingStatus.getInstance().getDocTypeList().setReIndBatStatusList(batchStatusList);
316             IndexerService indexerService = BeanLocator.getDocstoreFactory().getDocumentIndexManager(docs.get(0).getCategory(), docs.get(0).getType(), docs.get(0).getFormat());
317             String result = indexerService.indexDocuments(docs);
318             logger.debug(result);
319             indexTimer.stop();
320             Date endDate = new Date();
321             reIndexingBatchStatus.setBatchEndTime(dateFormat.format(endDate));
322             reIndexingBatchStatus.setBatchIndexingTime(indexTimer.toString());
323             reIndexingBatchStatus.setRecordsProcessed(records);
324             reIndexingBatchStatus.setStatus("Done");
325             reIndexingBatchStatus.setRecordsRemaining(recCount - records);
326             ReIndexingStatus.getInstance().getDocTypeList().setReIndBatStatusList(batchStatusList);
327             docs.clear();
328         } catch (Exception e) {
329             logger.error("Rebuild Indexes Processed(" + (records - docs.size()) + "), Failed @ batch(" + docs.size()
330                     + "): Cause: " + e + "\n\tContinuous", e);
331         }
332     }
333 
334     private void workBibMarcAndDublinAll(String docCategory, String docType, String docFormat) {
335 
336         long totalCount = 0;
337         long nodeCount = 0;
338         List<RequestDocument> docs = new ArrayList<RequestDocument>();
339         try {
340             RequestDocument rd = new RequestDocument();
341             rd.setCategory(docCategory);
342             rd.setType(docType);
343             rd.setFormat(docFormat);
344             String prefix = DocumentUniqueIDPrefix.getPrefix(docCategory, docType, docFormat);
345             Map prefixMap = new HashMap(0);
346             prefixMap.put("uniqueIdPrefix", prefix);
347             BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
348             List<BibRecord> bibRecords = (List<BibRecord>) businessObjectService.findMatching(BibRecord.class, prefixMap);
349             List<ReIndexingBatchStatus> batchStatusList = new ArrayList<ReIndexingBatchStatus>();
350             StopWatch loadTimer = new StopWatch();
351             StopWatch batchTimer = new StopWatch();
352             loadTimer.start();
353             batchTimer.start();
354             for (int i = 0; i < bibRecords.size(); i++) {
355                 if (docs.size() == ProcessParameters.BULK_PROCESSOR_SPLIT_SIZE) {
356                     if (!isStop()) {
357                         ReIndexingBatchStatus reIndexingBatchStatus = indexBeforeParams(loadTimer);
358                         indexDocs(docs, totalCount, nodeCount, batchStatusList, reIndexingBatchStatus);
359                         indexAfterParams(batchTimer, reIndexingBatchStatus, batchStatusList);
360                         resetTimers(batchTimer, loadTimer);
361                         totalCount = 0;
362                         logger.info("Rebuild");
363                     } else {
364                         return;
365                     }
366                 } else {
367                     BibRecord bibRecord = bibRecords.get(i);
368                     RequestDocument requestDocumentForIndex = (RequestDocument) rd.clone();
369                     String uuid = DocumentUniqueIDPrefix.getPrefixedId(bibRecord.getUniqueIdPrefix(), bibRecord.getBibId());
370                     RequestDocument requestDocument = buildRequestDocumentForCheckout(docCategory, docType, docFormat, uuid);
371                     ResponseDocument responseDocument = RdbmsWorkBibMarcDocumentManager.getInstance().checkout(requestDocument, businessObjectService);
372                     String content = responseDocument.getContent().getContent();
373                     requestDocumentForIndex.setId(uuid);
374                     requestDocumentForIndex.setUuid(uuid);
375                     Content contentObj = new Content();
376                     contentObj.setContent(content);
377                     requestDocumentForIndex.setContent(contentObj);
378                     requestDocumentForIndex.setAdditionalAttributes(requestDocument.getAdditionalAttributes());
379                     docs.add(requestDocumentForIndex);
380                     totalCount++;
381                 }
382             }
383             if (docs.size() > 0 && !isStop()) {
384                 ReIndexingBatchStatus reIndexingBatchStatus = indexBeforeParams(loadTimer);
385                 indexDocs(docs, totalCount, nodeCount, batchStatusList, reIndexingBatchStatus);
386                 indexAfterParams(batchTimer, reIndexingBatchStatus, batchStatusList);
387             }
388         } catch (Exception e) {
389             logger.error(
390                     "Rebuild Indexes Process(" + docCategory + " : " + docType + " : " + docFormat + ") Processed(" + (
391                             totalCount - docs.size()) + "), Failed @ batch(" + docs.size() + "): Cause: " + e, e);
392         } finally {
393             if (isStop) {
394                 ReIndexingStatus.getInstance().getDocTypeList().setStatus("Stopped");
395             } else {
396                 ReIndexingStatus.getInstance().getDocTypeList().setStatus("Done");
397             }
398         }
399     }
400 
401     private void workInstanceOLEML(String docCategory, String docType, String docFormat) {
402         long totalCount = 0;
403         long nodeCount = 0;
404         List<RequestDocument> docs = new ArrayList<RequestDocument>();
405         WorkInstanceOlemlRecordProcessor workInstanceOlemlRecordProcessor = new WorkInstanceOlemlRecordProcessor();
406         try {
407             RequestDocument rd = new RequestDocument();
408             rd.setCategory(docCategory);
409             rd.setType(docType);
410             rd.setFormat(docFormat);
411             List<ReIndexingBatchStatus> batchStatusList = new ArrayList<ReIndexingBatchStatus>();
412             BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
413             List<InstanceRecord> instanceRecords = (List<InstanceRecord>) businessObjectService.findAll(InstanceRecord.class);
414             StopWatch loadTimer = new StopWatch();
415             StopWatch batchTimer = new StopWatch();
416             loadTimer.start();
417             batchTimer.start();
418             for (int i = 0; i < instanceRecords.size(); i++) {
419                 if (docs.size() == ProcessParameters.BULK_PROCESSOR_SPLIT_SIZE) {
420                     if (!isStop()) {
421                         ReIndexingBatchStatus reIndexingBatchStatus = indexBeforeParams(loadTimer);
422                         indexDocs(docs, totalCount, nodeCount, batchStatusList, reIndexingBatchStatus);
423                         indexAfterParams(batchTimer, reIndexingBatchStatus, batchStatusList);
424                         resetTimers(batchTimer, loadTimer);
425                         totalCount = 0;
426                         logger.info("Rebuild");
427                     } else {
428                         return;
429                     }
430                 } else {
431                     InstanceRecord instanceRecord = instanceRecords.get(i);
432                     String uuid = DocumentUniqueIDPrefix.getPrefixedId(instanceRecord.getUniqueIdPrefix(), instanceRecord.getInstanceId());
433                     RequestDocument requestDocument = buildRequestDocumentForCheckout(docCategory, docType, docFormat, uuid);
434                     ResponseDocument responseDocument = RdbmsWorkInstanceDocumentManager.getInstance().checkoutContent(requestDocument, businessObjectService);
435                     String content = responseDocument.getContent().getContent();
436                     RequestDocument requestDocumentForIndex = (RequestDocument) rd.clone();
437                     requestDocumentForIndex.setAdditionalAttributes(responseDocument.getAdditionalAttributes());
438                     requestDocumentForIndex.setId(uuid);
439                     requestDocumentForIndex.setUuid(uuid);
440                     InstanceCollection instanceCollection = workInstanceOlemlRecordProcessor.fromXML(content);
441 //                    logger.debug("REBUILD_INDEXING_LINKING " + ProcessParameters.REBUILD_INDEXING_LINKING);
442 //                    if (!ProcessParameters.REBUILD_INDEXING_LINKING) {
443 //                        instanceCollection.getInstance().get(0).getResourceIdentifier().clear();
444 //                    }
445                     content = workInstanceOlemlRecordProcessor.toXML(instanceCollection);
446                     Content contentObj = new Content();
447                     contentObj.setContent(content);
448                     contentObj.setContentObject(instanceCollection);
449                     requestDocumentForIndex.setContent(contentObj);
450                     docs.add(requestDocumentForIndex);
451                     totalCount++;
452                 }
453             }
454             if (docs.size() > 0 && !isStop()) {
455                 ReIndexingBatchStatus reIndexingBatchStatus = indexBeforeParams(loadTimer);
456                 indexDocs(docs, totalCount, nodeCount, batchStatusList, reIndexingBatchStatus);
457                 indexAfterParams(batchTimer, reIndexingBatchStatus, batchStatusList);
458             }
459         } catch (Exception e) {
460             logger.error(
461                     "Rebuild Indexes Process(" + docCategory + " : " + docType + " : " + docFormat + ") Processed(" + (
462                             totalCount - docs.size()) + "), Failed @ batch(" + docs.size() + "): Cause: " + e, e);
463         } finally {
464             if (isStop) {
465                 ReIndexingStatus.getInstance().getDocTypeList().setStatus("Stopped");
466             } else {
467                 ReIndexingStatus.getInstance().getDocTypeList().setStatus("Done");
468             }
469         }
470     }
471 
472     private RequestDocument buildRequestDocumentForCheckout(String docCategory, String docType, String docFormat, String uuid) {
473         RequestDocument requestDocument = new RequestDocument();
474         requestDocument.setCategory(docCategory);
475         requestDocument.setType(docType);
476         requestDocument.setFormat(docFormat);
477         requestDocument.setUuid(uuid);
478         return requestDocument;
479     }
480 
481     private void linkingInstanceWithBib(InstanceCollection instanceCollection, Session session, Node fileNode) {
482         for (Instance instance : instanceCollection.getInstance()) {
483             instance.getResourceIdentifier().clear();
484             for (FormerIdentifier frids : instance.getFormerResourceIdentifier()) {
485                 try {
486                     if (frids != null && frids.getIdentifier() != null &&
487                             frids.getIdentifier().getIdentifierValue() != null &&
488                             frids.getIdentifier().getIdentifierValue().trim().length() >= 0) {
489                         List<SolrDocument> solrBibDocs = ServiceLocator.getIndexerService()
490                                 .getSolrDocument("SystemControlNumber",
491                                         frids.getIdentifier()
492                                                 .getIdentifierValue());
493                         SolrInputDocument solrInputDocument = new SolrInputDocument();
494                         WorkBibMarcDocBuilder marcDocBuilder = new WorkBibMarcDocBuilder();
495                         List<SolrInputDocument> solrInputDocs = new ArrayList<SolrInputDocument>();
496                         if (solrBibDocs != null && solrBibDocs.size() > 0) {
497                             for (SolrDocument solrbibDoc : solrBibDocs) {
498                                 if (checkApplicability(frids.getIdentifier().getIdentifierValue(),
499                                         solrbibDoc.getFieldValue("SystemControlNumber"))) {
500 
501                                     compareObjNAddValue(instance.getInstanceIdentifier(),
502                                             solrbibDoc.getFieldValue("instanceIdentifier"), solrbibDoc,
503                                             "instanceIdentifier");
504                                     solrInputDocument = new SolrInputDocument();
505                                     marcDocBuilder.buildSolrInputDocFromSolrDoc(solrbibDoc, solrInputDocument);
506                                     solrInputDocs.add(solrInputDocument);
507                                     String bibId = compareListRString(solrbibDoc.getFieldValue("id"));
508                                     instance.getResourceIdentifier().add(bibId);
509                                     modifyContentAddLinkedIdsInDocStore(instance, bibId, session, fileNode);
510                                     indexSolrDocs(solrInputDocs);
511                                 }
512                             }
513                         }
514                     }
515                 } catch (Exception e) {
516                     logger.error("error message" + e.getMessage(), e);
517                 }
518             }
519         }
520     }
521 
522     private void modifyContentAddLinkedIdsInDocStore(Instance instance, String id, Session session, Node fileNode) {
523 
524         try {
525             Node bibNode = getNodeByUUID(session, id);
526             bibNode.setProperty("instanceIdentifier", instance.getInstanceIdentifier());
527             fileNode.setProperty("bibIdentifier", id);
528 
529             WorkInstanceOlemlRecordProcessor recordProcessor = new WorkInstanceOlemlRecordProcessor();
530             NodeIterator nodeIterator = fileNode.getNodes();
531             while (nodeIterator.hasNext()) {
532                 Node instNode = nodeIterator.nextNode();
533                 if (instNode.getName().equalsIgnoreCase("instanceFile")) {
534                     InstanceCollection instCol = new InstanceCollection();
535                     Instance inst = new Instance();
536                     inst.setResourceIdentifier(instance.getResourceIdentifier());
537                     inst.setFormerResourceIdentifier(instance.getFormerResourceIdentifier());
538                     inst.setExtension(instance.getExtension());
539                     inst.setInstanceIdentifier(instance.getInstanceIdentifier());
540                     List<Instance> instanceList = new ArrayList<Instance>();
541                     instanceList.add(inst);
542                     instCol.setInstance(instanceList);
543 
544                     byte[] documentBytes = recordProcessor.toXML(instCol).getBytes();
545                     Binary binary = null;
546                     if (documentBytes != null && instNode != null && documentBytes.length > 0) {
547                         binary = session.getValueFactory().createBinary(new ByteArrayInputStream(documentBytes));
548                         instNode.getNode("jcr:content").setProperty("jcr:data", binary);
549                     }
550                 }
551             }
552         } catch (Exception e) {
553             logger.error("error while updating Docstore in reindexing Process" + e.getMessage(), e);
554         }
555     }
556 
557     private void indexSolrDocs(List<SolrInputDocument> solrInputDocs) {
558 
559         try {
560             ServiceLocator.getIndexerService().indexSolrDocuments(solrInputDocs);
561             logger.info("Linking Bib and Instance Records (" + solrInputDocs.size() + "): ");
562             solrInputDocs.clear();
563         } catch (Exception e) {
564             logger.error(
565                     "Linking Bib and Instance Records (" + (solrInputDocs.size()) + "), Failed @ batch(" + solrInputDocs
566                             .size() + "): Cause: " + e + "\n\tContinuous", e);
567         }
568     }
569 
570 
571     private boolean checkApplicability(Object value, Object fieldValue) {
572         if (fieldValue instanceof Collection) {
573             for (Object object : (Collection) fieldValue) {
574                 if (object.equals(value)) {
575                     return true;
576                 }
577             }
578             return false;
579         } else {
580             return value.equals(fieldValue);
581         }
582     }
583 
584 
585     private String compareListRString(Object id) {
586         if (id != null) {
587             if (id instanceof List) {
588                 List<String> idList = (List<String>) id;
589                 return idList.get(0);
590             } else if (id instanceof String) {
591                 String strId = (String) id;
592                 return strId;
593             }
594         }
595         return null;
596     }
597 
598     private void compareObjNAddValue(String id, Object idObj, SolrDocument solrDoc, String identifier) {
599         if (idObj != null) {
600             if (idObj instanceof List) {
601                 List<String> instBibIdList = (List<String>) idObj;
602                 if (!instBibIdList.contains(id)) {
603                     solrDoc.addField(identifier, id);
604                 }
605             } else if (idObj instanceof String) {
606                 String instBibId = (String) idObj;
607                 if (!instBibId.equalsIgnoreCase(id)) {
608                     solrDoc.addField(identifier, id);
609                 }
610             }
611         } else {
612             solrDoc.addField(identifier, id);
613         }
614     }
615 
616     private void workLicense(String docCategory, String docType, String docFormat) {
617         Session session = null;
618         long totalCount = 0;
619         long nodeCount = 0;
620         List<RequestDocument> docs = new ArrayList<RequestDocument>();
621         try {
622             session = RepositoryManager.getRepositoryManager().getSession(ProcessParameters.BULK_DEFAULT_USER,
623                     ProcessParameters.BULK_DEFUALT_ACTION);
624             RequestDocument rd = new RequestDocument();
625             rd.setCategory(docCategory);
626             rd.setType(docType);
627             rd.setFormat(docFormat);
628             DocumentIngester docIngester = new DocumentIngester();
629             Node nodeFormat = docIngester.getStaticFormatNode(rd, session);
630             NodeIterator nodesL1 = nodeFormat.getNodes();
631             List<ReIndexingBatchStatus> batchStatusList = new ArrayList<ReIndexingBatchStatus>();
632             StopWatch loadTimer = new StopWatch();
633             StopWatch batchTimer = new StopWatch();
634             loadTimer.start();
635             RepositoryBrowser repositoryBrowser = new RepositoryBrowser();
636             while (nodesL1.hasNext()) {
637                 Node nodeL1 = nodesL1.nextNode();
638                 NodeIterator nodesFile = nodeL1.getNodes();
639                 nodeCount = nodesFile.getSize();
640                 batchTimer.start();
641                 while (nodesFile.hasNext()) {
642                     if (docs.size() == ProcessParameters.BULK_PROCESSOR_SPLIT_SIZE && !isStop()) {
643                         if (!isStop()) {
644                             ReIndexingBatchStatus reIndexingBatchStatus = indexBeforeParams(loadTimer);
645                             indexDocs(docs, totalCount, nodeCount, batchStatusList, reIndexingBatchStatus);
646                             indexAfterParams(batchTimer, reIndexingBatchStatus, batchStatusList);
647                             resetTimers(batchTimer, loadTimer);
648                             totalCount = 0;
649                             logger.info("Rebuild");
650                         } else {
651                             return;
652                         }
653                     } else {
654 
655                         Node fileNode = nodesFile.nextNode();
656                         String content = null;
657                         if (docFormat.equals(DocFormat.ONIXPL.getCode())) {
658                             content = checkoutManager.getData(fileNode);
659                         } else if (docFormat.equals(DocFormat.PDF.getCode()) || docFormat
660                                 .equals(DocFormat.DOC.getCode())) {
661                             content = checkoutManager
662                                     .checkOutBinary(fileNode.getIdentifier(), ProcessParameters.BULK_DEFAULT_USER,
663                                             ProcessParameters.BULK_DEFUALT_ACTION, docFormat);
664                         }
665                         RequestDocument reqDoc = (RequestDocument) rd.clone();
666                         reqDoc.setId(fileNode.getIdentifier());
667                         reqDoc.setUuid(fileNode.getIdentifier());
668                         Content contentObj = new Content();
669                         contentObj.setContent(content);
670                         reqDoc.setContent(contentObj);
671                         docs.add(reqDoc);
672                         totalCount++;
673                     }
674                 }
675             }
676             if (docs.size() > 0 && !isStop()) {
677                 ReIndexingBatchStatus reIndexingBatchStatus = indexBeforeParams(loadTimer);
678                 indexDocs(docs, totalCount, nodeCount, batchStatusList, reIndexingBatchStatus);
679                 indexAfterParams(batchTimer, reIndexingBatchStatus, batchStatusList);
680             }
681         } catch (Exception e) {
682             logger.error(
683                     "Rebuild Indexes Process(" + docCategory + " : " + docType + " : " + docFormat + ") Processed(" + (
684                             totalCount - docs.size()) + "), Failed @ batch(" + docs.size() + "): Cause: " + e, e);
685         } finally {
686             try {
687                 if (isStop) {
688                     ReIndexingStatus.getInstance().getDocTypeList().setStatus("Stopped");
689                 } else {
690                     ReIndexingStatus.getInstance().getDocTypeList().setStatus("Done");
691                 }
692                 RepositoryManager.getRepositoryManager().logout(session);
693             } catch (OleException e) {
694                 logger.error(e.getMessage(), e);
695             }
696         }
697     }
698 
699     private void resetTimers(StopWatch batchTimer, StopWatch loadTimer) {
700         batchTimer.reset();
701         batchTimer.start();
702         loadTimer.reset();
703         loadTimer.start();
704     }
705 
706     private void indexAfterParams(StopWatch batchTimer, ReIndexingBatchStatus reIndexingBatchStatus,
707                                   List<ReIndexingBatchStatus> batchStatusList) {
708         batchTimer.stop();
709         reIndexingBatchStatus.setBatchTotalTime(batchTimer.toString());
710         ReIndexingStatus.getInstance().getDocTypeList().setReIndBatStatusList(batchStatusList);
711     }
712 
713     private ReIndexingBatchStatus indexBeforeParams(StopWatch loadTimer) {
714         loadTimer.stop();
715         ReIndexingBatchStatus reIndexingBatchStatus = new ReIndexingBatchStatus();
716         reIndexingBatchStatus.setBatchTotalTime(" ");
717         reIndexingBatchStatus.setBatchLoadTime(loadTimer.toString());
718         return reIndexingBatchStatus;
719     }
720 
721     private Node getNodeByUUID(Session newSession, String uuid) throws OleException {
722         return new NodeHandler().getNodeByUUID(newSession, uuid);
723     }
724 }