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  import org.kuali.ole.docstore.discovery.service.IndexerService;
19  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  /**
29   * Class for Indexing Documents.
30   *
31   * @author Rajesh Chowdary K
32   * @created Feb 16, 2012
33   */
34  public class DocumentIndexer {
35  
36      private static final Logger LOG = LoggerFactory.getLogger(DocumentIndexer.class);
37  
38      /**
39       * Method to index Bib And Linked Instance Documents for a given list.
40       *
41       * @param reqDocs
42       * @throws Exception
43       */
44      public void indexDocuments(List<RequestDocument> reqDocs) throws Exception {
45          String result = ServiceLocator.getIndexerService().indexDocuments(reqDocs);
46          if (!result.startsWith("success")) {
47              throw new Exception(result);
48          }
49      }
50  
51      /**
52       * Method to index Documents For Bulk Process.
53       *
54       * @param reqDocs
55       * @throws Exception
56       */
57      public void indexDocumentsForBulk(List<RequestDocument> reqDocs, boolean isCommit) throws Exception {
58          String result = ServiceLocator.getIndexerService().bulkIndexDocuments(reqDocs, isCommit);
59          if (!result.startsWith("success")) {
60              throw new Exception(result);
61          }
62      }
63  
64      /**
65       * Method to index Bib And Linked Instance Documents
66       *
67       * @param reqDoc
68       * @throws Exception
69       */
70      public void indexDocument(RequestDocument reqDoc) throws Exception {
71          String result = ServiceLocator.getIndexerService().indexDocument(reqDoc);
72          if (result.startsWith(IndexerService.FAILURE)) {
73              throw new Exception("Indexing failed. Message=" + result);
74          }
75      }
76  
77      /**
78       * Method to roll back Indexed Data for a given Request Docuemnts.
79       *
80       * @param requestDocuments
81       */
82      public void rollbackIndexedData(List<RequestDocument> requestDocuments) {
83          try {
84              Map<String, List<String>> uuids = new HashMap<String, List<String>>();
85              for (RequestDocument document : requestDocuments) {
86                  for (RequestDocument linkedDoc : document.getLinkedRequestDocuments()) {
87                      if (uuids.get(linkedDoc.getCategory()) == null) {
88                          uuids.put(linkedDoc.getCategory(), new ArrayList<String>());
89                      }
90                      uuids.get(linkedDoc.getCategory()).add(linkedDoc.getUuid());
91                  }
92                  if (uuids.get(document.getCategory()) == null) {
93                      uuids.put(document.getCategory(), new ArrayList<String>());
94                  }
95                  uuids.get(document.getCategory()).add(document.getUuid());
96              }
97              for (String category : uuids.keySet()) {
98                  ServiceLocator.getIndexerService().deleteDocuments(category, uuids.get(category));
99              }
100         }
101         catch (Exception e) {
102             LOG.info(e.getMessage());
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 }