1 package org.kuali.ole.docstore.document.jcr;
2
3 import org.kuali.ole.docstore.OleDocStoreException;
4 import org.kuali.ole.docstore.common.document.content.instance.CallNumber;
5 import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
6 import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
7 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
8 import org.kuali.ole.docstore.common.document.content.instance.ShelvingOrder;
9 import org.kuali.ole.docstore.common.document.content.instance.ShelvingScheme;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import javax.jcr.Node;
14 import javax.jcr.RepositoryException;
15 import javax.jcr.Session;
16 import java.io.FileNotFoundException;
17 import java.util.List;
18
19
20
21
22
23
24
25
26 public class JcrJcrWorkHoldingsDocumentManager extends JcrWorkInstanceDocumentManager {
27
28 private static JcrJcrWorkHoldingsDocumentManager ourInstance = null;
29 private static Logger LOG = LoggerFactory.getLogger(JcrJcrWorkHoldingsDocumentManager.class);
30
31
32 public static JcrJcrWorkHoldingsDocumentManager getInstance() {
33 if (null == ourInstance) {
34 ourInstance = new JcrJcrWorkHoldingsDocumentManager();
35 }
36 return ourInstance;
37 }
38
39 @Override
40 protected void modifyContent(RequestDocument reqDoc, Session session, Node nodeByUUID)
41 throws RepositoryException, FileNotFoundException, OleDocStoreException {
42 HoldingOlemlRecordProcessor holdProcessor = new HoldingOlemlRecordProcessor();
43 if (reqDoc != null && reqDoc.getContent() != null) {
44
45 OleHoldings newHold = holdProcessor.fromXML(reqDoc.getContent().getContent());
46 if (newHold != null && newHold.getCallNumber() != null) {
47 CallNumber cNum = newHold.getCallNumber();
48
49
50 if (cNum.getNumber() != null && cNum.getNumber().trim().length() > 0) {
51
52 if (cNum.getShelvingOrder() == null) {
53 cNum.setShelvingOrder(new ShelvingOrder());
54 }
55
56 String holdingsXml = nodeManager.getData(nodeByUUID);
57 JcrWorkItemDocumentManager workItemDocumentManager = JcrWorkItemDocumentManager.getInstance();
58 boolean status = true;
59 if (holdingsXml != null) {
60 OleHoldings existHol = holdProcessor.fromXML(holdingsXml);
61 setHoldValuesFromNullToEmpty(existHol.getCallNumber());
62 setHoldValuesFromNullToEmpty(newHold.getCallNumber());
63 if (!(existHol.getCallNumber().getNumber().equalsIgnoreCase(cNum.getNumber()) &&
64 existHol.getCallNumber().getShelvingScheme().getCodeValue().equalsIgnoreCase(cNum.getShelvingScheme().getCodeValue()))) {
65 processCallNumber(newHold);
66 String newHolXml = holdProcessor.toXML(newHold);
67 reqDoc.getContent().setContent(newHolXml);
68 workItemDocumentManager.updateItemCallNumberFromHoldings(nodeByUUID, newHold, reqDoc);
69 status = false;
70 }
71 }
72 if (status && !(cNum.getShelvingOrder().getFullValue() != null &&
73 cNum.getShelvingOrder().getFullValue().trim().length() > 0)) {
74 processCallNumber(newHold);
75
76 }
77 }
78
79 }
80 }
81 }
82
83 protected void updateContentToNode(RequestDocument reqDoc, Session session, byte[] documentBytes, Node nodeByUUID)
84 throws RepositoryException, OleDocStoreException {
85 documentBytes = convertContentToBytes(reqDoc);
86 super.updateContentToNode(reqDoc, session, documentBytes, nodeByUUID);
87 for (RequestDocument linkReqDoc : reqDoc.getLinkedRequestDocuments()) {
88 documentBytes = convertContentToBytes(linkReqDoc);
89 Node itemNode = nodeManager.getNodeByUUID(session, linkReqDoc.getId());
90 super.updateContentToNode(linkReqDoc, session, documentBytes, itemNode);
91 }
92 }
93
94 private void setHoldValuesFromNullToEmpty(CallNumber callNumber) {
95 if (callNumber.getNumber() == null) {
96 callNumber.setNumber("");
97 }
98 if (callNumber.getShelvingScheme() == null) {
99 callNumber.setShelvingScheme(new ShelvingScheme());
100 }
101 if (callNumber.getShelvingScheme().getCodeValue() == null) {
102 callNumber.getShelvingScheme().setCodeValue("");
103 }
104 }
105
106 @Override
107 public void validateInput(RequestDocument reqDoc, Object object, List<String> valuesList) throws OleDocStoreException {
108 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
109 if (reqDoc.getContent() != null && reqDoc.getContent().getContent() != null) {
110 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(reqDoc.getContent().getContent());
111 validateHoldings(oleHoldings);
112 }
113 }
114
115 public void validateHoldings(OleHoldings oleHoldings) throws OleDocStoreException {
116 if (oleHoldings.getCallNumber() != null) {
117 CallNumber callNumber = oleHoldings.getCallNumber();
118 validateCallNumber(callNumber);
119 }
120 }
121 }
122