1 package org.kuali.ole.docstore.document.rdbms;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.ole.DocumentUniqueIDPrefix;
5 import org.kuali.ole.docstore.OleDocStoreException;
6 import org.kuali.ole.docstore.model.rdbms.bo.*;
7 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
8 import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
9 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
10 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
11 import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.CallNumber;
12 import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.CallNumberType;
13 import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.EHoldings;
14 import org.kuali.ole.docstore.model.xmlpojo.work.einstance.oleml.ShelvingOrder;
15 import org.kuali.ole.docstore.model.xstream.work.oleml.WorkEHoldingOlemlRecordProcessor;
16 import org.kuali.ole.utility.callnumber.CallNumberFactory;
17 import org.kuali.rice.krad.service.BusinessObjectService;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26
27
28
29
30
31
32
33 public class RdbmsWorkEHoldingsDocumentManager extends RdbmsWorkEInstanceDocumentManager {
34
35 private static RdbmsWorkEHoldingsDocumentManager ourInstanceRdbms = null;
36 private static final Logger LOG = LoggerFactory.getLogger(RdbmsWorkEHoldingsDocumentManager.class);
37 private BusinessObjectService businessObjectService;
38
39 public static RdbmsWorkEHoldingsDocumentManager getInstance() {
40 if (null == ourInstanceRdbms) {
41 ourInstanceRdbms = new RdbmsWorkEHoldingsDocumentManager();
42 }
43 return ourInstanceRdbms;
44 }
45
46 @Override
47 public ResponseDocument checkoutContent(RequestDocument requestDocument, Object object) {
48 ResponseDocument respDoc = new ResponseDocument();
49 Map eInstanceMap = new HashMap();
50 eInstanceMap.put("eHoldingsIdentifier", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
51 EHoldingsRecord eHoldingsRecord = getBusinessObjectService().findByPrimaryKey(EHoldingsRecord.class, eInstanceMap);
52 EHoldings eHoldings = buildEHoldingsContent(eHoldingsRecord);
53 String content = new WorkEHoldingOlemlRecordProcessor().toXML(eHoldings);
54 Content contentObj = new Content();
55 contentObj.setContent(content);
56 respDoc.setUuid(requestDocument.getUuid());
57 respDoc.setCategory(requestDocument.getCategory());
58 respDoc.setType(requestDocument.getType());
59 respDoc.setFormat(requestDocument.getFormat());
60 respDoc.setContent(contentObj);
61 return respDoc;
62 }
63
64 @Override
65 public void deleteDocs(RequestDocument requestDocument, Object object) {
66 ResponseDocument responseDocument = new ResponseDocument();
67 BusinessObjectService businessObjectService = (BusinessObjectService) object;
68 EInstanceRecord eInstanceRecord = new EInstanceRecord();
69 EHoldingsRecord eHoldingsRecord = new EHoldingsRecord();
70 String eHoldingsId = DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid());
71 Map itemMap = new HashMap();
72 itemMap.put("eHoldingsIdentifier", eHoldingsId);
73 eHoldingsRecord.seteHoldingsIdentifier(eHoldingsId);
74 eInstanceRecord.seteInstanceIdentifier(eHoldingsId);
75 deleteEHoldingNChildRecords(eHoldingsRecord);
76 getBusinessObjectService().delete(eInstanceRecord);
77 buildResponseDocument(requestDocument, eHoldingsRecord, responseDocument);
78 }
79
80 @Override
81 public void checkInContent(RequestDocument requestDocument, Object object, ResponseDocument responseDocument) throws OleDocStoreException {
82 AdditionalAttributes attributes = requestDocument.getAdditionalAttributes();
83 BusinessObjectService businessObjectService = (BusinessObjectService) object;
84 if (requestDocument.getContent().getContent() != null) {
85 modifyContent(requestDocument, businessObjectService, DocumentUniqueIDPrefix.getDocumentId(requestDocument.getId()));
86 String content = requestDocument.getContent().getContent();
87 WorkEHoldingOlemlRecordProcessor workEHoldingOlemlRecordProcessor = new WorkEHoldingOlemlRecordProcessor();
88 EHoldings eHoldings = workEHoldingOlemlRecordProcessor.fromXML(content);
89 Map parentCriteria1 = new HashMap();
90 parentCriteria1.put("eHoldingsIdentifier", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
91 EHoldingsRecord eHoldingsRecord = getBusinessObjectService().findByPrimaryKey(EHoldingsRecord.class, parentCriteria1);
92 buildHoldingRecordForCheckIn(eHoldings, eHoldingsRecord);
93
94 requestDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(eHoldingsRecord.getUniqueIdPrefix(), eHoldingsRecord.geteHoldingsIdentifier()));
95 responseDocument.setAdditionalAttributes(attributes);
96 buildResponseDocument(requestDocument, eHoldingsRecord, responseDocument);
97 }
98 }
99
100 public ResponseDocument buildResponseDocument(RequestDocument requestDocument, EHoldingsRecord eHoldingsRecord,
101 ResponseDocument responseDocument) {
102 responseDocument.setId(eHoldingsRecord.geteHoldingsIdentifier());
103 responseDocument.setCategory(requestDocument.getCategory());
104 responseDocument.setType(requestDocument.getType());
105 responseDocument.setFormat(requestDocument.getFormat());
106 responseDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(eHoldingsRecord.getUniqueIdPrefix(), eHoldingsRecord.geteHoldingsIdentifier()));
107 responseDocument.setId(eHoldingsRecord.geteHoldingsIdentifier());
108 return responseDocument;
109 }
110
111 private void buildHoldingRecordForCheckIn(EHoldings eHoldings, EHoldingsRecord eHoldingsRecord) {
112 saveEHoldingsRecord(eHoldings, eHoldingsRecord);
113 }
114
115 protected void modifyContent(RequestDocument reqDoc, BusinessObjectService businessObjectService, String holdingId) throws OleDocStoreException {
116 WorkEHoldingOlemlRecordProcessor holdProcessor = new WorkEHoldingOlemlRecordProcessor();
117 String instanceId = "";
118 if (reqDoc != null && reqDoc.getContent() != null) {
119
120 EHoldings newHold = holdProcessor.fromXML(reqDoc.getContent().getContent());
121 if (newHold != null && newHold.getCallNumber() != null) {
122 CallNumber cNum = newHold.getCallNumber();
123
124
125 if (cNum.getNumber() != null && cNum.getNumber().trim().length() > 0) {
126
127 if (cNum.getShelvingOrder() == null) {
128 cNum.setShelvingOrder(new ShelvingOrder());
129 }
130
131 boolean status = true;
132 Map holdingsMap = new HashMap();
133 holdingsMap.put("eHoldingsIdentifier", holdingId);
134 EHoldings existHol = null;
135 List<EHoldingsRecord> eHoldingsRecords = (List<EHoldingsRecord>) businessObjectService.findMatching(EHoldingsRecord.class, holdingsMap);
136 if (eHoldingsRecords != null && eHoldingsRecords.size() > 0) {
137 EHoldingsRecord holdingsRecord = eHoldingsRecords.get(0);
138 existHol = buildEHoldingsContent(holdingsRecord);
139 instanceId = holdingsRecord.geteInstanceIdentifier();
140 }
141 if (existHol != null) {
142 setHoldValuesFromNullToEmpty(existHol.getCallNumber());
143 setHoldValuesFromNullToEmpty(newHold.getCallNumber());
144 if (existHol.getCallNumber() != null && existHol.getCallNumber().getNumber() != null && !(existHol.getCallNumber().getNumber().equalsIgnoreCase(cNum.getNumber()) &&
145 existHol.getCallNumber().getCallNumberType().getCodeValue().equalsIgnoreCase(cNum.getCallNumberType().getCodeValue()))) {
146 processCallNumber(newHold);
147 String newHolXml = holdProcessor.toXML(newHold);
148 reqDoc.getContent().setContent(newHolXml);
149 status = false;
150 }
151 }
152 if (status && !(cNum.getShelvingOrder().getFullValue() != null &&
153 cNum.getShelvingOrder().getFullValue().trim().length() > 0)) {
154 processCallNumber(newHold);
155 }
156 }
157
158 }
159 }
160 }
161
162 private void setHoldValuesFromNullToEmpty(CallNumber callNumber) {
163 if (callNumber != null) {
164 if (callNumber.getNumber() == null) {
165 callNumber.setNumber("");
166 }
167 if (callNumber.getCallNumberType() == null) {
168 callNumber.setCallNumberType(new CallNumberType());
169 }
170 if (callNumber.getCallNumberType().getCodeValue() == null) {
171 callNumber.getCallNumberType().setCodeValue("");
172 }
173 }
174 }
175
176 protected void processCallNumber(EHoldings eHolding) throws OleDocStoreException {
177 if (eHolding != null && eHolding.getCallNumber() != null) {
178
179 CallNumber cNum = eHolding.getCallNumber();
180 computeCallNumberType(cNum);
181 if (cNum.getNumber() != null && cNum.getNumber().trim().length() > 0) {
182
183 boolean isValid = validateCallNumber(cNum.getNumber(), cNum.getCallNumberType().getCodeValue());
184 String value = "";
185 if (isValid) {
186 value = buildSortableCallNumber(cNum.getNumber(), cNum.getCallNumberType().getCodeValue());
187 } else {
188 value = cNum.getNumber();
189 }
190 if (cNum.getShelvingOrder() == null) {
191 cNum.setShelvingOrder(new ShelvingOrder());
192 }
193 cNum.getShelvingOrder().setFullValue(value);
194 }
195 }
196 }
197
198
199
200
201
202
203 public void computeCallNumberType(CallNumber callNumber) {
204 Set<String> validCallNumberTypeSet = org.kuali.ole.utility.callnumber.CallNumberType.validCallNumberTypeCodeSet;
205 if (callNumber != null) {
206 if (callNumber.getCallNumberType() != null) {
207 String callNumberTypeCode = callNumber.getCallNumberType().getCodeValue();
208 String callNumberTypeName = "";
209
210 if ((StringUtils.isNotEmpty(callNumberTypeCode)) && (validCallNumberTypeSet
211 .contains(callNumberTypeCode))) {
212 callNumberTypeName = org.kuali.ole.utility.callnumber.CallNumberType.valueOf(callNumberTypeCode).getDescription();
213 callNumber.getCallNumberType().setFullValue(callNumberTypeName);
214 }
215 }
216 }
217 }
218
219 protected boolean validateCallNumber(String callNumber, String codeValue) throws OleDocStoreException {
220 boolean isValid = false;
221 if (StringUtils.isNotEmpty(callNumber) && StringUtils.isNotEmpty(codeValue)) {
222 org.kuali.ole.utility.callnumber.CallNumber callNumberObj = CallNumberFactory.getInstance().getCallNumber(codeValue);
223 if (callNumberObj != null) {
224 isValid = callNumberObj.isValid(callNumber);
225 }
226 }
227 return isValid;
228 }
229
230 protected String buildSortableCallNumber(String callNumber, String codeValue) throws OleDocStoreException {
231 String shelvingOrder = "";
232 if (StringUtils.isNotEmpty(callNumber) && StringUtils.isNotEmpty(codeValue)) {
233 org.kuali.ole.utility.callnumber.CallNumber callNumberObj = CallNumberFactory.getInstance().getCallNumber(codeValue);
234 if (callNumberObj != null) {
235 shelvingOrder = callNumberObj.getSortableKey(callNumber);
236
237 }
238 }
239 return shelvingOrder;
240 }
241 }