1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.docstore.service;
17  
18  
19  import org.kuali.ole.docstore.indexer.solr.IndexerService;
20  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  
30  
31  
32  
33  
34  
35  public class DocumentIndexer {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(DocumentIndexer.class);
38  
39      
40  
41  
42  
43  
44  
45      public void indexDocuments(List<RequestDocument> reqDocs) throws Exception {
46          String result = ServiceLocator.getIndexerService().indexDocuments(reqDocs);
47          if (!result.startsWith("success")) {
48              throw new Exception(result);
49          }
50      }
51  
52      
53  
54  
55  
56  
57  
58      public void indexDocumentsForBulk(List<RequestDocument> reqDocs, boolean isCommit) throws Exception {
59          String result = ServiceLocator.getIndexerService().bulkIndexDocuments(reqDocs, isCommit);
60          if (!result.startsWith("success")) {
61              throw new Exception(result);
62          }
63      }
64  
65      
66  
67  
68  
69  
70  
71      public void indexDocument(RequestDocument reqDoc) throws Exception {
72          String result = ServiceLocator.getIndexerService().indexDocument(reqDoc);
73          if (result.startsWith(IndexerService.FAILURE)) {
74              throw new Exception("Indexing failed. Message=" + result);
75          }
76      }
77  
78      
79  
80  
81  
82  
83      public void rollbackIndexedData(List<RequestDocument> requestDocuments) {
84          try {
85              Map<String, List<String>> uuids = new HashMap<String, List<String>>();
86              for (RequestDocument document : requestDocuments) {
87                  for (RequestDocument linkedDoc : document.getLinkedRequestDocuments()) {
88                      if (uuids.get(linkedDoc.getCategory()) == null) {
89                          uuids.put(linkedDoc.getCategory(), new ArrayList<String>());
90                      }
91                      uuids.get(linkedDoc.getCategory()).add(linkedDoc.getUuid());
92                  }
93                  if (uuids.get(document.getCategory()) == null) {
94                      uuids.put(document.getCategory(), new ArrayList<String>());
95                  }
96                  uuids.get(document.getCategory()).add(document.getUuid());
97              }
98              for (String category : uuids.keySet()) {
99                  ServiceLocator.getIndexerService().deleteDocuments(category, uuids.get(category));
100             }
101         } catch (Exception e) {
102             LOG.info(e.getMessage(),e);
103         }
104     }
105 
106     
107 
108 
109 
110 
111     public void optimizeSolr() throws Exception {
112         ServiceLocator.getDiscoveryAdminService().optimize();
113     }
114 
115     
116 
117 
118 
119 
120     public void optimizeSolr(Boolean waitFlush, Boolean waitSearcher) throws Exception {
121         ServiceLocator.getDiscoveryAdminService().optimize(waitFlush, waitSearcher);
122     }
123 
124 }