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