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.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.CircController;
11  import org.kuali.ole.deliver.controller.checkout.CircUtilController;
12  import org.kuali.ole.deliver.form.CheckinForm;
13  import org.kuali.ole.deliver.service.ParameterValueResolver;
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.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  /**
25   * Created by angelind on 8/18/15.
26   */
27  public class DamagedItemNoteHandler {
28  
29  
30      public void savePatronNoteForDamaged(CheckinForm checkinForm, OlePatronDocument olePatronDocument) {
31          String note = getPatronNoteToRecord(OLEConstants.DAMAGED_ITEM_CHECKED_IN_FLAG, checkinForm);
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, CheckinForm checkinForm) {
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          if(StringUtils.isNotBlank(checkinForm.getItemBarcode())) {
59              note = note.replace("[0]", checkinForm.getItemBarcode());
60          }
61          if(checkinForm.getCustomDueDateMap() != null) {
62              try {
63                  Timestamp checkInDate = new CircUtilController().processDateAndTimeForAlterDueDate(checkinForm.getCustomDueDateMap(), checkinForm.getCustomDueDateTime());
64                  note = note.replace("[1]",dateFormat.format(checkInDate));
65              } catch (Exception e) {
66                  e.printStackTrace();
67              }
68          }
69          if(StringUtils.isNotBlank(checkinForm.getSelectedCirculationDesk())) {
70              OleCirculationDesk oleCirculationDesk = KRADServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(OleCirculationDesk.class, checkinForm.getSelectedCirculationDesk());
71              if(oleCirculationDesk != null) {
72                  note = note.replace("[2]", oleCirculationDesk.getCirculationDeskCode());
73              }
74          }
75          return note;
76      }
77  }