View Javadoc
1   package org.kuali.ole.deliver.util;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.log4j.Logger;
6   import org.kuali.ole.OLEConstants;
7   import org.kuali.ole.OLEParameterConstants;
8   import org.kuali.ole.deliver.batch.OleDeliverBatchServiceImpl;
9   import org.kuali.ole.deliver.batch.OleMailer;
10  import org.kuali.ole.deliver.batch.OleNoticeBo;
11  import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
12  import org.kuali.ole.deliver.bo.OleDeliverRequestHistoryRecord;
13  import org.kuali.ole.deliver.bo.OleLoanDocument;
14  import org.kuali.ole.deliver.bo.OlePatronDocument;
15  import org.kuali.ole.deliver.service.ParameterValueResolver;
16  import org.kuali.rice.core.api.config.property.ConfigContext;
17  import org.kuali.rice.core.api.mail.EmailBody;
18  import org.kuali.rice.core.api.mail.EmailFrom;
19  import org.kuali.rice.core.api.mail.EmailSubject;
20  import org.kuali.rice.core.api.mail.EmailTo;
21  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.krad.util.GlobalVariables;
23  
24  import java.util.*;
25  
26  /**
27   * Created by pvsubrah on 9/1/15.
28   */
29  public class DeliverRequestUtil extends OLEUtil {
30      private static final Logger LOG = Logger.getLogger(DeliverRequestUtil.class);
31  
32  
33      public boolean cancelDocument(String itemBarcode, OlePatronDocument patronDocument, OleLoanDocument oleLoanDocument, String operatorId) {
34          boolean cancelResult = true;
35          OleDeliverRequestBo oleDeliverRequestBo = ItemInfoUtil.getInstance().getRequestByPatronBarcode(patronDocument.getBarcode(), itemBarcode);
36          if (null != oleDeliverRequestBo) {
37              //1. Create Temp history record and delete
38              String itemId = oleDeliverRequestBo.getItemId();
39              String requestOutCome = ConfigContext.getCurrentContextConfig().getProperty(OLEConstants.REQUEST_FULFILLED);
40              String loanTransactionNumber = oleDeliverRequestBo.getLoanTransactionRecordNumber();
41  
42              OleDeliverRequestHistoryRecord requestHistoryRecord = createRequestHistoryRecord(oleDeliverRequestBo, operatorId, loanTransactionNumber, requestOutCome);
43  
44              //2. Delete the existing request
45              try {
46                  getBusinessObjectService().delete(oleDeliverRequestBo);
47  
48                  //3. Reorder Q
49                  HashMap<String, Object> map = new HashMap<>();
50                  map.put("itemId", itemId);
51                  List<OleDeliverRequestBo> oleDeliverRequestBos =
52                          (List<OleDeliverRequestBo>) getBusinessObjectService().findMatchingOrderBy(OleDeliverRequestBo.class, map, OLEConstants.OleDeliverRequest.QUEUE_POSITION, true);
53  
54                  for (Iterator<OleDeliverRequestBo> iterator = oleDeliverRequestBos.iterator(); iterator.hasNext(); ) {
55                      OleDeliverRequestBo deliverRequestBo = iterator.next();
56                      deliverRequestBo.setBorrowerQueuePosition(deliverRequestBo.getBorrowerQueuePosition() == 1? deliverRequestBo.getBorrowerQueuePosition() : deliverRequestBo.getBorrowerQueuePosition() - 1);
57                  }
58  
59                  //Save the re-ordered Q
60                  if (CollectionUtils.isNotEmpty(oleDeliverRequestBos)) {
61                      getBusinessObjectService().save(oleDeliverRequestBos);
62                  }
63  
64                  getBusinessObjectService().save(requestHistoryRecord);
65  
66                  //4: Prepare cancellation notice
67            //      List<OleNoticeBo> noticeBos = generateCancelNotices(patronDocument, oleLoanDocument);
68  
69                  //5: Send cancel notice;
70              //    sendCancelNotice(noticeBos);
71  
72              } catch (Exception e) {
73                  e.printStackTrace();
74                  cancelResult = false;
75              }
76          }
77          return cancelResult;
78  
79      }
80  
81  
82      private OleDeliverRequestHistoryRecord createRequestHistoryRecord(OleDeliverRequestBo oleDeliverRequestBo, String operatorId, String loanTransactionNumber, String requestOutCome) {
83          LOG.debug("Inside createRequestHistoryRecord");
84  
85          OleDeliverRequestHistoryRecord oleDeliverRequestHistoryRecord = new OleDeliverRequestHistoryRecord();
86          oleDeliverRequestHistoryRecord.setRequestId(oleDeliverRequestBo.getRequestId());
87          oleDeliverRequestHistoryRecord.setPatronId(oleDeliverRequestBo.getOlePatron() != null ? oleDeliverRequestBo.getOlePatron().getOlePatronId() : null);
88          oleDeliverRequestHistoryRecord.setItemId(oleDeliverRequestBo.getItemId());
89          oleDeliverRequestHistoryRecord.setArchiveDate(new java.sql.Date(System.currentTimeMillis()));
90          oleDeliverRequestHistoryRecord.setPickUpLocationCode(oleDeliverRequestBo.getPickUpLocationCode());
91          oleDeliverRequestHistoryRecord.setCreateDate(oleDeliverRequestBo.getCreateDate());
92          if (StringUtils.isNotBlank(operatorId)) {
93              oleDeliverRequestHistoryRecord.setOperatorId(operatorId);
94          } else {
95              oleDeliverRequestHistoryRecord.setOperatorId(" ");
96          }
97          oleDeliverRequestHistoryRecord.setDeliverRequestTypeCode(oleDeliverRequestBo.getRequestTypeCode());
98          oleDeliverRequestHistoryRecord.setPoLineItemNumber("");
99          oleDeliverRequestHistoryRecord.setLoanTransactionId(loanTransactionNumber);
100         oleDeliverRequestHistoryRecord.setRequestOutComeStatus(requestOutCome);
101         return oleDeliverRequestHistoryRecord;
102     }
103 
104     public List<OleNoticeBo> generateCancelNotices(OlePatronDocument patronDocument, OleLoanDocument oleLoanDocument) throws Exception {
105         List<OleNoticeBo> oleNoticeBos = new ArrayList<>();
106 
107         OleNoticeBo oleNoticeBo = new OleNoticeBo();
108         oleNoticeBo.setNoticeName(OLEConstants.CANCELLATION_NOTICE);
109 
110         oleNoticeBo.setPatronName(patronDocument.getPatronName());
111         oleNoticeBo.setPatronAddress(null != patronDocument.getPreferredAddress() ? patronDocument.getPreferredAddress() : "");
112         oleNoticeBo.setPatronEmailAddress(null != patronDocument.getEmail() ? patronDocument.getEmail() : "");
113         //TODO: Populate phone number
114         oleNoticeBo.setPatronPhoneNumber("");
115 
116 
117         oleNoticeBo.setAuthor(oleLoanDocument.getAuthor());
118         oleNoticeBo.setItemCallNumber(oleLoanDocument.getItemCallNumber());
119         oleNoticeBo.setItemShelvingLocation(oleLoanDocument.getLocation());
120         oleNoticeBo.setItemId(oleLoanDocument.getItemId());
121         oleNoticeBo.setTitle(oleLoanDocument.getTitle());
122         oleNoticeBo.setOleItem(oleLoanDocument.getOleItem());
123         if (oleNoticeBo.getPatronEmailAddress() != null && !oleNoticeBo.getPatronEmailAddress().isEmpty()) {
124             oleNoticeBos.add(oleNoticeBo);
125         }
126         return oleNoticeBos;
127     }
128 
129     public void sendCancelNotice(List<OleNoticeBo> oleNoticeBos) throws Exception {
130         OleDeliverBatchServiceImpl oleDeliverBatchService = new OleDeliverBatchServiceImpl();
131         for (OleNoticeBo oleNoticeBo : oleNoticeBos) {
132             List list = oleDeliverBatchService.getNoticeForPatron(oleNoticeBos);
133             String content = list.toString();
134             content = content.replace('[', ' ');
135             content = content.replace(']', ' ');
136             if (!content.trim().equals("")) {
137                 OleMailer oleMailer = GlobalResourceLoader.getService("oleMailer");
138                 String replyToEmail = getCircDeskLocationResolver().getReplyToEmail(oleNoticeBo.getItemShelvingLocation());
139                 if (replyToEmail != null) {
140                     oleMailer.sendEmail(new EmailFrom(replyToEmail), new EmailTo(oleNoticeBo.getPatronEmailAddress()), new EmailSubject(OLEConstants.CANCELLATION_NOTICE), new EmailBody(content), true);
141                 } else {
142                     String fromAddress = ParameterValueResolver.getInstance().getParameter(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, OLEParameterConstants.NOTICE_FROM_MAIL);
143                     if (fromAddress != null && (fromAddress.equals("") || fromAddress.trim().isEmpty())) {
144                         fromAddress = OLEConstants.KUALI_MAIL;
145                     }
146                     oleMailer.sendEmail(new EmailFrom(fromAddress), new EmailTo(oleNoticeBo.getPatronEmailAddress()), new EmailSubject(OLEConstants.CANCELLATION_NOTICE), new EmailBody(content), true);
147                 }
148             }
149         }
150     }
151 
152 
153 }