1 package org.kuali.ole.deliver.controller.checkin;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.apache.commons.lang3.StringUtils;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.deliver.bo.OleCirculationDesk;
7 import org.kuali.ole.deliver.bo.OlePatronDocument;
8 import org.kuali.ole.deliver.bo.OlePatronNoteType;
9 import org.kuali.ole.deliver.bo.OlePatronNotes;
10 import org.kuali.ole.deliver.controller.checkout.CircUtilController;
11 import org.kuali.ole.deliver.service.ParameterValueResolver;
12 import org.kuali.ole.sys.context.SpringContext;
13 import org.kuali.rice.core.api.datetime.DateTimeService;
14 import org.kuali.rice.core.api.util.RiceConstants;
15 import org.kuali.rice.krad.service.KRADServiceLocator;
16
17 import java.sql.Timestamp;
18 import java.text.SimpleDateFormat;
19 import java.util.*;
20
21
22
23
24 public class DamagedItemNoteHandler {
25
26
27 public void savePatronNoteForDamaged(Map<String, Object> damagedRecordInfo, OlePatronDocument olePatronDocument) {
28 String note = getPatronNoteToRecord(OLEConstants.DAMAGED_ITEM_CHECKED_IN_FLAG, damagedRecordInfo);
29 Map map = new HashMap();
30 if(olePatronDocument != null) {
31 OlePatronNotes olePatronNotes = new OlePatronNotes();
32 olePatronNotes.setPatronNoteText(note);
33 map.clear();
34 map.put("patronNoteTypeCode", "GENERAL");
35 OlePatronNoteType olePatronNoteType = (OlePatronNoteType) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronNoteType.class, map);
36 if(olePatronNoteType != null) {
37 olePatronNotes.setPatronNoteTypeId(olePatronNoteType.getPatronNoteTypeId());
38 olePatronNotes.setOlePatronId(olePatronDocument.getOlePatronId());
39 if(CollectionUtils.isNotEmpty(olePatronDocument.getNotes())) {
40 olePatronDocument.getNotes().add(olePatronNotes);
41 } else {
42 List<OlePatronNotes> olePatronNotesList = new ArrayList<>();
43 olePatronNotesList.add(olePatronNotes);
44 olePatronDocument.setNotes(olePatronNotesList);
45 }
46 KRADServiceLocator.getBusinessObjectService().save(olePatronDocument);
47 }
48 }
49 }
50
51 private String getPatronNoteToRecord(String systemParameter, Map<String, Object> damagedRecordInfo) {
52 String note = ParameterValueResolver.getInstance().getParameter(OLEConstants
53 .APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, systemParameter);
54 SimpleDateFormat dateFormat = new SimpleDateFormat(RiceConstants.SIMPLE_DATE_FORMAT_FOR_DATE + " " + RiceConstants.SIMPLE_DATE_FORMAT_FOR_TIME);
55 String itemBarcode = (String) damagedRecordInfo.get("itemBarcode");
56 Date customDate = (Date) damagedRecordInfo.get("customDate");
57 String customTime = (String) damagedRecordInfo.get("customTime");
58 String selectedCirculationDesk = (String) damagedRecordInfo.get("selectedCirculationDesk");
59 if(StringUtils.isNotBlank(itemBarcode)) {
60 note = note.replace("[0]", itemBarcode);
61 }
62 if(customDate != null) {
63 try {
64 Timestamp customDateTime = new CircUtilController().processDateAndTimeForAlterDueDate(customDate, customTime);
65 note = note.replace("[1]",dateFormat.format(customDateTime));
66 } catch (Exception e) {
67 e.printStackTrace();
68 }
69 } else {
70 note = note.replace("[1]",dateFormat.format(getDateTimeService().getCurrentDate()));
71 }
72 if(StringUtils.isNotBlank(selectedCirculationDesk)) {
73 OleCirculationDesk oleCirculationDesk = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleCirculationDesk.class, selectedCirculationDesk);
74 if(oleCirculationDesk != null) {
75 note = note.replace("[2]", oleCirculationDesk.getCirculationDeskCode());
76 }
77 }
78 return note;
79 }
80
81 public DateTimeService getDateTimeService() {
82 return (DateTimeService) SpringContext.getService("dateTimeService");
83 }
84 }