View Javadoc
1   package org.kuali.ole.docstore.indexer.solr;
2   
3   import org.apache.commons.lang.time.StopWatch;
4   import org.apache.solr.client.solrj.SolrQuery;
5   import org.apache.solr.client.solrj.SolrServer;
6   import org.apache.solr.client.solrj.response.QueryResponse;
7   import org.apache.solr.client.solrj.response.UpdateResponse;
8   import org.apache.solr.common.SolrDocument;
9   import org.apache.solr.common.SolrInputDocument;
10  import org.kuali.ole.docstore.discovery.service.SolrServerManager;
11  import org.kuali.ole.docstore.discovery.solr.work.bib.marc.WorkBibMarcDocBuilder;
12  import org.kuali.ole.docstore.discovery.solr.work.einstance.WorkEInstanceOlemlDocBuilder;
13  import org.kuali.ole.docstore.discovery.solr.work.instance.oleml.WorkInstanceOlemlDocBuilder;
14  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  /**
22   * Created with IntelliJ IDEA.
23   * User: sambasivam
24   * Date: 7/18/13
25   * Time: 1:46 PM
26   * To change this template use File | Settings | File Templates.
27   */
28  public class WorkEInstanceDocumentIndexer extends AbstractDocumentIndexer {
29      private Logger logger = LoggerFactory.getLogger(this.getClass());
30      private static WorkEInstanceDocumentIndexer ourInstance = null;
31  
32      public static WorkEInstanceDocumentIndexer getInstance() {
33          if (null == ourInstance) {
34              ourInstance = new WorkEInstanceDocumentIndexer();
35          }
36          return ourInstance;
37      }
38  
39      @Override
40      public String indexDocuments(List<RequestDocument> requestDocuments, boolean commit) {
41  
42          String result = null;
43          StopWatch timer = new StopWatch();
44          StopWatch xmlToObjTime = new StopWatch();
45          xmlToObjTime.start();
46          xmlToObjTime.suspend();
47          timer.start();
48          List<SolrInputDocument> solrInputDocuments = new ArrayList<SolrInputDocument>();
49          if (requestDocuments != null && requestDocuments.size() > 0) {
50              StopWatch buildSolrInputDocTime = new StopWatch();
51              StopWatch xmlToPojoTimer = new StopWatch();
52              buildSolrInputDocTime.start();
53              buildSolrInputDocTime.suspend();
54              xmlToPojoTimer.start();
55              xmlToPojoTimer.suspend();
56              try {
57                  for (RequestDocument requestDocument : requestDocuments) {
58                      new WorkEInstanceOlemlDocBuilder()
59                              .buildSolrInputDocument(requestDocument, solrInputDocuments);
60                      assignUUIDs(solrInputDocuments, null);
61                  }
62              } catch (Exception e1) {
63                  result = buildFailureMsg(null, "Indexing failed. " + e1.getMessage());
64                  logger.error(result, e1);
65              }
66              timer.stop();
67              if ((null == solrInputDocuments) || (solrInputDocuments.isEmpty())) {
68                  result = buildFailureMsg(null, "No valid documents found in input.");
69                  return result;
70              }
71              int numDocs = solrInputDocuments.size();
72  //            batchStatistics.setTimeToConvertXmlToPojo(xmlToPojoTimer.getTime());
73  //            batchStatistics.setTimeToConvertToSolrInputDocs(buildSolrInputDocTime.getTime());
74              logger.info("Conversion to Solr docs- Num:" + numDocs + ": Time taken:" + timer.toString());
75              result = indexSolrDocuments(solrInputDocuments, commit);
76          }
77          return result;
78      }
79  
80      @Override
81      public String delete(RequestDocument requestDocument) throws Exception {
82          SolrServer server = SolrServerManager.getInstance().getSolrServer();
83          List<SolrInputDocument> solrInputDocumentList = new ArrayList<SolrInputDocument>();
84          SolrQuery solrQuery = new SolrQuery();
85          String query = "id:" + requestDocument.getUuid();
86          solrQuery.setQuery(query);
87          QueryResponse response = server.query(solrQuery);
88          List<SolrDocument> solrDocumentList = response.getResults();
89          List<String> deleteIdList = new ArrayList<String>();
90          for (SolrDocument solrDocument : solrDocumentList) {
91              Object holId = solrDocument.getFieldValue("holdingsIdentifier");
92              if (holId != null) {
93                  if (holId instanceof List) {
94                      List<String> holIdList = (List<String>) solrDocument.getFieldValue("holdingsIdentifier");
95                      deleteIdList.addAll(holIdList);
96                  } else if (holId instanceof String) {
97                      String holdId = (String) holId;
98                      deleteIdList.add(holdId);
99                  }
100             }
101         }
102         deleteIdList.add(requestDocument.getUuid());
103         deleteDocumentsByUUIDList(deleteIdList, "eInstance", true);
104         query = "(instanceIdentifier:" + requestDocument.getUuid() + ") AND " + "(DocType:bibliographic)";
105         solrQuery.setQuery(query);
106         response = server.query(solrQuery);
107         solrDocumentList = response.getResults();
108         for (SolrDocument bibSolrDocument : solrDocumentList) {
109             List<String> instanceIdentifierList = new ArrayList<String>();
110             Object instanceIdentifier = bibSolrDocument.getFieldValue("instanceIdentifier");
111             if (instanceIdentifier instanceof List) {
112                 instanceIdentifierList = (List<String>) bibSolrDocument.getFieldValue("instanceIdentifier");
113                 if (instanceIdentifierList.contains(requestDocument.getUuid())) {
114                     instanceIdentifierList.remove(requestDocument.getUuid());
115                     bibSolrDocument.setField("instanceIdentifier", instanceIdentifierList);
116                 }
117             } else if (instanceIdentifier instanceof String) {
118                 String instId = (String) instanceIdentifier;
119                 if (instId.equalsIgnoreCase(requestDocument.getUuid())) {
120                     bibSolrDocument.removeFields("instanceIdentifier");
121                 }
122             }
123             solrInputDocumentList.add(new WorkBibMarcDocBuilder().buildSolrInputDocFromSolrDoc(bibSolrDocument));
124         }
125         String result = indexSolrDocuments(solrInputDocumentList, true);
126         if (!result.contains(SUCCESS)) {
127             return result;
128         }
129         return result;
130     }
131 }