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