View Javadoc
1   package org.kuali.ole.service;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
5   import org.kuali.ole.docstore.model.xmlpojo.work.instance.oleml.Item;
6   import org.kuali.ole.docstore.model.xstream.work.instance.oleml.WorkItemOlemlRecordProcessor;
7   import org.kuali.ole.describe.service.DocstoreHelperService;
8   import org.kuali.ole.deliver.bo.OleLoanDocument;
9   import org.kuali.ole.deliver.bo.FeeType;
10  import org.kuali.ole.deliver.bo.PatronBillPayment;
11  import org.kuali.ole.deliver.bo.OleAddressBo;
12  import org.kuali.ole.deliver.bo.OlePatronDocument;
13  import org.kuali.ole.deliver.bo.OleProxyPatronDocument;
14  import org.kuali.rice.krad.service.KRADServiceLocator;
15  
16  import java.sql.Timestamp;
17  import java.text.SimpleDateFormat;
18  import java.util.*;
19  import java.util.concurrent.TimeUnit;
20  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  /**
24   * Created with IntelliJ IDEA.
25   * User: Bala.km
26   * Date: 7/2/12
27   * Time: 2:04 PM
28   * Service class to provide input to the krms rule engine for loans circulation policy
29   */
30  public class OleCirculationPolicyServiceImpl implements OleCirculationPolicyService {
31      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleLocationServiceImpl.class);
32  
33      /**
34       *  This method checks valid barcode using barcode.
35       *
36       * @param barcode
37       * @return boolean
38       */
39      @Override
40      public boolean isValidBarcode(String barcode, String pattern) {
41          boolean valid = false;
42          //"^[0-9]{1}(([0-9]*-[0-9]*)*|([0-9]* [0-9]*)*|[0-9]*)[0-9]{1}$"
43          if(pattern!=null && barcode!=null){
44              Pattern p = Pattern.compile(pattern);
45              Matcher m = p.matcher(barcode);
46              StringBuffer sb = new StringBuffer();
47              boolean result = m.matches();
48  
49              if (!result) {
50                  valid = true;
51              }
52          }
53          return valid;
54      }
55  
56      /**
57       *  This method returns MembershipExpireDate using patronBarcode.
58       * @param patronBarcode
59       * @return Date
60       */
61      @Override
62      public Date getPatronMembershipExpireDate(String patronBarcode) {
63          Date expirationDate=null;
64          try{
65              Map<String, String> criteria = new HashMap<String, String>();
66              criteria.put("barcode", patronBarcode);
67              List<OlePatronDocument> olePatronDocument = (List<OlePatronDocument>)  KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class,criteria);
68              if(olePatronDocument.size()==1) {
69                  expirationDate = olePatronDocument.get(0).getExpirationDate();
70              }
71          }catch(Exception e){
72              LOG.error(e.getMessage());
73          }
74          return expirationDate;
75      }
76  
77      /**
78       *  This method returns number of items loaned for a particular patron id.
79       * @param olePatronId
80       * @return int
81       */
82      @Override
83      public int getNoOfItemsLoaned(String olePatronId,boolean renewalFlag) {
84          Map<String, String> criteria = new HashMap<String, String>();
85          Collection<OleLoanDocument> oleLoanDocuments=null;
86          try{
87              criteria.put("patronId", olePatronId);
88              oleLoanDocuments = KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class,criteria);
89          }catch(Exception e){
90              LOG.error(e.getMessage());
91          }
92          int loanedItems = 0;
93          if(oleLoanDocuments!=null){
94              loanedItems = renewalFlag ?oleLoanDocuments.size() : oleLoanDocuments.size()+1;
95          }
96          return loanedItems;
97      }
98  
99      /**
100      *  This method returns loan due date using loan period.
101      * @param loanPeriod
102      * @return  Timestamp
103      */
104     @Override
105     public Timestamp calculateLoanDueDate(String loanPeriod) {
106         String loanPeriodType[]=null;
107         Timestamp dueDate = null;
108         Calendar calendar = Calendar.getInstance();
109         if(loanPeriod != null && loanPeriod.trim().length()>0){
110             loanPeriodType =  loanPeriod.split("-");
111             int loanPeriodValue =  Integer.parseInt(loanPeriodType[0].toString());
112             String loanPeriodTypeValue =  loanPeriodType[1].toString();
113             if(loanPeriodTypeValue.equalsIgnoreCase("MINUTE")){
114                 calendar.add(Calendar.MINUTE, loanPeriodValue);
115             } else if(loanPeriodTypeValue.equalsIgnoreCase("HOUR")) {
116                 calendar.add(Calendar.HOUR, loanPeriodValue);
117             } else if(loanPeriodTypeValue.equalsIgnoreCase("WEEK")) {
118                 calendar.add(Calendar.WEEK_OF_MONTH, loanPeriodValue);
119             } else {
120                 calendar.add(Calendar.DATE, loanPeriodValue);
121             }
122             dueDate =  new Timestamp(calendar.getTime().getTime());
123         }
124         return dueDate;
125     }
126     public List<FeeType> getPatronBillPayment(String patronId){
127         List<FeeType> feeTypeList = new ArrayList<FeeType>();
128         Map<String, String> criteria = new HashMap<String, String>();
129         criteria.put("patronId",patronId);
130         List<PatronBillPayment> patronBillPayments =  (List<PatronBillPayment>)KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,criteria);
131         for(PatronBillPayment patronBillPayment : patronBillPayments){
132             feeTypeList.addAll(patronBillPayment.getFeeType());
133         }
134         return feeTypeList;
135     }
136 
137     @Override
138     public HashMap getNumberOfOverdueItemsCheckedOut(String patronId) {
139         int count = 0;
140         HashMap overdueItems = new HashMap();
141 
142         List<OleLoanDocument> overdueLoanedItem = new ArrayList<OleLoanDocument>();
143         Map<String, String> criteria = new HashMap<String, String>();
144         criteria.put("patronId", patronId);
145         List<OleLoanDocument> oleLoanDocuments= (List<OleLoanDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class,criteria);
146         oleLoanDocuments = oleLoanDocuments!=null?oleLoanDocuments:new ArrayList<OleLoanDocument>();
147         for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
148             Integer numberOfOverdueNoticesSent = new Integer(oleLoanDocument.getNumberOfOverdueNoticesSent()!=null?oleLoanDocument.getNumberOfOverdueNoticesSent():"0");
149             if(numberOfOverdueNoticesSent>0){
150                 overdueLoanedItem.add(oleLoanDocument);
151                 count++;
152             }
153         }
154         overdueItems.put("count",count);
155         overdueItems.put("oleLoanDocumentList",overdueLoanedItem);
156         return overdueItems;
157     }
158 
159     @Override
160     public HashMap getRecalledOverdueItemsCheckedOut(List<OleLoanDocument> oleLoanDocuments) {
161         int count = 0;
162         List<Integer> listOfRecalledOverdueDays = new ArrayList<Integer>();
163         HashMap recalledOverdueItems = new HashMap();
164         List<OleLoanDocument> recallOverdueLoanedItem = new ArrayList<OleLoanDocument>();
165         oleLoanDocuments = oleLoanDocuments!=null?oleLoanDocuments:new ArrayList<OleLoanDocument>();
166         for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
167             Map<String, String> criteria = new HashMap<String, String>();
168             criteria.put("itemId", oleLoanDocument.getItemId());
169             List<OleDeliverRequestBo> oleDeliverRequestBoList =(List<OleDeliverRequestBo>)KRADServiceLocator.getBusinessObjectService().findMatching(OleDeliverRequestBo.class,criteria);
170             for(OleDeliverRequestBo oleDeliverRequestBo : oleDeliverRequestBoList){
171                 if(oleDeliverRequestBo.getOleDeliverRequestType().getRequestTypeCode().contains("recall")){
172                     count ++;
173                     recallOverdueLoanedItem.add(oleLoanDocument);
174                     if(oleLoanDocument!=null && oleLoanDocument.getLoanDueDate().compareTo(new Date())<=0){
175                         listOfRecalledOverdueDays.add(new Integer(getTimeDiff(oleLoanDocument.getLoanDueDate(),new Date())));
176                     }
177                     break;
178                 }
179             }
180         }
181         recalledOverdueItems.put("count",count);
182         recalledOverdueItems.put("oleLoanDocumentList",recallOverdueLoanedItem);
183         recalledOverdueItems.put("listOfRecalledOverdueDays",listOfRecalledOverdueDays);
184         return recalledOverdueItems;
185     }
186 
187     @Override
188     public List<Integer> getNumberOfOverdueDays(String patronId) {
189         List<Integer> listOfOverdueDays = new ArrayList<Integer>();
190         Map<String, String> criteria = new HashMap<String, String>();
191         criteria.put("patronId", patronId);
192         OleLoanDocument oleLoanDocument=KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleLoanDocument.class,criteria);
193         if(oleLoanDocument!=null && oleLoanDocument.getLoanDueDate().compareTo(new Date())<=0){
194             listOfOverdueDays.add(new Integer(getTimeDiff(oleLoanDocument.getLoanDueDate(),new Date())));
195         }
196         return listOfOverdueDays;
197     }
198     public String getTimeDiff(Date dateOne, Date dateTwo) {
199         String diff = "";
200         long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
201         diff = String.format("%d",TimeUnit.MILLISECONDS.toDays(timeDiff),
202                 -TimeUnit.HOURS.toDays(timeDiff));
203         return diff;
204     }
205     @Override
206     public int getNumberOfClaimsReturned(String patronId) {
207         int count = 0;
208         Map<String, String> criteria = new HashMap<String, String>();
209         criteria.put("patronId", patronId);
210         List<OleLoanDocument> oleLoanDocuments= (List<OleLoanDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class,criteria);
211         oleLoanDocuments = oleLoanDocuments!=null?oleLoanDocuments:new ArrayList<OleLoanDocument>();
212         for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
213             if(oleLoanDocument.isClaimsReturnedIndicator()){
214                 count++;
215             }
216         }
217         return count;
218     }
219 
220     public Integer getHoursDiff(Date dateOne, Date dateTwo) {
221         if(dateOne!=null && dateTwo!=null && dateOne.compareTo(dateTwo)<=0){
222             String diff = "";
223             long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
224             diff = String.format("%d",TimeUnit.MILLISECONDS.toHours(timeDiff),
225                     -TimeUnit.HOURS.toMinutes(timeDiff));
226             return new Integer(diff);
227         }
228         return 0;
229     }
230 
231     public List<OlePatronDocument> isProxyPatron(String partonId) throws Exception{
232         List realPatron = new ArrayList();
233         Map<String, String> criteria = new HashMap<String, String>();
234         Map<String, String> proxyCriteria = new HashMap<String, String>();
235         criteria.put("proxyPatronId", partonId);
236         List<OleProxyPatronDocument> oleProxyPatronDocuments = (List<OleProxyPatronDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OleProxyPatronDocument.class,criteria);
237         if(oleProxyPatronDocuments != null && oleProxyPatronDocuments.size() >0){
238             for(int proxyPatron=0;proxyPatron<oleProxyPatronDocuments.size();proxyPatron++){
239                 proxyCriteria.put("olePatronId", oleProxyPatronDocuments.get(proxyPatron).getOlePatronId());
240                 List<OlePatronDocument> olePatronDocument = (List<OlePatronDocument>)  KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class,proxyCriteria);
241                 if(olePatronDocument!=null && olePatronDocument.size()>0)
242                     realPatron.add(olePatronDocument.get(0));
243                 // realPatron.add(oleProxyPatronDocuments.get(proxyPatron));
244             }
245             // return realPatron;
246         }
247         return realPatron;
248     }
249 
250     public boolean isAddressVerified(String patronId) throws Exception{
251         int count =0;
252         Map<String, String> criteria = new HashMap<String, String>();
253         Map<String, String> addressCriteria = new HashMap<String, String>();
254         criteria.put("olePatronId", patronId);
255         List<OlePatronDocument> olePatronDocument = (List<OlePatronDocument>)  KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class,criteria);
256         if(olePatronDocument != null && olePatronDocument.size() >0){
257             if(olePatronDocument.get(0).getEntity().getEntityTypeContactInfos().get(0).getAddresses() != null && olePatronDocument.get(0).getEntity().getEntityTypeContactInfos().get(0).getAddresses().size() >0){
258                 for(int address=0;address<olePatronDocument.get(0).getEntity().getEntityTypeContactInfos().get(0).getAddresses().size();address ++ ){
259                     addressCriteria.put("id",olePatronDocument.get(0).getEntity().getEntityTypeContactInfos().get(0).getAddresses().get(address).getId());
260                     List<OleAddressBo> oleAddressBos = (List<OleAddressBo>)  KRADServiceLocator.getBusinessObjectService().findMatching(OleAddressBo.class,addressCriteria);
261                     if(oleAddressBos != null && oleAddressBos.size() > 0 && oleAddressBos.get(0).isAddressVerified()){
262                         count ++;
263                     }
264                 }
265                 if(count == olePatronDocument.get(0).getEntity().getEntityTypeContactInfos().get(0).getAddresses().size()){
266                     return true;
267                 }
268             }
269 
270         }
271         return false;
272     }
273 
274 
275 }