View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Class for Indexing Documents.
31   *
32   * @author Rajesh Chowdary K
33   * @created Feb 16, 2012
34   */
35  public class DocumentIndexer {
36  
37      private static final Logger LOG = LoggerFactory.getLogger(DocumentIndexer.class);
38  
39      /**
40       * Method to index Bib And Linked Instance Documents for a given list.
41       *
42       * @param reqDocs
43       * @throws Exception
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       * Method to index Documents For Bulk Process.
54       *
55       * @param reqDocs
56       * @throws Exception
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       * Method to index Bib And Linked Instance Documents
67       *
68       * @param reqDoc
69       * @throws Exception
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       * Method to roll back Indexed Data for a given Request Docuemnts.
80       *
81       * @param requestDocuments
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      * Method to optimizeSolr
108      *
109      * @throws Exception
110      */
111     public void optimizeSolr() throws Exception {
112         ServiceLocator.getDiscoveryAdminService().optimize();
113     }
114 
115     /**
116      * Method to optimizeSolr
117      *
118      * @throws Exception
119      */
120     public void optimizeSolr(Boolean waitFlush, Boolean waitSearcher) throws Exception {
121         ServiceLocator.getDiscoveryAdminService().optimize(waitFlush, waitSearcher);
122     }
123 
124 }