View Javadoc

1   package org.kuali.ole.service;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.deliver.processor.LoanProcessor;
5   import org.kuali.ole.deliver.bo.OleLoanDocument;
6   import org.kuali.ole.deliver.bo.OleTemporaryCirculationHistory;
7   import org.kuali.ole.deliver.api.OleDeliverRequestDefinition;
8   import org.kuali.ole.deliver.bo.OleDeliverRequestBo;
9   import org.kuali.ole.ingest.pojo.OlePatron;
10  import org.kuali.ole.deliver.bo.OlePatronLoanDocument;
11  import org.kuali.ole.deliver.bo.OlePatronLoanDocuments;
12  import org.kuali.ole.deliver.bo.OleRenewalLoanDocument;
13  import org.kuali.ole.deliver.api.*;
14  import org.kuali.ole.deliver.bo.PatronBillPayment;
15  import org.kuali.ole.deliver.bo.*;
16  import org.kuali.rice.core.api.criteria.CriteriaLookupService;
17  import org.kuali.rice.core.api.criteria.GenericQueryResults;
18  import org.kuali.rice.core.api.criteria.QueryByCriteria;
19  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20  import org.kuali.rice.kim.api.identity.IdentityService;
21  import org.kuali.rice.kim.api.identity.address.EntityAddress;
22  import org.kuali.rice.kim.api.identity.email.EntityEmail;
23  import org.kuali.rice.kim.api.identity.entity.Entity;
24  import org.kuali.rice.kim.api.identity.name.EntityName;
25  import org.kuali.rice.kim.api.identity.phone.EntityPhone;
26  import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfo;
27  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
28  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
29  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
30  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
31  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
32  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
33  import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
34  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
35  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
36  import org.kuali.rice.krad.service.BusinessObjectService;
37  import org.kuali.rice.krad.service.KRADServiceLocator;
38  import org.kuali.rice.krad.util.ObjectUtils;
39  
40  import java.io.Serializable;
41  import java.text.SimpleDateFormat;
42  import java.util.*;
43  
44  /**
45   * OlePatronServiceImpl performs patron operation(create,update).
46   */
47  public class OlePatronServiceImpl implements OlePatronService {
48      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronServiceImpl.class);
49      private BusinessObjectService businessObjectService;
50      private IdentityService identityService;
51      private CriteriaLookupService criteriaLookupService;
52      private LoanProcessor loanProcessor;
53      /**
54       * This method initiate LoanProcessor.
55       * @return LoanProcessor
56       */
57      protected LoanProcessor getLoanProcessor() {
58          if(loanProcessor==null)
59              loanProcessor=new LoanProcessor();
60          return loanProcessor;
61      }
62  
63      /**
64       * Gets the instance of BusinessObjectService
65       * @return businessObjectService(BusinessObjectService)
66       */
67      protected BusinessObjectService getBusinessObjectService() {
68          if (businessObjectService == null) {
69              businessObjectService = KRADServiceLocator.getBusinessObjectService();
70          }
71          return businessObjectService;
72      }
73      /**
74       * Gets the instance of IdentityService
75       * @return identityService(IdentityService)
76       */
77      protected IdentityService getIdentityService() {
78          if (identityService == null) {
79              identityService = KimApiServiceLocator.getIdentityService();
80          }
81          return identityService;
82      }
83  
84      /**
85       * Gets the instance of CriteriaLookupService
86       * @return criteriaLookupService(CriteriaLookupService)
87       */
88      protected CriteriaLookupService getCriteriaLookupService() {
89          if(criteriaLookupService == null) {
90              criteriaLookupService = GlobalResourceLoader.getService(OLEConstants.OlePatron.CRITERIA_LOOKUP_SERVICE);
91          }
92          return criteriaLookupService;
93      }
94      /**
95       * Based on patron Id it will return the patron object
96       * @param patronId
97       * @return OlePatronDefinition
98       */
99      @Override
100     public OlePatronDefinition getPatron(String patronId) {
101         LOG.debug("Inside the getPatron method");
102         Map<String, Object> criteria = new HashMap<String, Object>(4);
103         criteria.put(OLEConstants.OlePatron.PATRON_ID, patronId);
104         return OlePatronDocument.to(getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, criteria));
105     }
106     /**
107      * This method will create and persist the patron document
108      * @param olePatron
109      * @return savedOlePatronDefinition(OlePatronDefinition)
110      */
111     @Override
112     public OlePatronDefinition createPatron(OlePatronDefinition olePatron) {
113         LOG.info(" Inside create patron ");
114         BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
115         OlePatronDocument olePatronDocument = OlePatronDocument.from(olePatron);
116         OlePatronDocument savedPatronDocument = businessObjectService.save(olePatronDocument);
117         OlePatronDefinition savedOlePatronDefinition = OlePatronDocument.to(savedPatronDocument);
118         return savedOlePatronDefinition;
119     }
120 
121     /**
122      * This method will update the patron object which is already persist
123      * @param olePatronDefinition
124      * @return OlePatronDefinition
125      */
126     @Override
127     public OlePatronDefinition updatePatron(OlePatronDefinition olePatronDefinition) {
128         LOG.debug("Inside the updatePatron method");
129         boolean doc = false;
130         String documentNumber = "";
131         OlePatronDocument updatedPatronDocument = null;
132 
133         try {
134             if (olePatronDefinition.getOlePatronId() != null) {
135                 OlePatronDocument newPatronBo = OlePatronDocument.from(olePatronDefinition);
136                 Map<String, Object> criteria = new HashMap<String, Object>(4);
137                 criteria.put(OLEConstants.OlePatron.PATRON_ID,olePatronDefinition.getOlePatronId());
138                 OlePatronDocument olePatronBo = getBusinessObjectService().findByPrimaryKey(OlePatronDocument.class, criteria);
139                 if (olePatronBo != null) {
140                     HashMap<String, String> map = new HashMap<String, String>();
141                     map.put(OLEConstants.BORROWER_TYPE_ID, newPatronBo.getBorrowerType());
142                     List<OleBorrowerType> borrowerTypes = (List<OleBorrowerType>) getBusinessObjectService().findMatching(OleBorrowerType.class, map);
143                     if (borrowerTypes.size() > 0) {
144                         newPatronBo.setOleBorrowerType(borrowerTypes.get(0));
145                     }
146                     EntityBo kimEntity = (EntityBo)ObjectUtils.deepCopy((Serializable)olePatronBo.getEntity());
147                     newPatronBo.getEntity().setId(kimEntity.getId());
148                     List<EntityTypeContactInfoBo> entityTypeContactInfoBoList = (List<EntityTypeContactInfoBo>)ObjectUtils.deepCopy((Serializable) kimEntity.getEntityTypeContactInfos());
149                     newPatronBo.getEntity().getNames().get(0).setId(kimEntity.getNames().get(0).getId());
150                     kimEntity.getNames().get(0).setFirstName(newPatronBo.getEntity().getNames().get(0).getFirstName());
151                     kimEntity.getNames().get(0).setLastName(newPatronBo.getEntity().getNames().get(0).getLastName());
152                     kimEntity.getNames().get(0).setNamePrefix(newPatronBo.getEntity().getNames().get(0).getNamePrefix());
153                     kimEntity.getNames().get(0).setNameSuffix(newPatronBo.getEntity().getNames().get(0).getNameSuffix());
154                     entityTypeContactInfoBoList.get(0).setAddresses((List<EntityAddressBo>)
155                             ObjectUtils.deepCopy((Serializable)newPatronBo.getEntity().getEntityTypeContactInfos().get(0).getAddresses()));
156                     entityTypeContactInfoBoList.get(0).setEmailAddresses((List<EntityEmailBo>)
157                             ObjectUtils.deepCopy((Serializable) newPatronBo.getEntity().getEntityTypeContactInfos().get(0).getEmailAddresses()));
158                     entityTypeContactInfoBoList.get(0).setPhoneNumbers((List<EntityPhoneBo>)
159                             ObjectUtils.deepCopy((Serializable) newPatronBo.getEntity().getEntityTypeContactInfos().get(0).getPhoneNumbers()));
160                     kimEntity.setEntityTypeContactInfos(entityTypeContactInfoBoList);
161                     newPatronBo.setEntity(kimEntity);
162                     List<EntityAddressBo> addressBoList = olePatronBo.getEntity().getEntityTypeContactInfos().get(0).getAddresses();
163                     if(addressBoList.size() > 0) {
164                         for(int i=0;i<addressBoList.size();i++){
165                             EntityAddressBo entityAddressBo = addressBoList.get(i);
166                             Map<String, Object> addressMap = new HashMap<String, Object>();
167                             addressMap.put(OLEConstants.OlePatron.ENTITY_BO_ID,entityAddressBo.getId());
168                             //List<OleAddressBo> oleAddressBo = (List<OleAddressBo>) KRADServiceLocator.getBusinessObjectService().findMatching(OleAddressBo.class,criteria);
169                             OleAddressBo oleAddressBo =  KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleAddressBo.class,addressMap);
170                             if(entityAddressBo != null) {
171                                 KRADServiceLocator.getBusinessObjectService().delete(oleAddressBo);
172                                 //KRADServiceLocator.getBusinessObjectService().delete(entityAddressBo);
173                             }
174                         }
175                     }
176 
177                     List<EntityPhoneBo> phoneBoList = olePatronBo.getEntity().getEntityTypeContactInfos().get(0).getPhoneNumbers();
178                     if(phoneBoList.size() > 0) {
179                         KRADServiceLocator.getBusinessObjectService().delete(phoneBoList);
180                     }
181                     List<EntityEmailBo> emailBoList = olePatronBo.getEntity().getEntityTypeContactInfos().get(0).getEmailAddresses();
182                     if(emailBoList.size() > 0) {
183                         KRADServiceLocator.getBusinessObjectService().delete(emailBoList);
184                     }
185                     List<OlePatronNotes> patronNotesList = olePatronBo.getNotes();
186                     if(patronNotesList.size() > 0) {
187                         KRADServiceLocator.getBusinessObjectService().delete(patronNotesList);
188                     }
189                     List<OlePatronLostBarcode> lostBarcodeList = olePatronBo.getLostBarcodes();
190                     if(lostBarcodeList.size() > 0) {
191                         KRADServiceLocator.getBusinessObjectService().delete(lostBarcodeList);
192                     }
193                     List<EntityEmploymentBo> employmentBoList = olePatronBo.getEntity().getEmploymentInformation();
194                     if(employmentBoList.size() > 0) {
195                         KRADServiceLocator.getBusinessObjectService().delete(employmentBoList);
196                     }
197                     List<EntityAffiliationBo> affiliationBoList = olePatronBo.getEntity().getAffiliations();
198                     if(affiliationBoList.size() > 0) {
199                         KRADServiceLocator.getBusinessObjectService().delete(affiliationBoList);
200                     }
201                     List<OleProxyPatronDocument> proxyPatronDocuments = olePatronBo.getOleProxyPatronDocuments();
202                     if(proxyPatronDocuments.size() > 0) {
203                         KRADServiceLocator.getBusinessObjectService().delete(proxyPatronDocuments);
204                     }
205                     List<OlePatronLocalIdentificationBo> patronLocalIdentificationBos = olePatronBo.getOlePatronLocalIds();
206                     if(patronLocalIdentificationBos.size() > 0) {
207                         KRADServiceLocator.getBusinessObjectService().delete(patronLocalIdentificationBos);
208                     }
209 
210                     //EntityBo entity = getBusinessObjectService().save(kimEntity);
211                     newPatronBo.setEntity(kimEntity);
212                     List<OleAddressBo> oleAddressBos = new ArrayList<OleAddressBo>();
213                     List<OleEntityAddressBo> oleEntityAddressBos = newPatronBo.getOleEntityAddressBo();
214                     newPatronBo.setEntity(kimEntity);
215                     List<EntityAddressBo> entityAddresses = kimEntity.getEntityTypeContactInfos().get(0).getAddresses();
216                     for(int i=0;i<entityAddresses.size();i++){
217                         oleEntityAddressBos.get(i).setEntityAddressBo(entityAddresses.get(i));
218                     }
219                     if(oleEntityAddressBos.size() > 0) {
220                         for(int i=0;i<oleEntityAddressBos.size();i++){
221                             OleAddressBo addressBo = oleEntityAddressBos.get(i).getOleAddressBo();
222                             EntityAddressBo entityAddressBo = oleEntityAddressBos.get(i).getEntityAddressBo();
223                                 Map<String, Object> addMap = new HashMap<String, Object>();
224                                 addMap.put(OLEConstants.OlePatron.ENTITY_BO_ID,entityAddressBo.getId());
225                                 OleAddressBo oleAddressBo =  KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleAddressBo.class,addMap);
226                                 if(oleAddressBo==null){
227                                     oleAddressBo = new OleAddressBo();
228                                 }
229                                 oleAddressBo.setId(entityAddressBo.getId());
230                                 oleAddressBo.setOlePatronId(kimEntity.getId());
231                                 oleAddressBo.setOleAddressId(KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("OLE_DLVR_ADD_S").toString());
232                                 oleAddressBo.setAddressValidFrom(addressBo.getAddressValidFrom());
233                                 oleAddressBo.setAddressValidTo(addressBo.getAddressValidTo());
234                                 oleAddressBo.setAddressVerified(addressBo.isAddressVerified());
235                                 oleAddressBo.setAddressSource(addressBo.getAddressSource());
236                                 oleAddressBos.add(oleAddressBo);
237 
238                             }
239                         newPatronBo.setOleAddresses(oleAddressBos);
240                         }
241                     updatedPatronDocument =  getBusinessObjectService().save(newPatronBo);
242                 }
243             }
244         } catch (Exception ex) {
245             LOG.error(ex.getMessage());
246         }
247         LOG.debug("Leaving the updatePatron method");
248         return OlePatronDocument.to(updatedPatronDocument);
249     }
250 
251     /**
252      * If the EntityNameBo is not null,it will add to the EntityBo object
253      * @param name
254      * @param entity
255      */
256     private void addName(EntityNameBo name, EntityBo entity) {
257         LOG.debug("Inside the addName method");
258         List<EntityNameBo> entityName = entity.getNames();
259         if (name != null) {
260             //name.setEntityId(entity.getId());
261             entityName.add(name);
262             entity.setNames(entityName);
263         }
264     }
265 
266     /**
267      * Add the EntityName object to the Entity object
268      * @param name
269      * @param entity
270      */
271     @Override
272     public void addNameToEntity(EntityName name, Entity entity) {
273         LOG.debug("Inside the addNameToEntity method");
274         EntityBo entityBo = EntityBo.from(entity);
275         EntityNameBo nameBo = EntityNameBo.from(name);
276         addName(nameBo, entityBo);
277     }
278     /**
279      * Add the EntityEmail object to the EntityTypeContactInfo object
280      * @param emails
281      * @param entityTypeContactInfo
282      */
283     @Override
284     public void addEmailToEntity(List<EntityEmail> emails, EntityTypeContactInfo entityTypeContactInfo) {
285         LOG.debug("Inside the addEmailToEntity method");
286         List<EntityEmailBo> emailBos = new ArrayList<EntityEmailBo>();
287         for (EntityEmail email : emails) {
288             emailBos.add(EntityEmailBo.from(email));
289         }
290         addEmail(emailBos, EntityTypeContactInfoBo.from(entityTypeContactInfo));
291     }
292 
293     /**
294      * If the EntityEmailBo is not null,it will add to the EntityTypeContactInfoBo object
295      * @param emails
296      * @param entityTypeContactInfoBo
297      */
298     private void addEmail(List<EntityEmailBo> emails, EntityTypeContactInfoBo entityTypeContactInfoBo) {
299         LOG.debug("Inside the addEmail method");
300         if (emails != null) {
301             entityTypeContactInfoBo.setEmailAddresses(emails);
302         }
303     }
304     /**
305      * If the EntityAddressBo is not null,it will add to the EntityTypeContactInfoBo object
306      * @param entityAddress
307      * @param entityTypeContactInfoBo
308      */
309     private void addAddress(List<EntityAddressBo> entityAddress, EntityTypeContactInfoBo entityTypeContactInfoBo) {
310         LOG.debug("Inside the addAddress method");
311         if (entityAddress != null) {
312             entityTypeContactInfoBo.setAddresses(entityAddress);
313         }
314     }
315     /**
316      * Add the EntityAddress object to the EntityTypeContactInfo object
317      * @param oleEntityAddress
318      * @param entityTypeContactInfo
319      */
320     @Override
321     public void addAddressToEntity(List<OleEntityAddressDefinition> oleEntityAddress, EntityTypeContactInfo entityTypeContactInfo) {
322         List<EntityAddressBo> addrBos = new ArrayList<EntityAddressBo>();
323         List<OleEntityAddressBo> oleAddrBos = new ArrayList<OleEntityAddressBo>();
324         for (OleEntityAddressDefinition oleAddr : oleEntityAddress) {
325             EntityAddressBo address = EntityAddressBo.from(oleAddr.getEntityAddressBo());
326             OleAddressBo oleAddress = OleAddressBo.from(oleAddr.getOleAddressBo());
327 
328             oleAddrBos.add(OleEntityAddressBo.from(oleAddr));
329             addrBos.add(address);
330         }
331         addAddress(addrBos, EntityTypeContactInfoBo.from(entityTypeContactInfo));
332     }
333     /**
334      * If the EntityPhoneBo is not null,it will add to the EntityTypeContactInfoBo object
335      * @param entityPhone
336      * @param entityTypeContactInfoBo
337      */
338     public void addPhone(List<EntityPhoneBo> entityPhone, EntityTypeContactInfoBo entityTypeContactInfoBo) {
339         LOG.debug("Inside the addPhoneToEntity method");
340         if (entityPhone != null) {
341             entityTypeContactInfoBo.setPhoneNumbers(entityPhone);
342         }
343     }
344     /**
345      * Add the EntityPhone object to the EntityTypeContactInfo object
346      * @param entityPhone
347      * @param entityTypeContactInfo
348      */
349     @Override
350     public void addPhoneToEntity(List<EntityPhone> entityPhone, EntityTypeContactInfo entityTypeContactInfo) {
351         LOG.debug("Inside the addPhoneToEntity method");
352         List<EntityPhoneBo> phoneBos = new ArrayList<EntityPhoneBo>();
353         for (EntityPhone phone : entityPhone) {
354             phoneBos.add(EntityPhoneBo.from(phone));
355         }
356         addPhone(phoneBos, EntityTypeContactInfoBo.from(entityTypeContactInfo));
357     }
358 
359     /**
360      * Returns EntityBo based on entityId
361      * @param entityId
362      * @return entityImpl(EntityBo)
363      */
364     private EntityBo getEntityBo(String entityId) {
365         LOG.debug("Inside the getEntityBo method");
366         EntityBo entityImpl = getBusinessObjectService().findBySinglePrimaryKey(EntityBo.class, entityId);
367         if (entityImpl != null && entityImpl.getEntityTypeContactInfos() != null) {
368             for (EntityTypeContactInfoBo et : entityImpl.getEntityTypeContactInfos()) {
369                 et.refresh();
370             }
371         }
372         return entityImpl;
373     }
374 
375     /**
376      * This method will set the active indicator of the patron object as false
377      * @param patronId
378      * @return OlePatronDefinition
379      */
380     @Override
381     public OlePatronDefinition inactivatePatron(String patronId) {
382         LOG.debug("Inside the inactivatePatron method");
383         OlePatronDocument patronDocument = OlePatronDocument.from(getPatron(patronId));
384         patronDocument.setActiveIndicator(false);
385         return (OlePatronDocument.to(patronDocument));
386     }
387 
388     /**
389      * This method will update the EntityName object
390      * @param name
391      * @return EntityName
392      */
393     @Override
394     public EntityName updateName(EntityName name) {
395         LOG.debug("Inside the updateName method");
396         EntityNameBo entityName = EntityNameBo.from(getIdentityService().updateName(name));
397         return EntityNameBo.to(entityName);
398     }
399     /**
400      * This method will set the active indicator of the EntityNameBo object as false
401      * @param nameId
402      * @return
403      */
404     @Override
405     public boolean inactivateName(String nameId) {
406         LOG.debug("Inside the inactivateName method");
407         EntityNameBo entityName = EntityNameBo.from(getIdentityService().inactivateName(nameId));
408         if (entityName != null) {
409             return true;
410         }
411         return false;
412     }
413 
414     /**
415      * This method will update the EntityEmail object
416      * @param entityEmail
417      * @return
418      */
419     @Override
420     public boolean updateEmail(EntityEmail entityEmail) {
421         LOG.debug("Inside the updateEmail method");
422         EntityEmailBo email = EntityEmailBo.from(getIdentityService().updateEmail(entityEmail));
423         if (email != null) {
424             return true;
425         }
426         return false;
427     }
428     /**
429      * This method will set the active indicator of the EntityEmailBo object as false
430      * @param emailId
431      * @return
432      */
433     @Override
434     public boolean inactivateEmail(String emailId) {
435         LOG.debug("Inside the inactivateEmail method");
436         EntityEmailBo entityEmail = EntityEmailBo.from(getIdentityService().inactivateEmail(emailId));
437         if (entityEmail != null) {
438             return true;
439         }
440         return false;
441     }
442     /**
443      * This method will update the EntityAddress object
444      * @param entityAddress
445      * @return boolean
446      */
447     @Override
448     public boolean updateAddress(EntityAddress entityAddress) {
449         LOG.debug("Inside the updateAddress method");
450         EntityAddressBo address = EntityAddressBo.from(getIdentityService().updateAddress(entityAddress));
451         if (address != null) {
452             return true;
453         }
454         return false;
455     }
456     /**
457      * This method will set the active indicator of the EntityAddressBo object as false
458      * @param addressId
459      * @return true,if entityAddress is not equal to null otherwise returns false
460      */
461     @Override
462     public boolean inactivateAddress(String addressId) {
463         LOG.debug("Inside the inactivateAddress method");
464         EntityAddressBo entityAddress = EntityAddressBo.from(getIdentityService().inactivateAddress(addressId));
465         if (entityAddress != null) {
466             return true;
467         }
468         return false;
469     }
470 
471     /**
472      * This method will update the EntityPhone object
473      * @param entityPhone
474      * @return true,if phone is not equal to null otherwise returns false
475      */
476     @Override
477     public boolean updatePhone(EntityPhone entityPhone) {
478         LOG.debug("Inside the updatePhone method");
479         EntityPhoneBo phone = EntityPhoneBo.from(getIdentityService().updatePhone(entityPhone));
480         if (phone != null) {
481             return true;
482         }
483         return false;
484     }
485 
486     /**
487      * This method will set the active indicator of the EntityPhoneBo object as false
488      * @param phoneId
489      * @return true,if entityPhone is not equal to null otherwise returns false
490      */
491     @Override
492     public boolean inactivatePhone(String phoneId) {
493         LOG.debug("Inside the inactivatePhone method");
494         EntityPhoneBo entityPhone = EntityPhoneBo.from(getIdentityService().inactivatePhone(phoneId));
495         if (entityPhone != null) {
496             return true;
497         }
498         return false;
499     }
500     /**
501      * This method will Persist the OlePatronNotes object
502      * @param patronNote
503      * @return boolean
504      */
505     @Override
506     public boolean addNoteToPatron(OlePatronNotesDefinition patronNote) {
507         LOG.debug("Inside the addNoteToPatron method");
508         OlePatronNotes patronNoteBo = OlePatronNotes.from(patronNote);
509         if (patronNoteBo.getOlePatronId() != null && patronNoteBo.getOlePatronNoteType() != null) {
510             getBusinessObjectService().save(patronNoteBo);
511             return true;
512         }
513         return false;
514     }
515     /**
516      * This method will Update the OlePatronNotes object
517      * @param patronNote
518      * @return boolean
519      */
520     @Override
521     public boolean updateNote(OlePatronNotesDefinition patronNote) {
522         LOG.debug("Inside the updateNote method");
523         OlePatronNotes patronNoteBo = OlePatronNotes.from(patronNote);
524         if (patronNoteBo.getOlePatronId() != null && patronNoteBo.getPatronNoteId() != null) {
525             Map<String, Object> criteria = new HashMap<String, Object>();
526             criteria.put(OLEConstants.OlePatron.PATRON_NOTE_ID, patronNote.getPatronNoteId());
527             if (getBusinessObjectService().findByPrimaryKey(OlePatronNotes.class, criteria) != null) {
528                 getBusinessObjectService().save(patronNoteBo);
529                 return true;
530             }
531         }
532         return false;
533     }
534 
535     /**
536      * Set the active indicator of the OlePatronNotes object as false
537      * @param patronNoteId
538      * @return boolean
539      */
540     @Override
541     public boolean inactivateNote(String patronNoteId) {
542         LOG.debug("Inside the inactivateNote method");
543         if (patronNoteId != null) {
544             Map<String, Object> criteria = new HashMap<String, Object>();
545             criteria.put(OLEConstants.OlePatron.PATRON_NOTE_ID, patronNoteId);
546             OlePatronNotes patronNotes = getBusinessObjectService().findByPrimaryKey(OlePatronNotes.class, criteria);
547             patronNotes.setActive(false);
548             getBusinessObjectService().save(patronNotes);
549             return true;
550         }
551         return false;
552     }
553 
554     /**
555      * This method will return all the patron documents
556      * @return OlePatronQueryResults
557      */
558     @Override
559     public OlePatronQueryResults getPatrons() {
560         LOG.debug("Inside the findPatron method");
561         GenericQueryResults<OlePatronDocument> results = getCriteriaLookupService().lookup(OlePatronDocument.class, QueryByCriteria.Builder.create().build());
562         OlePatronQueryResults.Builder builder = OlePatronQueryResults.Builder.create();
563         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
564         builder.setTotalRowCount(results.getTotalRowCount());
565 
566         final List<OlePatronDefinition.Builder> ims = new ArrayList<OlePatronDefinition.Builder>();
567         for (OlePatronDocument bo : results.getResults()) {
568             ims.add(OlePatronDefinition.Builder.create(bo));
569         }
570 
571         builder.setResults(ims);
572         return builder.build();
573 
574     }
575     /**
576      * This method will return patron objects based on search criteria
577      * @param queryCriteria
578      * @return OlePatronQueryResults
579      */
580     @Override
581     public OlePatronQueryResults findPatron(QueryByCriteria queryCriteria) {
582         LOG.debug("Inside the findPatron method");
583         GenericQueryResults<OlePatronDocument> results = getCriteriaLookupService().lookup(OlePatronDocument.class, QueryByCriteria.Builder.create().build());
584         OlePatronQueryResults.Builder builder = OlePatronQueryResults.Builder.create();
585         builder.setMoreResultsAvailable(results.isMoreResultsAvailable());
586         builder.setTotalRowCount(results.getTotalRowCount());
587 
588         final List<OlePatronDefinition.Builder> ims = new ArrayList<OlePatronDefinition.Builder>();
589         for (OlePatronDocument bo : results.getResults()) {
590             ims.add(OlePatronDefinition.Builder.create(bo));
591         }
592 
593         builder.setResults(ims);
594         return builder.build();
595     }
596 
597     /**
598      * This method will return Patron loan items list  information
599      * @param patronBarcode
600      * @return OleLoanDocument
601      */
602     @Override
603     public OlePatronLoanDocuments getPatronLoanedItems(String patronBarcode) {
604 
605 
606         List<OleLoanDocument> oleLoanDocumentList=new ArrayList<OleLoanDocument>();
607         List<OlePatronLoanDocument> olePatronLoanItemList=new ArrayList<OlePatronLoanDocument>();
608         try{
609             oleLoanDocumentList=getLoanProcessor().getPatronLoanedItem(patronBarcode);
610             OlePatronLoanDocuments olePatronLoanDocuments=convertPatronLoanDocuments(oleLoanDocumentList);
611             return olePatronLoanDocuments;
612         }
613         catch(Exception e){
614             LOG.info("exception ----->  "+e);
615         }
616 
617         return  null;
618     }
619 
620     public List<OleDeliverRequestDefinition> getPatronRequestItems(String patronId) {
621 
622 
623         List<OleDeliverRequestBo> oleDeliverRequestBos =new ArrayList<OleDeliverRequestBo>();
624         List<OleDeliverRequestDefinition> oleDeliverRequestDefinitions =new ArrayList<OleDeliverRequestDefinition>();
625         try{
626             oleDeliverRequestBos = getLoanProcessor().getPatronRequestRecords(patronId);
627             OleDeliverRequestDefinition oleDeliverRequestDefinition = new OleDeliverRequestDefinition();
628             for(OleDeliverRequestBo oleDeliverRequestBo : oleDeliverRequestBos) {
629              oleDeliverRequestDefinition = OleDeliverRequestBo.to(oleDeliverRequestBo);
630                 oleDeliverRequestDefinitions.add(oleDeliverRequestDefinition);
631             }
632             return oleDeliverRequestDefinitions;
633         }
634         catch(Exception e){
635             LOG.info("exception ----->  "+e);
636         }
637 
638         return  null;
639     }
640 
641 
642 
643     public OlePatronLoanDocuments performRenewalItems(OlePatronLoanDocuments olePatronLoanDocuments){
644 
645         List<OleLoanDocument> oleLoanDocumentList=new ArrayList<OleLoanDocument>(0);
646         List<OleRenewalLoanDocument>  oleRenewalLoanDocumentList=convertRenewalLoanDocuments(olePatronLoanDocuments);
647         try{
648 
649             for(int i=0;i<oleRenewalLoanDocumentList.size();i++){
650                 OleRenewalLoanDocument oleRenewalLoanDocument=oleRenewalLoanDocumentList.get(i);
651                 OleLoanDocument oleLoanDocument=getLoanProcessor().getPatronRenewalItem(oleRenewalLoanDocument.getItemBarcode());
652                 if(!getLoanProcessor().checkPendingRequestforItem(oleLoanDocument.getItemId())){
653                         oleLoanDocument.setRenewalItemFlag(true);
654                         oleLoanDocument = getLoanProcessor().addLoan(oleLoanDocument.getPatronBarcode(),oleLoanDocument.getItemId(),oleLoanDocument);
655                 }
656                 else
657                   oleLoanDocument.setErrorMessage(OLEConstants.PENDING_RQST_RENEWAL_ITM_INFO+"( Title: "+oleLoanDocument.getTitle()+" , Author: "+oleLoanDocument.getAuthor()+" , Item : "+oleLoanDocument.getItemId()+" )");
658                 oleLoanDocumentList.add(oleLoanDocument);
659             }
660             olePatronLoanDocuments=convertPatronLoanDocuments(oleLoanDocumentList);
661             return olePatronLoanDocuments;
662         }
663         catch(Exception e){
664             LOG.info("exception ---> "+e);
665         }
666         return null;
667     }
668 
669 
670     private OlePatronLoanDocuments convertPatronLoanDocuments(List<OleLoanDocument> oleLoanDocumentList) {
671 
672 
673         List<OlePatronLoanDocument> olePatronLoanItemList=new ArrayList<OlePatronLoanDocument>();
674 
675         for(int i=0;i<oleLoanDocumentList.size();i++)  {
676             OleLoanDocument oleLoanDocument=oleLoanDocumentList.get(i);
677             OleRenewalLoanDocument oleRenewalLoanDocument=new OleRenewalLoanDocument();
678             oleRenewalLoanDocument.setAuthor(oleLoanDocument.getAuthor());
679             oleRenewalLoanDocument.setTitle(oleLoanDocument.getTitle());
680             oleRenewalLoanDocument.setCallNumber(oleLoanDocument.getItemCallNumber());
681             oleRenewalLoanDocument.setDueDate(oleLoanDocument.getLoanDueDate());
682             oleRenewalLoanDocument.setItemBarcode(oleLoanDocument.getItemId());
683             oleRenewalLoanDocument.setLocation(oleLoanDocument.getLocation());
684 
685             if(oleLoanDocument.getErrorMessage() == null){
686                 oleRenewalLoanDocument.setMessageInfo(OLEConstants.RENEWAL_ITM_SUCCESS_INFO);
687             }else
688             {
689                 String errMsg="";
690                 if(oleLoanDocument.getErrorMessage().contains("(OR)"))
691                    errMsg=oleLoanDocument.getErrorMessage().substring(0,oleLoanDocument.getErrorMessage().lastIndexOf("(OR)"));
692                 else
693                    errMsg= oleLoanDocument.getErrorMessage();
694                 oleRenewalLoanDocument.setMessageInfo(errMsg);
695             }
696             OlePatronLoanDocument olePatronLoanDocument=OlePatronLoanDocuments.to(oleRenewalLoanDocument);
697             olePatronLoanItemList.add(olePatronLoanDocument);
698         }
699         OlePatronLoanDocuments olePatronLoanDocuments=getOlePatronLoanDocuments(olePatronLoanItemList);
700         return olePatronLoanDocuments;
701 
702     }
703 
704 
705     private List<OleRenewalLoanDocument>  convertRenewalLoanDocuments(OlePatronLoanDocuments olePatronLoanDocuments){
706 
707         List<OleRenewalLoanDocument> oleRenewalLoanDocumentList=new ArrayList<OleRenewalLoanDocument>();
708         for(int i=0;i<olePatronLoanDocuments.getOlePatronLoanDocuments().size();i++){
709             OlePatronLoanDocument olePatronLoanDocument=(OlePatronLoanDocument)olePatronLoanDocuments.getOlePatronLoanDocuments().get(i);
710             OleRenewalLoanDocument oleRenewalLoanDocument=new OleRenewalLoanDocument();
711             oleRenewalLoanDocument.setItemBarcode(olePatronLoanDocument.getItemBarcode());
712             oleRenewalLoanDocument.setCallNumber(olePatronLoanDocument.getCallNumber());
713             oleRenewalLoanDocument.setDueDate(olePatronLoanDocument.getDueDate());
714             oleRenewalLoanDocument.setLocation(olePatronLoanDocument.getLocation());
715             oleRenewalLoanDocument.setTitle(olePatronLoanDocument.getTitle());
716             oleRenewalLoanDocument.setAuthor(olePatronLoanDocument.getAuthor());
717             oleRenewalLoanDocumentList.add(oleRenewalLoanDocument);
718         }
719         return oleRenewalLoanDocumentList;
720     }
721 
722     private  OlePatronLoanDocuments convertOlePatronLoanDocuments(List<OleRenewalLoanDocument> oleRenewalLoanDocumentList){
723         List<OlePatronLoanDocument> olePatronLoanItemList=new ArrayList<OlePatronLoanDocument>();
724         for(int i=0;i<oleRenewalLoanDocumentList.size();i++){
725             OleRenewalLoanDocument oleRenewalLoanDocument= oleRenewalLoanDocumentList.get(i);
726             OlePatronLoanDocument olePatronLoanDocument=OlePatronLoanDocuments.to(oleRenewalLoanDocument);
727             olePatronLoanItemList.add(olePatronLoanDocument);
728         }
729         OlePatronLoanDocuments olePatronLoanDocuments=getOlePatronLoanDocuments(olePatronLoanItemList);
730         return olePatronLoanDocuments;
731     }
732 
733     private OlePatronLoanDocuments  getOlePatronLoanDocuments(List<OlePatronLoanDocument> olePatronLoanItemList) {
734         OleRenewalLoanDocument oleRenewalLoanDocument=new OleRenewalLoanDocument();
735         if(olePatronLoanItemList.size()!=0) {
736             oleRenewalLoanDocument.setOlePatronLoanDocuments(olePatronLoanItemList);
737             OlePatronLoanDocuments olePatronLoanDocuments=OlePatronLoanDocuments.Builder.create(oleRenewalLoanDocument).build();
738             return olePatronLoanDocuments;
739         }
740 
741         return null;
742 
743     }
744 
745 
746 
747     public void deletePatronBatchProgram() {
748         LOG.debug("Inside deletePatronDocument (Batch Delete Program)");
749         OlePatronDocument olePatronDocument = new OlePatronDocument();
750         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OlePatron.PATRON_MAINTENANCE_DATE_FORMAT);
751         OlePatron olePatron;
752         List<OlePatronDocument> patronImpls = (List<OlePatronDocument>) getBusinessObjectService().findAll(OlePatronDocument.class);
753         for (Iterator<OlePatronDocument> patronIterator = patronImpls.iterator(); patronIterator.hasNext(); ) {
754             olePatronDocument = patronIterator.next();
755             if ((olePatronDocument.getExpirationDate() != null && fmt.format(new Date(System.currentTimeMillis())).compareTo(fmt.format(olePatronDocument.getExpirationDate())) > 0) || (olePatronDocument.getExpirationDate() == null)) {
756                 List<OleLoanDocument> oleLoanDocuments = olePatronDocument.getOleLoanDocuments();
757                 List<OleTemporaryCirculationHistory> oleTemporaryCirculationHistories = olePatronDocument.getOleTemporaryCirculationHistoryRecords();
758                 List<OleDeliverRequestBo> oleDeliverRequestBos = olePatronDocument.getOleDeliverRequestBos();
759                 Map billMap = new HashMap();
760                 billMap.put(OLEConstants.OlePatron.PAY_BILL_PATRON_ID, olePatronDocument.getOlePatronId());
761                 List<PatronBillPayment> patronBillPayments = (List<PatronBillPayment>) KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class, billMap);
762                 if ((oleLoanDocuments.size() == 0 && oleTemporaryCirculationHistories.size() == 0 && oleDeliverRequestBos.size() == 0 && patronBillPayments.size() == 0)) {
763                     olePatronDocument.setActiveIndicator(false);
764                     KRADServiceLocator.getBusinessObjectService().save(olePatronDocument);
765                 }
766             }
767         }
768     }
769 
770 }