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