View Javadoc
1   package org.kuali.ole.deliver;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.OLEParameterConstants;
6   import org.kuali.ole.deliver.batch.OleDeliverBatchServiceImpl;
7   import org.kuali.ole.deliver.batch.OleMailer;
8   import org.kuali.ole.deliver.bo.*;
9   import org.kuali.ole.deliver.service.CircDeskLocationResolver;
10  import org.kuali.ole.service.OlePatronHelperService;
11  import org.kuali.ole.service.OlePatronHelperServiceImpl;
12  import org.kuali.rice.core.api.mail.EmailBody;
13  import org.kuali.rice.core.api.mail.EmailFrom;
14  import org.kuali.rice.core.api.mail.EmailSubject;
15  import org.kuali.rice.core.api.mail.EmailTo;
16  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
17  import org.kuali.rice.core.api.util.type.KualiDecimal;
18  import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
19  import org.kuali.rice.coreservice.api.parameter.Parameter;
20  import org.kuali.rice.coreservice.api.parameter.ParameterKey;
21  import org.kuali.rice.krad.service.BusinessObjectService;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  
24  import java.math.BigDecimal;
25  import java.util.*;
26  
27  /**
28   * Created by pvsubrah on 7/27/15.
29   */
30  public class PatronBillGenerator {
31      private static final Logger LOG = Logger.getLogger(PatronBillGenerator.class);
32  
33      private BusinessObjectService businessObjectService;
34      private OlePatronHelperServiceImpl olePatronHelperService;
35      private CircDeskLocationResolver circDeskLocationResolver;
36  
37      public String generatePatronBillPayment(OleLoanDocument oleLoanDocument, String feeTypeName, Double fineAmount) throws  Exception {
38          long begin = System.currentTimeMillis();
39          StringBuffer contentForSendMail = new StringBuffer();
40  
41          PatronBillPayment patronBillPayment;
42          OlePaymentStatus olePaymentStatus = getPaymentStatus();
43  
44          FeeType feeType = new FeeType();
45          OleFeeType oleFeeType = getFeeTypeId(feeTypeName);
46          if(null != oleFeeType){
47              feeType.setFeeType(oleFeeType.getFeeTypeId());
48              feeType.setOleFeeType(oleFeeType);
49          }
50          feeType.setFeeAmount(new KualiDecimal(fineAmount));
51          feeType.setItemBarcode(oleLoanDocument.getItemId());
52          feeType.setItemType(oleLoanDocument.getItemTypeName());
53          feeType.setItemTitle(oleLoanDocument.getTitle());
54          feeType.setItemUuid(oleLoanDocument.getItemUuid());
55          feeType.setPaymentStatus(olePaymentStatus.getPaymentStatusId());
56          feeType.setBalFeeAmount(new KualiDecimal(fineAmount));
57          feeType.setFeeSource(OLEConstants.SYSTEM);
58          feeType.setDueDate(oleLoanDocument.getLoanDueDate());
59          feeType.setCheckInDate(oleLoanDocument.getCheckInDate());
60          feeType.setCheckOutDate(oleLoanDocument.getCreateDate());
61          List<FeeType> feeTypes = new ArrayList<FeeType>();
62          feeTypes.add(feeType);
63  
64          Date billdate = new Date();
65  
66          patronBillPayment = new PatronBillPayment();
67          patronBillPayment.setBillDate(oleLoanDocument.getCheckInDate() != null ? new java.sql.Date(oleLoanDocument.getCheckInDate().getTime()) : new java.sql.Date(billdate.getTime()));
68          patronBillPayment.setFeeType(feeTypes);
69          //commented for jira OLE-5675
70          patronBillPayment.setPatronId(oleLoanDocument.getPatronId());
71          patronBillPayment.setProxyPatronId(oleLoanDocument.getProxyPatronId());
72          patronBillPayment.setTotalAmount(new KualiDecimal(fineAmount));
73          patronBillPayment.setUnPaidBalance(new KualiDecimal(fineAmount));
74  
75          PatronBillPayment patronBillPayments = null;
76          try {
77              patronBillPayments = getBusinessObjectService().save(patronBillPayment);
78          } catch (Exception e) {
79              throw new Exception(e.getMessage());
80          }
81  
82          Map<String, String> patronMap = new HashMap<String, String>();
83          patronMap.put(OLEConstants.OlePatron.PATRON_ID, oleLoanDocument.getPatronId());
84  
85          String patronMail = "";
86          try {
87              patronMail = getOlePatronHelperService().getPatronHomeEmailId(oleLoanDocument.getOlePatron().getEntity().getEntityTypeContactInfos().get(0));
88          } catch (Exception e) {
89              LOG.error("Exception while getting patron home mail id", e);
90          }
91  
92          OleDeliverBatchServiceImpl oleDeliverBatchService = new OleDeliverBatchServiceImpl();
93          contentForSendMail.append(oleDeliverBatchService.generateMailContentFromPatronBill(oleLoanDocument, oleLoanDocument.getOlePatron(), feeTypeName, String.valueOf(fineAmount), patronBillPayment));
94          OleMailer oleMail = GlobalResourceLoader.getService("oleMailer");
95          String replyToEmail = getCircDeskLocationResolver().getReplyToEmail(oleLoanDocument.getItemLocation());
96          if (replyToEmail != null) {
97              oleMail.sendEmail(new EmailFrom(replyToEmail), new EmailTo(patronMail), new EmailSubject(feeTypeName), new EmailBody(contentForSendMail.toString()), true);
98          } else {
99              oleMail.sendEmail(new EmailFrom(getParameter(OLEParameterConstants.NOTICE_FROM_MAIL)), new EmailTo(patronMail), new EmailSubject(feeTypeName), new EmailBody(contentForSendMail.toString()), true);
100         }
101         if (LOG.isInfoEnabled()){
102             LOG.info("Mail send successfully to " + patronMail);
103         }
104         String billNumber = patronBillPayments.getBillNumber();
105         long end = System.currentTimeMillis();
106         return billNumber;
107     }
108 
109     private OlePaymentStatus getPaymentStatus() {
110         LOG.debug("Inside the getPaymentStatus method");
111         Map statusMap = new HashMap();
112         statusMap.put("paymentStatusName", OLEConstants.PAYMENT_STATUS_OUTSTANDING);
113         List<OlePaymentStatus> olePaymentStatusList = (List<OlePaymentStatus>) getBusinessObjectService().findMatching(OlePaymentStatus.class, statusMap);
114         return olePaymentStatusList != null && olePaymentStatusList.size() > 0 ? olePaymentStatusList.get(0) : null;
115     }
116 
117     private OleFeeType getFeeTypeId(String feeTypeName) {
118         LOG.debug("Inside the getOverdueFeeTypeId method");
119         Map feeMap = new HashMap();
120         feeMap.put("feeTypeName", feeTypeName);
121         List<OleFeeType> oleFeeTypes = (List<OleFeeType>) getBusinessObjectService().findMatching(OleFeeType.class, feeMap);
122         return oleFeeTypes != null && oleFeeTypes.size() > 0 ? oleFeeTypes.get(0) : null;
123     }
124 
125     public String getParameter(String name) {
126         ParameterKey parameterKey = ParameterKey.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,name);
127         Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
128         if(parameter==null){
129             parameterKey = ParameterKey.create(OLEConstants.APPL_ID_OLE, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT,name);
130             parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
131         }
132         return parameter!=null?parameter.getValue():null;
133     }
134 
135     public BusinessObjectService getBusinessObjectService() {
136         if(null == businessObjectService){
137             businessObjectService = KRADServiceLocator.getBusinessObjectService();
138         }
139         return businessObjectService;
140     }
141 
142     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
143         this.businessObjectService = businessObjectService;
144     }
145 
146     public OlePatronHelperService getOlePatronHelperService(){
147         if(olePatronHelperService==null)
148             olePatronHelperService=new OlePatronHelperServiceImpl();
149         return olePatronHelperService;
150     }
151 
152     private CircDeskLocationResolver getCircDeskLocationResolver() {
153         if (circDeskLocationResolver == null) {
154             circDeskLocationResolver = new CircDeskLocationResolver();
155         }
156         return circDeskLocationResolver;
157     }
158 }