1 package org.kuali.ole.docstore.document.jcr;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.ole.docstore.OleDocStoreException;
5 import org.kuali.ole.docstore.common.document.content.instance.CallNumber;
6 import org.kuali.ole.docstore.common.document.content.instance.Item;
7 import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
8 import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
9 import org.kuali.ole.docstore.discovery.service.QueryServiceImpl;
10 import org.kuali.ole.docstore.document.DocumentManager;
11 import org.kuali.ole.docstore.model.enums.DocCategory;
12 import org.kuali.ole.docstore.model.enums.DocFormat;
13 import org.kuali.ole.docstore.model.enums.DocType;
14 import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
15 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
16 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
17 import org.kuali.ole.docstore.common.document.content.instance.ShelvingScheme;
18 import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
19 import org.kuali.ole.docstore.process.ProcessParameters;
20 import org.kuali.ole.docstore.service.BeanLocator;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import javax.jcr.Node;
25 import javax.jcr.RepositoryException;
26 import javax.jcr.Session;
27 import java.io.FileNotFoundException;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31
32 import static org.kuali.ole.docstore.process.ProcessParameters.FILE_HOLDINGS;
33
34
35
36
37
38
39
40
41 public class JcrWorkItemDocumentManager extends JcrWorkInstanceDocumentManager {
42
43 private static JcrWorkItemDocumentManager ourInstance = null;
44 private static Logger LOG = LoggerFactory.getLogger(JcrWorkItemDocumentManager.class);
45
46 public static JcrWorkItemDocumentManager getInstance() {
47 if (null == ourInstance) {
48 ourInstance = new JcrWorkItemDocumentManager();
49 }
50 return ourInstance;
51 }
52
53
54 @Override
55 public List<ResponseDocument> deleteVerify(List<RequestDocument> requestDocument, Object object) {
56 return null;
57 }
58
59 @Override
60 public ResponseDocument deleteVerify(RequestDocument requestDocument, Object object) throws Exception {
61 Session session = (Session) object;
62 LOG.debug("workItemDocumentManager deleteVerify method");
63 List<String> instanceIdentifierList = new ArrayList<String>();
64 ResponseDocument responseDocument = new ResponseDocument();
65 List<String> itemIdentifierList = new ArrayList<String>();
66 itemIdentifierList.add(requestDocument.getUuid());
67 boolean exists = checkInstancesOrItemsExistsInOLE(itemIdentifierList);
68 if (exists) {
69 responseDocument.setId(requestDocument.getId());
70 responseDocument.setCategory(requestDocument.getCategory());
71 responseDocument.setType(requestDocument.getType());
72 responseDocument.setFormat(requestDocument.getFormat());
73 responseDocument.setUuid(requestDocument.getUuid());
74 responseDocument.setStatus("failure'");
75 responseDocument.setStatusMessage("Instances or Items in use. So deletion cannot be done");
76 return responseDocument;
77 }
78 Node itemNode = session.getNodeByIdentifier(requestDocument.getUuid());
79
80 long itemSize = itemNode.getParent().getNodes().getSize() - 2;
81 LOG.debug("JcrWorkItemDocumentManager : itemSize " + itemSize);
82 String instanceIdentifier = itemNode.getParent().getParent().getIdentifier();
83 LOG.debug("JcrWorkItemDocumentManager : instanceIdentifier " + instanceIdentifier);
84
85
86
87
88
89
90
91
92 responseDocument.setUuid(requestDocument.getUuid());
93 responseDocument.setId(requestDocument.getId());
94 responseDocument.setCategory(requestDocument.getCategory());
95 responseDocument.setType(requestDocument.getType());
96 responseDocument.setFormat(requestDocument.getFormat());
97 responseDocument.setStatus("success");
98 responseDocument.setStatusMessage("success");
99
100 return responseDocument;
101 }
102
103 @Override
104 public ResponseDocument delete(RequestDocument requestDocument, Object object) throws Exception {
105 Session session = (Session) object;
106 LOG.debug("in JcrWorkItemDocumentManager :delete");
107 DocumentManager documentManager = null;
108 ResponseDocument responseDocument = new ResponseDocument();
109 ResponseDocument responseDocumentFromDeleteVerify = deleteVerify(requestDocument, session);
110 LOG.debug("in JcrWorkItemDocumentManager :delete after verify getUuid " + responseDocumentFromDeleteVerify.getUuid());
111 String status = responseDocumentFromDeleteVerify.getStatus();
112 LOG.debug("in JcrWorkItemDocumentManager :delete status " + status);
113
114 if (status.equalsIgnoreCase("success")) {
115 if (responseDocumentFromDeleteVerify.getCategory().equalsIgnoreCase("work") && responseDocumentFromDeleteVerify.getFormat().equalsIgnoreCase("oleml") && responseDocumentFromDeleteVerify.getType().equalsIgnoreCase("instance")) {
116 LOG.debug("in JcrWorkItemDocumentManager :delete if of instance");
117 RequestDocument requestDocumentForInstance = prepareRequestDocument(responseDocumentFromDeleteVerify);
118 documentManager = BeanLocator.getDocstoreFactory().getDocumentManager(requestDocumentForInstance.getCategory(), requestDocumentForInstance.getType(), requestDocumentForInstance.getFormat());
119 responseDocument = documentManager.delete(requestDocumentForInstance, session);
120 } else if (responseDocumentFromDeleteVerify.getCategory().equalsIgnoreCase("work") && responseDocumentFromDeleteVerify.getFormat().equalsIgnoreCase("marc") && responseDocumentFromDeleteVerify.getType().equalsIgnoreCase("bibliographic")) {
121 LOG.debug("in JcrWorkItemDocumentManager :delete if of marc");
122 RequestDocument requestDocumentForBib = prepareRequestDocument(responseDocumentFromDeleteVerify);
123 documentManager = BeanLocator.getDocstoreFactory().getDocumentManager(requestDocumentForBib.getCategory(), requestDocumentForBib.getType(), requestDocument.getFormat());
124 responseDocument = documentManager.delete(requestDocumentForBib, session);
125 } else if (responseDocumentFromDeleteVerify.getCategory().equalsIgnoreCase("work") && responseDocumentFromDeleteVerify.getFormat().equalsIgnoreCase("oleml") && responseDocumentFromDeleteVerify.getType().equalsIgnoreCase("item")) {
126 LOG.debug("in JcrWorkItemDocumentManager :delete if of item");
127 List<String> itemIdentifierList = new ArrayList<String>();
128 itemIdentifierList.add(requestDocument.getUuid());
129 deleteFromRepository(itemIdentifierList, session);
130
131 }
132 }
133 return responseDocument;
134 }
135
136 @Override
137 protected void modifyContent(RequestDocument reqDoc, Session session, Node nodeByUUID)
138 throws RepositoryException, FileNotFoundException, OleDocStoreException {
139 if (reqDoc.getOperation().equalsIgnoreCase("checkIn")) {
140 ItemOlemlRecordProcessor itemProcessor = new ItemOlemlRecordProcessor();
141
142 if (reqDoc.getContent() != null && reqDoc.getContent().getContent() != null) {
143
144 Item newItem = itemProcessor.fromXML(reqDoc.getContent().getContent());
145
146 Boolean status = true;
147 if (newItem.getCallNumber() == null) {
148 newItem.setCallNumber(new CallNumber());
149 }
150 CallNumber cNum = newItem.getCallNumber();
151 if (!(cNum.getShelvingOrder() != null && cNum.getShelvingOrder().getFullValue() != null &&
152 cNum.getShelvingOrder().getFullValue().trim().length() > 0)) {
153 if (!(cNum.getNumber() != null && cNum.getNumber().trim().length() > 0)) {
154 OleHoldings hold = getHoldings(nodeByUUID);
155 updateShelvingOrder(newItem, hold);
156 status = false;
157 } else {
158 updateShelvingOrder(newItem, null);
159 status = false;
160 }
161 }
162 if (status) {
163
164 String existingItemXml = nodeManager.getData(nodeByUUID);
165
166 if (existingItemXml != null) {
167 Item existItem = itemProcessor.fromXML(existingItemXml);
168 if (existItem.getCallNumber() == null) {
169 existItem.setCallNumber(new CallNumber());
170 }
171 CallNumber existCNum = existItem.getCallNumber();
172 setItemValuesFromNullToEmpty(existItem);
173 setItemValuesFromNullToEmpty(newItem);
174 cNum = newItem.getCallNumber();
175 computeCallNumberType(cNum);
176 if (!(existCNum.getNumber().equalsIgnoreCase((cNum.getNumber()))) |
177 !(existCNum.getShelvingScheme().getCodeValue().equalsIgnoreCase(cNum.getShelvingScheme().getCodeValue())) |
178 !((existItem.getEnumeration()).equalsIgnoreCase(newItem.getEnumeration())) |
179 !((existItem.getChronology()).equalsIgnoreCase(newItem.getChronology())) |
180 !((existItem.getCopyNumber()).equalsIgnoreCase(newItem.getCopyNumber()))) {
181 if (!(cNum.getNumber() != null && cNum.getNumber().trim().length() > 0)) {
182 OleHoldings hold = getHoldings(nodeByUUID);
183 updateShelvingOrder(newItem, hold);
184 } else {
185 updateShelvingOrder(newItem, null);
186 }
187
188 }
189 }
190 }
191 reqDoc.getContent().setContent(itemProcessor.toXML(newItem));
192 }
193 }
194 }
195
196 private OleHoldings getHoldings(Node nodeByUUID) throws RepositoryException, FileNotFoundException, OleDocStoreException {
197
198 Node holdingsNode = nodeByUUID.getParent();
199 Node holdingsFileNode = holdingsNode.getNode(FILE_HOLDINGS);
200 String holdXml = nodeManager.getData(holdingsFileNode);
201 HoldingOlemlRecordProcessor holdProc = new HoldingOlemlRecordProcessor();
202 OleHoldings hold = holdProc.fromXML(holdXml);
203 return hold;
204 }
205
206 protected void updateItemCallNumberFromHoldings(Node nodeByUUID, OleHoldings newHold, RequestDocument reqDoc)
207 throws RepositoryException, FileNotFoundException, OleDocStoreException {
208 Node holdingsNode = nodeByUUID.getParent();
209
210 for (Iterator<Node> itemIterator = holdingsNode.getNodes(ProcessParameters.FILE_ITEM); itemIterator.hasNext(); ) {
211 Node itemNode = itemIterator.next();
212 String itemXml = nodeManager.getData(itemNode);
213 String uuid = itemNode.getProperty("jcr:uuid").getString();
214 ItemOlemlRecordProcessor itemProcessor = new ItemOlemlRecordProcessor();
215 Item item = itemProcessor.fromXML(itemXml);
216 if (item != null) {
217 if (item.getCallNumber() == null) {
218 item.setCallNumber(new CallNumber());
219 }
220 item.getCallNumber().setNumber("");
221 if (item.getCallNumber().getShelvingScheme() != null)
222 item.getCallNumber().getShelvingScheme().setCodeValue("");
223 updateShelvingOrder(item, newHold);
224 String newItemXml = itemProcessor.toXML(item);
225 buildRequestDocumentForItem(newItemXml, reqDoc, uuid);
226 }
227 }
228 }
229
230 protected void updateContentToNode(RequestDocument reqDoc, Session session, byte[] documentBytes, Node nodeByUUID)
231 throws RepositoryException, OleDocStoreException {
232 documentBytes = convertContentToBytes(reqDoc);
233 super.updateContentToNode(reqDoc, session, documentBytes, nodeByUUID);
234 }
235
236 private void buildRequestDocumentForItem(String newItemXml, RequestDocument reqDoc, String uuid) {
237 RequestDocument requestDocument = new RequestDocument();
238 requestDocument.setOperation("checkIn");
239 requestDocument.setCategory(DocCategory.WORK.getCode());
240 requestDocument.setType(DocType.ITEM.getCode());
241 requestDocument.setFormat(DocFormat.OLEML.getCode());
242 requestDocument.setUuid(uuid);
243 requestDocument.setId(uuid);
244 Content content = new Content();
245 content.setContent(newItemXml);
246 requestDocument.setContent(content);
247 List<RequestDocument> reqDocList = reqDoc.getLinkedRequestDocuments();
248 if (reqDocList == null) {
249 reqDoc.setLinkedRequestDocuments(new ArrayList<RequestDocument>());
250 }
251 reqDocList.add(requestDocument);
252 }
253
254 private void setItemValuesFromNullToEmpty(Item item) {
255 CallNumber existCNum = item.getCallNumber();
256 if (existCNum == null) {
257 item.setCallNumber(new CallNumber());
258 }
259 if (existCNum.getNumber() == null) {
260 existCNum.setNumber("");
261 }
262 ShelvingScheme existSS = existCNum.getShelvingScheme();
263 if (existSS == null) {
264 existCNum.setShelvingScheme(new ShelvingScheme());
265 existSS = existCNum.getShelvingScheme();
266 }
267 if (existSS != null && !(existSS.getCodeValue() != null && existSS.getCodeValue().trim().length() > 0)) {
268 existSS.setCodeValue("");
269 }
270 if (item.getEnumeration() == null) {
271 item.setEnumeration("");
272 }
273 if (item.getChronology() == null) {
274 item.setChronology("");
275 }
276 if (item.getCopyNumber() == null) {
277 item.setCopyNumber("");
278 }
279 }
280
281 @Override
282 public void validateInput(RequestDocument requestDocument, Object object, List<String> valuesList) throws OleDocStoreException {
283 Session session = (Session) object;
284 validateItem(requestDocument, session, valuesList);
285 }
286
287
288 public void validateNewItem(RequestDocument linkedRequestDocument, Session session, List<String> fieldValues, String instanceUuid) throws OleDocStoreException {
289 try {
290 String itemContent = linkedRequestDocument.getContent().getContent();
291 ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
292 Item item = itemOlemlRecordProcessor.fromXML(itemContent);
293 itemBarcodeValidation(item, fieldValues, null);
294 if (item.getCallNumber() != null) {
295 Node instanceNode = nodeManager.getNodeByUUID(session, instanceUuid);
296 if (instanceNode != null && instanceNode.hasNode(ProcessParameters.NODE_HOLDINGS)) {
297 Node holdingsParentNode = instanceNode.getNode(ProcessParameters.NODE_HOLDINGS);
298 if (holdingsParentNode.hasNode(ProcessParameters.FILE_HOLDINGS)) {
299 Node holdingsNode = holdingsParentNode.getNode(ProcessParameters.FILE_HOLDINGS);
300 if (holdingsNode != null) {
301 getHoldingsContentNValidateItem(holdingsNode, item);
302 }
303 }
304 }
305 }
306 } catch (Exception e) {
307 throw new OleDocStoreException(e.getMessage(), e);
308 }
309 }
310
311
312 public void validateItem(RequestDocument linkedRequestDocument, Session session, List<String> fieldValues) throws OleDocStoreException {
313 try {
314 String itemContent = linkedRequestDocument.getContent().getContent();
315 ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
316 Item item = itemOlemlRecordProcessor.fromXML(itemContent);
317
318 itemBarcodeValidation(item, fieldValues, linkedRequestDocument.getId());
319 if (item.getCallNumber() != null) {
320 Node node = nodeManager.getNodeByUUID(session, linkedRequestDocument.getUuid());
321 Node parentNode = null;
322 parentNode = node.getParent();
323 if (parentNode != null) {
324 if (parentNode.hasNode(ProcessParameters.FILE_HOLDINGS)) {
325 Node holdingsNode = parentNode.getNode(ProcessParameters.FILE_HOLDINGS);
326 getHoldingsContentNValidateItem(holdingsNode, item);
327 }
328 }
329 }
330 } catch (Exception e) {
331 throw new OleDocStoreException(e.getMessage(), e);
332 }
333 }
334
335 public void itemBarcodeValidation(Item item, List<String> fieldValues, String id) throws OleDocStoreException {
336 if (item.getAccessInformation() != null && StringUtils.isNotEmpty(item.getAccessInformation().getBarcode())) {
337 try {
338 if (fieldValues.contains(item.getAccessInformation().getBarcode()) ||
339 QueryServiceImpl.getInstance().isFieldValueExists(DocType.ITEM.getCode(), "ItemBarcode_display", item.getAccessInformation().getBarcode(), id)) {
340 throw new OleDocStoreException("Barcode " + item.getAccessInformation().getBarcode() + " already exists ");
341 }
342 fieldValues.add(item.getAccessInformation().getBarcode());
343 } catch (Exception e) {
344 throw new OleDocStoreException(e.getMessage(), e);
345 }
346 }
347
348 }
349
350 private void getHoldingsContentNValidateItem(Node holdingsNode, Item item) throws Exception {
351 String content = checkOutContent(holdingsNode, DocFormat.OLEML.getCode(), "ole-khuntley");
352 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
353 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(content);
354 CallNumber callNumber = item.getCallNumber();
355 validateCallNumber(callNumber, oleHoldings);
356 }
357
358
359 }