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