001/*
002 * Copyright 2011 The Kuali Foundation.
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.docstore.service;
017
018
019import org.kuali.ole.docstore.indexer.solr.IndexerService;
020import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029/**
030 * Class for Indexing Documents.
031 *
032 * @author Rajesh Chowdary K
033 * @created Feb 16, 2012
034 */
035public class DocumentIndexer {
036
037    private static final Logger LOG = LoggerFactory.getLogger(DocumentIndexer.class);
038
039    /**
040     * Method to index Bib And Linked Instance Documents for a given list.
041     *
042     * @param reqDocs
043     * @throws Exception
044     */
045    public void indexDocuments(List<RequestDocument> reqDocs) throws Exception {
046        String result = ServiceLocator.getIndexerService().indexDocuments(reqDocs);
047        if (!result.startsWith("success")) {
048            throw new Exception(result);
049        }
050    }
051
052    /**
053     * Method to index Documents For Bulk Process.
054     *
055     * @param reqDocs
056     * @throws Exception
057     */
058    public void indexDocumentsForBulk(List<RequestDocument> reqDocs, boolean isCommit) throws Exception {
059        String result = ServiceLocator.getIndexerService().bulkIndexDocuments(reqDocs, isCommit);
060        if (!result.startsWith("success")) {
061            throw new Exception(result);
062        }
063    }
064
065    /**
066     * Method to index Bib And Linked Instance Documents
067     *
068     * @param reqDoc
069     * @throws Exception
070     */
071    public void indexDocument(RequestDocument reqDoc) throws Exception {
072        String result = ServiceLocator.getIndexerService().indexDocument(reqDoc);
073        if (result.startsWith(IndexerService.FAILURE)) {
074            throw new Exception("Indexing failed. Message=" + result);
075        }
076    }
077
078    /**
079     * Method to roll back Indexed Data for a given Request Docuemnts.
080     *
081     * @param requestDocuments
082     */
083    public void rollbackIndexedData(List<RequestDocument> requestDocuments) {
084        try {
085            Map<String, List<String>> uuids = new HashMap<String, List<String>>();
086            for (RequestDocument document : requestDocuments) {
087                for (RequestDocument linkedDoc : document.getLinkedRequestDocuments()) {
088                    if (uuids.get(linkedDoc.getCategory()) == null) {
089                        uuids.put(linkedDoc.getCategory(), new ArrayList<String>());
090                    }
091                    uuids.get(linkedDoc.getCategory()).add(linkedDoc.getUuid());
092                }
093                if (uuids.get(document.getCategory()) == null) {
094                    uuids.put(document.getCategory(), new ArrayList<String>());
095                }
096                uuids.get(document.getCategory()).add(document.getUuid());
097            }
098            for (String category : uuids.keySet()) {
099                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}