View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.documenthandler;
17  
18  import org.apache.solr.common.SolrDocument;
19  import org.kuali.ole.docstore.model.enums.DocCategory;
20  import org.kuali.ole.docstore.model.enums.DocFormat;
21  import org.kuali.ole.docstore.model.enums.DocType;
22  import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
23  import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
24  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
25  import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
26  import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkInstanceOlemlRecordProcessor;
27  import org.kuali.ole.docstore.process.ProcessParameters;
28  import org.kuali.ole.docstore.service.DocumentIngester;
29  import org.kuali.ole.docstore.service.ServiceLocator;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import java.util.ArrayList;
34  import java.util.Collection;
35  import java.util.List;
36  
37  /**
38   * Class to InstanceRequestDocumentResolver.
39   *
40   * @author Rajesh Chowdary K
41   * @created Feb 22, 2012
42   */
43  public class InstanceRequestDocumentResolver {
44      private static Logger logger = LoggerFactory.getLogger(InstanceRequestDocumentResolver.class);
45  
46      private WorkInstanceOlemlRecordProcessor olemlProcessor = new WorkInstanceOlemlRecordProcessor();
47  
48      /**
49       * Method to get Parsed Holdings & Item Documents.
50       *
51       * @param instanceDoc  - Instance document in format OleML
52       * @param linkedBibIds TODO
53       * @return
54       */
55      public List<RequestDocument> getParsedHoldingsNItemDocuments(RequestDocument instanceDoc,
56                                                                   List<String> linkedBibIds) {
57          List<RequestDocument> parsedItemNHoldingsDocuments = new ArrayList<RequestDocument>();
58          if (instanceDoc != null && DocCategory.WORK.isEqualTo(instanceDoc.getCategory())
59                  && DocType.INSTANCE.isEqualTo(instanceDoc.getType()) && DocFormat.OLEML
60                  .isEqualTo(instanceDoc.getFormat())) {
61              String docContent = instanceDoc.getContent().getContent();
62              InstanceCollection instanceCollection = olemlProcessor.fromXML(docContent);
63              instanceDoc.getContent().setContentObject(instanceCollection);
64              // XML conversion
65              if (instanceCollection.getInstance() != null && instanceCollection.getInstance().size() > 0) {
66                  Instance instance = instanceCollection.getInstance().get(0);
67                  resolveLinkingWithBib(instance);
68  
69                  if (instance.getResourceIdentifier().size() == 1) {
70                      if (instance.getResourceIdentifier().get(0) == null || ""
71                              .equals(instance.getResourceIdentifier().get(0))) {
72                          instance.getResourceIdentifier().remove(0);
73                      }
74                  }
75                  if (linkedBibIds != null && linkedBibIds.size() > 0) {
76                      for (String likedBibId : linkedBibIds) {
77                          instance.getResourceIdentifier().add(likedBibId);
78                      }
79                  }
80                  parsedItemNHoldingsDocuments.add(generateInstanceDocument(instance));
81  
82                  OleHoldings oleHolding = instance.getOleHoldings();
83                  RequestDocument rdHol = (RequestDocument) instanceDoc.clone();
84                  Content content = new Content();
85                  content.setContent(olemlProcessor.toXML(oleHolding));
86                  content.setContentObject(oleHolding);
87                  rdHol.setContent(content);
88                  if (oleHolding.getExtension() != null) {
89                      rdHol.setAdditionalAttributes(getFirstAdditionalAttributes(oleHolding.getExtension()));
90                  }
91                  parsedItemNHoldingsDocuments.add(rdHol);
92  
93                  SourceHoldings sourceHoldings = instance.getSourceHoldings();
94                  RequestDocument rdSrcHol = (RequestDocument) instanceDoc.clone();
95                  Content sourceHolContent = new Content();
96                  sourceHolContent.setContent(olemlProcessor.toXML(sourceHoldings));
97                  sourceHolContent.setContentObject(sourceHoldings);
98                  rdSrcHol.setContent(sourceHolContent);
99                  if (sourceHoldings != null && sourceHoldings.getExtension() != null) {
100                     rdSrcHol.setAdditionalAttributes(getFirstAdditionalAttributes(sourceHoldings.getExtension()));
101                 }
102                 parsedItemNHoldingsDocuments.add(rdSrcHol);
103 
104                 for (Item oleItem : instance.getItems().getItem()) {
105                     RequestDocument rdItm = (RequestDocument) instanceDoc.clone();
106                     Content itemContent = new Content();
107                     itemContent.setContent(olemlProcessor.toXML(oleItem));
108                     itemContent.setContentObject(oleItem);
109                     rdItm.setContent(itemContent);
110                     if (oleItem != null && oleItem.getExtension() != null) {
111                         rdItm.setAdditionalAttributes(getFirstAdditionalAttributes(oleItem.getExtension()));
112                     }
113                     parsedItemNHoldingsDocuments.add(rdItm);
114                 }
115             }
116         }
117         return parsedItemNHoldingsDocuments;
118     }
119 
120     private void resolveLinkingWithBib(Instance instance) {
121         instance.getResourceIdentifier().clear();
122         if (ProcessParameters.BULK_INGEST_IS_LINKING_ENABLED) {
123             for (FormerIdentifier frids : instance.getFormerResourceIdentifier()) {
124                 Identifier identifier = frids.getIdentifier();
125                 try {
126                     if (identifier.getIdentifierValue() != null
127                             && identifier.getIdentifierValue().trim().length() != 0) {
128                         List<SolrDocument> solrDocs = ServiceLocator.getIndexerService()
129                                 .getSolrDocument("SystemControlNumber",
130                                         "\"" + identifier
131                                                 .getIdentifierValue()
132                                                 + "\"");
133                         if (solrDocs != null && solrDocs.size() > 0) {
134                             for (SolrDocument solrDoc : solrDocs) {
135                                 if (checkApplicability(identifier.getIdentifierValue(),
136                                         solrDoc.getFieldValue("SystemControlNumber"))) {
137                                     instance.getResourceIdentifier().add(solrDoc.getFieldValue("id").toString());
138                                 }
139                             }
140                         }
141                     }
142                 } catch (Exception e) {
143                     logger.error("Exception (ignored) while linking instance with bib: ", e);
144                 }
145             }
146         }
147     }
148 
149     private boolean checkApplicability(Object value, Object fieldValue) {
150         if (fieldValue instanceof Collection) {
151             for (Object object : (Collection) fieldValue) {
152                 if (object.equals(value)) {
153                     return true;
154                 }
155             }
156             return false;
157         } else {
158             return value.equals(fieldValue);
159         }
160     }
161 
162     private RequestDocument generateInstanceDocument(Instance instance) {
163         InstanceCollection instanceCollection = new InstanceCollection();
164         List<Instance> instances = new ArrayList<Instance>();
165         instanceCollection.setInstance(instances);
166         Instance inst = new Instance();
167         instances.add(inst);
168         inst.setInstanceIdentifier(instance.getInstanceIdentifier());
169         inst.setResourceIdentifier(instance.getResourceIdentifier());
170         inst.setFormerResourceIdentifier(instance.getFormerResourceIdentifier());
171         inst.setExtension(instance.getExtension());
172         String cont = olemlProcessor.toXML(instanceCollection);
173         RequestDocument requestDocument = new RequestDocument();
174         requestDocument.setCategory(DocCategory.WORK.getCode());
175         requestDocument.setType(DocType.INSTANCE.getCode());
176         requestDocument.setFormat(DocFormat.OLEML.getCode());
177         requestDocument.setContent(new Content());
178         requestDocument.getContent().setContent(cont);
179         requestDocument.getContent().setContentObject(inst);
180         return requestDocument;
181     }
182 
183     /**
184      * Method to get Additional Attributes
185      *
186      * @param extension
187      * @return
188      */
189     private AdditionalAttributes getFirstAdditionalAttributes(Extension extension) {
190         if (extension != null && extension.getContent() != null) {
191             for (Object obj : extension.getContent()) {
192                 if (obj instanceof AdditionalAttributes) {
193                     return (AdditionalAttributes) obj;
194                 }
195             }
196         }
197         return null;
198     }
199 }