1 package org.kuali.ole.service;
2
3 import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
4 import org.kuali.ole.deliver.bo.OleLoanDocument;
5 import org.kuali.ole.deliver.bo.FeeType;
6 import org.kuali.ole.deliver.bo.PatronBillPayment;
7 import org.kuali.ole.deliver.bo.OleAddressBo;
8 import org.kuali.ole.deliver.bo.OlePatronDocument;
9 import org.kuali.ole.deliver.bo.OleProxyPatronDocument;
10 import org.kuali.ole.deliver.processor.LoanProcessor;
11 import org.kuali.ole.sys.context.SpringContext;
12 import org.kuali.rice.kim.impl.identity.entity.EntityBo;
13 import org.kuali.rice.krad.service.KRADServiceLocator;
14 import org.kuali.ole.docstore.common.document.content.instance.Item;
15
16 import java.sql.Timestamp;
17 import java.util.*;
18 import java.util.concurrent.TimeUnit;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22
23
24
25
26
27
28
29 public class OleCirculationPolicyServiceImpl implements OleCirculationPolicyService {
30 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleLocationServiceImpl.class);
31 private LoanProcessor loanProcessor;
32
33 private LoanProcessor getLoanProcessor() {
34 if (loanProcessor == null) {
35 loanProcessor = SpringContext.getBean(LoanProcessor.class);
36 }
37 return loanProcessor;
38 }
39
40 public void setLoanProcessor(LoanProcessor loanProcessor) {
41 this.loanProcessor = loanProcessor;
42 }
43
44
45
46
47
48
49
50 @Override
51 public boolean isValidBarcode(String barcode, String pattern) {
52 boolean valid = false;
53
54 if(pattern!=null && barcode!=null){
55 Pattern p = Pattern.compile(pattern);
56 Matcher m = p.matcher(barcode);
57 boolean result = m.matches();
58
59 if (!result) {
60 valid = true;
61 }
62 }
63 return valid;
64 }
65
66
67
68
69
70
71 @Override
72 public Date getPatronMembershipExpireDate(String patronBarcode) {
73 Date expirationDate=null;
74 try{
75 Map<String, String> criteria = new HashMap<String, String>();
76 criteria.put("barcode", patronBarcode);
77 List<OlePatronDocument> olePatronDocument = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class,criteria);
78 if(olePatronDocument.size()==1) {
79 expirationDate = olePatronDocument.get(0).getExpirationDate();
80 }
81 }catch(Exception e){
82 LOG.error(e.getMessage());
83 }
84 return expirationDate;
85 }
86
87
88
89
90
91
92 @Override
93 public int getNoOfItemsLoaned(String olePatronId,boolean renewalFlag) {
94 Map<String, String> criteria = new HashMap<String, String>();
95 Collection<OleLoanDocument> oleLoanDocuments=null;
96 try{
97 criteria.put("patronId", olePatronId);
98 oleLoanDocuments = KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class,criteria);
99 }catch(Exception e){
100 LOG.error(e.getMessage());
101 }
102 int loanedItems = 0;
103 if(oleLoanDocuments!=null){
104 loanedItems = renewalFlag ?oleLoanDocuments.size() : oleLoanDocuments.size()+1;
105 }
106 return loanedItems;
107 }
108
109
110
111
112
113
114 @Override
115 public Timestamp calculateLoanDueDate(String loanPeriod) {
116 String loanPeriodType[]=null;
117 Timestamp dueDate = null;
118 Calendar calendar = Calendar.getInstance();
119 if(loanPeriod != null && loanPeriod.trim().length()>0){
120 loanPeriodType = loanPeriod.split("-");
121 int loanPeriodValue = Integer.parseInt(loanPeriodType[0].toString());
122 String loanPeriodTypeValue = loanPeriodType[1].toString();
123 if(loanPeriodTypeValue.equalsIgnoreCase("MINUTE")){
124 calendar.add(Calendar.MINUTE, loanPeriodValue);
125 } else if(loanPeriodTypeValue.equalsIgnoreCase("HOUR")) {
126 calendar.add(Calendar.HOUR, loanPeriodValue);
127 } else if(loanPeriodTypeValue.equalsIgnoreCase("WEEK")) {
128 calendar.add(Calendar.WEEK_OF_MONTH, loanPeriodValue);
129 } else {
130 calendar.add(Calendar.DATE, loanPeriodValue);
131 }
132 dueDate = new Timestamp(calendar.getTime().getTime());
133 }
134 return dueDate;
135 }
136 public List<FeeType> getPatronBillPayment(String patronId){
137 List<FeeType> feeTypeList = new ArrayList<FeeType>();
138 Map<String, String> criteria = new HashMap<String, String>();
139 criteria.put("patronId",patronId);
140 List<PatronBillPayment> patronBillPayments = (List<PatronBillPayment>)KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class,criteria);
141 for(PatronBillPayment patronBillPayment : patronBillPayments){
142 feeTypeList.addAll(patronBillPayment.getFeeType());
143 }
144 return feeTypeList;
145 }
146
147 @Override
148 public HashMap getNumberOfOverdueItemsCheckedOut(String patronId) {
149 int count = 0;
150 HashMap overdueItems = new HashMap();
151
152 List<OleLoanDocument> overdueLoanedItem = new ArrayList<OleLoanDocument>();
153 Map<String, String> criteria = new HashMap<String, String>();
154 criteria.put("patronId", patronId);
155 List<OleLoanDocument> oleLoanDocuments= (List<OleLoanDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class,criteria);
156 oleLoanDocuments = oleLoanDocuments!=null?oleLoanDocuments:new ArrayList<OleLoanDocument>();
157 for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
158 Integer numberOfOverdueNoticesSent = new Integer(oleLoanDocument.getNumberOfOverdueNoticesSent()!=null?oleLoanDocument.getNumberOfOverdueNoticesSent():"0");
159 if(numberOfOverdueNoticesSent>0){
160 overdueLoanedItem.add(oleLoanDocument);
161 count++;
162 }
163 }
164 overdueItems.put("count",count);
165 overdueItems.put("oleLoanDocumentList",overdueLoanedItem);
166 return overdueItems;
167 }
168
169 @Override
170 public HashMap getRecalledOverdueItemsCheckedOut(List<OleLoanDocument> oleLoanDocuments) {
171 int count = 0;
172 List<Integer> listOfRecalledOverdueDays = new ArrayList<Integer>();
173 HashMap recalledOverdueItems = new HashMap();
174 List<OleLoanDocument> recallOverdueLoanedItem = new ArrayList<OleLoanDocument>();
175 oleLoanDocuments = oleLoanDocuments!=null?oleLoanDocuments:new ArrayList<OleLoanDocument>();
176 for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
177 Map<String, String> criteria = new HashMap<String, String>();
178 criteria.put("itemId", oleLoanDocument.getItemId());
179 List<OleDeliverRequestBo> oleDeliverRequestBoList =(List<OleDeliverRequestBo>)KRADServiceLocator.getBusinessObjectService().findMatching(OleDeliverRequestBo.class,criteria);
180 for(OleDeliverRequestBo oleDeliverRequestBo : oleDeliverRequestBoList){
181 if(oleDeliverRequestBo.getOleDeliverRequestType().getRequestTypeCode().contains("recall")){
182 count ++;
183 recallOverdueLoanedItem.add(oleLoanDocument);
184 if(oleLoanDocument!=null && oleLoanDocument.getLoanDueDate()!=null && oleLoanDocument.getLoanDueDate().compareTo(new Date())<=0){
185 listOfRecalledOverdueDays.add(new Integer(getTimeDiff(oleLoanDocument.getLoanDueDate(),new Date())));
186 }
187 break;
188 }
189 }
190 }
191 recalledOverdueItems.put("count",count);
192 recalledOverdueItems.put("oleLoanDocumentList",recallOverdueLoanedItem);
193 recalledOverdueItems.put("listOfRecalledOverdueDays",listOfRecalledOverdueDays);
194 return recalledOverdueItems;
195 }
196
197 @Override
198 public List<Integer> getNumberOfOverdueDays(String patronId) {
199 List<Integer> listOfOverdueDays = new ArrayList<Integer>();
200 Map<String, String> criteria = new HashMap<String, String>();
201 criteria.put("patronId", patronId);
202 OleLoanDocument oleLoanDocument=KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleLoanDocument.class,criteria);
203 if(oleLoanDocument!=null && oleLoanDocument.getLoanDueDate()!=null && oleLoanDocument.getLoanDueDate().compareTo(new Date())<=0){
204 listOfOverdueDays.add(new Integer(getTimeDiff(oleLoanDocument.getLoanDueDate(),new Date())));
205 }
206 return listOfOverdueDays;
207 }
208 public String getTimeDiff(Date dateOne, Date dateTwo) {
209 String diff = "";
210 long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
211 diff = String.format("%d",TimeUnit.MILLISECONDS.toDays(timeDiff),
212 -TimeUnit.HOURS.toDays(timeDiff));
213 return diff;
214 }
215 @Override
216 public int getNumberOfClaimsReturned(String patronId) {
217 int count = 0;
218 Map<String, String> criteria = new HashMap<String, String>();
219 criteria.put("patronId", patronId);
220 List<OleLoanDocument> oleLoanDocuments= (List<OleLoanDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class,criteria);
221 oleLoanDocuments = oleLoanDocuments!=null?oleLoanDocuments:new ArrayList<OleLoanDocument>();
222 for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
223 try {
224 String itemXmlContent = getLoanProcessor().getItemXML(oleLoanDocument.getItemUuid());
225 Item oleItem = getLoanProcessor().getItemPojo(itemXmlContent);
226 if (oleItem.isClaimsReturnedFlag()) {
227 count++;
228 }
229 } catch (Exception e) {
230 throw new RuntimeException(e);
231 }
232 }
233 return count;
234 }
235
236 public Integer getHoursDiff(Date dateOne, Date dateTwo) {
237 if(dateOne!=null && dateTwo!=null && dateOne.compareTo(dateTwo)<=0){
238 String diff = "";
239 long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
240 diff = String.format("%d",TimeUnit.MILLISECONDS.toHours(timeDiff),
241 -TimeUnit.HOURS.toMinutes(timeDiff));
242 return new Integer(diff);
243 }
244 return 0;
245 }
246
247 public List<OlePatronDocument> isProxyPatron(String partonId) throws Exception{
248 List realPatron = new ArrayList();
249 Map<String, String> criteria = new HashMap<String, String>();
250 Map<String, String> proxyCriteria = new HashMap<String, String>();
251 criteria.put("proxyPatronId", partonId);
252 List<OleProxyPatronDocument> oleProxyPatronDocuments = (List<OleProxyPatronDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OleProxyPatronDocument.class,criteria);
253 if(oleProxyPatronDocuments != null && oleProxyPatronDocuments.size() >0){
254 for(int proxyPatron=0;proxyPatron<oleProxyPatronDocuments.size();proxyPatron++){
255 proxyCriteria.put("olePatronId", oleProxyPatronDocuments.get(proxyPatron).getOlePatronId());
256 List<OlePatronDocument> olePatronDocument = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class,proxyCriteria);
257 if(olePatronDocument!=null && olePatronDocument.size()>0)
258 realPatron.add(olePatronDocument.get(0));
259
260 }
261
262 }
263 return realPatron;
264 }
265
266 public List<OlePatronDocument> isProxyPatron(OlePatronDocument olePatronDocument) throws Exception{
267 List realPatron = new ArrayList();
268 List<OleProxyPatronDocument> oleProxyPatronDocuments = olePatronDocument.getOleProxyPatronDocumentList();
269 for(OleProxyPatronDocument oleProxyPatronDocument : oleProxyPatronDocuments){
270 realPatron.add(oleProxyPatronDocument.getOlePatronDocument());
271 }
272 return realPatron;
273 }
274
275 public boolean isAddressVerified(String patronId) throws Exception{
276 int count =0;
277 Map<String, String> criteria = new HashMap<String, String>();
278 Map<String, String> addressCriteria = new HashMap<String, String>();
279 criteria.put("olePatronId", patronId);
280 List<OlePatronDocument> olePatronDocument = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class,criteria);
281 if(olePatronDocument != null && olePatronDocument.size() >0){
282 EntityBo entityBo = olePatronDocument.get(0).getEntity();
283 if(entityBo.getEntityTypeContactInfos().get(0).getAddresses() != null && entityBo.getEntityTypeContactInfos().get(0).getAddresses().size() >0){
284 for(int address=0;address<entityBo.getEntityTypeContactInfos().get(0).getAddresses().size();address ++ ){
285 addressCriteria.put("id",entityBo.getEntityTypeContactInfos().get(0).getAddresses().get(address).getId());
286 List<OleAddressBo> oleAddressBos = (List<OleAddressBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleAddressBo.class,addressCriteria);
287 if(oleAddressBos != null && oleAddressBos.size() > 0 && oleAddressBos.get(0).isAddressVerified()){
288 count ++;
289 }
290 }
291 if(count == entityBo.getEntityTypeContactInfos().get(0).getAddresses().size()){
292 return true;
293 }
294 }
295
296 }
297 return false;
298 }
299
300
301 public boolean isAddressVerified(OlePatronDocument olePatronDocument,String patronId) throws Exception{
302 if (olePatronDocument == null){
303 return isAddressVerified(patronId);
304 }
305
306 Map<String, String> addressCriteria = new HashMap<String, String>();
307 EntityBo entityBo = olePatronDocument.getEntity();
308 if(entityBo.getEntityTypeContactInfos().get(0).getAddresses() != null && entityBo.getEntityTypeContactInfos().get(0).getAddresses().size() >0){
309 for(int address=0;address<entityBo.getEntityTypeContactInfos().get(0).getAddresses().size();address ++ ){
310 addressCriteria.put("id",entityBo.getEntityTypeContactInfos().get(0).getAddresses().get(address).getId());
311 List<OleAddressBo> oleAddressBos = (List<OleAddressBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleAddressBo.class,addressCriteria);
312 if(oleAddressBos != null && oleAddressBos.size() > 0 && entityBo.getEntityTypeContactInfos().get(0).getAddresses().get(address).isDefaultValue() && oleAddressBos.get(0).isAddressVerified()){
313 return true;
314 }
315
316
317
318 }
319
320
321
322 }
323 return false;
324 }
325
326 public HashMap getLoanedKeyMap(String patronId,boolean renewalFlag) {
327 Long begin = System.currentTimeMillis();
328
329 OlePatronDocument olePatronDocument;
330 int recallCount = 0;
331 int overdueCount=0;
332 int loanedItems=0;
333 List<Integer> listOfRecalledOverdueDays = new ArrayList<Integer>();
334 HashMap keyMap = new HashMap();
335 List<OleLoanDocument> recallOverdueLoanedItem = new ArrayList<OleLoanDocument>();
336 List<OleLoanDocument> overdueLoanedItem = new ArrayList<OleLoanDocument>();
337
338 List<Integer> listOfOverdueDays = new ArrayList<Integer>();
339 Map<String, String> criteria = new HashMap<String, String>();
340 criteria.put("patronId", patronId);
341 List<OleLoanDocument> oleLoanDocuments= (List<OleLoanDocument>)KRADServiceLocator.getBusinessObjectService().findMatching(OleLoanDocument.class,criteria);
342 oleLoanDocuments = oleLoanDocuments!=null?oleLoanDocuments:new ArrayList<OleLoanDocument>();
343 olePatronDocument = oleLoanDocuments!=null && oleLoanDocuments.size()>0 ? oleLoanDocuments.get(0).getOlePatron() : null;
344 for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
345
346 Integer numberOfOverdueNoticesSent = new Integer(oleLoanDocument.getNumberOfOverdueNoticesSent()!=null?oleLoanDocument.getNumberOfOverdueNoticesSent():"0");
347 if(numberOfOverdueNoticesSent>0){
348
349 overdueLoanedItem.add(oleLoanDocument);
350 criteria.clear();
351 criteria.put("itemId", oleLoanDocument.getItemId());
352 List<OleDeliverRequestBo> oleDeliverRequestBoList =(List<OleDeliverRequestBo>)KRADServiceLocator.getBusinessObjectService().findMatching(OleDeliverRequestBo.class,criteria);
353 for(OleDeliverRequestBo oleDeliverRequestBo : oleDeliverRequestBoList){
354 if(oleDeliverRequestBo.getOleDeliverRequestType().getRequestTypeCode().contains("recall")){
355 recallCount ++;
356 recallOverdueLoanedItem.add(oleLoanDocument);
357 if(oleLoanDocument!=null && oleLoanDocument.getLoanDueDate()!=null && oleLoanDocument.getLoanDueDate().compareTo(new Date())<=0){
358 listOfRecalledOverdueDays.add(new Integer(getTimeDiff(oleLoanDocument.getLoanDueDate(),new Date())));
359 }
360 break;
361 }
362 }
363 overdueCount++;
364 }
365
366
367 if(oleLoanDocument!=null && oleLoanDocument.getLoanDueDate()!=null && oleLoanDocument.getLoanDueDate().compareTo(new Date())<=0){
368 listOfOverdueDays.add(new Integer(getTimeDiff(oleLoanDocument.getLoanDueDate(),new Date())));
369 }
370 }
371 if(oleLoanDocuments!=null){
372 loanedItems = renewalFlag ?oleLoanDocuments.size() : oleLoanDocuments.size()+1;
373 }
374
375 HashMap<String,Integer> itemTypeMap = new HashMap<>();
376 for(OleLoanDocument oleLoanDocument : oleLoanDocuments){
377 if(itemTypeMap.containsKey(oleLoanDocument.getItemType())){
378 Integer count = itemTypeMap.get(oleLoanDocument.getItemType());
379 count++;
380 itemTypeMap.put(oleLoanDocument.getItemType(),count);
381 }else{
382 itemTypeMap.put(oleLoanDocument.getItemType(),1);
383 }
384 }
385
386 keyMap.put("loanedItemCount",loanedItems);
387 keyMap.put("recallCount", recallCount);
388 keyMap.put("oleLoanDocumentList", recallOverdueLoanedItem);
389 keyMap.put("listOfRecalledOverdueDays", listOfRecalledOverdueDays);
390
391 keyMap.put("overdueCount",overdueCount);
392 keyMap.put("oleLoanDocumentList",overdueLoanedItem);
393 keyMap.put("listOfOverdueDays",listOfOverdueDays);
394
395 keyMap.put("patronDetails",olePatronDocument);
396 keyMap.put("itemTypeMap",itemTypeMap);
397 Long end = System.currentTimeMillis();
398 Long timeTaken = end-begin;
399 LOG.info("The Time Taken for getLoanedKeyMap in Add Item"+timeTaken);
400 return keyMap;
401 }
402
403 }