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.document.DocumentManager;
7 import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.*;
8 import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
9 import org.kuali.ole.docstore.model.xmlpojo.ingest.Content;
10 import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
11 import org.kuali.ole.docstore.model.xmlpojo.ingest.RequestDocument;
12 import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
13 import org.kuali.ole.docstore.model.xmlpojo.ingest.ResponseDocument;
14 import org.kuali.ole.utility.Constants;
15 import org.kuali.rice.krad.service.BusinessObjectService;
16 import org.kuali.rice.krad.service.KRADServiceLocator;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import javax.jcr.Node;
21 import java.sql.Timestamp;
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31
32
33
34
35
36
37
38
39 public class RdbmsWorkBibDocumentManager extends RdbmsAbstarctDocumentManager {
40
41 private Logger logger = LoggerFactory.getLogger(this.getClass());
42 private static RdbmsWorkBibDocumentManager ourInstance = null;
43
44 public static RdbmsWorkBibDocumentManager getInstance() {
45 if (null == ourInstance) {
46 ourInstance = new RdbmsWorkBibDocumentManager();
47 }
48 return ourInstance;
49 }
50
51
52 public Node storeDocument(RequestDocument requestDocument, Object object, ResponseDocument respDoc) {
53 BusinessObjectService businessObjectService = (BusinessObjectService) object;
54 BibRecord bibRecord = new BibRecord();
55
56 modifyAdditionalAttributes(requestDocument);
57 AdditionalAttributes attributes = requestDocument.getAdditionalAttributes();
58 bibRecord.setContent(requestDocument.getContent().getContent());
59 boolean isBibIdFlag = getBibIdFromBibXMLContent(bibRecord);
60 if (attributes != null) {
61
62
63
64
65
66
67
68
69
70
71
72
73 bibRecord.setCreatedBy(attributes.getAttribute(AdditionalAttributes.CREATED_BY));
74 bibRecord.setStatusUpdatedBy(attributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_BY));
75 bibRecord.setDateCreated(Timestamp.valueOf(attributes.getAttribute(AdditionalAttributes.DATE_ENTERED)));
76
77 if (StringUtils.isNotEmpty(attributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_ON))) {
78 bibRecord.setStatusUpdatedDate(Timestamp.valueOf(attributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_ON)));
79 }
80 bibRecord.setFassAddFlag(Boolean.valueOf(attributes.getAttribute(AdditionalAttributes.FAST_ADD_FLAG)));
81 bibRecord.setFormerId("");
82 bibRecord.setSuppressFromPublic(attributes.getAttribute(AdditionalAttributes.SUPRESS_FROM_PUBLIC));
83 bibRecord.setStatus(attributes.getAttribute(AdditionalAttributes.STATUS));
84
85 bibRecord.setStaffOnlyFlag(Boolean.valueOf(attributes.getAttribute(AdditionalAttributes.STAFFONLYFLAG)));
86 }
87 bibRecord.setUniqueIdPrefix(DocumentUniqueIDPrefix.getPrefix(requestDocument.getCategory(), requestDocument.getType(), requestDocument.getFormat()));
88 businessObjectService.save(bibRecord);
89 String uuid = DocumentUniqueIDPrefix.getPrefixedId(bibRecord.getUniqueIdPrefix(), bibRecord.getBibId());
90 if (isBibIdFlag) {
91 modifyDocumentContent(requestDocument, bibRecord.getBibId(), businessObjectService);
92 }
93 requestDocument.setUuid(uuid);
94 buildResponseDocument(requestDocument, bibRecord, respDoc);
95 return null;
96 }
97
98
99 public ResponseDocument buildResponseDocument(RequestDocument requestDocument, BibRecord bibRecord, ResponseDocument responseDocument) {
100 responseDocument.setId(requestDocument.getId());
101 responseDocument.setCategory(requestDocument.getCategory());
102 responseDocument.setType(requestDocument.getType());
103 responseDocument.setFormat(requestDocument.getFormat());
104 responseDocument.setStatus("Success");
105 responseDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(bibRecord.getUniqueIdPrefix(), bibRecord.getBibId()));
106 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
107 additionalAttributes.setAttribute(AdditionalAttributes.DATE_ENTERED,bibRecord.getDateEntered() != null ? bibRecord.getDateEntered().toString() : "");
108 additionalAttributes.setAttribute(AdditionalAttributes.STATUS, bibRecord.getStatus());
109 additionalAttributes.setAttribute(AdditionalAttributes.STATUS_UPDATED_ON, "");
110 additionalAttributes.setAttribute(AdditionalAttributes.CREATED_BY, "");
111 additionalAttributes.setAttribute(AdditionalAttributes.LAST_UPDATED, "");
112 if(bibRecord.getFassAddFlag() != null) {
113 additionalAttributes.setAttribute(AdditionalAttributes.FAST_ADD_FLAG, bibRecord.getFassAddFlag().toString());
114 }
115 additionalAttributes.setAttribute(AdditionalAttributes.STATUS, bibRecord.getStatus());
116 if (bibRecord.getStaffOnlyFlag() != null) {
117 additionalAttributes.setAttribute(AdditionalAttributes.STAFFONLYFLAG, bibRecord.getStaffOnlyFlag().toString());
118 } else {
119 additionalAttributes.setAttribute(AdditionalAttributes.STAFFONLYFLAG, "false");
120 }
121 additionalAttributes
122 .setAttribute(AdditionalAttributes.SUPRESS_FROM_PUBLIC, bibRecord.getSuppressFromPublic());
123 responseDocument.setAdditionalAttributes(additionalAttributes);
124 return responseDocument;
125 }
126
127
128 @Override
129 public void deleteDocs(RequestDocument requestDocument, Object object) {
130 ResponseDocument responseDocument = new ResponseDocument();
131 BusinessObjectService businessObjectService = (BusinessObjectService) object;
132 BibRecord bibRecord = new BibRecord();
133 Map map = new HashMap();
134 map.put("bibId", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
135 List<BibRecord> bibRecords = (List<BibRecord>) businessObjectService.findMatching(BibRecord.class, map);
136 if (bibRecords != null && bibRecords.size() > 0) {
137 bibRecord = bibRecords.get(0);
138
139 InstanceRecord instanceRecord = new InstanceRecord();
140 List<InstanceRecord> instanceRecords = null;
141 if (instanceRecords != null && instanceRecords.size() > 0) {
142 for (int i = 0; i < instanceRecords.size(); i++) {
143 instanceRecord = instanceRecords.get(i);
144 if (instanceRecord.getHoldingsRecords() != null && instanceRecord.getHoldingsRecords().size() > 0) {
145 HoldingsRecord holdingsRecord = instanceRecord.getHoldingsRecords().get(0);
146
147 if (holdingsRecord.getExtentOfOwnerShipRecords() != null && holdingsRecord.getExtentOfOwnerShipRecords().size() > 0) {
148 List<ExtentOfOwnerShipRecord> extentOfOwnerShipRecords = holdingsRecord.getExtentOfOwnerShipRecords();
149 for (int j = 0; j < extentOfOwnerShipRecords.size(); j++) {
150 List<ExtentNoteRecord> extentNoteRecords = extentOfOwnerShipRecords.get(j).getExtentNoteRecords();
151 if (extentNoteRecords != null && extentNoteRecords.size() > 0) {
152 businessObjectService.delete(extentNoteRecords);
153 }
154 }
155 businessObjectService.delete(extentOfOwnerShipRecords);
156 }
157 if (holdingsRecord.getHoldingsNoteRecords() != null && holdingsRecord.getHoldingsNoteRecords().size() > 0) {
158 List<HoldingsNoteRecord> holdingsNoteRecords = holdingsRecord.getHoldingsNoteRecords();
159 businessObjectService.delete(holdingsNoteRecords);
160 }
161
162
163
164
165
166 holdingsRecord.setCallNumberTypeId(null);
167 holdingsRecord.setReceiptStatusId(null);
168 businessObjectService.delete(holdingsRecord);
169 }
170 List<ItemRecord> itemRecords = instanceRecord.getItemRecords();
171 for (ItemRecord itemRecord : itemRecords) {
172 if (itemRecord.getFormerIdentifierRecords() != null && itemRecord.getFormerIdentifierRecords().size() > 0) {
173 List<FormerIdentifierRecord> formerIdentifierRecords = itemRecord.getFormerIdentifierRecords();
174 businessObjectService.delete(formerIdentifierRecords);
175 }
176 if (itemRecord.getItemNoteRecords() != null && itemRecord.getItemNoteRecords().size() > 0) {
177 List<ItemNoteRecord> itemNoteRecords = itemRecord.getItemNoteRecords();
178 businessObjectService.delete(itemNoteRecords);
179 }
180 if (itemRecord.getLocationsCheckinCountRecords() != null && itemRecord.getLocationsCheckinCountRecords().size() > 0) {
181 List<LocationsCheckinCountRecord> locationsCheckinCountRecords = itemRecord.getLocationsCheckinCountRecords();
182 businessObjectService.delete(locationsCheckinCountRecords);
183 }
184 itemRecord.setItemStatusId(null);
185 itemRecord.setItemTypeId(null);
186 itemRecord.setTempItemTypeId(null);
187 itemRecord.setStatisticalSearchId(null);
188 itemRecord.setHighDensityStorageId(null);
189 businessObjectService.delete(itemRecord);
190
191 }
192 businessObjectService.delete(instanceRecord);
193 }
194 }
195
196 }
197 businessObjectService.delete(bibRecord);
198
199 buildResponseDocument(requestDocument, bibRecord, responseDocument);
200 }
201
202
203
204
205
206
207 protected void modifyAdditionalAttributes(RequestDocument requestDocument) {
208 Date date = new Date();
209 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
210 String dateStr = sdf.format(date);
211 String user = requestDocument.getUser();
212 String statusFromReqDoc = "";
213 String statusFromNode = "";
214 AdditionalAttributes additionalAttributes = requestDocument.getAdditionalAttributes();
215 if (additionalAttributes == null) {
216 additionalAttributes = new AdditionalAttributes();
217 }
218 if (requestDocument.getOperation() != null) {
219 if (requestDocument.getOperation().equalsIgnoreCase(Request.Operation.ingest.toString())) {
220 statusFromReqDoc = additionalAttributes.getAttribute(AdditionalAttributes.STATUS);
221 additionalAttributes.setAttribute(AdditionalAttributes.DATE_ENTERED, dateStr);
222 additionalAttributes.setAttribute(AdditionalAttributes.CREATED_BY, user);
223
224 if (StringUtils.isNotEmpty(statusFromReqDoc)) {
225 additionalAttributes.setAttribute(AdditionalAttributes.STATUS_UPDATED_BY, user);
226 additionalAttributes.setAttribute(AdditionalAttributes.STATUS_UPDATED_ON, dateStr);
227 }
228 } else if (requestDocument.getOperation().equalsIgnoreCase(Request.Operation.checkIn.toString())) {
229 if (requestDocument.getAdditionalAttributes() != null) {
230 statusFromReqDoc = additionalAttributes.getAttribute(Constants.STATUS);
231 }
232 HashMap<String, String> bibMap = new HashMap<String, String>();
233 bibMap.put("bibId", DocumentUniqueIDPrefix.getDocumentId(DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid())));
234 List<BibRecord> bibRecordList = (List<BibRecord>) KRADServiceLocator.getBusinessObjectService().findMatching(BibRecord.class, bibMap);
235 if (bibRecordList.size() > 0) {
236 BibRecord bibRecord = bibRecordList.get(0);
237 String status = bibRecord.getStatus();
238 if (status == null) {
239 status = "";
240 }
241
242 if (status != null && !status.equals(statusFromReqDoc)) {
243 additionalAttributes.setAttribute(Constants.STATUS_UPDATED_BY, user);
244 additionalAttributes.setAttribute(Constants.STATUS_UPDATED_ON, dateStr);
245 }
246
247 }
248 additionalAttributes.setAttribute(Constants.UPDATED_BY, user);
249 additionalAttributes.setAttribute(Constants.DATE_ENTERED, dateStr);
250 }
251 requestDocument.setAdditionalAttributes(additionalAttributes);
252 }
253 }
254
255
256 @Override
257 public ResponseDocument checkoutContent(RequestDocument requestDocument, Object object) {
258 BusinessObjectService businessObjectService = (BusinessObjectService) object;
259 Date date = new Date();
260 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
261 String dateStr = sdf.format(date);
262 ResponseDocument respDoc = new ResponseDocument();
263 if (null == businessObjectService) {
264 businessObjectService = KRADServiceLocator.getBusinessObjectService();
265 }
266 Map parentCriteria1 = new HashMap();
267 parentCriteria1.put("bibId", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
268 List<BibRecord> bibRecords = (List<BibRecord>) businessObjectService.findMatching(BibRecord.class, parentCriteria1);
269 if (bibRecords != null && bibRecords.size() > 0) {
270 BibRecord bibRecord = bibRecords.get(0);
271 String content = bibRecord.getContent();
272 AdditionalAttributes additionalAttributes = new AdditionalAttributes();
273 if (bibRecord.getFassAddFlag() != null) {
274 additionalAttributes.setAttribute(AdditionalAttributes.FAST_ADD_FLAG, bibRecord.getFassAddFlag().toString());
275 }
276 if (bibRecord.getDateEntered() != null && !"".equals(bibRecord.getDateEntered())) {
277 additionalAttributes.setAttribute(AdditionalAttributes.DATE_ENTERED, bibRecord.getDateEntered().toString());
278 }
279 if (bibRecord.getStatus() != null) {
280 additionalAttributes.setAttribute(AdditionalAttributes.STATUS, bibRecord.getStatus());
281 }
282 if (bibRecord.getStatusUpdatedDate() != null) {
283 additionalAttributes.setAttribute(AdditionalAttributes.STATUS_UPDATED_ON, bibRecord.getStatusUpdatedDate().toString());
284 }
285 if (bibRecord.getSuppressFromPublic() != null) {
286 additionalAttributes.setAttribute(AdditionalAttributes.SUPRESS_FROM_PUBLIC, bibRecord.getSuppressFromPublic());
287 }
288 if (bibRecord.getStaffOnlyFlag() != null) {
289 additionalAttributes.setAttribute(AdditionalAttributes.STAFFONLYFLAG, bibRecord.getStaffOnlyFlag().toString());
290 }
291 if (bibRecord.getCreatedBy() != null) {
292 additionalAttributes.setAttribute(AdditionalAttributes.CREATED_BY, bibRecord.getCreatedBy());
293 }
294 if (bibRecord.getUpdatedBy() != null) {
295 additionalAttributes.setAttribute(AdditionalAttributes.UPDATED_BY, bibRecord.getUpdatedBy());
296 }
297 if (bibRecord.getStatusUpdatedBy() != null) {
298 additionalAttributes.setAttribute(AdditionalAttributes.STATUS_UPDATED_BY, bibRecord.getStatusUpdatedBy());
299 }
300 if (bibRecord.getDateCreated() != null && !"".equals(bibRecord.getDateCreated())) {
301 additionalAttributes.setAttribute(AdditionalAttributes.DATE_ENTERED, bibRecord.getDateCreated().toString());
302 }
303
304 if (bibRecord.getDateEntered() != null && !"".equals(bibRecord.getDateEntered())) {
305 additionalAttributes.setAttribute(AdditionalAttributes.LAST_UPDATED, bibRecord.getDateEntered().toString());
306 }
307 Content contentObj = new Content();
308 contentObj.setContent(content);
309 respDoc.setUuid(requestDocument.getUuid());
310 respDoc.setCategory(requestDocument.getCategory());
311 respDoc.setType(requestDocument.getType());
312 respDoc.setFormat(requestDocument.getFormat());
313 respDoc.setContent(contentObj);
314 respDoc.setStatus("Success");
315 respDoc.setAdditionalAttributes(additionalAttributes);
316 } else {
317 respDoc.setStatus("Failed");
318 respDoc.setStatusMessage("Bib Record Does not exist.");
319 }
320 return respDoc;
321 }
322
323
324 @Override
325 public void checkInContent(RequestDocument requestDocument, Object object, ResponseDocument responseDocument) {
326 modifyAdditionalAttributes(requestDocument);
327 BusinessObjectService businessObjectService = (BusinessObjectService) object;
328 if (null == businessObjectService) {
329 businessObjectService = KRADServiceLocator.getBusinessObjectService();
330 }
331 Map parentCriteria1 = new HashMap();
332 parentCriteria1.put("bibId", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
333 AdditionalAttributes attributes = requestDocument.getAdditionalAttributes();
334 BibRecord bibRecord = businessObjectService.findByPrimaryKey(BibRecord.class, parentCriteria1);
335 bibRecord.setContent(requestDocument.getContent().getContent());
336 if (attributes != null) {
337 bibRecord.setFassAddFlag(Boolean.valueOf(attributes.getAttribute(AdditionalAttributes.FAST_ADD_FLAG)));
338 bibRecord.setSuppressFromPublic(attributes.getAttribute(AdditionalAttributes.SUPRESS_FROM_PUBLIC));
339 bibRecord.setStatus(attributes.getAttribute(AdditionalAttributes.STATUS));
340
341
342
343
344
345
346
347
348 bibRecord.setUpdatedBy(attributes.getAttribute(AdditionalAttributes.UPDATED_BY));
349 bibRecord.setDateEntered(Timestamp.valueOf(attributes.getAttribute(AdditionalAttributes.DATE_ENTERED)));
350 if (attributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_BY) != null) {
351 bibRecord.setStatusUpdatedBy(attributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_BY));
352 }
353 if (attributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_ON) != null) {
354 bibRecord.setStatusUpdatedDate(Timestamp.valueOf(attributes.getAttribute(AdditionalAttributes.STATUS_UPDATED_ON)));
355 }
356 bibRecord.setStaffOnlyFlag(Boolean.valueOf(attributes.getAttribute(AdditionalAttributes.STAFFONLYFLAG)));
357 bibRecord.setUpdatedBy(attributes.getAttribute(AdditionalAttributes.UPDATED_BY));
358 }
359 businessObjectService.save(bibRecord);
360 requestDocument.setUuid(DocumentUniqueIDPrefix.getPrefixedId(bibRecord.getUniqueIdPrefix(), bibRecord.getBibId()));
361 buildResponseDocument(requestDocument, bibRecord, responseDocument);
362 }
363
364 protected void modifyDocumentContent(RequestDocument doc, String identifier, BusinessObjectService businessObjectService) {
365
366 }
367
368
369
370
371
372
373
374
375
376
377 @Override
378 public void validateInput(RequestDocument requestDocument, Object object, List<String> valuesList) throws OleDocStoreException {
379 BusinessObjectService businessObjectService = (BusinessObjectService) object;
380
381
382
383
384
385 validateLinkedRequestDocument(requestDocument, valuesList, businessObjectService);
386
387 }
388
389 protected void validateRequestDocument(RequestDocument requestDocument) throws OleDocStoreException {
390
391 }
392
393 protected void validateLinkedRequestDocument(RequestDocument requestDocument, List<String> valuesList, BusinessObjectService businessObjectService) throws OleDocStoreException {
394 for (RequestDocument linkDoc : requestDocument.getLinkedRequestDocuments()) {
395 DocumentManager documentManager = RdbmsDocumentManagerFactory.getInstance().getDocumentManager(linkDoc);
396 documentManager.validateInput(linkDoc, businessObjectService, valuesList);
397 }
398 }
399
400
401 @Override
402 public ResponseDocument deleteVerify(RequestDocument requestDocument, Object object) throws Exception {
403 BusinessObjectService businessObjectService = (BusinessObjectService) object;
404 Response response = new Response();
405 ResponseDocument responseDocument = new ResponseDocument();
406 List<ResponseDocument> responseDocumentList = new ArrayList<ResponseDocument>();
407 List<String> instanceIdentifierList = new ArrayList<String>();
408
409 Map bibMap = new HashMap();
410 bibMap.put("bibId", DocumentUniqueIDPrefix.getDocumentId(requestDocument.getUuid()));
411 List<BibInstanceRecord> bibInstanceRecordList = (List<BibInstanceRecord>) businessObjectService
412 .findMatching(BibInstanceRecord.class, bibMap);
413 for (BibInstanceRecord bibInstanceRecord : bibInstanceRecordList) {
414 Map instanceMap = new HashMap();
415 instanceMap.put("instanceId", bibInstanceRecord.getInstanceId());
416 List<BibInstanceRecord> bibInstanceRecords = (List<BibInstanceRecord>) businessObjectService.findMatching(BibInstanceRecord.class, instanceMap);
417 if (bibInstanceRecords.size() > 1) {
418 responseDocument.setCategory(requestDocument.getCategory());
419 responseDocument.setType(requestDocument.getType());
420 responseDocument.setFormat(requestDocument.getFormat());
421 responseDocument.setUuid(requestDocument.getUuid());
422 responseDocument.setStatus("failure'");
423 responseDocument
424 .setStatusMessage("Instance is bound with more than one bib. So deletion cannot be done");
425 return responseDocument;
426 }
427 boolean exists = checkInstancesOrItemsExistsInOLE(bibInstanceRecord.getInstanceId(), businessObjectService);
428 if (exists) {
429 responseDocument.setId(requestDocument.getId());
430 responseDocument.setCategory(requestDocument.getCategory());
431 responseDocument.setType(requestDocument.getType());
432 responseDocument.setFormat(requestDocument.getFormat());
433 responseDocument.setUuid(requestDocument.getUuid());
434 responseDocument.setStatus("failure");
435 responseDocument.setStatusMessage("Instances or Items in use. So deletion cannot be done");
436 return responseDocument;
437 }
438 }
439 responseDocument.setId(requestDocument.getId());
440 responseDocument.setCategory(requestDocument.getCategory());
441 responseDocument.setType(requestDocument.getType());
442 responseDocument.setFormat(requestDocument.getFormat());
443 responseDocument.setUuid(requestDocument.getUuid());
444 responseDocument.setStatus("success");
445 responseDocument.setStatusMessage("success");
446 return responseDocument;
447 }
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511 protected boolean validateIdField (String bibId) {
512 if (StringUtils.isNotEmpty(bibId)) {
513 String idPattern = "[0-9]+";
514 Matcher match = Pattern.compile(idPattern).matcher(bibId);
515 return match.matches();
516 }
517 return false;
518 }
519
520 protected boolean getBibIdFromBibXMLContent(BibRecord bibRecord) {
521 return true;
522 }
523 }