001/* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.ole.documenthandler; 017 018import org.apache.solr.common.SolrDocument; 019import org.kuali.ole.docstore.common.document.content.instance.*; 020import org.kuali.ole.docstore.common.document.content.instance.xstream.InstanceOlemlRecordProcessor; 021import org.kuali.ole.docstore.model.enums.DocCategory; 022import org.kuali.ole.docstore.model.enums.DocFormat; 023import org.kuali.ole.docstore.model.enums.DocType; 024import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes; 025import org.kuali.ole.docstore.model.xmlpojo.ingest.Content; 026import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument; 027import org.kuali.ole.docstore.process.ProcessParameters; 028import org.kuali.ole.docstore.service.ServiceLocator; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032import java.util.ArrayList; 033import java.util.Collection; 034import java.util.List; 035 036/** 037 * Class to InstanceRequestDocumentResolver. 038 * 039 * @author Rajesh Chowdary K 040 * @created Feb 22, 2012 041 */ 042public class InstanceRequestDocumentResolver { 043 private static Logger logger = LoggerFactory.getLogger(InstanceRequestDocumentResolver.class); 044 045 private InstanceOlemlRecordProcessor olemlProcessor = new InstanceOlemlRecordProcessor(); 046 047 /** 048 * Method to get Parsed Holdings & Item Documents. 049 * 050 * @param instanceDoc - Instance document in format OleML 051 * @param linkedBibIds TODO 052 * @return 053 */ 054 public List<RequestDocument> getParsedHoldingsNItemDocuments(RequestDocument instanceDoc, 055 List<String> linkedBibIds) { 056 List<RequestDocument> parsedItemNHoldingsDocuments = new ArrayList<RequestDocument>(); 057 if (instanceDoc != null && DocCategory.WORK.isEqualTo(instanceDoc.getCategory()) 058 && DocType.INSTANCE.isEqualTo(instanceDoc.getType()) && DocFormat.OLEML 059 .isEqualTo(instanceDoc.getFormat())) { 060 String docContent = instanceDoc.getContent().getContent(); 061 InstanceCollection instanceCollection = olemlProcessor.fromXML(docContent); 062 instanceDoc.getContent().setContentObject(instanceCollection); 063 // XML conversion 064 if (instanceCollection.getInstance() != null && instanceCollection.getInstance().size() > 0) { 065 Instance instance = instanceCollection.getInstance().get(0); 066 resolveLinkingWithBib(instance); 067 068 if (instance.getResourceIdentifier().size() == 1) { 069 if (instance.getResourceIdentifier().get(0) == null || "" 070 .equals(instance.getResourceIdentifier().get(0))) { 071 instance.getResourceIdentifier().remove(0); 072 } 073 } 074 if (linkedBibIds != null && linkedBibIds.size() > 0) { 075 for (String likedBibId : linkedBibIds) { 076 instance.getResourceIdentifier().add(likedBibId); 077 } 078 } 079 parsedItemNHoldingsDocuments.add(generateInstanceDocument(instance)); 080 081 OleHoldings oleHolding = instance.getOleHoldings(); 082 RequestDocument rdHol = (RequestDocument) instanceDoc.clone(); 083 Content content = new Content(); 084 content.setContent(olemlProcessor.toXML(oleHolding)); 085 content.setContentObject(oleHolding); 086 rdHol.setContent(content); 087 if (oleHolding.getExtension() != null) { 088 rdHol.setAdditionalAttributes(getFirstAdditionalAttributes(oleHolding.getExtension())); 089 } 090 parsedItemNHoldingsDocuments.add(rdHol); 091 092 SourceHoldings sourceHoldings = instance.getSourceHoldings(); 093 RequestDocument rdSrcHol = (RequestDocument) instanceDoc.clone(); 094 Content sourceHolContent = new Content(); 095 sourceHolContent.setContent(olemlProcessor.toXML(sourceHoldings)); 096 sourceHolContent.setContentObject(sourceHoldings); 097 rdSrcHol.setContent(sourceHolContent); 098 if (sourceHoldings != null && sourceHoldings.getExtension() != null) { 099 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}