1 package org.kuali.ole.docstore.document.rdbms;
2
3 import org.kuali.ole.DocumentUniqueIDPrefix;
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.OleHoldings;
7 import org.kuali.ole.docstore.common.document.content.instance.ShelvingOrder;
8 import org.kuali.ole.docstore.common.document.content.instance.ShelvingScheme;
9 import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.*;
10 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
11 import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
12 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
13 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
14 import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
15 import org.kuali.ole.docstore.common.document.content.instance.xstream.InstanceOlemlRecordProcessor;
16 import org.kuali.rice.krad.service.BusinessObjectService;
17 import org.kuali.rice.krad.service.KRADServiceLocator;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import javax.jcr.Node;
22 import java.sql.Timestamp;
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29
30
31
32
33
34
35
36 public class RdbmsWorkHoldingsDocumentManager
37 extends RdbmsWorkInstanceDocumentManager {
38
39 private static RdbmsWorkHoldingsDocumentManager ourInstanceRdbms = null;
40 private static final Logger LOG = LoggerFactory
41 .getLogger(RdbmsWorkHoldingsDocumentManager.class);
42 private BusinessObjectService businessObjectService;
43
44 public static RdbmsWorkHoldingsDocumentManager getInstance() {
45 if (null == ourInstanceRdbms) {
46 ourInstanceRdbms = new RdbmsWorkHoldingsDocumentManager();
47 }
48 return ourInstanceRdbms;
49 }
50
51 public BusinessObjectService getBusinessObjectService() {
52 if (null == businessObjectService) {
53 businessObjectService = KRADServiceLocator.getBusinessObjectService();
54 }
55 return businessObjectService;
56 }
57
58 @Override
59 public void deleteDocs(RequestDocument requestDocument, Object object) {
60 ResponseDocument responseDocument = new ResponseDocument();
61 BusinessObjectService businessObjectService = (BusinessObjectService) object;
62 HoldingsRecord holdingsRecord = new HoldingsRecord();
63 String holdingsId = DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid());
64 Map itemMap = new HashMap();
65 itemMap.put("holdingsId", holdingsId);
66 holdingsRecord.setHoldingsId(holdingsId);
67 getBusinessObjectService().delete(holdingsRecord);
68 buildResponseDocument(requestDocument, holdingsRecord, responseDocument);
69
70 }
71
72 @Override
73 public ResponseDocument checkoutContent(RequestDocument requestDocument, Object object) {
74 BusinessObjectService businessObjectService = (BusinessObjectService) object;
75 ResponseDocument responseDocument = new ResponseDocument();
76 Map parentCriteria1 = new HashMap();
77 parentCriteria1.put("holdingsId", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
78 List<HoldingsRecord> holdingsRecordList = (List<HoldingsRecord>) getBusinessObjectService().findMatching(HoldingsRecord.class, parentCriteria1);
79 if(holdingsRecordList != null && holdingsRecordList.size() > 0) {
80 HoldingsRecord holdingsRecord = holdingsRecordList.get(0);
81 OleHoldings oleHoldings = buildHoldingsContent(holdingsRecord);
82 String content = new InstanceOlemlRecordProcessor().toXML(oleHoldings);
83 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
84 additionalAttributes.setAttribute(AdditionalAttributes.STAFFONLYFLAG, holdingsRecord.getStaffOnlyFlag().toString());
85 Map<String, String> mapObject = new HashMap<String, String>();
86 mapObject.put("staffOnlyFlag", holdingsRecord.getStaffOnlyFlag().toString());
87 additionalAttributes.setAttributeMap(mapObject);
88 Content contentObj = new Content();
89 contentObj.setContent(content);
90 responseDocument.setUuid(requestDocument.getUuid());
91 responseDocument.setCategory(requestDocument.getCategory());
92 responseDocument.setType(requestDocument.getType());
93 responseDocument.setFormat(requestDocument.getFormat());
94 responseDocument.setContent(contentObj);
95 responseDocument.setAdditionalAttributes(additionalAttributes);
96 } else {
97 responseDocument.setStatus("Failed");
98 responseDocument.setStatusMessage("Holdings does not exist.");
99 }
100 return responseDocument;
101 }
102
103
104 @Override
105 public void checkInContent(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
106 AdditionalAttributes attributes = requestDocument.getAdditionalAttributes();
107 BusinessObjectService businessObjectService = (BusinessObjectService) object;
108 if (requestDocument.getContent().getContent() != null) {
109 modifyContent(requestDocument, businessObjectService, DocumentUniqueIDPrefix.getDocumentId(requestDocument.getId()));
110 String content = requestDocument.getContent().getContent();
111 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
112 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(content);
113 Map parentCriteria1 = new HashMap();
114 parentCriteria1.put("holdingsId", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
115 HoldingsRecord holdingsRecord = getBusinessObjectService().findByPrimaryKey(HoldingsRecord.class, parentCriteria1);
116 if (attributes != null) {
117 holdingsRecord.setStaffOnlyFlag(Boolean.valueOf(attributes.getAttribute(AdditionalAttributes.STAFFONLYFLAG)));
118 holdingsRecord.setCreatedBy(attributes.getAttribute(AdditionalAttributes.HOLDINGS_CREATED_BY));
119 holdingsRecord.setUpdatedBy(attributes.getAttribute(AdditionalAttributes.HOLDINGS_UPDATED_BY));
120
121 String createdDateForHoldings = attributes.getAttribute(AdditionalAttributes.HOLDINGS_DATE_ENTERED);
122 createdDateForHoldings(holdingsRecord,requestDocument.getAdditionalAttributes());
123
124 }
125 buildHoldingRecordForCheckIn(oleHoldings, holdingsRecord);
126
127 requestDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(holdingsRecord.getUniqueIdPrefix(), holdingsRecord.getHoldingsId()));
128 respDoc.setAdditionalAttributes(attributes);
129 buildResponseDocument(requestDocument, holdingsRecord, respDoc);
130 }
131 }
132
133 @Override
134 public Node storeDocument(RequestDocument requestDocument, Object object, ResponseDocument responseDocument)
135 throws OleDocStoreException {
136 return null;
137 }
138
139 @Override
140 public void validateInput(RequestDocument requestDocument, Object object, List<String> valuesList) throws OleDocStoreException {
141 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
142 if (requestDocument.getContent() != null && requestDocument.getContent().getContent() != null) {
143 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(requestDocument.getContent().getContent());
144 validateHoldings(oleHoldings);
145 }
146 }
147
148 public ResponseDocument buildResponseDocument(RequestDocument requestDocument, HoldingsRecord holdingsRecord,
149 ResponseDocument responseDocument) {
150 responseDocument.setId(holdingsRecord.getHoldingsId());
151 responseDocument.setCategory(requestDocument.getCategory());
152 responseDocument.setType(requestDocument.getType());
153 responseDocument.setFormat(requestDocument.getFormat());
154 responseDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(holdingsRecord.getUniqueIdPrefix(), holdingsRecord.getHoldingsId()));
155
156 return responseDocument;
157 }
158
159 public void validateHoldings(OleHoldings oleHoldings) throws OleDocStoreException {
160 if (oleHoldings.getCallNumber() != null) {
161 CallNumber callNumber = oleHoldings.getCallNumber();
162 validateCallNumber(callNumber);
163 }
164 }
165
166 private void buildHoldingRecordForCheckIn(OleHoldings oleHoldings, HoldingsRecord holdingsRecord) {
167 saveHoldingsRecord(oleHoldings, holdingsRecord);
168 }
169
170 protected void modifyContent(RequestDocument reqDoc, BusinessObjectService businessObjectService, String holdingId) throws OleDocStoreException {
171 HoldingOlemlRecordProcessor holdProcessor = new HoldingOlemlRecordProcessor();
172 String instanceId = "";
173 if (reqDoc != null && reqDoc.getContent() != null) {
174
175 OleHoldings newHold = holdProcessor.fromXML(reqDoc.getContent().getContent());
176 if (newHold != null && newHold.getCallNumber() != null) {
177 CallNumber cNum = newHold.getCallNumber();
178
179
180 if (cNum.getNumber() != null && cNum.getNumber().trim().length() > 0) {
181
182 if (cNum.getShelvingOrder() == null) {
183 cNum.setShelvingOrder(new ShelvingOrder());
184 }
185
186 boolean status = true;
187 RdbmsWorkItemDocumentManager rdbmsWorkItemDocumentManager = RdbmsWorkItemDocumentManager.getInstance();
188 Map holdingsMap = new HashMap();
189 holdingsMap.put("holdingsId", holdingId);
190 OleHoldings existHol = null;
191 List<HoldingsRecord> holdingsRecords = (List<HoldingsRecord>) businessObjectService.findMatching(HoldingsRecord.class, holdingsMap);
192 if (holdingsRecords != null && holdingsRecords.size() > 0) {
193 HoldingsRecord holdingsRecord = holdingsRecords.get(0);
194 existHol = buildHoldingsContent(holdingsRecord);
195
196 }
197 if (existHol != null) {
198 setHoldValuesFromNullToEmpty(existHol.getCallNumber());
199 setHoldValuesFromNullToEmpty(newHold.getCallNumber());
200 if (existHol.getCallNumber() != null && existHol.getCallNumber().getNumber() != null && !(existHol.getCallNumber().getNumber().equalsIgnoreCase(cNum.getNumber()) &&
201 existHol.getCallNumber().getShelvingScheme().getCodeValue().equalsIgnoreCase(cNum.getShelvingScheme().getCodeValue()))) {
202 processCallNumber(newHold);
203 String newHolXml = holdProcessor.toXML(newHold);
204 reqDoc.getContent().setContent(newHolXml);
205 rdbmsWorkItemDocumentManager.updateItemCallNumberFromHoldings(instanceId, newHold, reqDoc);
206 status = false;
207 }
208 }
209 if (status && !(cNum.getShelvingOrder().getFullValue() != null &&
210 cNum.getShelvingOrder().getFullValue().trim().length() > 0)) {
211 processCallNumber(newHold);
212 String newHolXml = holdProcessor.toXML(newHold);
213 reqDoc.getContent().setContent(newHolXml);
214
215 }
216 }
217
218 }
219 }
220 }
221
222 private void setHoldValuesFromNullToEmpty(CallNumber callNumber) {
223 if (callNumber != null) {
224 if (callNumber.getNumber() == null) {
225 callNumber.setNumber("");
226 }
227 if (callNumber.getShelvingScheme() == null) {
228 callNumber.setShelvingScheme(new ShelvingScheme());
229 }
230 if (callNumber.getShelvingScheme().getCodeValue() == null) {
231 callNumber.getShelvingScheme().setCodeValue("");
232 }
233 }
234 }
235
236
237 private void createdDateForHoldings(HoldingsRecord holdingsRecord, AdditionalAttributes additionalAttributes) {
238 DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
239 Timestamp createdDate = null;
240 String createdDateForHoldings = additionalAttributes.getAttribute(AdditionalAttributes.HOLDINGS_DATE_ENTERED);
241 String updateDateForHoldings = additionalAttributes.getAttribute(AdditionalAttributes.HOLDINGS_LAST_UPDATED);
242 holdingsRecord.setCreatedBy(additionalAttributes.getAttribute(AdditionalAttributes.HOLDINGS_CREATED_BY));
243 holdingsRecord.setUpdatedBy(additionalAttributes.getAttribute(AdditionalAttributes.HOLDINGS_UPDATED_BY));
244 try {
245 createdDate = new Timestamp(df.parse(createdDateForHoldings).getTime());
246 holdingsRecord.setCreatedDate(createdDate);
247 holdingsRecord.setUpdatedDate(new Timestamp(df.parse(updateDateForHoldings).getTime()));
248
249 } catch (Exception e) {
250 LOG.error("Created Date for Holdings" + e);
251 }
252 }
253
254 private void updatedDateForHoldings(HoldingsRecord holdingsRecord, String DateForHoldings) {
255 DateFormat df = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
256 Timestamp updatedDate = null;
257 try {
258 updatedDate = new Timestamp(df.parse(DateForHoldings).getTime());
259 holdingsRecord.setUpdatedDate(updatedDate);
260
261 } catch (Exception e) {
262 LOG.error("Created Date for Holdings" + e);
263 }
264 }
265
266 }