001package org.kuali.ole.docstore.indexer.solr;
002
003import org.apache.commons.lang.time.StopWatch;
004import org.apache.solr.common.SolrInputDocument;
005import org.kuali.ole.docstore.discovery.solr.work.instance.oleml.WorkInstanceOlemlDocBuilder;
006import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
007import org.kuali.ole.docstore.utility.BatchIngestStatistics;
008import org.kuali.ole.docstore.utility.BulkIngestStatistics;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012import java.util.ArrayList;
013import java.util.List;
014
015/**
016 * Created with IntelliJ IDEA.
017 * User: mjagan
018 * Date: 7/2/13
019 * Time: 7:05 PM
020 * To change this template use File | Settings | File Templates.
021 */
022public class WorkHoldingsDocumentIndexer extends AbstractDocumentIndexer {
023
024    private Logger logger = LoggerFactory.getLogger(this.getClass());
025    private static WorkHoldingsDocumentIndexer ourInstance = null;
026
027    public static WorkHoldingsDocumentIndexer getInstance() {
028        if (null == ourInstance) {
029            ourInstance = new WorkHoldingsDocumentIndexer();
030        }
031        return ourInstance;
032    }
033
034    @Override
035    public String indexDocuments(List<RequestDocument> requestDocuments, boolean commit) {
036        BatchIngestStatistics batchStatistics = BulkIngestStatistics.getInstance().getCurrentBatch();
037
038        String result = null;
039        StopWatch timer = new StopWatch();
040        StopWatch xmlToObjTime = new StopWatch();
041        xmlToObjTime.start();
042        xmlToObjTime.suspend();
043        timer.start();
044        List<SolrInputDocument> solrInputDocuments = new ArrayList<SolrInputDocument>();
045        if (requestDocuments != null && requestDocuments.size() > 0) {
046            StopWatch buildSolrInputDocTime = new StopWatch();
047            StopWatch xmlToPojoTimer = new StopWatch();
048            buildSolrInputDocTime.start();
049            buildSolrInputDocTime.suspend();
050            xmlToPojoTimer.start();
051            xmlToPojoTimer.suspend();
052            try {
053                for (RequestDocument requestDocument : requestDocuments) {
054                    new WorkInstanceOlemlDocBuilder()
055                            .buildSolrInputDocument(requestDocument, solrInputDocuments);
056                    assignUUIDs(solrInputDocuments, null);
057                }
058            } catch (Exception e1) {
059                result = buildFailureMsg(null, "Indexing failed. " + e1.getMessage());
060                logger.error(result, e1);
061            }
062            timer.stop();
063            if ((null == solrInputDocuments) || (solrInputDocuments.isEmpty())) {
064                result = buildFailureMsg(null, "No valid documents found in input.");
065                return result;
066            }
067            int numDocs = solrInputDocuments.size();
068            batchStatistics.setTimeToConvertXmlToPojo(xmlToPojoTimer.getTime());
069            batchStatistics.setTimeToConvertToSolrInputDocs(buildSolrInputDocTime.getTime());
070            logger.info("Conversion to Solr docs- Num:" + numDocs + ": Time taken:" + timer.toString());
071            result = indexSolrDocuments(solrInputDocuments, commit);
072        }
073        return result;
074    }
075}