View Javadoc
1   package org.kuali.ole.deliver.service;
2   
3   import org.apache.log4j.Logger;
4   import org.apache.ojb.broker.query.Criteria;
5   import org.apache.ojb.broker.query.QueryByCriteria;
6   import org.apache.ojb.broker.query.QueryFactory;
7   import org.kuali.ole.OLEConstants;
8   import org.kuali.ole.deliver.bo.*;
9   import org.kuali.ole.deliver.bo.OLEDeliverNotice;
10  import org.kuali.ole.deliver.calendar.service.DateUtil;
11  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
12  
13  import java.sql.Timestamp;
14  import java.util.*;
15  
16  import org.kuali.rice.krad.service.BusinessObjectService;
17  import org.kuali.rice.krad.service.KRADServiceLocator;
18  
19  /**
20   * Created with IntelliJ IDEA.
21   * User: maheswarang
22   * Date: 6/11/14
23   * Time: 7:52 PM
24   * To change this template use File | Settings | File Templates.
25   */
26  public class OleLoanDocumentDaoOjb extends PlatformAwareDaoBaseOjb {
27      private static final Logger LOG = Logger.getLogger(OleLoanDocumentDaoOjb.class);
28  
29      public BusinessObjectService businessObjectService;
30  
31      public BusinessObjectService getBusinessObjectService() {
32          if (businessObjectService == null) {
33              businessObjectService = KRADServiceLocator.getBusinessObjectService();
34          }
35          return businessObjectService;
36      }
37  
38      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
39          this.businessObjectService = businessObjectService;
40      }
41  
42      public Collection<Object> getOverdueLoanDocument() {
43          Criteria criteria = new Criteria();
44          criteria.addLessThan("loanDueDate", new Timestamp(System.currentTimeMillis()));
45          QueryByCriteria query = QueryFactory.newQuery(OleLoanDocument.class, criteria);
46          criteria.addColumnIsNull("REPMNT_FEE_PTRN_BILL_ID");
47          query.addOrderBy("patronId");
48          Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
49          return results;
50  
51      }
52  
53  
54      public Collection<Object> getCourtesyLoanDocument(Integer interval) {
55          Criteria criteria = new Criteria();
56          criteria.addEqualTo("courtesyNoticeFlag", "N");
57          Date formDate = DateUtil.addDays(new Timestamp(System.currentTimeMillis()), interval - 1);
58          Date toDate = DateUtil.addDays(new Timestamp(System.currentTimeMillis()), interval + 1);
59          criteria.addBetween("loanDueDate", new Timestamp(formDate.getTime()), new Timestamp(toDate.getTime()));
60          QueryByCriteria query = QueryFactory.newQuery(OleLoanDocument.class, criteria);
61          query.addOrderBy("patronId");
62          Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
63          return results;
64      }
65  
66  
67      public Collection<Object> getLoanDocumentsUsingItemIdsAndPatronId(String patronId, List<String> itemIds) {
68          Criteria criteria = new Criteria();
69          criteria.addEqualTo("patronId", patronId);
70          criteria.addIn("itemUuid", itemIds);
71          QueryByCriteria query = QueryFactory.newQuery(OleLoanDocument.class, criteria);
72          Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
73          return results;
74      }
75  
76      public Collection<Object> getLoanDocumentsUsingItemBarcodeAndPatronIdForRenewal(String patronId, List<String> itemBarcode) {
77          Criteria criteria = new Criteria();
78          criteria.addEqualTo("patronId", patronId);
79          criteria.addIn("itemId", itemBarcode);
80          QueryByCriteria query = QueryFactory.newQuery(OleLoanDocument.class, criteria);
81          Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
82          return results;
83      }
84  
85      public Collection<Object> getHoldRequests(List<String> requestTypeIds) {
86          Criteria criteria = new Criteria();
87          criteria.addIn("requestTypeId", requestTypeIds);
88          criteria.addEqualTo("borrowerQueuePosition", "1");
89          criteria.addColumnIsNull("ONHLD_NTC_SNT_DT");
90          String pickupLocation = getPickUpLocation();
91          if (pickupLocation != null && !pickupLocation.trim().isEmpty()) {
92              criteria.addEqualTo("PCKUP_LOC_ID", pickupLocation);
93          }
94          QueryByCriteria query = QueryFactory.newQuery(OleDeliverRequestBo.class, criteria);
95          query.addOrderBy("borrowerId");
96          Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
97          return results;
98      }
99  
100 
101     public Collection<Object> getHoldRequestsByPickupLocation(List<String> requestTypeIds, String pickupLocationId) {
102         Criteria criteria = new Criteria();
103         criteria.addIn("requestTypeId", requestTypeIds);
104         criteria.addEqualTo("borrowerQueuePosition", "1");
105         criteria.addColumnIsNull("ONHLD_NTC_SNT_DT");
106         if (pickupLocationId != null && !pickupLocationId.trim().isEmpty()) {
107             criteria.addEqualTo("PCKUP_LOC_ID", pickupLocationId);
108         }
109         QueryByCriteria query = QueryFactory.newQuery(OleDeliverRequestBo.class, criteria);
110         query.addOrderBy("borrowerId");
111         Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
112         return results;
113     }
114 
115     public String getPickUpLocation() {
116         String pickupLocation = null;
117         Map<String, String> oleBatchBoMap = new HashMap<String, String>();
118         oleBatchBoMap.put("jobTriggerName", "generateOnHoldNoticeJob");
119         List<OleBatchJobBo> oleBatchJobBos = (List<OleBatchJobBo>) getBusinessObjectService().findMatching(OleBatchJobBo.class, oleBatchBoMap);
120         if (oleBatchJobBos != null && oleBatchJobBos.size() > 0) {
121             pickupLocation = oleBatchJobBos.get(0).getPickupLocation();
122         }
123         return pickupLocation;
124     }
125 
126     public Collection<Object> getExpiredRequests() {
127         Criteria criteria = new Criteria();
128         criteria.addLessOrEqualThan("requestExpiryDate", new Timestamp(System.currentTimeMillis()));
129         QueryByCriteria query = QueryFactory.newQuery(OleDeliverRequestBo.class, criteria);
130         query.addOrderBy("borrowerId");
131         Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
132         return results;
133     }
134 
135 
136     public Collection<Object> getDeliverNotices(String noticeType) {
137         Criteria criteria = new Criteria();
138         Date fromDate = DateUtil.addDays(new Timestamp(System.currentTimeMillis()), -1);
139         Date toDate = DateUtil.addDays(new Timestamp(System.currentTimeMillis()), 1);
140         if (noticeType.equals(OLEConstants.NOTICE_OVERDUE)) {
141             List<String> noticeTypes = new ArrayList<>();
142             noticeTypes.add(noticeType);
143             noticeTypes.add(OLEConstants.NOTICE_LOST);
144             criteria.addIn("noticeType", noticeTypes);
145             criteria.addLessOrEqualThan("noticeToBeSendDate", new Timestamp(new Date().getTime()));
146         } else {
147             criteria.addEqualTo("noticeType", noticeType);
148             criteria.addBetween("noticeToBeSendDate", fromDate, toDate);
149         }
150         QueryByCriteria query = QueryFactory.newQuery(OLEDeliverNotice.class, criteria);
151         query.addOrderBy("patronId");
152         Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
153         return results;
154     }
155 
156     public List<OleLoanDocument> getLoanDocumentsForNoticeGeneration(String noticeType, Collection<Object> deliverNotices) {
157         List<OleLoanDocument> oleDeliverLoanDocuments = new ArrayList<>();
158         List<String> loanIds = getLoanIds(deliverNotices);
159         if (loanIds.size() > 0) {
160             Criteria criteria = new Criteria();
161             criteria.addColumnIn("LOAN_TRAN_ID", loanIds);
162             if (noticeType.equals(OLEConstants.NOTICE_OVERDUE) || noticeType.equals(OLEConstants.NOTICE_LOST)) {
163                 criteria.addLessOrEqualThan("loanDueDate", new Timestamp(System.currentTimeMillis()));
164             } else if (noticeType.equals(OLEConstants.NOTICE_COURTESY)) {
165                 criteria.addGreaterOrEqualThan("loanDueDate", new Timestamp(System.currentTimeMillis()));
166             }
167             QueryByCriteria query = QueryFactory.newQuery(OleLoanDocument.class, criteria);
168             query.addOrderBy("patronId");
169             Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
170             for (Object oleLoanDocument : results) {
171                 oleDeliverLoanDocuments.add((OleLoanDocument) oleLoanDocument);
172             }
173         }
174         return oleDeliverLoanDocuments;
175     }
176 
177     /**
178      * This method is used to get the loan ids from the notice table
179      *
180      * @param deliverNotices
181      * @return
182      */
183     private List<String> getLoanIds(Collection<Object> deliverNotices) {
184         List<String> loanIds = new ArrayList<String>();
185         OLEDeliverNotice oleDeliverNotice = null;
186         if (deliverNotices != null && deliverNotices.size() > 0) {
187             for (Object object : deliverNotices) {
188                 oleDeliverNotice = (OLEDeliverNotice) object;
189                 loanIds.add(oleDeliverNotice.getLoanId());
190             }
191         }
192         return loanIds;
193     }
194 
195     /**
196      * @param deliverNotices
197      * @param loanDocuments
198      * @return
199      */
200     public List<OleLoanDocument> processLoanDocumentWithNoticeData(Collection<Object> deliverNotices, List<OleLoanDocument> loanDocuments) {
201         List<Object> deliverNoticesList = new ArrayList(deliverNotices);
202         for (OleLoanDocument loanDocument : loanDocuments) {
203             for (Object deliverNotice : deliverNoticesList) {
204                 OLEDeliverNotice oleDeliverNotice = (OLEDeliverNotice) deliverNotice;
205                 if (oleDeliverNotice.getLoanId().equals(loanDocument.getLoanId())) {
206                     if (oleDeliverNotice.getNoticeType().equalsIgnoreCase(OLEConstants.NOTICE_LOST)) {
207                         loanDocument.setReplacementBill(oleDeliverNotice.getReplacementFeeAmount());
208                     }
209                     loanDocument.getDeliverNotices().add(oleDeliverNotice);
210                 }
211             }
212         }
213         return loanDocuments;
214     }
215 
216 
217     /**
218      * This method is used to get the loan documents which does not have the over due notice entry
219      *
220      * @return
221      */
222     public List<OleLoanDocument> getUnprocessedOverdueLoanDocuments() {
223         LOG.info("Inside the getUnprocessedOverdueLoanDocuments");
224         Long startTime = System.currentTimeMillis();
225         List<OleLoanDocument> oleLoanDocuments = new ArrayList<OleLoanDocument>();
226         Map<String, OleLoanDocument> loanDocumentMap = new HashMap<String, OleLoanDocument>();
227         Map<String, OleCirculationDesk> circulationDeskMap = new HashMap<String, OleCirculationDesk>();
228 
229         List<Map<String, OleLoanDocument>> loanDocumentMapList = new ArrayList<Map<String, OleLoanDocument>>();
230 
231         Criteria criteria = new Criteria();
232         // get the loan documents from the table which does not have the replacement fee
233         Long startTimeToGetTheLoanDocument = System.currentTimeMillis();
234         QueryByCriteria query = QueryFactory.newQuery(OleLoanDocument.class, criteria);
235         criteria.addColumnIsNull("REPMNT_FEE_PTRN_BILL_ID");
236         query.addOrderBy("patronId");
237         Collection loanDocumentResultSet = getPersistenceBrokerTemplate().getCollectionByQuery(query);
238         List<OleLoanDocument> loanDocumentsList = new ArrayList(loanDocumentResultSet);
239         Long endTimeToGetTheLoanDocument = System.currentTimeMillis();
240         Long timeDifferenceToGetTheLoanDocument = endTimeToGetTheLoanDocument - startTimeToGetTheLoanDocument;
241         LOG.info("Time taken to get the loan documents in milliseconds  : " + timeDifferenceToGetTheLoanDocument);
242         //get existing overdue notice
243         criteria = new Criteria();
244         QueryByCriteria noticeOverdueQuery = QueryFactory.newQuery(OLEDeliverNotice.class, criteria, true);
245         criteria.addEqualTo("noticeType", OLEConstants.NOTICE_OVERDUE);
246         Collection loanOverdueNoticeResultSet = getPersistenceBrokerTemplate().getCollectionByQuery(noticeOverdueQuery);
247         List<OLEDeliverNotice> oleOverDueDeliverNoticeList = new ArrayList(loanOverdueNoticeResultSet);
248 
249         //get existing courtesy notice
250         criteria = new Criteria();
251         QueryByCriteria noticeCourtesyQuery = QueryFactory.newQuery(OLEDeliverNotice.class, criteria, true);
252         criteria.addEqualTo("noticeType", OLEConstants.NOTICE_COURTESY);
253         Collection loanCourtesyNoticeResultSet = getPersistenceBrokerTemplate().getCollectionByQuery(noticeCourtesyQuery);
254         List<OLEDeliverNotice> oleCourtesyDeliverNoticeList = new ArrayList(loanCourtesyNoticeResultSet);
255 
256         criteria = new Criteria();
257         QueryByCriteria noticeLostQuery = QueryFactory.newQuery(OLEDeliverNotice.class, criteria, true);
258         criteria.addEqualTo("noticeType", OLEConstants.NOTICE_LOST);
259         Collection noticeLostQueryResultSet = getPersistenceBrokerTemplate().getCollectionByQuery(noticeLostQuery);
260         List<OLEDeliverNotice> noticeLostList = new ArrayList(noticeLostQueryResultSet);
261         //get all the circulation desk from the db
262         List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>) getBusinessObjectService().findAll(OleCirculationDesk.class);
263         if (oleCirculationDeskList != null && oleCirculationDeskList.size() > 0) {
264             for (OleCirculationDesk oleCirculationDesk : oleCirculationDeskList) {
265                 circulationDeskMap.put(oleCirculationDesk.getCirculationDeskId(), oleCirculationDesk);
266             }
267         }
268         LOG.info("Number of loan records before filtering : " + loanDocumentsList.size());
269         OlePatronDocument patronDocument = null;
270         Map<String, String> patronMap = new HashMap<String, String>();
271 
272         List<OlePatronDocument> olePatronDocuments = null;
273         //iterate over all the loan documents to set the circulation desk and borrower type
274         if (loanDocumentsList != null && loanDocumentsList.size() > 0) {
275             for (OleLoanDocument oleLoanDocument : loanDocumentsList) {
276                 try {
277                     if (oleLoanDocument.getOlePatron() != null) {
278                         patronDocument = oleLoanDocument.getOlePatron();
279                     } else {
280                         patronMap = new HashMap<String, String>();
281                         patronMap.put("olePatronId", oleLoanDocument.getPatronId());
282                         olePatronDocuments = (List<OlePatronDocument>) getBusinessObjectService().findMatching(OlePatronDocument.class, patronMap);
283                         if (olePatronDocuments != null && olePatronDocuments.size() > 0) {
284                             patronDocument = olePatronDocuments.get(0);
285                         }
286                     }
287                     if (patronDocument == null) {
288                         LOG.info("Patron not found for the given patron id : " + oleLoanDocument.getPatronId() + "Loan Id " + oleLoanDocument.getLoanId());
289                         throw new Exception("Patron Not Found For The Given Patron Id");
290                     } else {
291                         loanDocumentMap.put(oleLoanDocument.getLoanId(), oleLoanDocument);
292                         oleLoanDocument.setBorrowerTypeCode(patronDocument.getOleBorrowerType().getBorrowerTypeCode());
293                         oleLoanDocument.setOleCirculationDesk(circulationDeskMap.get(oleLoanDocument.getCirculationLocationId()));
294                     }
295                 } catch (Exception e) {
296                     LOG.info("Exception occured while setting the borrower type information to the loan document");
297                     LOG.error(e, e);
298                 }
299             }
300         }
301         //remove the loan document for the existing processed loans
302         if (oleOverDueDeliverNoticeList != null && oleOverDueDeliverNoticeList.size() > 0) {
303             for (OLEDeliverNotice oleDeliverNotice : oleOverDueDeliverNoticeList) {
304                 loanDocumentMap.remove(oleDeliverNotice.getLoanId());
305             }
306         }
307 
308         if (oleCourtesyDeliverNoticeList != null && oleCourtesyDeliverNoticeList.size() > 0) {
309             for (OLEDeliverNotice oleDeliverNotice : oleCourtesyDeliverNoticeList) {
310                 loanDocumentMap.remove(oleDeliverNotice.getLoanId());
311             }
312         }
313 
314         if (noticeLostList != null && noticeLostList.size() > 0) {
315             for (OLEDeliverNotice oleDeliverNotice : noticeLostList) {
316                 loanDocumentMap.remove(oleDeliverNotice.getLoanId());
317             }
318         }
319         Collection loanList = loanDocumentMap.values();
320         List<OleLoanDocument> oleLoanDocumentList = new ArrayList(loanList);
321         LOG.info("Number of loan records after filtering : " + oleLoanDocumentList.size());
322         Long endTime = System.currentTimeMillis();
323         Long timeDifference = endTime - startTime;
324         LOG.info("Time taken for identifying the loan records in milliseconds " + timeDifference);
325         return oleLoanDocumentList;
326     }
327 
328     public List<OleDeliverRequestBo> getDeliverRequests(String patronBarcode) {
329         Criteria criteria = new Criteria();
330         QueryByCriteria noticeOverdueQuery = QueryFactory.newQuery(OleDeliverRequestBo.class, criteria, true);
331         criteria.addEqualTo("borrowerBarcode", patronBarcode);
332         Collection loanOverdueNoticeResultSet = getPersistenceBrokerTemplate().getCollectionByQuery(noticeOverdueQuery);
333         List<OleDeliverRequestBo> oleOverDueDeliverNoticeList = new ArrayList(loanOverdueNoticeResultSet);
334         return oleOverDueDeliverNoticeList;
335     }
336 
337     public List<OleLoanDocument> getDeliverLoans(String patronId) {
338 
339         Criteria criteria = new Criteria();
340         QueryByCriteria noticeOverdueQuery = QueryFactory.newQuery(OleLoanDocument.class, criteria, true);
341         criteria.addEqualTo("patronId", patronId);
342         Collection loanOverdueNoticeResultSet = getPersistenceBrokerTemplate().getCollectionByQuery(noticeOverdueQuery);
343         List<OleLoanDocument> oleOverDueDeliverNoticeList = new ArrayList(loanOverdueNoticeResultSet);
344         return oleOverDueDeliverNoticeList;
345     }
346 
347 
348     public List<PatronBillPayment> getPatronBillPayments(String patronId) {
349         Criteria criteria = new Criteria();
350         QueryByCriteria noticeOverdueQuery = QueryFactory.newQuery(PatronBillPayment.class, criteria, true);
351         criteria.addEqualTo("patronId", patronId);
352         Collection loanOverdueNoticeResultSet = getPersistenceBrokerTemplate().getCollectionByQuery(noticeOverdueQuery);
353         List<PatronBillPayment> oleOverDueDeliverNoticeList = new ArrayList(loanOverdueNoticeResultSet);
354         return oleOverDueDeliverNoticeList;
355     }
356 
357 
358     public Collection<Object> getRequestTypeForHoldNotice(List<String> requestTypeCodes) {
359         Criteria criteria = new Criteria();
360         criteria.addIn("requestTypeCode", requestTypeCodes);
361         QueryByCriteria query = QueryFactory.newQuery(OleDeliverRequestType.class, criteria);
362         Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
363         return results;
364     }
365 
366     public List<String> getRequestTypeIds(Collection<Object> requestTypeObjects) {
367         List<String> requestTypeIds = new ArrayList<String>();
368         OleDeliverRequestType oleDeliverRequestType = null;
369         if (requestTypeObjects != null && requestTypeObjects.size() > 0) {
370             for (Object obj : requestTypeObjects) {
371                 oleDeliverRequestType = (OleDeliverRequestType) obj;
372                 requestTypeIds.add(oleDeliverRequestType.getRequestTypeId());
373             }
374         }
375         return requestTypeIds;
376     }
377 
378 
379     public List<String> getRequestTypeIdsForHoldNotice(List<String> requestTypeCodes) {
380         Collection<Object> requestTypes = getRequestTypeForHoldNotice(requestTypeCodes);
381         return getRequestTypeIds(requestTypes);
382     }
383 
384     public Collection<PatronBillPayment> getPatronBills(String patronId) {
385         Criteria criteria = new Criteria();
386         criteria.addNotEqualTo("unPaidBalance", OLEConstants.KUALI_BIGDECIMAL_DEF_VALUE);
387         criteria.addEqualTo("patronId", patronId);
388         QueryByCriteria query = QueryFactory.newQuery(PatronBillPayment.class, criteria);
389         Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
390         return new ArrayList<PatronBillPayment>(results);
391     }
392 
393     public Collection<OleDeliverRequestHistoryRecord> getDeliverRequestHistoryRecords(String itemBarcode) {
394         Criteria criteria = new Criteria();
395         criteria.addEqualTo(OLEConstants.OleDeliverRequest.ITEM_ID, itemBarcode);
396         QueryByCriteria query = QueryFactory.newQuery(OleDeliverRequestHistoryRecord.class, criteria);
397         query.addOrderByDescending(OLEConstants.ARCHIVE_DATE);
398         Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(query);
399         return results;
400     }
401 
402     public List<OleLoanDocument> getLaonDocumentsFromLaondId(List<String> loanIds) {
403         Criteria criteria = new Criteria();
404         QueryByCriteria noticeOverdueQuery = QueryFactory.newQuery(OleLoanDocument.class, criteria, true);
405         criteria.addColumnIn("LOAN_TRAN_ID", loanIds);
406         criteria.addOrderBy("patronId");
407         return (List<OleLoanDocument>)getPersistenceBrokerTemplate().getCollectionByQuery
408                 (noticeOverdueQuery);
409     }
410 
411 }