1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.documenthandler;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import org.apache.solr.common.SolrDocument;
24 import org.kuali.ole.docstore.model.enums.DocCategory;
25 import org.kuali.ole.docstore.model.enums.DocFormat;
26 import org.kuali.ole.docstore.model.enums.DocType;
27 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
28 import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
29 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
30 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Extension;
31 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.FormerResourceIdentifiers;
32 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.InstanceCollection;
33 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.OleHolding;
34 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.OleInstance;
35 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.OleItem;
36 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.ResourceIdentifier;
37 import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkInstanceOlemlRecordProcessor;
38 import org.kuali.ole.docstore.process.ProcessParameters;
39 import org.kuali.ole.docstore.service.ServiceLocator;
40
41
42
43
44
45
46
47 public class InstanceRequestDocumentResolver {
48
49 private WorkInstanceOlemlRecordProcessor olemlProcessor = new WorkInstanceOlemlRecordProcessor();
50
51
52
53
54
55
56
57
58
59
60
61 public List<RequestDocument> getParsedHoldingsNItemDocuments(RequestDocument instanceDoc, List<String> linkedBibIds) {
62 List<RequestDocument> parsedItemNHoldingsDocuments = new ArrayList<RequestDocument>();
63 if (instanceDoc != null && DocCategory.WORK.isEqualTo(instanceDoc.getCategory()) && DocType.INSTANCE.isEqualTo(instanceDoc.getType())
64 && DocFormat.OLEML.isEqualTo(instanceDoc.getFormat())) {
65 String docContent = instanceDoc.getContent().getContent();
66 InstanceCollection instanceCollection = olemlProcessor.fromXML(docContent);
67 instanceDoc.getContent().setContentObject(instanceCollection);
68
69 if (instanceCollection.getInstanceCollection() != null && instanceCollection.getInstanceCollection().size() > 0) {
70 OleInstance instance = instanceCollection.getInstanceCollection().get(0);
71 resolveLinkingWithBib(instance);
72
73 if (instance.getResourceIdentifier().size() == 1) {
74 if (instance.getResourceIdentifier().get(0).getValue() == null || "".equals(instance.getResourceIdentifier().get(0).getValue()))
75 instance.getResourceIdentifier().remove(0);
76 }
77 if (linkedBibIds != null && linkedBibIds.size() > 0) {
78 for (String likedBibId : linkedBibIds) {
79 ResourceIdentifier ri = new ResourceIdentifier();
80 ri.setSource("docStore");
81 ri.setValue(likedBibId);
82 instance.getResourceIdentifier().add(ri);
83 }
84 }
85 parsedItemNHoldingsDocuments.add(generateInstanceDocument(instance));
86 OleHolding oleHolding = instance.getHoldings();
87 RequestDocument rd = (RequestDocument) instanceDoc.clone();
88 Content content = new Content();
89 content.setContent(olemlProcessor.toXML(oleHolding));
90 content.setContentObject(oleHolding);
91 rd.setContent(content);
92 rd.setAdditionalAttributes(getFirstAdditionalAttributes(oleHolding.getExtension()));
93 parsedItemNHoldingsDocuments.add(rd);
94 for (OleItem oleItem : instance.getItem()) {
95 rd = (RequestDocument) instanceDoc.clone();
96 Content itemContent = new Content();
97 itemContent.setContent(olemlProcessor.toXML(oleItem));
98 itemContent.setContentObject(oleItem);
99 rd.setContent(itemContent);
100 parsedItemNHoldingsDocuments.add(rd);
101 rd.setAdditionalAttributes(getFirstAdditionalAttributes(oleItem.getExtension()));
102 }
103 }
104 }
105 return parsedItemNHoldingsDocuments;
106 }
107
108 private void resolveLinkingWithBib(OleInstance instance) {
109 instance.getResourceIdentifier().clear();
110 if (ProcessParameters.BULK_INGEST_IS_LINKING_ENABLED)
111 for (FormerResourceIdentifiers frids : instance.getFormerResourceIdentifiers()) {
112 try {
113 if (frids.getValue() != null && frids.getValue().trim().length() != 0) {
114 List<SolrDocument> solrDocs = ServiceLocator.getIndexerService().getSolrDocument("SystemControlNumber",
115 "\"" + frids.getValue() + "\"");
116 if (solrDocs != null && solrDocs.size() > 0) {
117 for (SolrDocument solrDoc : solrDocs) {
118 if (checkApplicability(frids.getValue(), solrDoc.getFieldValue("SystemControlNumber"))) {
119 ResourceIdentifier ri = new ResourceIdentifier();
120 ri.setSource("docStore");
121 ri.setValue(solrDoc.getFieldValue("id").toString());
122 instance.getResourceIdentifier().add(ri);
123 }
124 }
125 }
126 }
127 } catch (Exception e) {
128 }
129 }
130 }
131
132 private boolean checkApplicability(Object value, Object fieldValue) {
133 if (fieldValue instanceof Collection) {
134 for (Object object : (Collection) fieldValue)
135 if (object.equals(value))
136 return true;
137 return false;
138 } else
139 return value.equals(fieldValue);
140 }
141
142 private RequestDocument generateInstanceDocument(OleInstance instance) {
143 InstanceCollection instanceCollection = new InstanceCollection();
144 List<OleInstance> instances = new ArrayList<OleInstance>();
145 instanceCollection.setInstanceCollection(instances);
146 OleInstance inst = new OleInstance();
147 instances.add(inst);
148 inst.setInstanceIdentifier(instance.getInstanceIdentifier());
149 inst.setResourceIdentifier(instance.getResourceIdentifier());
150 inst.setFormerResourceIdentifiers(instance.getFormerResourceIdentifiers());
151 inst.setExtension(instance.getExtension());
152 String cont = olemlProcessor.toXML(instanceCollection);
153 RequestDocument requestDocument = new RequestDocument();
154 requestDocument.setCategory(DocCategory.WORK.getCode());
155 requestDocument.setType(DocType.INSTANCE.getCode());
156 requestDocument.setFormat(DocFormat.OLEML.getCode());
157 requestDocument.setContent(new Content());
158 requestDocument.getContent().setContent(cont);
159 requestDocument.getContent().setContentObject(inst);
160 return requestDocument;
161 }
162
163
164
165
166
167
168
169
170 private AdditionalAttributes getFirstAdditionalAttributes(List<Extension> extenstions) {
171 for (Extension ext : extenstions) {
172 for (Object obj : ext.getContent()) {
173 if (obj instanceof AdditionalAttributes) {
174 return (AdditionalAttributes) obj;
175 }
176 }
177 }
178 return null;
179 }
180 }