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.common.document.content.instance.*;
7 import org.kuali.ole.docstore.common.document.content.instance.xstream.HoldingOlemlRecordProcessor;
8 import org.kuali.ole.docstore.common.document.content.instance.xstream.InstanceOlemlRecordProcessor;
9 import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
10 import org.kuali.ole.docstore.discovery.service.QueryServiceImpl;
11
12 import org.kuali.ole.docstore.model.enums.DocCategory;
13 import org.kuali.ole.docstore.model.enums.DocFormat;
14 import org.kuali.ole.docstore.model.enums.DocType;
15 import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.*;
16 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
17 import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
18 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
19 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
20 import org.kuali.ole.docstore.common.document.content.instance.OleHoldings;
21 import org.kuali.ole.docstore.common.document.content.instance.ShelvingScheme;
22 import org.kuali.rice.krad.service.BusinessObjectService;
23 import org.kuali.rice.krad.service.KRADServiceLocator;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import javax.jcr.Node;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33
34
35
36
37
38
39
40 @Deprecated
41 public class RdbmsWorkItemDocumentManager extends RdbmsWorkInstanceDocumentManager {
42
43 private static RdbmsWorkItemDocumentManager ourInstanceRdbms = null;
44 private static final Logger LOG = LoggerFactory
45 .getLogger(RdbmsWorkItemDocumentManager.class);
46 private BusinessObjectService businessObjectService;
47
48 public static RdbmsWorkItemDocumentManager getInstance() {
49 if (null == ourInstanceRdbms) {
50 ourInstanceRdbms = new RdbmsWorkItemDocumentManager();
51 }
52 return ourInstanceRdbms;
53 }
54
55 public BusinessObjectService getBusinessObjectService() {
56 if (null == businessObjectService) {
57 businessObjectService = KRADServiceLocator.getBusinessObjectService();
58 }
59 return businessObjectService;
60 }
61
62 @Override
63 public void deleteDocs(RequestDocument requestDocument, Object object) {
64
65 ResponseDocument responseDocument = new ResponseDocument();
66 BusinessObjectService businessObjectService = (BusinessObjectService) object;
67
68 String itemId = DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid());
69 Map itemMap = new HashMap();
70 itemMap.put("itemId", itemId);
71 List<ItemRecord> itemRecords = (List<ItemRecord>) businessObjectService.findMatching(ItemRecord.class, itemMap);
72 if (itemRecords != null && itemRecords.size() > 0) {
73 ItemRecord itemRecord = itemRecords.get(0);
74 if (itemRecord.getFormerIdentifierRecords() != null && itemRecord.getFormerIdentifierRecords().size() > 0) {
75 List<FormerIdentifierRecord> formerIdentifierRecords = itemRecord.getFormerIdentifierRecords();
76 businessObjectService.delete(formerIdentifierRecords);
77 }
78
79
80 if (itemRecord.getItemNoteRecords() != null && itemRecord.getItemNoteRecords().size() > 0) {
81 List<ItemNoteRecord> itemNoteRecords = itemRecord.getItemNoteRecords();
82 businessObjectService.delete(itemNoteRecords);
83 }
84
85
86 if (itemRecord.getLocationsCheckinCountRecords() != null && itemRecord.getLocationsCheckinCountRecords().size() > 0) {
87 List<LocationsCheckinCountRecord> locationsCheckinCountRecords = itemRecord.getLocationsCheckinCountRecords();
88 businessObjectService.delete(locationsCheckinCountRecords);
89 }
90 itemRecord.setItemStatusId(null);
91 itemRecord.setItemTypeId(null);
92 itemRecord.setTempItemTypeId(null);
93 itemRecord.setStatisticalSearchId(null);
94 itemRecord.setHighDensityStorageId(null);
95 businessObjectService.delete(itemRecord);
96 buildResponseDocument(requestDocument, itemRecord, responseDocument);
97 }
98
99 }
100
101 @Override
102 public ResponseDocument checkoutContent(RequestDocument requestDocument, Object object) {
103 ResponseDocument responseDocument = new ResponseDocument();
104 BusinessObjectService businessObjectService = (BusinessObjectService) object;
105 Map parentCriteria1 = new HashMap();
106 parentCriteria1.put("itemId", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
107 List<ItemRecord> itemRecordList = (List<ItemRecord>) getBusinessObjectService().findMatching(ItemRecord.class, parentCriteria1);
108 if (itemRecordList != null && itemRecordList.size() > 0) {
109 ItemRecord itemRecord = itemRecordList.get(0);
110 Item item = buildItemContent(itemRecord);
111 String content = new InstanceOlemlRecordProcessor().toXML(item);
112 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
113
114
115
116
117
118
119 if (itemRecord.getStaffOnlyFlag() != null) {
120 additionalAttributes.setAttribute(AdditionalAttributes.STAFFONLYFLAG, itemRecord.getStaffOnlyFlag().toString());
121 }
122 Content contentObj = new Content();
123 contentObj.setContent(content);
124 responseDocument.setUuid(requestDocument.getUuid());
125 responseDocument.setCategory(requestDocument.getCategory());
126 responseDocument.setType(requestDocument.getType());
127 responseDocument.setFormat(requestDocument.getFormat());
128 responseDocument.setContent(contentObj);
129 responseDocument.setAdditionalAttributes(additionalAttributes);
130 } else {
131 responseDocument.setStatus("Failed");
132 responseDocument.setStatusMessage("Item does not exist.");
133 }
134 return responseDocument;
135
136 }
137
138
139 @Override
140 public void checkInContent(RequestDocument requestDocument, Object object, ResponseDocument respDoc) throws OleDocStoreException {
141 BusinessObjectService businessObjectService = (BusinessObjectService) object;
142 modifyContent(requestDocument, DocumentUniqueIDPrefix.getDocumentId(requestDocument.getId()));
143 AdditionalAttributes attributes = requestDocument.getAdditionalAttributes();
144 List<ItemRecord> itemRecords = new ArrayList<ItemRecord>();
145 String instanceId = "";
146 Map parentCriteria1 = new HashMap();
147 parentCriteria1.put("itemId", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getId()));
148 ItemRecord itemRecord = getBusinessObjectService().findByPrimaryKey(ItemRecord.class, parentCriteria1);
149 Item item = new ItemOlemlRecordProcessor().fromXML(requestDocument.getContent().getContent());
150 if (attributes != null) {
151 itemRecord.setStaffOnlyFlag(Boolean.valueOf(attributes.getAttribute(AdditionalAttributes.STAFFONLYFLAG)));
152 }
153 buildItemContentForCheckin(item, itemRecord);
154 requestDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(itemRecord.getUniqueIdPrefix(), itemRecord.getItemId()));
155 respDoc.setAdditionalAttributes(attributes);
156 buildResponseDocument(requestDocument, itemRecord, respDoc);
157 }
158
159 @Override
160 public Node storeDocument(RequestDocument requestDocument, Object object, ResponseDocument responseDocument)
161 throws OleDocStoreException {
162 return null;
163 }
164
165 @Override
166 public void validateInput(RequestDocument requestDocument, Object object, List<String> valuesList) throws OleDocStoreException {
167 BusinessObjectService businessObjectService = (BusinessObjectService) object;
168 validateItem(requestDocument, businessObjectService, valuesList);
169 }
170
171 @Override
172 public ResponseDocument deleteVerify(RequestDocument requestDocument, Object object) throws Exception {
173
174
175 LOG.debug("workItemDocumentManager deleteVerify method");
176
177 ResponseDocument responseDocument = new ResponseDocument();
178 List<String> itemIdentifierList = new ArrayList<String>();
179 itemIdentifierList.add(requestDocument.getUuid());
180 boolean exists = checkInstancesOrItemsExistsInOLE(itemIdentifierList);
181 if (exists) {
182 responseDocument.setId(requestDocument.getId());
183 responseDocument.setCategory(requestDocument.getCategory());
184 responseDocument.setType(requestDocument.getType());
185 responseDocument.setFormat(requestDocument.getFormat());
186 responseDocument.setUuid(requestDocument.getUuid());
187 responseDocument.setStatus("failure'");
188 responseDocument.setStatusMessage("Instances or Items in use. So deletion cannot be done");
189 return responseDocument;
190 }
191 else {
192 responseDocument.setUuid(requestDocument.getUuid());
193 responseDocument.setId(requestDocument.getId());
194 responseDocument.setCategory(requestDocument.getCategory());
195 responseDocument.setType(requestDocument.getType());
196 responseDocument.setFormat(requestDocument.getFormat());
197 responseDocument.setStatus("success");
198 responseDocument.setStatusMessage("success");
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234 return responseDocument;
235 }
236
237 public ResponseDocument buildResponseDocument(RequestDocument requestDocument, ItemRecord itemRecord, ResponseDocument responseDocument) {
238 responseDocument.setId(itemRecord.getItemId());
239 responseDocument.setCategory(requestDocument.getCategory());
240 responseDocument.setType(requestDocument.getType());
241 responseDocument.setFormat(requestDocument.getFormat());
242 responseDocument.setUuid(itemRecord.getItemId());
243
244 return responseDocument;
245 }
246
247 public void validateNewItem(RequestDocument linkedRequestDocument, BusinessObjectService businessObjectService, List<String> fieldValues, String id) throws OleDocStoreException {
248 try {
249 String itemContent = linkedRequestDocument.getContent().getContent();
250 ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
251 Item item = itemOlemlRecordProcessor.fromXML(itemContent);
252 itemBarcodeValidation(item, fieldValues, null);
253
254 if (item.getCallNumber() != null) {
255 Map instanceMap = new HashMap();
256 instanceMap.put("instanceId", DocumentUniqueIDPrefix.getDocumentId(id));
257 List<InstanceRecord> instanceRecords = (List<InstanceRecord>) getBusinessObjectService().findMatching(InstanceRecord.class, instanceMap);
258 HoldingsRecord holdingsRecord = null;
259 if (instanceRecords != null && instanceRecords.size() > 0) {
260 List<HoldingsRecord> holdingsRecords = instanceRecords.get(0).getHoldingsRecords();
261 if (holdingsRecords != null && holdingsRecords.size() > 0) {
262 holdingsRecord = holdingsRecords.get(0);
263 getHoldingsContentNValidateItem(holdingsRecord, item, getBusinessObjectService());
264 }
265 }
266 }
267 } catch (Exception e) {
268 throw new OleDocStoreException(e.getMessage(), e);
269 }
270 }
271
272 public void itemBarcodeValidation(Item item, List<String> fieldValues, String id) throws OleDocStoreException {
273 if (item.getAccessInformation() != null && StringUtils.isNotEmpty(item.getAccessInformation().getBarcode())) {
274 try {
275 if (fieldValues.contains(item.getAccessInformation().getBarcode()) ||
276 QueryServiceImpl.getInstance().isFieldValueExists(DocType.ITEM.getCode(), "ItemBarcode_display", item.getAccessInformation().getBarcode(), id)) {
277 throw new OleDocStoreException("Barcode " + item.getAccessInformation().getBarcode() + " already exists ");
278 }
279 fieldValues.add(item.getAccessInformation().getBarcode());
280 } catch (Exception e) {
281 throw new OleDocStoreException(e.getMessage(), e);
282 }
283 }
284
285 }
286
287 public void validateItem(RequestDocument linkedRequestDocument, BusinessObjectService businessObjectService, List<String> fieldValues) throws OleDocStoreException {
288 try {
289 String itemContent = linkedRequestDocument.getContent().getContent();
290 ItemOlemlRecordProcessor itemOlemlRecordProcessor = new ItemOlemlRecordProcessor();
291 Item item = itemOlemlRecordProcessor.fromXML(itemContent);
292
293 itemBarcodeValidation(item, fieldValues, linkedRequestDocument.getId());
294 if (item.getCallNumber() != null) {
295 Map itemMap = new HashMap();
296 itemMap.put("itemId", DocumentUniqueIDPrefix.getDocumentId(linkedRequestDocument.getId()));
297 List<ItemRecord> itemRecords = (List<ItemRecord>) getBusinessObjectService().findMatching(ItemRecord.class, itemMap);
298 if (itemRecords != null && itemRecords.size() > 0) {
299 Map instanceMap = new HashMap();
300
301 List<InstanceRecord> instanceRecords = (List<InstanceRecord>) getBusinessObjectService().findMatching(InstanceRecord.class, instanceMap);
302 HoldingsRecord holdingsRecord = null;
303 if (instanceRecords != null && instanceRecords.size() > 0) {
304 List<HoldingsRecord> holdingsRecords = instanceRecords.get(0).getHoldingsRecords();
305 if (holdingsRecords != null && holdingsRecords.size() > 0) {
306 holdingsRecord = holdingsRecords.get(0);
307 getHoldingsContentNValidateItem(holdingsRecord, item, getBusinessObjectService());
308 }
309 }
310
311 }
312 }
313 } catch (Exception e) {
314 throw new OleDocStoreException(e.getMessage(), e);
315 }
316 }
317
318 private void getHoldingsContentNValidateItem(HoldingsRecord holdingsRecord, Item item, BusinessObjectService businessObjectService) throws Exception {
319 RequestDocument requestDocument = new RequestDocument();
320 requestDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(holdingsRecord.getUniqueIdPrefix(), holdingsRecord.getHoldingsId()));
321 ResponseDocument responseDocument = RdbmsWorkHoldingsDocumentManager.getInstance().checkoutContent(requestDocument, businessObjectService);
322 String content = responseDocument.getContent().getContent();
323 HoldingOlemlRecordProcessor holdingOlemlRecordProcessor = new HoldingOlemlRecordProcessor();
324 OleHoldings oleHoldings = holdingOlemlRecordProcessor.fromXML(content);
325 CallNumber callNumber = item.getCallNumber();
326 validateCallNumber(callNumber, oleHoldings);
327 }
328
329 protected void buildItemContentForCheckin(Item item, ItemRecord itemRecord) {
330
331 saveItemRecord(item, itemRecord);
332 }
333
334 protected void modifyContent(RequestDocument reqDoc, String itemId)
335 throws OleDocStoreException {
336 if (reqDoc.getOperation().equalsIgnoreCase("checkIn")) {
337 ItemOlemlRecordProcessor itemProcessor = new ItemOlemlRecordProcessor();
338
339 if (reqDoc.getContent() != null && reqDoc.getContent().getContent() != null) {
340
341 Item newItem = itemProcessor.fromXML(reqDoc.getContent().getContent());
342
343 Boolean status = true;
344 if (newItem.getCallNumber() == null) {
345 newItem.setCallNumber(new CallNumber());
346 }
347 CallNumber cNum = newItem.getCallNumber();
348 if (!(cNum.getShelvingOrder() != null && cNum.getShelvingOrder().getFullValue() != null &&
349 cNum.getShelvingOrder().getFullValue().trim().length() > 0)) {
350 if (!(cNum.getNumber() != null && cNum.getNumber().trim().length() > 0)) {
351
352 OleHoldings hold = getHoldings(itemId);
353 updateShelvingOrder(newItem, hold);
354 status = false;
355 } else {
356 updateShelvingOrder(newItem, null);
357 status = false;
358 }
359 }
360 if (status) {
361
362 String existingItemXml = null;
363 Map itemMap = new HashMap();
364 itemMap.put("itemId", itemId);
365 List<ItemRecord> itemRecords = (List<ItemRecord>) getBusinessObjectService().findMatching(ItemRecord.class, itemMap);
366 if (itemRecords != null && itemRecords.size() > 0) {
367 ItemRecord itemRecord = itemRecords.get(0);
368 existingItemXml = itemProcessor.toXML(buildItemContent(itemRecord));
369 }
370
371
372 if (existingItemXml != null) {
373 Item existItem = itemProcessor.fromXML(existingItemXml);
374 if (existItem.getCallNumber() == null) {
375 existItem.setCallNumber(new CallNumber());
376 }
377 CallNumber existCNum = existItem.getCallNumber();
378 setItemValuesFromNullToEmpty(existItem);
379 setItemValuesFromNullToEmpty(newItem);
380 cNum = newItem.getCallNumber();
381 computeCallNumberType(cNum);
382 if (!(existCNum.getNumber().equalsIgnoreCase((cNum.getNumber()))) |
383 !(existCNum.getShelvingScheme().getCodeValue().equalsIgnoreCase(cNum.getShelvingScheme().getCodeValue())) |
384 !((existItem.getEnumeration()).equalsIgnoreCase(newItem.getEnumeration())) |
385 !((existItem.getChronology()).equalsIgnoreCase(newItem.getChronology())) |
386 !((existItem.getCopyNumber()).equalsIgnoreCase(newItem.getCopyNumber()))) {
387 if (!(cNum.getNumber() != null && cNum.getNumber().trim().length() > 0)) {
388 OleHoldings hold = getHoldings(itemId);
389 updateShelvingOrder(newItem, hold);
390 } else {
391 updateShelvingOrder(newItem, null);
392 }
393
394 }
395 }
396 }
397 reqDoc.getContent().setContent(itemProcessor.toXML(newItem));
398 }
399 }
400 }
401
402 private OleHoldings getHoldings(String itemId) {
403 OleHoldings oleHoldings = null;
404 Map itemMap = new HashMap();
405 itemMap.put("itemId", itemId);
406 List<ItemRecord> itemRecords = (List<ItemRecord>) getBusinessObjectService().findMatching(ItemRecord.class, itemMap);
407 if (itemRecords != null && itemRecords.size() > 0) {
408 ItemRecord itemRecord = itemRecords.get(0);
409 String instanceId = null;
410 Map instanceMap = new HashMap();
411 instanceMap.put("instanceId", instanceId);
412 List<InstanceRecord> instanceRecords = (List<InstanceRecord>) getBusinessObjectService().findMatching(InstanceRecord.class, instanceMap);
413 if (instanceRecords != null && instanceRecords.size() > 0) {
414 List<HoldingsRecord> holdingsRecords = instanceRecords.get(0).getHoldingsRecords();
415 if (holdingsRecords != null && holdingsRecords.size() > 0) {
416 HoldingsRecord holdingsRecord = holdingsRecords.get(0);
417 oleHoldings = buildHoldingsContent(holdingsRecord);
418 }
419 }
420 }
421 return oleHoldings;
422 }
423
424 private void setItemValuesFromNullToEmpty(Item item) {
425 CallNumber existCNum = item.getCallNumber();
426 if (existCNum == null) {
427 item.setCallNumber(new CallNumber());
428 }
429 if (existCNum.getNumber() == null) {
430 existCNum.setNumber("");
431 }
432 ShelvingScheme existSS = existCNum.getShelvingScheme();
433 if (existSS == null) {
434 existCNum.setShelvingScheme(new ShelvingScheme());
435 existSS = existCNum.getShelvingScheme();
436 }
437 if (existSS != null && !(existSS.getCodeValue() != null && existSS.getCodeValue().trim().length() > 0)) {
438 existSS.setCodeValue("");
439 }
440 if (item.getEnumeration() == null) {
441 item.setEnumeration("");
442 }
443 if (item.getChronology() == null) {
444 item.setChronology("");
445 }
446 if (item.getCopyNumber() == null) {
447 item.setCopyNumber("");
448 }
449 }
450
451 protected void updateItemCallNumberFromHoldings(String instanceId, OleHoldings newHold, RequestDocument reqDoc)
452 throws OleDocStoreException {
453 Map instanceMap = new HashMap();
454 instanceMap.put("instanceId", instanceId);
455 List<InstanceRecord> instanceRecords = (List<InstanceRecord>) getBusinessObjectService().findMatching(InstanceRecord.class, instanceMap);
456 InstanceRecord instanceRecord = null;
457 if (instanceRecords != null && instanceRecords.size() > 0) {
458 instanceRecord = instanceRecords.get(0);
459 }
460 if (instanceRecord != null) {
461 for (ItemRecord itemRecord : instanceRecord.getItemRecords()) {
462 ItemOlemlRecordProcessor itemProcessor = new ItemOlemlRecordProcessor();
463 Item item = buildItemContent(itemRecord);
464 if (item != null) {
465 String itemId = itemRecord.getItemId();
466 if (item.getCallNumber() == null) {
467 item.setCallNumber(new CallNumber());
468 }
469 item.getCallNumber().setNumber("");
470 if (item.getCallNumber().getShelvingScheme() != null)
471 item.getCallNumber().getShelvingScheme().setCodeValue("");
472 updateShelvingOrder(item, newHold);
473 String newItemXml = itemProcessor.toXML(item);
474 buildRequestDocumentForItem(newItemXml, reqDoc, itemId);
475 }
476
477 }
478 }
479 }
480
481 private void buildRequestDocumentForItem(String newItemXml, RequestDocument reqDoc, String uuid) {
482 RequestDocument requestDocument = new RequestDocument();
483 requestDocument.setOperation("checkIn");
484 requestDocument.setCategory(DocCategory.WORK.getCode());
485 requestDocument.setType(DocType.ITEM.getCode());
486 requestDocument.setFormat(DocFormat.OLEML.getCode());
487 requestDocument.setUuid(uuid);
488 requestDocument.setId(uuid);
489 Content content = new Content();
490 content.setContent(newItemXml);
491 requestDocument.setContent(content);
492 List<RequestDocument> reqDocList = reqDoc.getLinkedRequestDocuments();
493 if (reqDocList == null) {
494 reqDoc.setLinkedRequestDocuments(new ArrayList<RequestDocument>());
495 }
496 reqDocList.add(requestDocument);
497 }
498
499
500 }