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