View Javadoc
1   package org.kuali.ole.docstore.engine.service.index.solr;
2   
3   import org.apache.solr.common.SolrInputDocument;
4   import org.kuali.ole.docstore.common.document.Bib;
5   import org.kuali.ole.docstore.common.document.content.bib.dc.BibDcRecord;
6   import org.kuali.ole.docstore.common.document.content.bib.dc.DCValue;
7   import org.kuali.ole.docstore.common.document.content.bib.dc.xstream.BibDcRecordProcessor;
8   
9   import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
10  import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
11  import org.kuali.ole.docstore.common.document.content.enums.DocType;
12  import org.kuali.ole.docstore.discovery.util.*;
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  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  /**
24   * Created with IntelliJ IDEA.
25   * User: sambasivam
26   * Date: 12/17/13
27   * Time: 4:59 PM
28   * To change this template use File | Settings | File Templates.
29   */
30  public class BibDCIndexer extends BibMarcIndexer {
31  
32  
33      private static BibDCIndexer bibDCIndexer = null;
34  
35      public static BibDCIndexer getInstance() {
36          if(bibDCIndexer == null) {
37              bibDCIndexer = new BibDCIndexer();
38          }
39          return bibDCIndexer;
40      }
41  
42      private BibDcRecordProcessor bibDcRecordProcessor = new BibDcRecordProcessor();
43  
44      private static DocumentMetaData dublinCoreMetaData = new DocumentMetaData();
45  
46      static {
47  
48          List<DocumentCategory> docCategories = DocumentConfig.getInstance().getDocumentCategories();
49  
50          for (DocumentCategory cat : docCategories) {
51  
52              for (DocumentType type : cat.getDocumentTypes()) {
53  
54                  for (DocumentFormat format : type.getDocumentFormats()) {
55  
56                      if (DocCategory.WORK.isEqualTo(cat.getId()) && DocType.BIB.isEqualTo(type.getId())
57                              && DocFormat.DUBLIN_CORE.isEqualTo(format.getId())) {
58  
59                          for (org.kuali.ole.docstore.model.xmlpojo.config.Field field : format.getFields()) {
60  
61                              Field dublinField = new Field();
62                              dublinField.setName(field.getId());
63  
64                              String q = null;
65                              String el = null;
66                              if (field.getMapping().getInclude() != null && field.getMapping().getInclude().length() != 0) {
67                                  if (field.getMapping().getInclude().contains(";")) {
68                                      q = field.getMapping().getInclude().substring(field.getMapping().getInclude().indexOf(';') + 1);
69                                      el = field.getMapping().getInclude().substring(0, field.getMapping().getInclude().indexOf(';'));
70                                  } else {
71                                      el = field.getMapping().getInclude();
72                                  }
73                              } else {
74                                  continue;
75                              }
76  
77                              dublinField.set("element", el);
78  
79                              if (q != null)
80                                  dublinField.set("qualifier", q);
81  
82                              dublinCoreMetaData.getFields().add(dublinField);
83  
84                          }
85  
86                      }
87  
88                  }
89  
90              }
91  
92          }
93      }
94  
95  
96      protected void buildSolrInputDocument(Object object, List<SolrInputDocument> solrInputDocuments) {
97          Bib bib = (Bib) object;
98          BibDcRecord bibMarcRecords = bibDcRecordProcessor.fromXML(bib.getContent());
99          SolrInputDocument solrInputDocument = buildSolrInputDocument(bibMarcRecords);
100 
101         setCommonFields(bib, solrInputDocument);
102 
103         solrInputDocuments.add(solrInputDocument);
104 
105     }
106 
107     public SolrInputDocument buildSolrInputDocument(BibDcRecord record) {
108         SolrInputDocument solrDoc = new SolrInputDocument();
109         solrDoc.setField(DOC_CATEGORY, DocCategory.WORK.getCode());
110         solrDoc.addField(DOC_TYPE, DocType.BIB.getDescription());
111         solrDoc.addField(DOC_FORMAT, DocFormat.DUBLIN_CORE.getCode());
112 
113         for (Field field : dublinCoreMetaData.getFields()) {
114             if (field.getName().equals(PUBLICATIONDATE_FACET)) {
115                 addFieldToSolrDoc(field.getName(),
116                         buildPublicationDateFacetValues(buildFieldValue(field.get("element"), field.get("qualifier"), record)), solrDoc);
117             } else {
118                 addFieldToSolrDoc(field.getName(), buildFieldValue(field.get("element"), field.get("qualifier"), record), solrDoc);
119             }
120         }
121         addFieldToSolrDoc(ALL_TEXT, getAllText(record), solrDoc);
122         return solrDoc;
123 
124     }
125 
126     /**
127      * Method to build Field Value.
128      *
129      * @param element
130      * @param qualifier
131      * @param record
132      * @return
133      */
134     private List buildFieldValue(String element, String qualifier, BibDcRecord record) {
135         List fieldValues = new ArrayList();
136         for (DCValue dcValue : record.getDcValues()) {
137 
138 
139             if (qualifier == null) {
140                 if (element.equals(dcValue.getElement())) {
141                     fieldValues.add(dcValue.getValue());
142                 }
143             } else {
144                 if (element.equals(dcValue.getElement()) && qualifier.equals(dcValue.getQualifier())) {
145                     if (element.equals("date") && qualifier.equals("issued")) {
146                         String pubDate = "";
147                         pubDate = extractPublicationDateWithRegex(dcValue.getValue());
148                         fieldValues.add(pubDate);
149                     } else if ("language".equals(element) && "iso".equals(qualifier)) {
150                         String lang = org.kuali.ole.docstore.discovery.util.Languages.getInstance(org.kuali.ole.docstore.discovery.util.Languages.ISO_639_1_CC)
151                                 .getLanguageDescription(dcValue.getValue().replace('_', '-'));
152                         fieldValues.add(lang == null ? "Undefined" : lang);
153                     } else {
154                         fieldValues.add(dcValue.getValue());
155                     }
156                 }
157             }
158         }
159         return fieldValues;
160     }
161 
162     /**
163      * Method to give all_text field to a given WorkBibDublinRecord.
164      *
165      * @param record
166      * @return
167      */
168     public List getAllText(BibDcRecord record) {
169         List allText = new ArrayList();
170         for (DCValue dcValue : record.getDcValues()) {
171             allText.add(dcValue.getValue());
172         }
173         return allText;
174     }
175 
176     private void addFieldToSolrDoc(String fieldName, Object value, SolrInputDocument solrDoc) {
177 //        if (value instanceof List) {
178 //            for (Object obj : (List<Object>) value) {
179 //                solrDoc.addField(fieldName, obj);
180 //            }
181 //        }
182 //        else {
183 //            solrDoc.addField(fieldName, value);
184 //        }
185 
186         if (value instanceof List) {
187             if (fieldName.toLowerCase().endsWith("_sort")) // Sort fields only the first value to be inserted.
188             {
189                 if (((List) value).size() > 0) {
190                     solrDoc.addField(fieldName, ((List) value).get(0));
191                 }
192             } else if (fieldName.endsWith("_facet")) {
193                 solrDoc.addField(fieldName, getSortString((List) value));
194             } else {
195                 for (Object obj : (List<Object>) value)
196                 // All non Sort and Multi Valued Fields
197                 {
198                     solrDoc.addField(fieldName, obj);
199                 }
200             }
201         } else {
202             if (fieldName.toLowerCase().endsWith("_sort")) {
203                 if (value != null)
204                     solrDoc.addField(fieldName, value.toString());
205             } else if (fieldName.endsWith("_facet")) {
206                 if (value != null)
207                     solrDoc.addField(fieldName, getSortString(value.toString()));
208             } else {
209                 solrDoc.addField(fieldName, value);
210             }
211         }
212     }
213 
214 }