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.unqualified.BibDublinUnQualifiedRecord;
6   import org.kuali.ole.docstore.common.document.content.bib.dc.unqualified.OaiDcDoc;
7   import org.kuali.ole.docstore.common.document.content.bib.dc.unqualified.Record;
8   import org.kuali.ole.docstore.common.document.content.bib.dc.unqualified.Tag;
9   import org.kuali.ole.docstore.common.document.content.bib.dc.unqualified.xstream.BibDublinUnQualifiedRecordProcessor;
10  import org.kuali.ole.docstore.common.document.content.enums.DocCategory;
11  import org.kuali.ole.docstore.common.document.content.enums.DocFormat;
12  import org.kuali.ole.docstore.common.document.content.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  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  /**
24   * Created with IntelliJ IDEA.
25   * User: sambasivam
26   * Date: 1/21/14
27   * Time: 2:46 PM
28   * To change this template use File | Settings | File Templates.
29   */
30  public class BibDcUnqualifiedIndexer extends BibMarcIndexer {
31  
32      private static BibDcUnqualifiedIndexer bibDcUnqualifiedIndexer = null;
33  
34      public static BibDcUnqualifiedIndexer getInstance() {
35          if(bibDcUnqualifiedIndexer == null) {
36              bibDcUnqualifiedIndexer = new BibDcUnqualifiedIndexer();
37          }
38          return bibDcUnqualifiedIndexer;
39      }
40      private static DocumentMetaData dublinUnqMetaData = new DocumentMetaData();
41  
42      static {
43  
44          List<DocumentCategory> docCategories = DocumentConfig.getInstance().getDocumentCategories();
45  
46          for (DocumentCategory cat : docCategories) {
47  
48              for (DocumentType type : cat.getDocumentTypes()) {
49  
50                  for (DocumentFormat format : type.getDocumentFormats()) {
51  
52                      if (DocCategory.WORK.isEqualTo(cat.getId()) && DocType.BIB.isEqualTo(type.getId())
53                              && DocFormat.DUBLIN_UNQUALIFIED.isEqualTo(format.getId())) {
54  
55                          for (org.kuali.ole.docstore.model.xmlpojo.config.Field field : format.getFields()) {
56  
57                              Field dublinField = new Field();
58                              dublinField.setName(field.getId());
59  
60                              if (field.getMapping().getInclude() != null)
61                                  dublinField.set("tag", field.getMapping().getInclude());
62  
63                              dublinUnqMetaData.getFields().add(dublinField);
64  
65                          }
66  
67                      }
68  
69                  }
70  
71              }
72  
73          }
74  
75          // dublinUnqMetaData = DocumentsMetaData.getInstance().getDocumentMetaData(DocCategory.WORK.getCode(), DocType.BIB.getCode(),
76          // DocFormat.DUBLIN_UNQUALIFIED.getCode());
77      }
78  
79  
80      private BibDublinUnQualifiedRecordProcessor bibDublinUnQualifiedRecordProcessor = new BibDublinUnQualifiedRecordProcessor();
81      protected void buildSolrInputDocument(Object object, List<SolrInputDocument> solrInputDocuments) {
82          Bib bib = (Bib) object;
83  
84          BibDublinUnQualifiedRecord bibDublinUnQualifiedRecord = bibDublinUnQualifiedRecordProcessor.fromXML(bib.getContent());
85  
86         List<SolrInputDocument> solrInputDocumentList = buildSolrInputDocuments(bibDublinUnQualifiedRecord);
87  
88          for(SolrInputDocument solrInputDocument : solrInputDocumentList) {
89              setCommonFields(bib, solrInputDocument);
90          }
91  
92          solrInputDocuments.addAll(solrInputDocumentList);
93  
94      }
95  
96      /**
97       * Method to build SolrInputDocument for a given WorkBibDublinRecord.
98       *
99       * @param doc
100      * @return
101      */
102     public SolrInputDocument buildSolrInputDocument(OaiDcDoc doc) {
103         SolrInputDocument solrDoc = new SolrInputDocument();
104         solrDoc.addField(DOC_TYPE, DocType.BIB.getCode());
105         solrDoc.addField(DOC_FORMAT, DocFormat.DUBLIN_UNQUALIFIED.getCode());
106 
107         for (Field field : dublinUnqMetaData.getFields()) {
108 
109             if (field.getName().equals(PUBLICATIONDATE_FACET)) {
110                 addFieldToSolrDoc(field.getName(), buildPublicationDateFacetValues(buildFieldValue(field.getName(), field.get("tag"), doc)), solrDoc);
111             } else {
112                 addFieldToSolrDoc(field.getName(), buildFieldValue(field.getName(), field.get("tag"), doc), solrDoc);
113             }
114 
115         }
116 
117         addFieldToSolrDoc(ALL_TEXT, getAllText(doc), solrDoc);
118         return solrDoc;
119     }
120 
121 
122     /**
123      * Method to build SolrInputDocuments for a given WorkBibDublinRecord.
124      *
125      * @param
126      * @return
127      */
128     public List<SolrInputDocument> buildSolrInputDocuments(BibDublinUnQualifiedRecord unqRecord) {
129         List<SolrInputDocument> solrDocs = new ArrayList<SolrInputDocument>();
130         if (unqRecord.getListRecords() != null) {
131             for (Record rec : unqRecord.getListRecords().getRecords()) {
132                 if (rec.getMetadata() != null) {
133                     for (OaiDcDoc doc : rec.getMetadata().getOaiDcDocs()) {
134                         solrDocs.add(buildSolrInputDocument(doc));
135                     }
136                 }
137             }
138         }
139         return solrDocs;
140 
141     }
142 
143     private void addFieldToSolrDoc(String fieldName, Object value, SolrInputDocument solrDoc) {
144 //        if (value instanceof List) {
145 //            for (Object obj : (List<Object>) value) {
146 //                solrDoc.addField(fieldName, obj);
147 //            }
148 //        } else {
149 //            solrDoc.addField(fieldName, value);
150 //        }
151         if (value instanceof List) {
152             if (fieldName.toLowerCase().endsWith("_sort")) // Sort fields only the first value to be inserted.
153             {
154                 solrDoc.addField(fieldName, ((List) value).get(0));
155             } else if (fieldName.endsWith("_facet")) {
156                 solrDoc.addField(fieldName, getSortString((List) value));
157             } else {
158                 for (Object obj : (List<Object>) value)
159                 // All non Sort and Multi Valued Fields
160                 {
161                     solrDoc.addField(fieldName, obj);
162                 }
163             }
164         } else {
165             if (fieldName.endsWith("_sort")) {
166                 if (value != null) {
167                     solrDoc.addField(fieldName, value.toString());
168                 }
169             } else if (fieldName.endsWith("_facet")) {
170                 if (value != null) {
171                     solrDoc.addField(fieldName, getSortString(value.toString()));
172                 }
173             } else {
174                 solrDoc.addField(fieldName, value);
175             }
176         }
177     }
178 
179     /**
180      * Method to build Field Value.
181      *
182      * @param name    - Name of the Field defined in {@link }
183      * @param record
184      * @param tagName
185      * @return
186      */
187     private List<String> buildFieldValue(String name, String tagName, OaiDcDoc record) {
188 //        String tagName = null;
189         List<String> fieldValues = new ArrayList<String>();
190 
191 //        for (Field field : dublinUnqMetaData.getFields())
192 //            if (field.getName().endsWith(name)) {
193 //                tagName = field.get("tag");
194 //                break;
195 //            }
196 
197         for (Tag tag : record.get(tagName))
198             if (name.startsWith("PublicationDate")) {
199                 String pubDate = extractPublicationDateWithRegex(tag.getValue());
200                 fieldValues.add(pubDate);
201             } else if (tagName.equals(OaiDcDoc.LANGUAGE)) {
202                 String lang = Languages.getInstance(Languages.ISO_639_3).getLanguageDescription(tag.getValue());
203                 fieldValues.add(lang == null ? "Undefined" : lang);
204             } else if (name.startsWith("ISBN")) {
205                 if (tag.getValue().toUpperCase().contains("(ISBN)"))
206                     fieldValues.add(tag.getValue().toUpperCase().replace("(ISBN)", ""));
207             } else if (name.startsWith("ISSN")) {
208                 if (tag.getValue().toUpperCase().contains("(ISSN)"))
209                     fieldValues.add(tag.getValue().toUpperCase().replace("(ISSN)", ""));
210             } else {
211                 fieldValues.add(tag.getValue());
212             }
213 
214         if (fieldValues.size() > 0) {
215             return fieldValues;
216         } else {
217             return null;
218         }
219     }
220 
221     /**
222      * Method to give all_text field to a given WorkBibDublinRecord.
223      *
224      * @param record
225      * @return
226      */
227     public List getAllText(OaiDcDoc record) {
228         List allTxt = new ArrayList();
229         for (Tag tag : record.getAllTags()) {
230             allTxt.add(tag.getValue());
231         }
232         return allTxt;
233     }
234 
235     protected void updateRecordInSolr(Object object, List<SolrInputDocument> solrInputDocuments) {
236 
237     }
238 
239 }