001package org.kuali.ole.web;
002
003import org.apache.commons.lang.StringUtils;
004import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
005import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
006import org.kuali.ole.docstore.common.document.content.enums.DocType;
007import org.kuali.ole.docstore.engine.service.index.solr.BibConstants;
008import org.kuali.ole.docstore.metrics.reindex.ReIndexingStatus;
009import org.kuali.ole.docstore.process.RebuildIndexesHandler;
010import org.kuali.ole.logger.DocStoreLogger;
011
012import javax.servlet.ServletException;
013import javax.servlet.http.HttpServlet;
014import javax.servlet.http.HttpServletRequest;
015import javax.servlet.http.HttpServletResponse;
016import java.io.IOException;
017import java.io.PrintWriter;
018
019/**
020 * Created by IntelliJ IDEA.
021 * User: pvsubrah
022 * Date: 11/12/11
023 * Time: 3:41 PM
024 * To change this template use File | Settings | File Templates.
025 */
026public class RebuildIndexServlet extends HttpServlet {
027
028    private static final long serialVersionUID = 1L;
029    DocStoreLogger docStoreLogger = new DocStoreLogger(getClass().getName());
030
031    @Override
032    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
033        doPost(req, resp);
034    }
035
036    @Override
037    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
038        try {
039
040            String docCategory = DocCategory.WORK.getCode();
041            String docType = DocType.BIB.getCode();
042            String docFormat = DocFormat.MARC.getCode();
043            String action = req.getParameter("action");
044            String batchSizeReq = req.getParameter("batchSize");
045            String startIndexReq = req.getParameter("startIndex");
046            String endIndexReq = req.getParameter("endIndex");
047            String updateDate=null;
048            if(req.getParameter("updateDate")!=null){
049                updateDate=req.getParameter("updateDate").trim();
050            }
051            int batchSize = 0;
052            int startIndex = 0;
053            int endIndex = 0;
054            if(StringUtils.isNotEmpty(batchSizeReq)) {
055                batchSize = Integer.parseInt(batchSizeReq);
056            }
057            if(StringUtils.isNotEmpty(startIndexReq)) {
058                startIndex = Integer.parseInt(startIndexReq);
059            }
060            if(StringUtils.isNotEmpty(endIndexReq)) {
061                endIndex = Integer.parseInt(endIndexReq);
062            }
063            if(startIndex > endIndex) {
064                String result = "Please provide valid input " +  startIndex + " > " + endIndex;
065                outputMessage(resp, result);
066            }
067            RebuildIndexesHandler rebuildIndexesHandler = RebuildIndexesHandler.getInstance();
068
069            if (action.equalsIgnoreCase("start")) {
070                String result = rebuildIndexesHandler.startProcess(docCategory, docType, docFormat, batchSize, startIndex, endIndex,updateDate);
071                outputMessage(resp, result);
072            } else if (action.equalsIgnoreCase("stop")) {
073                String result = rebuildIndexesHandler.stopProcess();
074                outputMessage(resp, result);
075            } else if (action.equalsIgnoreCase("status")) {
076//                ReIndexingStatus reIndexingStatus = ReIndexingStatus.getInstance();
077//                String result = reIndexingStatus.getJsonString();
078                String result = rebuildIndexesHandler.showStatus();
079                outputMessage(resp, result);
080            } else if (action.equalsIgnoreCase("bibStatus")) {
081                String result = rebuildIndexesHandler.showBibStatus();
082                outputMessage(resp, result);
083            } else if (action.equalsIgnoreCase("Clear Status")) {
084                ReIndexingStatus reIndexingStatus = ReIndexingStatus.getInstance();
085                reIndexingStatus.reset();
086            } else if (action.equalsIgnoreCase("store")) {
087                String result = rebuildIndexesHandler.storeBibInfo(batchSize);
088                outputMessage(resp, result);
089            } else {
090                String result = "Invalid action :" + action;
091                outputMessage(resp, result);
092            }
093        } catch (Exception e) {
094            docStoreLogger.log("Error during rebuilding of the indexes from documentstore", e);
095        }
096    }
097
098    private void outputMessage(HttpServletResponse resp, String s) throws IOException {
099        PrintWriter out = resp.getWriter();
100        out.println(s);
101        out.flush();
102        out.close();
103    }
104}