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