1 package org.kuali.ole.deliver.service;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.jfree.util.Log;
6 import org.kuali.ole.OLEConstants;
7 import org.kuali.ole.OLEParameterConstants;
8 import org.kuali.ole.deliver.bo.*;
9 import org.kuali.ole.deliver.processor.LoanProcessor;
10 import org.kuali.ole.docstore.common.document.BibTree;
11 import org.kuali.ole.docstore.common.document.BibTrees;
12 import org.kuali.ole.docstore.common.document.DocstoreDocument;
13 import org.kuali.ole.docstore.common.document.HoldingsTree;
14 import org.kuali.ole.docstore.common.document.content.instance.Item;
15 import org.kuali.ole.docstore.common.document.content.instance.ItemStatus;
16 import org.kuali.ole.docstore.common.document.content.instance.xstream.ItemOlemlRecordProcessor;
17 import org.kuali.ole.docstore.engine.client.DocstoreLocalClient;
18 import org.kuali.ole.sys.context.SpringContext;
19 import org.kuali.rice.core.api.util.type.KualiDecimal;
20
21 import java.math.BigDecimal;
22 import java.sql.Timestamp;
23 import java.text.SimpleDateFormat;
24 import java.util.*;
25
26
27
28
29 public class LostNoticesExecutor extends NoticesExecutor {
30 private static final Logger LOG = Logger.getLogger(LostNoticesExecutor.class);
31 private NoticeMailContentFormatter noticeMailContentFormatter;
32 private ItemOlemlRecordProcessor itemOlemlRecordProcessor;
33
34
35 public ItemOlemlRecordProcessor getItemOlemlRecordProcessor() {
36 if (itemOlemlRecordProcessor == null) {
37 itemOlemlRecordProcessor = SpringContext.getBean(ItemOlemlRecordProcessor.class);
38 }
39 return itemOlemlRecordProcessor;
40 }
41
42 public LostNoticesExecutor(List<OleLoanDocument> loanDocuments) {
43 super(loanDocuments);
44 }
45
46 @Override
47 protected void postProcess(List<OleLoanDocument> loanDocuments) {
48 List<String> itemUUIDS = new ArrayList<String>();
49 for (Iterator<OleLoanDocument> iterator = loanDocuments.iterator(); iterator.hasNext(); ) {
50 OleLoanDocument loanDocument = iterator.next();
51 for (OLEDeliverNotice oleDeliverNotice : loanDocument.getDeliverNotices()) {
52 LOG.info("LostNoticesExecutor thread id---->" + Thread.currentThread().getId() + "current thread---->" + Thread.currentThread() + "Loan id-->" + loanDocument.getLoanId() + "notice id--->" + oleDeliverNotice.getId());
53 Timestamp toBeSendDate = oleDeliverNotice.getNoticeToBeSendDate();
54 if (oleDeliverNotice.getNoticeType().equals(OLEConstants.NOTICE_LOST) && toBeSendDate.compareTo(getSendToDate(OLEConstants.LOST_NOTICE_TO_DATE)) <
55 0) {
56 try {
57 itemUUIDS.add(loanDocument.getItemUuid());
58 } catch (Exception e) {
59 Log.info(e.getStackTrace());
60 }
61 }
62 }
63 }
64 itemStatusBulkUpdate(itemUUIDS);
65 }
66
67 @Override
68 protected void preProcess(List<OleLoanDocument> loanDocuments) {
69 for (Iterator<OleLoanDocument> iterator = loanDocuments.iterator(); iterator.hasNext(); ) {
70 OleLoanDocument loanDocument = iterator.next();
71 BigDecimal fineAmount = getFineAmount(loanDocument);
72 String patronBillPayment = getPatronBillPayment(loanDocument, fineAmount);
73 loanDocument.setRepaymentFeePatronBillId(patronBillPayment);
74 }
75 }
76
77 private BigDecimal getFineAmount(OleLoanDocument oleLoanDocument) {
78 BigDecimal feeAmount = BigDecimal.ZERO;
79 for (Iterator<OLEDeliverNotice> iterator = oleLoanDocument.getDeliverNotices().iterator(); iterator.hasNext(); ) {
80 OLEDeliverNotice deliverNotice = iterator.next();
81 if (deliverNotice.getNoticeType().equals(OLEConstants.NOTICE_LOST)) {
82 BigDecimal replacementFeeAmount = deliverNotice.getReplacementFeeAmount();
83 feeAmount = feeAmount.add(replacementFeeAmount);
84 }
85 }
86 return feeAmount;
87 }
88
89 public String getPatronBillPayment(OleLoanDocument oleLoanDocument, BigDecimal fineAmount) {
90 FeeType feeType = new FeeType();
91 feeType.setFeeType(getFeeTypeId(OLEConstants.REPLACEMENT_FEE));
92 feeType.setFeeAmount(new KualiDecimal(fineAmount));
93 feeType.setItemBarcode(oleLoanDocument.getItemId());
94 feeType.setItemType(oleLoanDocument.getItemTypeName());
95 feeType.setItemTitle(oleLoanDocument.getTitle());
96 feeType.setItemUuid(oleLoanDocument.getItemUuid());
97 feeType.setPaymentStatus(getOlePaymentStatus().getPaymentStatusId());
98 feeType.setBalFeeAmount(new KualiDecimal(fineAmount));
99 feeType.setFeeSource(OLEConstants.SYSTEM);
100 feeType.setDueDate(oleLoanDocument.getLoanDueDate());
101 feeType.setCheckInDate(oleLoanDocument.getCheckInDate());
102 feeType.setCheckOutDate(oleLoanDocument.getCreateDate());
103
104 List<FeeType> feeTypes = new ArrayList<FeeType>();
105 feeTypes.add(feeType);
106 java.util.Date billdate = new java.util.Date();
107
108
109 PatronBillPayment patronBillPayment = new PatronBillPayment();
110 patronBillPayment.setBillDate(oleLoanDocument.getCheckInDate() != null ? new java.sql.Date(oleLoanDocument.getCheckInDate().getTime()) : new java.sql.Date(billdate.getTime()));
111 patronBillPayment.setFeeType(feeTypes);
112
113 patronBillPayment.setPatronId(oleLoanDocument.getPatronId());
114 patronBillPayment.setProxyPatronId(oleLoanDocument.getProxyPatronId());
115 patronBillPayment.setTotalAmount(new KualiDecimal(fineAmount));
116 patronBillPayment.setUnPaidBalance(new KualiDecimal(fineAmount));
117 oleLoanDocument.setReplacementBill(fineAmount);
118 PatronBillPayment patronBillPayments = getBusinessObjectService().save(patronBillPayment);
119 return patronBillPayments.getBillNumber();
120 }
121
122
123 private String getFeeTypeId(String feeTypeName) {
124 Map feeMap = new HashMap();
125 feeMap.put("feeTypeName", feeTypeName);
126 List<OleFeeType> oleFeeTypes = (List<OleFeeType>) getBusinessObjectService().findMatching(OleFeeType.class, feeMap);
127 return oleFeeTypes != null && oleFeeTypes.size() > 0 ? oleFeeTypes.get(0).getFeeTypeId() : null;
128 }
129
130 public OlePaymentStatus getOlePaymentStatus() {
131 Map statusMap = new HashMap();
132 statusMap.put("paymentStatusName", OLEConstants.PAYMENT_STATUS_OUTSTANDING);
133 List<OlePaymentStatus> olePaymentStatusList = (List<OlePaymentStatus>) getBusinessObjectService().findMatching(OlePaymentStatus.class, statusMap);
134 return olePaymentStatusList != null && olePaymentStatusList.size() > 0 ? olePaymentStatusList.get(0) : null;
135 }
136
137
138 @Override
139 public List<OLEDeliverNotice> buildNoticesForDeletion() {
140 List<OLEDeliverNotice> oleDeliverNotices = new ArrayList<>();
141
142 for (OleLoanDocument loanDocument : loanDocuments) {
143
144 if (loanDocument.getItemTypeName() != null) {
145 loanDocument.setItemType(getItemTypeCodeByName(loanDocument.getItemTypeName()));
146 }
147 Timestamp lostNoticetoSendDate = getSendToDate(OLEConstants.LOST_NOTICE_TO_DATE);
148 for (OLEDeliverNotice oleDeliverNotice : loanDocument.getDeliverNotices()) {
149 LOG.info("LostNoticesExecutor thread id---->" + Thread.currentThread().getId() + "current thread---->" + Thread.currentThread() + "Loan id-->" + loanDocument.getLoanId() + "notice id--->" + oleDeliverNotice.getId());
150 Timestamp toBeSendDate = oleDeliverNotice.getNoticeToBeSendDate();
151 if (oleDeliverNotice.getNoticeType().equals(OLEConstants.NOTICE_LOST) && toBeSendDate.compareTo(lostNoticetoSendDate) < 0) {
152 try {
153
154 oleDeliverNotices.add(oleDeliverNotice);
155 } catch (Exception e) {
156 Log.info(e.getStackTrace());
157 }
158 }
159 }
160 }
161 return oleDeliverNotices;
162 }
163
164
165 @Override
166 public String generateMailContent(List<OleLoanDocument> oleLoanDocuments) {
167 String mailContent = getNoticeMailContentFormatter().generateMailContentForPatron(oleLoanDocuments, "", "");
168 return mailContent;
169 }
170
171 private NoticeMailContentFormatter getNoticeMailContentFormatter() {
172 if (null == noticeMailContentFormatter) {
173 noticeMailContentFormatter = new ReplacementBillNoticeEmailContentFormattter();
174 }
175 return noticeMailContentFormatter;
176 }
177
178 public void setNoticeMailContentFormatter(NoticeMailContentFormatter noticeMailContentFormatter) {
179 this.noticeMailContentFormatter = noticeMailContentFormatter;
180 }
181
182
183 private void itemStatusBulkUpdate(List<String> itemUUIDs) {
184 List<org.kuali.ole.docstore.common.document.Item> items = null;
185 DocstoreLocalClient docstoreLocalClient = new DocstoreLocalClient();
186 try {
187 items = docstoreLocalClient.retrieveItems(itemUUIDs);
188
189 } catch (Exception e) {
190 StringBuffer itemUUIDsBuffer = new StringBuffer();
191 for (String itemUUID : itemUUIDs) {
192 itemUUIDsBuffer.append(itemUUID + ",");
193 }
194 LOG.info("Exception occured while retrieving the item for updating the item status to the following itemIds : " + itemUUIDsBuffer.toString());
195 LOG.error(e, e);
196 }
197
198 BibTrees bibTrees = new BibTrees();
199 for (org.kuali.ole.docstore.common.document.Item item : items) {
200 Item oleItem = (Item) item.getContentObject();
201 String itemXml = null;
202 try {
203 itemXml = buildItemContentWithItemStatus(oleItem, OLEConstants.ITEM_STATUS_LOST);
204 } catch (Exception e) {
205 LOG.info("Exception occured while updating the item status for the item id : " + item.getId() + "and barcode : " + item.getBarcode());
206 LOG.error(e, e);
207 }
208 if (itemXml != null) {
209 item.setContent(itemXml);
210 item.setOperation(DocstoreDocument.OperationType.UPDATE);
211 BibTree bibTree = new BibTree();
212 bibTree.setBib(item.getHolding().getBib());
213 HoldingsTree holdingsTree = new HoldingsTree();
214 holdingsTree.setHoldings(item.getHolding());
215 holdingsTree.getItems().add(item);
216 bibTree.getHoldingsTrees().add(holdingsTree);
217 bibTrees.getBibTrees().add(bibTree);
218 }
219 }
220 try {
221 docstoreLocalClient.processBibTrees(bibTrees);
222 } catch (Exception e) {
223 LOG.error(e, e);
224 StringBuffer itemUUIDsBuffer = new StringBuffer();
225 for (String itemUUID : itemUUIDs) {
226 itemUUIDsBuffer.append(itemUUID + ",");
227 }
228 LOG.info("Exception occured while updating item status to the following itemIds : " + itemUUIDsBuffer.toString());
229 }
230
231 }
232
233 public String buildItemContentWithItemStatus(org.kuali.ole.docstore.common.document.content.instance.Item oleItem, String itemStatus) throws Exception {
234 LOG.debug("Inside the buildItemContentWithItemStatus method");
235 ItemStatus itemStatus1 = new ItemStatus();
236 itemStatus1.setCodeValue(itemStatus);
237 itemStatus1.setFullValue(itemStatus);
238 oleItem.setItemStatus(itemStatus1);
239 oleItem.setItemStatusEffectiveDate(String.valueOf(new SimpleDateFormat(OLEConstants.DAT_FORMAT_EFFECTIVE).format(new Date())));
240 String itemContent = getItemOlemlRecordProcessor().toXML(oleItem);
241 return itemContent;
242 }
243 }