1 package org.kuali.ole.docstore.document;
2
3 import org.apache.solr.common.SolrDocument;
4 import org.kuali.ole.docstore.OleDocStoreException;
5 import org.kuali.ole.docstore.model.enums.DocCategory;
6 import org.kuali.ole.docstore.model.enums.DocFormat;
7 import org.kuali.ole.docstore.model.enums.DocType;
8 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
9 import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
10 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
11 import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.*;
12 import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkInstanceOlemlRecordProcessor;
13 import org.kuali.ole.docstore.process.ProcessParameters;
14 import org.kuali.ole.docstore.repository.WorkInstanceNodeManager;
15 import org.kuali.ole.docstore.service.ServiceLocator;
16
17 import javax.jcr.Node;
18 import javax.jcr.Session;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.List;
22
23 import static org.kuali.ole.docstore.process.ProcessParameters.FILE;
24
25
26
27
28
29
30
31
32 public class WorkInstanceDocumentManager
33 extends AbstractDocumentManager {
34
35 private static WorkInstanceDocumentManager ourInstance = null;
36 private WorkInstanceOlemlRecordProcessor olemlProcessor = new WorkInstanceOlemlRecordProcessor();
37
38 public static WorkInstanceDocumentManager getInstance() {
39 if (null == ourInstance) {
40 ourInstance = new WorkInstanceDocumentManager();
41 }
42 return ourInstance;
43 }
44
45 protected WorkInstanceDocumentManager() {
46 super();
47 this.nodeManager = WorkInstanceNodeManager.getInstance();
48 }
49
50 @Override
51 public void modifyDocumentContent(RequestDocument document, String nodeIdentifier, String parentNodeIdentifier) {
52
53
54 if (document.getContent().getContentObject() instanceof OleHoldings) {
55 ((OleHoldings) document.getContent().getContentObject()).setHoldingsIdentifier(nodeIdentifier);
56 document.getContent()
57 .setContent(olemlProcessor.toXML((OleHoldings) document.getContent().getContentObject()));
58 }
59 else if (document.getContent().getContentObject() instanceof SourceHoldings) {
60 ((SourceHoldings) document.getContent().getContentObject()).setHoldingsIdentifier(nodeIdentifier);
61 document.getContent()
62 .setContent(olemlProcessor.toXML((SourceHoldings) document.getContent().getContentObject()));
63 }
64 else if (document.getContent().getContentObject() instanceof Item) {
65 ((Item) document.getContent().getContentObject()).setItemIdentifier(nodeIdentifier);
66 document.getContent().setContent(olemlProcessor.toXML((Item) document.getContent().getContentObject()));
67 }
68 else if (document.getContent().getContentObject() instanceof Instance) {
69 Instance inst = ((Instance) document.getContent().getContentObject());
70 inst.setInstanceIdentifier(parentNodeIdentifier);
71 InstanceCollection instanceCollection = new InstanceCollection();
72 ArrayList<Instance> oleinsts = new ArrayList<Instance>();
73 oleinsts.add(inst);
74 instanceCollection.setInstance((oleinsts));
75 document.getContent().setContent(olemlProcessor.toXML(instanceCollection));
76 }
77 }
78
79 @Override
80 protected Node storeDocument(RequestDocument requestDocument, Session session) throws OleDocStoreException {
81 Node fileNode = null;
82 try {
83 AdditionalAttributes additionalAttributes = requestDocument.getAdditionalAttributes();
84 modifyAdditionalAttributes(additionalAttributes, requestDocument);
85
86
87 Node parentNode = nodeManager.getParentNode(requestDocument, session);
88 String fileName = requestDocument.getFormat() + FILE;
89
90
91 fileNode = nodeManager.createFileNode(requestDocument, fileName, parentNode, session);
92
93 if (isVersioningEnabled()) {
94 nodeManager.enableVersioning(fileNode);
95 }
96 }
97 catch (Exception e) {
98 throw new OleDocStoreException(e);
99 }
100 return fileNode;
101 }
102
103
104
105
106
107
108
109
110
111 public List<RequestDocument> getParsedHoldingsNItemDocuments(RequestDocument instanceDoc,
112 List<String> linkedBibIds) {
113 List<RequestDocument> parsedItemNHoldingsDocuments = new ArrayList<RequestDocument>();
114 if (instanceDoc != null && DocCategory.WORK.isEqualTo(instanceDoc.getCategory())
115 && DocType.INSTANCE.isEqualTo(instanceDoc.getType()) && DocFormat.OLEML
116 .isEqualTo(instanceDoc.getFormat())) {
117 String docContent = instanceDoc.getContent().getContent();
118 InstanceCollection instanceCollection = olemlProcessor.fromXML(docContent);
119 instanceDoc.getContent().setContentObject(instanceCollection);
120
121 if (instanceCollection.getInstance() != null && instanceCollection.getInstance().size() > 0) {
122 Instance instance = instanceCollection.getInstance().get(0);
123 resolveLinkingWithBib(instance);
124
125 if (instance.getResourceIdentifier().size() == 1) {
126 if (instance.getResourceIdentifier().get(0) == null || ""
127 .equals(instance.getResourceIdentifier().get(0))) {
128 instance.getResourceIdentifier().remove(0);
129 }
130 }
131 if (linkedBibIds != null && linkedBibIds.size() > 0) {
132 for (String likedBibId : linkedBibIds) {
133 instance.getResourceIdentifier().add(likedBibId);
134 }
135 }
136 parsedItemNHoldingsDocuments.add(generateInstanceDocument(instance));
137
138 OleHoldings oleHolding = instance.getOleHoldings();
139 RequestDocument rdHol = (RequestDocument) instanceDoc.clone();
140 Content content = new Content();
141 content.setContent(olemlProcessor.toXML(oleHolding));
142 content.setContentObject(oleHolding);
143 rdHol.setContent(content);
144 if (oleHolding.getExtension() != null) {
145 rdHol.setAdditionalAttributes(getFirstAdditionalAttributes(oleHolding.getExtension()));
146 }
147 parsedItemNHoldingsDocuments.add(rdHol);
148
149 SourceHoldings sourceHoldings = instance.getSourceHoldings();
150 RequestDocument rdSrcHol = (RequestDocument) instanceDoc.clone();
151 Content sourceHolContent = new Content();
152 sourceHolContent.setContent(olemlProcessor.toXML(sourceHoldings));
153 sourceHolContent.setContentObject(sourceHoldings);
154 rdSrcHol.setContent(sourceHolContent);
155 if (sourceHoldings != null && sourceHoldings.getExtension() != null) {
156 rdSrcHol.setAdditionalAttributes(getFirstAdditionalAttributes(sourceHoldings.getExtension()));
157 }
158 parsedItemNHoldingsDocuments.add(rdSrcHol);
159
160 for (Item oleItem : instance.getItems().getItem()) {
161 RequestDocument rdItm = (RequestDocument) instanceDoc.clone();
162 Content itemContent = new Content();
163 itemContent.setContent(olemlProcessor.toXML(oleItem));
164 itemContent.setContentObject(oleItem);
165 rdItm.setContent(itemContent);
166 if (oleItem != null && oleItem.getExtension() != null) {
167 rdItm.setAdditionalAttributes(getFirstAdditionalAttributes(oleItem.getExtension()));
168 }
169 parsedItemNHoldingsDocuments.add(rdItm);
170 }
171 }
172 }
173 return parsedItemNHoldingsDocuments;
174 }
175
176 private RequestDocument generateInstanceDocument(Instance instance) {
177 InstanceCollection instanceCollection = new InstanceCollection();
178 List<Instance> instances = new ArrayList<Instance>();
179 instanceCollection.setInstance(instances);
180 Instance inst = new Instance();
181 instances.add(inst);
182 inst.setInstanceIdentifier(instance.getInstanceIdentifier());
183 inst.setResourceIdentifier(instance.getResourceIdentifier());
184 inst.setFormerResourceIdentifier(instance.getFormerResourceIdentifier());
185 inst.setExtension(instance.getExtension());
186 String cont = olemlProcessor.toXML(instanceCollection);
187 RequestDocument requestDocument = new RequestDocument();
188 requestDocument.setCategory(DocCategory.WORK.getCode());
189 requestDocument.setType(DocType.INSTANCE.getCode());
190 requestDocument.setFormat(DocFormat.OLEML.getCode());
191 requestDocument.setContent(new Content());
192 requestDocument.getContent().setContent(cont);
193 requestDocument.getContent().setContentObject(inst);
194 return requestDocument;
195 }
196
197 private void resolveLinkingWithBib(Instance instance) {
198 instance.getResourceIdentifier().clear();
199 if (ProcessParameters.BULK_INGEST_IS_LINKING_ENABLED) {
200 for (FormerIdentifier frids : instance.getFormerResourceIdentifier()) {
201 Identifier identifier = frids.getIdentifier();
202 try {
203 if (identifier.getIdentifierValue() != null
204 && identifier.getIdentifierValue().trim().length() != 0) {
205 List<SolrDocument> solrDocs = ServiceLocator.getIndexerService()
206 .getSolrDocument("SystemControlNumber", "\""
207 + identifier
208 .getIdentifierValue() + "\"");
209 if (solrDocs != null && solrDocs.size() > 0) {
210 for (SolrDocument solrDoc : solrDocs) {
211 if (checkApplicability(identifier.getIdentifierValue(),
212 solrDoc.getFieldValue("SystemControlNumber"))) {
213 FormerIdentifier fi = new FormerIdentifier();
214 Identifier id = new Identifier();
215 id.setSource("docstore");
216 id.setIdentifierValue(solrDoc.getFieldValue("id").toString());
217 fi.setIdentifier(id);
218 instance.getResourceIdentifier().add(solrDoc.getFieldValue("id").toString());
219 instance.getFormerResourceIdentifier().add(fi);
220 }
221 }
222 }
223 }
224 }
225 catch (Exception e) {
226 }
227 }
228 }
229 }
230
231 private boolean checkApplicability(Object value, Object fieldValue) {
232 if (fieldValue instanceof Collection) {
233 for (Object object : (Collection) fieldValue) {
234 if (object.equals(value)) {
235 return true;
236 }
237 }
238 return false;
239 }
240 else {
241 return value.equals(fieldValue);
242 }
243 }
244
245
246
247
248
249
250
251 private AdditionalAttributes getFirstAdditionalAttributes(Extension extension) {
252 if (extension != null && extension.getContent() != null) {
253 for (Object obj : extension.getContent()) {
254 if (obj instanceof AdditionalAttributes) {
255 return (AdditionalAttributes) obj;
256 }
257 }
258 }
259 return null;
260 }
261
262 }