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          QueryByCriteria query = QueryFactory.newQuery(OleDeliverRequestBo.class, criteria);
95          query.addOrderBy("borrowerId");
96          Collection results=  getPersistenceBrokerTemplate().getCollectionByQuery(query);
97          return results;
98      }
99  
100     public Collection<Object> getExpiredRequests(){
101         Criteria criteria = new Criteria();
102         criteria.addLessOrEqualThan("requestExpiryDate",new Timestamp(System.currentTimeMillis()));
103         QueryByCriteria query = QueryFactory.newQuery(OleDeliverRequestBo.class, criteria);
104         query.addOrderBy("borrowerId");
105         Collection results=  getPersistenceBrokerTemplate().getCollectionByQuery(query);
106         return results;
107     }
108 
109 
110     public Collection<Object> getDeliverNotices(String noticeType){
111         Criteria criteria = new Criteria();
112         Date fromDate = DateUtil.addDays(new Timestamp(System.currentTimeMillis()),-1);
113         Date toDate = DateUtil.addDays(new Timestamp(System.currentTimeMillis()),1);
114         if(noticeType.equals(OLEConstants.NOTICE_OVERDUE)){
115         List<String> noticeTypes = new ArrayList<>();
116             noticeTypes.add(noticeType);
117             noticeTypes.add(OLEConstants.NOTICE_LOST);
118              criteria.addIn("noticeType",noticeTypes);
119             criteria.addLessOrEqualThan("noticeToBeSendDate", new Timestamp(new Date().getTime()));
120         }else{
121             criteria.addEqualTo("noticeType",noticeType);
122             criteria.addBetween("noticeToBeSendDate", fromDate,toDate);
123         }
124         QueryByCriteria query = QueryFactory.newQuery(OLEDeliverNotice.class, criteria);
125         query.addOrderBy("patronId");
126         Collection results=  getPersistenceBrokerTemplate().getCollectionByQuery(query);
127         return results;
128     }
129 
130     public List<OleLoanDocument> getLoanDocumentsForNoticeGeneration(String noticeType){
131         List<OLEDeliverNotice> oleDeliverNotices = new ArrayList<>();
132         List<OleLoanDocument> oleDeliverLoanDocuments = new ArrayList<OleLoanDocument>();
133         Collection<Object> deliverNotices = getDeliverNotices(noticeType);
134         List<String> loanIds = getLoanIds(deliverNotices);
135         if(loanIds.size()>0){
136         Criteria criteria = new Criteria();
137         criteria.addColumnIn("LOAN_TRAN_ID", loanIds);
138             if(!noticeType.equals(OLEConstants.NOTICE_COURTESY)){
139         criteria.addLessOrEqualThan("loanDueDate",new Timestamp(System.currentTimeMillis()));
140             } else if(noticeType.equals(OLEConstants.NOTICE_COURTESY)){
141                 criteria.addGreaterOrEqualThan("loanDueDate", new Timestamp(System.currentTimeMillis()));
142             }
143         QueryByCriteria query = QueryFactory.newQuery(OleLoanDocument.class, criteria);
144         query.addOrderBy("patronId");
145         Collection results=  getPersistenceBrokerTemplate().getCollectionByQuery(query);
146          if(results.size()>0){
147          oleDeliverLoanDocuments = processLoanDocumentWithNoticeData(deliverNotices,results);
148          }
149         }
150         return oleDeliverLoanDocuments;
151     }
152 
153     /**
154      * This method is used to get the loan ids from the notice table
155      * @param deliverNotices
156      * @return
157      */
158     private List<String> getLoanIds( Collection<Object> deliverNotices){
159         List<String> loanIds = new ArrayList<String>();
160         OLEDeliverNotice oleDeliverNotice = null;
161         if(deliverNotices!=null && deliverNotices.size()>0){
162         for(Object object : deliverNotices){
163             oleDeliverNotice = (OLEDeliverNotice)object;
164             loanIds.add(oleDeliverNotice.getLoanId());
165         }
166         }
167         return loanIds;
168     }
169 
170     /**
171      *
172      * @param deliverNotices
173      * @param loanDocuments
174      * @return
175      */
176     private List<OleLoanDocument> processLoanDocumentWithNoticeData(Collection<Object> deliverNotices,Collection<Object> loanDocuments){
177         List<OleLoanDocument> oleDeliverLoanDocuments = new ArrayList<OleLoanDocument>();
178         OLEDeliverNotice oleDeliverNotice = null;
179         OleLoanDocument oleLoanDocument = null;
180         List<Object> loanDocumentsList = new ArrayList(loanDocuments);
181         List<Object> deliverNoticesList = new ArrayList(deliverNotices);
182         Map<String,OleLoanDocument> loanMap = new HashMap<String,OleLoanDocument>();
183         for(int i =0;i<loanDocumentsList.size();i++){
184             oleDeliverNotice = (OLEDeliverNotice)deliverNoticesList.get(i);
185             oleLoanDocument = (OleLoanDocument) loanDocumentsList.get(i);
186             if(loanMap.get(oleLoanDocument.getLoanId())==null){
187                if(oleDeliverNotice.getLoanId().equals(oleLoanDocument.getLoanId())){
188                    oleLoanDocument.setReplacementBill(oleDeliverNotice.getReplacementFeeAmount());
189                    oleLoanDocument.setNoticeType(oleDeliverNotice.getNoticeType());
190                    oleLoanDocument.setNoticeSendType(oleDeliverNotice.getNoticeSendType());
191                    oleLoanDocument.setOleDeliverNotice(oleDeliverNotice);
192                } else for(int j=0;j<deliverNoticesList.size();j++){
193                     oleDeliverNotice = (OLEDeliverNotice)deliverNoticesList.get(j);
194                     oleLoanDocument = (OleLoanDocument) loanDocumentsList.get(i);
195                     if(oleDeliverNotice.getLoanId().equals(oleLoanDocument.getLoanId())){
196                         oleLoanDocument.setReplacementBill(oleDeliverNotice.getReplacementFeeAmount());
197                         oleLoanDocument.setNoticeType(oleDeliverNotice.getNoticeType());
198                         oleLoanDocument.setNoticeSendType(oleDeliverNotice.getNoticeSendType());
199                         oleLoanDocument.setOleDeliverNotice(oleDeliverNotice);
200                         break;
201                     }
202             }
203                 loanMap.put(oleLoanDocument.getLoanId(),oleLoanDocument);
204                 oleDeliverLoanDocuments.add(oleLoanDocument);
205             }
206             }
207 
208 
209     return oleDeliverLoanDocuments;
210     }
211 
212 
213     /**
214      * This method is used to get the loan documents which does not have the over due notice entry
215      * @return
216      */
217     public List<OleLoanDocument> getUnprocessedOverdueLoanDocuments(){
218         LOG.info("Inside the getUnprocessedOverdueLoanDocuments");
219         Long startTime = System.currentTimeMillis();
220         List<OleLoanDocument> oleLoanDocuments = new ArrayList<OleLoanDocument>();
221         Map<String,OleLoanDocument> loanDocumentMap = new HashMap<String,OleLoanDocument>();
222         Map<String,OleCirculationDesk> circulationDeskMap = new HashMap<String,OleCirculationDesk>();
223 
224         List<Map<String,OleLoanDocument>> loanDocumentMapList = new ArrayList<Map<String,OleLoanDocument>>();
225 
226         Criteria criteria = new Criteria();
227         // get the loan documents from the table which does not have the replacement fee
228         Long startTimeToGetTheLoanDocument = System.currentTimeMillis();
229         QueryByCriteria query = QueryFactory.newQuery(OleLoanDocument.class, criteria);
230         criteria.addColumnIsNull("REPMNT_FEE_PTRN_BILL_ID");
231         query.addOrderBy("patronId");
232         Collection loanDocumentResultSet=  getPersistenceBrokerTemplate().getCollectionByQuery(query);
233         List<OleLoanDocument> loanDocumentsList = new ArrayList(loanDocumentResultSet);
234         Long endTimeToGetTheLoanDocument = System.currentTimeMillis();
235         Long timeDifferenceToGetTheLoanDocument = endTimeToGetTheLoanDocument-startTimeToGetTheLoanDocument;
236         LOG.info("Time taken to get the loan documents in milliseconds  : " + timeDifferenceToGetTheLoanDocument);
237         //get existing overdue notice
238         criteria = new Criteria();
239         QueryByCriteria noticeOverdueQuery = QueryFactory.newQuery(OLEDeliverNotice.class, criteria,true);
240         criteria.addEqualTo("noticeType",OLEConstants.NOTICE_OVERDUE);
241         Collection loanOverdueNoticeResultSet=  getPersistenceBrokerTemplate().getCollectionByQuery(noticeOverdueQuery);
242         List<OLEDeliverNotice> oleOverDueDeliverNoticeList = new ArrayList(loanOverdueNoticeResultSet);
243 
244         //get existing courtesy notice
245         criteria = new Criteria();
246         QueryByCriteria noticeCourtesyQuery = QueryFactory.newQuery(OLEDeliverNotice.class, criteria,true);
247         criteria.addEqualTo("noticeType",OLEConstants.NOTICE_COURTESY);
248         Collection loanCourtesyNoticeResultSet=  getPersistenceBrokerTemplate().getCollectionByQuery(noticeCourtesyQuery);
249         List<OLEDeliverNotice> oleCourtesyDeliverNoticeList = new ArrayList(loanCourtesyNoticeResultSet);
250 
251         criteria = new Criteria();
252         QueryByCriteria noticeLostQuery = QueryFactory.newQuery(OLEDeliverNotice.class, criteria,true);
253         criteria.addEqualTo("noticeType",OLEConstants.NOTICE_LOST);
254         Collection noticeLostQueryResultSet=  getPersistenceBrokerTemplate().getCollectionByQuery(noticeLostQuery);
255         List<OLEDeliverNotice> noticeLostList = new ArrayList(noticeLostQueryResultSet);
256         //get all the circulation desk from the db
257         List<OleCirculationDesk> oleCirculationDeskList = (List<OleCirculationDesk>)getBusinessObjectService().findAll(OleCirculationDesk.class);
258         if(oleCirculationDeskList!=null && oleCirculationDeskList.size()>0){
259            for(OleCirculationDesk oleCirculationDesk : oleCirculationDeskList){
260                circulationDeskMap.put(oleCirculationDesk.getCirculationDeskId(),oleCirculationDesk);
261            }
262         }
263       LOG.info("Number of loan records before filtering : "  +loanDocumentsList.size());
264         OlePatronDocument patronDocument = null;
265         Map<String,String> patronMap = new HashMap<String,String>();
266 
267        List<OlePatronDocument> olePatronDocuments = null;
268         //iterate over all the loan documents to set the circulation desk and borrower type
269         if(loanDocumentsList!=null && loanDocumentsList.size()>0){
270             for(OleLoanDocument oleLoanDocument : loanDocumentsList){
271                 try{
272                 if(oleLoanDocument.getOlePatron()!=null){
273                        patronDocument = oleLoanDocument.getOlePatron();
274                  }else{
275                        patronMap = new HashMap<String,String>();
276                     patronMap.put("olePatronId",oleLoanDocument.getPatronId());
277                     olePatronDocuments = (List<OlePatronDocument>)getBusinessObjectService().findMatching(OlePatronDocument.class,patronMap);
278                     if(olePatronDocuments!=null && olePatronDocuments.size()>0){
279                         patronDocument = olePatronDocuments.get(0);
280                     }
281                 }
282                     if(patronDocument == null){
283                         LOG.info("Patron not found for the given patron id : "+oleLoanDocument.getPatronId() +"Loan Id " + oleLoanDocument.getLoanId());
284                         throw new Exception("Patron Not Found For The Given Patron Id");
285                     }else{
286                         loanDocumentMap.put(oleLoanDocument.getLoanId(),oleLoanDocument);
287                         oleLoanDocument.setBorrowerTypeCode(patronDocument.getOleBorrowerType().getBorrowerTypeCode());
288                         oleLoanDocument.setOleCirculationDesk(circulationDeskMap.get(oleLoanDocument.getCirculationLocationId()));
289                     }
290                 }catch(Exception e){
291                     LOG.info("Exception occured while setting the borrower type information to the loan document");
292                     LOG.error(e, e);
293                 }
294                 }
295         }
296         //remove the loan document for the existing processed loans
297         if(oleOverDueDeliverNoticeList!=null && oleOverDueDeliverNoticeList.size()>0){
298             for(OLEDeliverNotice oleDeliverNotice : oleOverDueDeliverNoticeList){
299                 loanDocumentMap.remove(oleDeliverNotice.getLoanId());
300             }
301         }
302 
303         if(oleCourtesyDeliverNoticeList!=null && oleCourtesyDeliverNoticeList.size()>0){
304             for(OLEDeliverNotice oleDeliverNotice : oleCourtesyDeliverNoticeList){
305                 loanDocumentMap.remove(oleDeliverNotice.getLoanId());
306             }
307         }
308 
309         if(noticeLostList!=null && noticeLostList.size()>0){
310             for(OLEDeliverNotice oleDeliverNotice : noticeLostList){
311                 loanDocumentMap.remove(oleDeliverNotice.getLoanId());
312             }
313         }
314         Collection loanList = loanDocumentMap.values();
315         List<OleLoanDocument> oleLoanDocumentList = new ArrayList(loanList);
316         LOG.info("Number of loan records after filtering : "  +oleLoanDocumentList.size());
317         Long endTime = System.currentTimeMillis();
318         Long timeDifference = endTime-startTime;
319         LOG.info("Time taken for identifying the loan records in milliseconds " + timeDifference);
320         return oleLoanDocumentList;
321    }
322 
323     public List<OleDeliverRequestBo> getDeliverRequests(String patronBarcode){
324         Criteria criteria = new Criteria();
325         QueryByCriteria noticeOverdueQuery = QueryFactory.newQuery(OleDeliverRequestBo.class, criteria,true);
326         criteria.addEqualTo("borrowerBarcode",patronBarcode);
327         Collection loanOverdueNoticeResultSet=  getPersistenceBrokerTemplate().getCollectionByQuery(noticeOverdueQuery);
328         List<OleDeliverRequestBo> oleOverDueDeliverNoticeList = new ArrayList(loanOverdueNoticeResultSet);
329         return oleOverDueDeliverNoticeList;
330     }
331 
332     public List<OleLoanDocument> getDeliverLoans(String patronId){
333 
334         Criteria criteria = new Criteria();
335         QueryByCriteria noticeOverdueQuery = QueryFactory.newQuery(OleLoanDocument.class, criteria,true);
336         criteria.addEqualTo("patronId",patronId);
337         Collection loanOverdueNoticeResultSet=  getPersistenceBrokerTemplate().getCollectionByQuery(noticeOverdueQuery);
338         List<OleLoanDocument> oleOverDueDeliverNoticeList = new ArrayList(loanOverdueNoticeResultSet);
339         return oleOverDueDeliverNoticeList;
340     }
341 
342 
343     public List<PatronBillPayment> getPatronBillPayments(String patronId){
344         Criteria criteria = new Criteria();
345         QueryByCriteria noticeOverdueQuery = QueryFactory.newQuery(PatronBillPayment.class, criteria,true);
346         criteria.addEqualTo("patronId",patronId);
347         Collection loanOverdueNoticeResultSet=  getPersistenceBrokerTemplate().getCollectionByQuery(noticeOverdueQuery);
348         List<PatronBillPayment> oleOverDueDeliverNoticeList = new ArrayList(loanOverdueNoticeResultSet);
349         return  oleOverDueDeliverNoticeList;
350     }
351 
352 
353     public Collection<Object> getRequestTypeForHoldNotice(List<String> requestTypeCodes){
354         Criteria criteria = new Criteria();
355         criteria.addIn("requestTypeCode",requestTypeCodes);
356         QueryByCriteria query = QueryFactory.newQuery(OleDeliverRequestType.class, criteria);
357         Collection results=  getPersistenceBrokerTemplate().getCollectionByQuery(query);
358         return results;
359     }
360 
361  public List<String> getRequestTypeIds(Collection<Object> requestTypeObjects){
362      List<String> requestTypeIds =  new ArrayList<String>();
363      OleDeliverRequestType oleDeliverRequestType = null;
364       if(requestTypeObjects != null && requestTypeObjects.size()>0){
365          for(Object obj: requestTypeObjects){
366              oleDeliverRequestType = (OleDeliverRequestType)obj;
367              requestTypeIds.add(oleDeliverRequestType.getRequestTypeId());
368          }
369      }
370     return requestTypeIds;
371  }
372 
373 
374     public List<String> getRequestTypeIdsForHoldNotice(List<String> requestTypeCodes){
375     Collection<Object> requestTypes = getRequestTypeForHoldNotice(requestTypeCodes);
376     return getRequestTypeIds(requestTypes);
377     }
378 
379 
380 
381 
382 }