View Javadoc
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.DocumentUniqueIDPrefix;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.deliver.bo.OleCirculationDesk;
8   import org.kuali.ole.deliver.bo.OlePatronDocument;
9   import org.kuali.ole.deliver.bo.OlePatronNoteType;
10  import org.kuali.ole.deliver.bo.OlePatronNotes;
11  import org.kuali.ole.deliver.controller.checkout.CircUtilController;
12  import org.kuali.ole.deliver.service.ParameterValueResolver;
13  import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.ItemRecord;
14  import org.kuali.ole.docstore.engine.service.storage.rdbms.pojo.MissingPieceItemRecord;
15  import org.kuali.ole.sys.context.SpringContext;
16  import org.kuali.rice.core.api.datetime.DateTimeService;
17  import org.kuali.rice.core.api.util.RiceConstants;
18  import org.kuali.rice.krad.service.KRADServiceLocator;
19  import org.kuali.rice.krad.util.GlobalVariables;
20  
21  import java.sql.Timestamp;
22  import java.text.SimpleDateFormat;
23  import java.util.*;
24  
25  /**
26   * Created by angelind on 8/19/15.
27   */
28  public class MissingPieceNoteHandler {
29  
30      public void savePatronNoteForMissingPiece(Map<String, Object> missingPieceRecordInfo, OlePatronDocument olePatronDocument, ItemRecord itemRecord) {
31          String note = getPatronNoteToRecord(OLEConstants.MISSING_PIECE_ITEM_CHECKED_IN_FLAG, missingPieceRecordInfo, itemRecord);
32          Map map = new HashMap();
33          if(olePatronDocument != null) {
34              OlePatronNotes olePatronNotes = new OlePatronNotes();
35              olePatronNotes.setPatronNoteText(note);
36              map.clear();
37              map.put("patronNoteTypeCode", "GENERAL");
38              OlePatronNoteType olePatronNoteType = (OlePatronNoteType) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePatronNoteType.class, map);
39              if(olePatronNoteType != null) {
40                  olePatronNotes.setPatronNoteTypeId(olePatronNoteType.getPatronNoteTypeId());
41                  olePatronNotes.setOlePatronId(olePatronDocument.getOlePatronId());
42                  if(CollectionUtils.isNotEmpty(olePatronDocument.getNotes())) {
43                      olePatronDocument.getNotes().add(olePatronNotes);
44                  } else {
45                      List<OlePatronNotes> olePatronNotesList = new ArrayList<>();
46                      olePatronNotesList.add(olePatronNotes);
47                      olePatronDocument.setNotes(olePatronNotesList);
48                  }
49                  KRADServiceLocator.getBusinessObjectService().save(olePatronDocument);
50              }
51          }
52      }
53  
54      private String getPatronNoteToRecord(String systemParameter, Map<String, Object> missingPieceRecordInfo, ItemRecord itemRecord) {
55          String note = ParameterValueResolver.getInstance().getParameter(OLEConstants
56                  .APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, systemParameter);
57          SimpleDateFormat dateFormat = new SimpleDateFormat(RiceConstants.SIMPLE_DATE_FORMAT_FOR_DATE + " " + RiceConstants.SIMPLE_DATE_FORMAT_FOR_TIME);
58          String itemBarcode = (String) missingPieceRecordInfo.get("itemBarcode");
59          Date customDate = (Date) missingPieceRecordInfo.get("customDate");
60          String customTime = (String) missingPieceRecordInfo.get("customTime");
61          String selectedCirculationDesk = (String) missingPieceRecordInfo.get("selectedCirculationDesk");
62          String missingPieceCount = (String) missingPieceRecordInfo.get("missingPieceCount");
63          if(StringUtils.isNotBlank(itemBarcode)) {
64              note = note.replace("[0]", itemBarcode);
65          }
66          if(customDate != null) {
67              try {
68                  Timestamp customDateTime = new CircUtilController().processDateAndTimeForAlterDueDate(customDate, customTime);
69                  note = note.replace("[1]",dateFormat.format(customDateTime));
70              } catch (Exception e) {
71                  e.printStackTrace();
72              }
73          } else {
74              note = note.replace("[1]",dateFormat.format(getDateTimeService().getCurrentDate()));
75          }
76          if(StringUtils.isNotBlank(selectedCirculationDesk)) {
77              OleCirculationDesk oleCirculationDesk = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleCirculationDesk.class, selectedCirculationDesk);
78              if(oleCirculationDesk != null) {
79                  note = note.replace("[2]", oleCirculationDesk.getCirculationDeskCode());
80              }
81          }
82          int noOfMissingPiece = 0;
83          if (StringUtils.isNotBlank(missingPieceCount)) {
84              noOfMissingPiece = Integer.parseInt(missingPieceCount);
85          }
86          note = note.replace("[3]", noOfMissingPiece +"");
87          return note;
88      }
89  
90      public void updateMissingPieceRecord(ItemRecord itemRecord, Map<String, Object> missingPieceRecordInfo) {
91          int newMissingPieceCount = 0;
92          int oldMissingPieceCount = 0;
93          Timestamp customDateTime = null;
94          Date customDate = (Date) missingPieceRecordInfo.get("customDate");
95          String customTime = (String) missingPieceRecordInfo.get("customTime");
96          String missingPieceCount = (String) missingPieceRecordInfo.get("missingPieceCount");
97          String missingPieceNote = (String) missingPieceRecordInfo.get("missingPieceNote");
98          itemRecord.setMissingPieceFlag(true);
99          if(StringUtils.isNotBlank(missingPieceCount)) {
100             newMissingPieceCount = Integer.parseInt(missingPieceCount);
101         }
102         if(StringUtils.isNotBlank(itemRecord.getMissingPiecesCount())) {
103             oldMissingPieceCount = Integer.parseInt(itemRecord.getMissingPiecesCount());
104         }
105         itemRecord.setMissingPiecesCount(oldMissingPieceCount + newMissingPieceCount + "");
106         if(customDate != null) {
107             try {
108                 customDateTime = new CircUtilController().processDateAndTimeForAlterDueDate(customDate, customTime);
109             } catch (Exception e) {
110                 e.printStackTrace();
111             }
112         } else {
113             customDateTime = new Timestamp(new Date().getTime());
114         }
115         itemRecord.setMissingPieceEffectiveDate(customDateTime);
116         if(StringUtils.isNotBlank(missingPieceNote)) {
117             itemRecord.setMissingPieceFlagNote(missingPieceNote);
118         }
119         MissingPieceItemRecord missingPieceItemRecord = new MissingPieceItemRecord();
120         missingPieceItemRecord.setMissingPieceFlagNote(missingPieceNote);
121         missingPieceItemRecord.setMissingPieceCount(missingPieceCount);
122         missingPieceItemRecord.setOperatorId(GlobalVariables.getUserSession().getPrincipalId());
123         missingPieceItemRecord.setItemId(DocumentUniqueIDPrefix.getDocumentId(itemRecord.getItemId()));
124         missingPieceItemRecord.setMissingPieceDate(customDateTime);
125         itemRecord.getMissingPieceItemRecordList().add(missingPieceItemRecord);
126     }
127 
128     public DateTimeService getDateTimeService() {
129         return (DateTimeService) SpringContext.getService("dateTimeService");
130     }
131 }