View Javadoc
1   package org.kuali.ole.docstore.engine.service.index.solr;
2   
3   import org.apache.commons.jxpath.JXPathContext;
4   import org.apache.solr.common.SolrInputDocument;
5   import org.kuali.ole.docstore.common.document.License;
6   import org.kuali.ole.docstore.common.document.Licenses;
7   import org.kuali.ole.docstore.common.document.content.license.onixpl.ONIXPublicationsLicenseMessage;
8   import org.kuali.ole.docstore.common.document.content.license.onixpl.jaxb.LicenseOnixplRecordConverter;
9   import org.kuali.ole.docstore.indexer.solr.DocumentLocalId;
10  import org.kuali.ole.docstore.model.enums.DocCategory;
11  import org.kuali.ole.docstore.model.enums.DocFormat;
12  import org.kuali.ole.docstore.model.enums.DocType;
13  import org.kuali.ole.docstore.model.xmlpojo.config.DocumentCategory;
14  import org.kuali.ole.docstore.model.xmlpojo.config.DocumentConfig;
15  import org.kuali.ole.docstore.model.xmlpojo.config.DocumentFormat;
16  import org.kuali.ole.docstore.model.xmlpojo.config.DocumentType;
17  import org.kuali.ole.docstore.model.xmlpojo.metadata.DocumentMetaData;
18  import org.kuali.ole.docstore.model.xmlpojo.metadata.Field;
19  import org.kuali.ole.docstore.utility.XMLUtility;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  /**
28   * Created with IntelliJ IDEA.
29   * User: sambasivam
30   * Date: 2/25/14
31   * Time: 6:40 PM
32   * To change this template use File | Settings | File Templates.
33   */
34  public class LicenseOnixplIndexer extends DocstoreSolrIndexService {
35  
36      private static DocumentMetaData onixPlMetaData = new DocumentMetaData();
37      StringBuffer allText = new StringBuffer();
38      XMLUtility xmlUtility = new XMLUtility();
39      private static LicenseOnixplIndexer licenseOnixplIndexer;
40  
41      public static LicenseOnixplIndexer getInstance() {
42          if(licenseOnixplIndexer == null) {
43              licenseOnixplIndexer = new LicenseOnixplIndexer();
44          }
45          return licenseOnixplIndexer;
46      }
47      static {
48  
49          List<DocumentCategory> docCategories = DocumentConfig.getInstance().getDocumentCategories();
50  
51          for (DocumentCategory cat : docCategories) {
52  
53              for (DocumentType type : cat.getDocumentTypes()) {
54  
55                  for (DocumentFormat format : type.getDocumentFormats()) {
56  
57                      if (DocCategory.WORK.isEqualTo(cat.getId()) && DocType.LICENSE.isEqualTo(type.getId())
58                              && DocFormat.ONIXPL.isEqualTo(format.getId())) {
59  
60                          for (org.kuali.ole.docstore.model.xmlpojo.config.Field field : format.getFields()) {
61  
62                              Field onixPlField = new Field();
63                              onixPlField.setName(field.getId());
64                              if (field.getMapping().getInclude() != null)
65                                  onixPlField.set("xpath", field.getMapping().getInclude());
66                              onixPlMetaData.getFields().add(onixPlField);
67  
68                          }
69  
70                      }
71  
72                  }
73  
74              }
75  
76          }
77  
78      }
79  
80  
81      protected void buildSolrInputDocument(Object object, List<SolrInputDocument> solrInputDocuments) {
82          License license = (License) object;
83          LicenseOnixplRecordConverter licenseOnixplRecordConverter = new LicenseOnixplRecordConverter();
84  
85          String content = license.getContent();
86          ONIXPublicationsLicenseMessage onixPublicationsLicenseMessage = licenseOnixplRecordConverter.unmarshal(content);
87  
88          SolrInputDocument solrDoc = new SolrInputDocument();
89          solrDoc.addField(DOC_TYPE, DocType.LICENSE.getDescription());
90          solrDoc.addField(DOC_FORMAT, DocFormat.ONIXPL.getCode());
91          solrDoc.setField(DOC_CATEGORY, DocCategory.WORK.getCode());
92          JXPathContext message = JXPathContext.newContext(onixPublicationsLicenseMessage);
93          for (Field field : onixPlMetaData.getFields()) {
94              String xp = field.get("xpath");
95              if (xp != null && xp.trim().length() != 0) {
96                  Iterator values = message.iterate(xp);
97                  while (values.hasNext()) {
98                      Object value = values.next();
99                      solrDoc.addField(field.getName(), value);
100                 }
101             }
102         }
103         //TODO:set additional attributes
104 
105         if (content != null) {
106             allText.append(xmlUtility.getAllContentText(content));
107         }
108         solrDoc.addField(ALL_TEXT, allText.toString());
109         solrDoc.addField("dateEntered", new Date());
110         solrDoc.addField("createdBy", license.getCreatedBy());
111         solrDoc.setField("id", license.getId());
112         solrDoc.setField("uniqueId", license.getId());
113         solrDoc.setField("LocalId_search", DocumentLocalId.getDocumentId(license.getId()));
114         solrDoc.setField("LocalId_display", DocumentLocalId.getDocumentIdDisplay(license.getId()));
115 
116 
117         solrInputDocuments.add(solrDoc);
118 
119     }
120 
121 
122     public void createTrees(Object object) {
123         List<SolrInputDocument> solrInputDocuments = new ArrayList<>();
124         Licenses licenses = (Licenses) object;
125         for(License license : licenses.getLicenses()) {
126             buildSolrInputDocument(license, solrInputDocuments);
127         }
128 
129         indexSolrDocuments(solrInputDocuments, true);
130     }
131 
132 
133     protected void updateRecordInSolr(Object object, List<SolrInputDocument> solrInputDocuments) {
134         License license = (License) object;
135         this.buildSolrInputDocument(object, solrInputDocuments);
136         solrInputDocuments.get(0).addField("updatedBy", license.getUpdatedBy());
137         solrInputDocuments.get(0).addField("dateUpdated", new Date());
138 
139     }
140 
141 }