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.common.document.content.instance.*;
20  import org.kuali.ole.docstore.common.document.content.instance.xstream.InstanceOlemlRecordProcessor;
21  import org.kuali.ole.docstore.model.enums.DocCategory;
22  import org.kuali.ole.docstore.model.enums.DocFormat;
23  import org.kuali.ole.docstore.model.enums.DocType;
24  import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
25  import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
26  import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
27  import org.kuali.ole.docstore.process.ProcessParameters;
28  import org.kuali.ole.docstore.service.ServiceLocator;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.List;
35  
36  /**
37   * Class to InstanceRequestDocumentResolver.
38   *
39   * @author Rajesh Chowdary K
40   * @created Feb 22, 2012
41   */
42  public class InstanceRequestDocumentResolver {
43      private static Logger logger = LoggerFactory.getLogger(InstanceRequestDocumentResolver.class);
44  
45      private InstanceOlemlRecordProcessor olemlProcessor = new InstanceOlemlRecordProcessor();
46  
47      /**
48       * Method to get Parsed Holdings & Item Documents.
49       *
50       * @param instanceDoc  - Instance document in format OleML
51       * @param linkedBibIds TODO
52       * @return
53       */
54      public List<RequestDocument> getParsedHoldingsNItemDocuments(RequestDocument instanceDoc,
55                                                                   List<String> linkedBibIds) {
56          List<RequestDocument> parsedItemNHoldingsDocuments = new ArrayList<RequestDocument>();
57          if (instanceDoc != null && DocCategory.WORK.isEqualTo(instanceDoc.getCategory())
58                  && DocType.INSTANCE.isEqualTo(instanceDoc.getType()) && DocFormat.OLEML
59                  .isEqualTo(instanceDoc.getFormat())) {
60              String docContent = instanceDoc.getContent().getContent();
61              InstanceCollection instanceCollection = olemlProcessor.fromXML(docContent);
62              instanceDoc.getContent().setContentObject(instanceCollection);
63              // XML conversion
64              if (instanceCollection.getInstance() != null && instanceCollection.getInstance().size() > 0) {
65                  Instance instance = instanceCollection.getInstance().get(0);
66                  resolveLinkingWithBib(instance);
67  
68                  if (instance.getResourceIdentifier().size() == 1) {
69                      if (instance.getResourceIdentifier().get(0) == null || ""
70                              .equals(instance.getResourceIdentifier().get(0))) {
71                          instance.getResourceIdentifier().remove(0);
72                      }
73                  }
74                  if (linkedBibIds != null && linkedBibIds.size() > 0) {
75                      for (String likedBibId : linkedBibIds) {
76                          instance.getResourceIdentifier().add(likedBibId);
77                      }
78                  }
79                  parsedItemNHoldingsDocuments.add(generateInstanceDocument(instance));
80  
81                  OleHoldings oleHolding = instance.getOleHoldings();
82                  RequestDocument rdHol = (RequestDocument) instanceDoc.clone();
83                  Content content = new Content();
84                  content.setContent(olemlProcessor.toXML(oleHolding));
85                  content.setContentObject(oleHolding);
86                  rdHol.setContent(content);
87                  if (oleHolding.getExtension() != null) {
88                      rdHol.setAdditionalAttributes(getFirstAdditionalAttributes(oleHolding.getExtension()));
89                  }
90                  parsedItemNHoldingsDocuments.add(rdHol);
91  
92                  SourceHoldings sourceHoldings = instance.getSourceHoldings();
93                  RequestDocument rdSrcHol = (RequestDocument) instanceDoc.clone();
94                  Content sourceHolContent = new Content();
95                  sourceHolContent.setContent(olemlProcessor.toXML(sourceHoldings));
96                  sourceHolContent.setContentObject(sourceHoldings);
97                  rdSrcHol.setContent(sourceHolContent);
98                  if (sourceHoldings != null && sourceHoldings.getExtension() != null) {
99                      rdSrcHol.setAdditionalAttributes(getFirstAdditionalAttributes(sourceHoldings.getExtension()));
100                 }
101                 parsedItemNHoldingsDocuments.add(rdSrcHol);
102 
103                 for (Item oleItem : instance.getItems().getItem()) {
104                     RequestDocument rdItm = (RequestDocument) instanceDoc.clone();
105                     Content itemContent = new Content();
106                     itemContent.setContent(olemlProcessor.toXML(oleItem));
107                     itemContent.setContentObject(oleItem);
108                     rdItm.setContent(itemContent);
109                     if (oleItem != null && oleItem.getExtension() != null) {
110                         rdItm.setAdditionalAttributes(getFirstAdditionalAttributes(oleItem.getExtension()));
111                     }
112                     parsedItemNHoldingsDocuments.add(rdItm);
113                 }
114             }
115         }
116         return parsedItemNHoldingsDocuments;
117     }
118 
119     private void resolveLinkingWithBib(Instance instance) {
120         instance.getResourceIdentifier().clear();
121         if (ProcessParameters.BULK_INGEST_IS_LINKING_ENABLED) {
122             for (FormerIdentifier frids : instance.getFormerResourceIdentifier()) {
123                 Identifier identifier = frids.getIdentifier();
124                 try {
125                     if (identifier.getIdentifierValue() != null
126                             && identifier.getIdentifierValue().trim().length() != 0) {
127                         List<SolrDocument> solrDocs = ServiceLocator.getIndexerService()
128                                 .getSolrDocument("SystemControlNumber",
129                                         "\"" + identifier
130                                                 .getIdentifierValue()
131                                                 + "\"");
132                         if (solrDocs != null && solrDocs.size() > 0) {
133                             for (SolrDocument solrDoc : solrDocs) {
134                                 if (checkApplicability(identifier.getIdentifierValue(),
135                                         solrDoc.getFieldValue("SystemControlNumber"))) {
136                                     instance.getResourceIdentifier().add(solrDoc.getFieldValue("id").toString());
137                                 }
138                             }
139                         }
140                     }
141                 } catch (Exception e) {
142                     logger.error("Exception (ignored) while linking instance with bib: ", e);
143                 }
144             }
145         }
146     }
147 
148     private boolean checkApplicability(Object value, Object fieldValue) {
149         if (fieldValue instanceof Collection) {
150             for (Object object : (Collection) fieldValue) {
151                 if (object.equals(value)) {
152                     return true;
153                 }
154             }
155             return false;
156         } else {
157             return value.equals(fieldValue);
158         }
159     }
160 
161     private RequestDocument generateInstanceDocument(Instance instance) {
162         InstanceCollection instanceCollection = new InstanceCollection();
163         List<Instance> instances = new ArrayList<Instance>();
164         instanceCollection.setInstance(instances);
165         Instance inst = new Instance();
166         instances.add(inst);
167         inst.setInstanceIdentifier(instance.getInstanceIdentifier());
168         inst.setResourceIdentifier(instance.getResourceIdentifier());
169         inst.setFormerResourceIdentifier(instance.getFormerResourceIdentifier());
170         inst.setExtension(instance.getExtension());
171         String cont = olemlProcessor.toXML(instanceCollection);
172         RequestDocument requestDocument = new RequestDocument();
173         requestDocument.setCategory(DocCategory.WORK.getCode());
174         requestDocument.setType(DocType.INSTANCE.getCode());
175         requestDocument.setFormat(DocFormat.OLEML.getCode());
176         requestDocument.setContent(new Content());
177         requestDocument.getContent().setContent(cont);
178         requestDocument.getContent().setContentObject(inst);
179         return requestDocument;
180     }
181 
182     /**
183      * Method to get Additional Attributes
184      *
185      * @param extension
186      * @return
187      */
188     private AdditionalAttributes getFirstAdditionalAttributes(Extension extension) {
189         if (extension != null && extension.getContent() != null) {
190             for (Object obj : extension.getContent()) {
191                 if (obj instanceof AdditionalAttributes) {
192                     return (AdditionalAttributes) obj;
193                 }
194             }
195         }
196         return null;
197     }
198 }