View Javadoc
1   package org.kuali.ole.service;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang3.StringUtils;
5   import org.kuali.ole.OLEConstants;
6   import org.kuali.ole.deliver.bo.PatronBillPayment;
7   import org.kuali.ole.deliver.bo.*;
8   import org.kuali.rice.kim.api.KimConstants;
9   import org.kuali.rice.kim.api.identity.entity.Entity;
10  import org.kuali.rice.kim.api.identity.entity.EntityDefault;
11  import org.kuali.rice.kim.impl.KIMPropertyConstants;
12  import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
13  import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
14  import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
15  import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
16  import org.kuali.rice.kim.impl.identity.entity.EntityBo;
17  import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
18  import org.kuali.rice.kim.impl.identity.type.EntityTypeContactInfoBo;
19  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
20  import org.kuali.rice.kim.service.UiDocumentService;
21  import org.kuali.rice.krad.lookup.LookupableImpl;
22  import org.kuali.rice.krad.service.BusinessObjectService;
23  import org.kuali.rice.krad.service.KRADServiceLocator;
24  import org.kuali.rice.krad.util.GlobalVariables;
25  import org.kuali.rice.krad.util.KRADConstants;
26  
27  import java.text.SimpleDateFormat;
28  import java.util.*;
29  
30  /**
31   * OlePatronHelperServiceImpl converts PatronProperties to EntityProperties and generate new search criteria.
32   */
33  public class OlePatronHelperServiceImpl  implements OlePatronHelperService {
34      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePatronHelperServiceImpl.class);
35  
36      protected static final String ENTITY_EMAIL_PROPERTY_PREFIX = "entity.entityTypeContactInfos.emailAddresses.";
37      protected static final String ENTITY_NAME_PROPERTY_PREFIX = "entity.names.";
38      protected static final String BILL_ENTITY_NAME_PROPERTY_PREFIX = "olePatron.entity.names.";
39      private BusinessObjectService businessObjectService;
40      protected Map<String, String> criteriaConversion = new HashMap<String, String>();
41  
42      {
43          criteriaConversion.put(KIMPropertyConstants.Person.FIRST_NAME, "entity.names.firstName");
44          criteriaConversion.put(KIMPropertyConstants.Person.MIDDLE_NAME, "entity.names.middleName");
45          criteriaConversion.put(KIMPropertyConstants.Person.LAST_NAME, "entity.names.lastName");
46          criteriaConversion.put(KIMPropertyConstants.Person.EMAIL_ADDRESS, "entity.entityTypeContactInfos.emailAddresses.emailAddress");
47      }
48  
49      protected Map<String, String> billCriteriaConversion = new HashMap<String, String>();
50  
51      {
52          billCriteriaConversion.put(KIMPropertyConstants.Person.FIRST_NAME, "olePatron.entity.names.firstName");
53          billCriteriaConversion.put(KIMPropertyConstants.Person.LAST_NAME, "olePatron.entity.names.lastName");
54          billCriteriaConversion.put(KIMPropertyConstants.Person.EMAIL_ADDRESS, "olePatron.entity.entityTypeContactInfos.emailAddresses.emailAddress");
55      }
56  
57  
58  
59      public EntityBo editAndSaveEntityBo(OlePatronDocument patronDocument) {
60          LOG.debug("Inside the editAndSaveEntityBo method");
61          EntityBo kimEntity;
62          List<EntityTypeContactInfoBo> entityTypes;
63          EntityTypeContactInfoBo entityType;
64          kimEntity = patronDocument.getEntity();
65          if (kimEntity.getId() == null) {
66              String kimEntityId=KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("krim_entity_id_s").toString();
67              kimEntity.setId(kimEntityId);
68              entityTypes = new ArrayList<EntityTypeContactInfoBo>();
69              entityType = new EntityTypeContactInfoBo();
70              kimEntity.setActive(true);
71              entityType.setEntityTypeCode(KimConstants.EntityTypes.PERSON);
72              entityType.setActive(true);
73              entityType.setEntityId(kimEntity.getId());
74          } else {
75              kimEntity.setActive(true);
76              entityTypes = kimEntity.getEntityTypeContactInfos();
77              if (!entityTypes.isEmpty()) {
78                  entityType = entityTypes.get(0);
79                  entityType.setActive(true);
80              } else {
81                  entityType = new EntityTypeContactInfoBo();
82                  entityType.setEntityTypeCode(KimConstants.EntityTypes.PERSON);
83              }
84          }
85  
86          List<EntityAffiliationBo> affiliationBoList = new ArrayList<EntityAffiliationBo>();
87          List<EntityEmploymentBo> employeeList = new ArrayList<EntityEmploymentBo>();
88          String affiliationSeq = null;
89          if (patronDocument.getPatronAffiliations().size() > 0) {
90              for (OlePatronAffiliation patronAffiliation : patronDocument.getPatronAffiliations()) {
91                  patronAffiliation.setEntityId(kimEntity.getId());
92                  EntityAffiliationBo entityAffiliationBo = patronAffiliation.getEntityAffliationBo();
93                  if (entityAffiliationBo.getId() == null) {
94                      affiliationSeq = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("krim_entity_afltn_id_s").toString();
95                      entityAffiliationBo.setId(affiliationSeq);
96                  }
97                  for (EntityEmploymentBo employmentBo : patronAffiliation.getEmployments()) {
98                      employmentBo.setEntityAffiliation(entityAffiliationBo);
99                      employmentBo.setEntityAffiliationId(entityAffiliationBo.getId());
100                     employeeList.add(employmentBo);
101                 }
102                 affiliationBoList.add(entityAffiliationBo);
103             }
104         }
105         kimEntity.setAffiliations(affiliationBoList);
106         kimEntity.setEmploymentInformation(employeeList);
107         kimEntity.setNames(Arrays.asList(patronDocument.getName()));
108         List<EntityAddressBo> addressList = new ArrayList<EntityAddressBo>();
109         if (patronDocument.getOleEntityAddressBo().size() > 0) {
110             for (OleEntityAddressBo oleEntityAddress : patronDocument.getOleEntityAddressBo()) {
111                 if(oleEntityAddress.getEntityAddressBo()!=null && oleEntityAddress.getEntityAddressBo().getAddressType()!=null){
112                     oleEntityAddress.getEntityAddressBo().setAddressTypeCode(oleEntityAddress.getEntityAddressBo().getAddressType().getCode());
113                 }
114                 addressList.add(oleEntityAddress.getEntityAddressBo());
115             }
116         }
117         patronDocument.setEmployments(employeeList);
118         entityTypes.clear();
119         entityType.setAddresses(addressList);
120         entityType.setEmailAddresses(patronDocument.getEmails());
121         entityType.setPhoneNumbers(patronDocument.getPhones());
122         entityTypes.add(entityType);
123         kimEntity.setEntityTypeContactInfos(entityTypes);
124         try {
125           patronDocument.getIdentityService().updateEntity(EntityBo.to(kimEntity));
126         } catch (Exception e) {
127             LOG.error("Unable to save edited patron Record" + e);
128         }
129 
130         return kimEntity;
131     }
132 
133     public EntityBo copyAndSaveEntityBo(OlePatronDocument patronDocument) {
134         LOG.debug("Inside the copyAndSaveEntityBo method");
135 
136         EntityBo kimEntity;
137         List<EntityTypeContactInfoBo> entityTypes;
138         EntityTypeContactInfoBo entityType;
139         kimEntity = patronDocument.getEntity();
140         kimEntity.setId(null);
141         kimEntity.setObjectId(null);
142         kimEntity.setVersionNumber(null);
143         boolean isUpdatePatron=false;
144         if (kimEntity.getId() == null) {
145             String kimEntityId=KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("krim_entity_id_s").toString();
146             kimEntity.setId(kimEntityId);
147             entityTypes = new ArrayList<EntityTypeContactInfoBo>();
148             entityType = new EntityTypeContactInfoBo();
149             kimEntity.setActive(true);
150             entityType.setEntityTypeCode(KimConstants.EntityTypes.PERSON);
151             entityType.setActive(true);
152             entityType.setEntityId(kimEntity.getId());
153         } else {
154             isUpdatePatron=true;
155             kimEntity.setActive(true);
156             entityTypes = kimEntity.getEntityTypeContactInfos();
157             if (!entityTypes.isEmpty()) {
158                 entityType = entityTypes.get(0);
159                 entityType.setActive(true);
160             } else {
161                 entityType = new EntityTypeContactInfoBo();
162                 entityType.setEntityTypeCode(KimConstants.EntityTypes.PERSON);
163             }
164             entityType.setEntityId(kimEntity.getId());
165         }
166 
167         List<EntityAffiliationBo> affiliationBoList = new ArrayList<EntityAffiliationBo>();
168         List<EntityEmploymentBo> employeeList = new ArrayList<EntityEmploymentBo>();
169         String affiliationSeq = null;
170         if (patronDocument.getPatronAffiliations().size() > 0) {
171             for (OlePatronAffiliation patronAffiliation : patronDocument.getPatronAffiliations()) {
172                 EntityAffiliationBo entityAffiliationBo = patronAffiliation.getEntityAffliationBo();
173                 entityAffiliationBo.setEntityId(kimEntity.getId());
174                 entityAffiliationBo.setObjectId(null);
175                 entityAffiliationBo.setVersionNumber(null);
176                 if (entityAffiliationBo.getId() == null) {
177                     affiliationSeq = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("krim_entity_afltn_id_s").toString();
178                     entityAffiliationBo.setId(affiliationSeq);
179                 }
180                 for (EntityEmploymentBo employmentBo : patronAffiliation.getEmployments()) {
181                     employmentBo.setEntityAffiliation(entityAffiliationBo);
182                     employmentBo.setEntityAffiliationId(entityAffiliationBo.getId());
183                     employeeList.add(employmentBo);
184                 }
185                 affiliationBoList.add(entityAffiliationBo);
186             }
187         }
188         kimEntity.setAffiliations(affiliationBoList);
189         kimEntity.setEmploymentInformation(employeeList);
190         if (patronDocument.getName() != null) {
191             patronDocument.getName().setId(null);
192             patronDocument.getName().setVersionNumber(null);
193             patronDocument.getName().setObjectId(null);
194             patronDocument.getName().setEntityId(null);
195         }
196         kimEntity.setNames(Arrays.asList(patronDocument.getName()));
197 
198         List<EntityAddressBo> addressList = new ArrayList<EntityAddressBo>();
199         if (patronDocument.getOleEntityAddressBo() != null) {
200             for (OleEntityAddressBo oleEntityAddress : patronDocument.getOleEntityAddressBo()) {
201                 oleEntityAddress.getEntityAddressBo().setId(null);
202                 oleEntityAddress.getEntityAddressBo().setVersionNumber(null);
203                 oleEntityAddress.getEntityAddressBo().setObjectId(null);
204                 oleEntityAddress.getEntityAddressBo().setEntityId(kimEntity.getId());
205                 if(oleEntityAddress.getEntityAddressBo()!=null && oleEntityAddress.getEntityAddressBo().getAddressType()!=null){
206                     oleEntityAddress.getEntityAddressBo().setAddressTypeCode(oleEntityAddress.getEntityAddressBo().getAddressType().getCode());
207                 }
208                 addressList.add(oleEntityAddress.getEntityAddressBo());
209             }
210         }
211         List<EntityPhoneBo> entityPhoneBos = new ArrayList<EntityPhoneBo>();
212         if (patronDocument.getPhones() != null) {
213             for (EntityPhoneBo entityPhoneBo : patronDocument.getPhones()) {
214                 entityPhoneBo.setObjectId(null);
215                 entityPhoneBo.setVersionNumber(null);
216                 entityPhoneBo.setId(null);
217                 if(entityPhoneBo.getPhoneType()!=null){
218                     entityPhoneBo.setPhoneTypeCode(entityPhoneBo.getPhoneType().getCode());
219                 }
220                 entityPhoneBo.setEntityId(kimEntity.getId());
221                 entityPhoneBos.add(entityPhoneBo);
222             }
223         }
224         List<EntityEmailBo> entityEmailBos = new ArrayList<>();
225         if (patronDocument.getEmails() != null) {
226             for (EntityEmailBo emailBo : patronDocument.getEmails()) {
227                 emailBo.setObjectId(null);
228                 emailBo.setVersionNumber(null);
229                 emailBo.setId(null);
230                 if(emailBo.getEmailType()!=null){
231                     emailBo.setEmailTypeCode(emailBo.getEmailType().getCode());
232                 }
233                 emailBo.setEntityId(kimEntity.getId());
234                 entityEmailBos.add(emailBo);
235             }
236         }
237         entityType.setAddresses(addressList);
238         entityType.setEmailAddresses(entityEmailBos);
239         entityType.setPhoneNumbers(entityPhoneBos);
240         entityTypes.add(entityType);
241         kimEntity.setEntityTypeContactInfos(entityTypes);
242         try {
243             if (isUpdatePatron) {
244                 Entity entity = patronDocument.getIdentityService().updateEntity(EntityBo.to(kimEntity));
245             } else {
246                 Entity entity = patronDocument.getIdentityService().createEntity(EntityBo.to(kimEntity));
247             }
248 
249         } catch (Exception e) {
250             LOG.error("Copied to save edited patron Record" + e);
251         }
252         return kimEntity;
253     }
254 
255     public List<OleAddressBo> retrieveOleAddressBo(EntityBo entityBo, OlePatronDocument patronDocument) {
256         List<EntityAddressBo> addressBos = new ArrayList<>();
257         List<OleEntityAddressBo> oleEntityAddressBos = new ArrayList<>();
258         if(CollectionUtils.isNotEmpty(entityBo.getEntityTypeContactInfos())){
259             addressBos = entityBo.getEntityTypeContactInfos().get(0).getAddresses();
260         }
261         boolean emptyOleEntityAddress = false;
262         List<OleAddressBo> oleAddressBos = new ArrayList<OleAddressBo>();
263         for (int i = 0; i < addressBos.size(); i++) {
264             EntityAddressBo entityAddressBo = addressBos.get(i);
265             Map<String, Object> criteria = new HashMap<String, Object>();
266             criteria.put(OLEConstants.OlePatron.ENTITY_BO_ID, entityAddressBo.getId());
267             OleAddressBo oleAddressBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleAddressBo.class, criteria);
268             if (oleAddressBo == null) {
269                 oleAddressBo = new OleAddressBo();
270             }
271             oleAddressBo.setOlePatronId(entityBo.getId());
272             oleAddressBo.setOleAddressId(KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("OLE_DLVR_ADD_S").toString());
273             oleAddressBo.setEntityAddress(entityAddressBo);
274             if (CollectionUtils.isNotEmpty(patronDocument.getOleEntityAddressBo()) && !emptyOleEntityAddress) {
275                 oleAddressBo.setAddressValidFrom(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().getAddressValidFrom());
276                 oleAddressBo.setAddressValidTo(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().getAddressValidTo());
277                 oleAddressBo.setAddressVerified(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().isAddressVerified());
278                 oleAddressBo.setAddressSource(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().getAddressSource());
279                 oleAddressBo.setId(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().getId());
280                 oleAddressBo.setDeliverAddress(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().isDeliverAddress());
281             }else{
282                 emptyOleEntityAddress = true;
283                 OleEntityAddressBo oleEntityAddressBo = new OleEntityAddressBo();
284                 if(StringUtils.isBlank(oleAddressBo.getId())){
285                     oleAddressBo.setId(entityAddressBo.getId());
286                     if(CollectionUtils.isNotEmpty(patronDocument.getOleAddresses()) && patronDocument.getOleAddresses().size() > i && patronDocument.getOleAddresses().get(i) != null){
287                         oleAddressBo.setAddressValidFrom(patronDocument.getOleAddresses().get(i).getAddressValidFrom());
288                         oleAddressBo.setAddressValidTo(patronDocument.getOleAddresses().get(i).getAddressValidTo());
289                         oleAddressBo.setAddressVerified(patronDocument.getOleAddresses().get(i).isAddressVerified());
290                         oleAddressBo.setAddressSource(patronDocument.getOleAddresses().get(i).getAddressSource());
291                         oleAddressBo.setDeliverAddress(patronDocument.getOleAddresses().get(i).isDeliverAddress());
292                     }
293                 }
294                 oleEntityAddressBo.setOleAddressBo(oleAddressBo);
295                 oleEntityAddressBo.setEntityAddressBo(entityAddressBo);
296                 oleEntityAddressBos.add(oleEntityAddressBo);
297             }
298             oleAddressBos.add(oleAddressBo);
299 
300         }
301 
302         if(emptyOleEntityAddress){
303             patronDocument.setOleEntityAddressBo(oleEntityAddressBos);
304         }
305         return oleAddressBos;
306     }
307 
308 
309     /**
310      * Check whether name entity is present in search criteria
311      *
312      * @param propertyName
313      * @return boolean
314      */
315     protected boolean isNameEntityCriteria(String propertyName) {
316         LOG.debug("Inside the isNameEntityCriteria method");
317         return propertyName.startsWith(ENTITY_NAME_PROPERTY_PREFIX);
318     }
319 
320     /**
321      * Check whether email entity is present in search criteria
322      *
323      * @param propertyName
324      * @return boolean
325      */
326     protected boolean isEmailEntityCriteria(String propertyName) {
327         LOG.debug("Inside the isEmailEntityCriteria method");
328         return propertyName.startsWith(ENTITY_EMAIL_PROPERTY_PREFIX);
329     }
330 
331 
332     public boolean deletePatron(OlePatronDocument olePatronDocument) {
333         LOG.debug("Inside the deletePatron method");
334         boolean deleteFlag = false;
335         SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OlePatron.PATRON_MAINTENANCE_DATE_FORMAT);
336         Map billMap = new HashMap();
337         billMap.put(OLEConstants.OlePatron.PAY_BILL_PATRON_ID, olePatronDocument.getOlePatronId());
338         List<PatronBillPayment> patronBillPayments = (List<PatronBillPayment>) KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class, billMap);
339 
340         if (olePatronDocument != null && olePatronDocument.getOlePatronId() != null) {
341             if (olePatronDocument.getOleLoanDocuments() == null || olePatronDocument.getOleLoanDocuments().size() == 0) {
342                 if ((olePatronDocument.getOleTemporaryCirculationHistoryRecords() == null || olePatronDocument
343                         .getOleTemporaryCirculationHistoryRecords().size() == 0)) {
344                     if ((olePatronDocument.getOleDeliverRequestBos() == null || olePatronDocument.getOleDeliverRequestBos().size() == 0)) {
345                         if ((patronBillPayments == null || patronBillPayments.size() == 0)) {
346                             olePatronDocument.setActiveIndicator(false);
347                             List<OleAddressBo> addressBos = olePatronDocument.getOleAddresses();
348                             if (addressBos.size() > 0) {
349                                 for (OleAddressBo addressBo : addressBos) {
350                                     if (addressBo.getAddressSource().isEmpty() || "".equals(addressBo.getAddressSource())) {
351                                         addressBo.setAddressSource(null);
352                                     }
353                                 }
354                             }
355                             KRADServiceLocator.getBusinessObjectService().save(olePatronDocument);
356                             deleteFlag = true;
357                         }
358                     }
359                 }
360             }
361         }
362         return deleteFlag;
363     }
364 
365     /**
366      * This method is for checking the added address source or not
367      *
368      * @param oleAddresses
369      * @return true , if the address source has value in a single patron record, else false
370      */
371     public boolean checkAddressSource(List<OleAddressBo> oleAddresses) {
372 
373         boolean valid = true;
374         for (OleAddressBo oleAddress : oleAddresses) {
375             if ((oleAddress.getAddressSource() != null) && (oleAddress.getAddressSource().equals(""))) {
376                 valid = false;
377             }
378         }
379         return valid;
380     }
381 
382     /**
383      * This method is for checking the entity phone has multiple defaults
384      *
385      * @param phoneBoList
386      * @return true , if the entity phone has only one default in a single patron record, else false
387      */
388     public boolean checkPhoneMultipleDefault(List<EntityPhoneBo> phoneBoList) {
389         boolean valid = true;
390         boolean isDefaultSet = false;
391         int i = 0;
392         for (EntityPhoneBo phone : phoneBoList) {
393             if (phone.isDefaultValue()) {
394                 if (isDefaultSet) {
395                     valid = false;
396                 } else {
397                     isDefaultSet = true;
398                 }
399             }
400             i++;
401         }
402         if (!phoneBoList.isEmpty() && !isDefaultSet) {
403             valid = false;
404         }
405         return valid;
406     }
407 
408     /**
409      * This method is for checking the entity address has multiple defaults
410      *
411      * @param addrBoList
412      * @return true , if the entity address has only one default in a single patron record, else false
413      */
414     public boolean checkAddressMultipleDefault(List<OleEntityAddressBo> addrBoList) {
415         boolean valid = true;
416         boolean isDefaultSet = false;
417         int i = 0;
418         for (OleEntityAddressBo addr : addrBoList) {
419             EntityAddressBo entityAddressBo = addr.getEntityAddressBo();
420             if (entityAddressBo.isDefaultValue()) {
421                 if (isDefaultSet) {
422                     valid = false;
423                 } else {
424                     isDefaultSet = true;
425                 }
426             }
427             i++;
428         }
429         if (!addrBoList.isEmpty() && !isDefaultSet) {
430             valid = false;
431         }
432         return valid;
433     }
434 
435     /**
436      * This method is for checking the entity email address has multiple defaults
437      *
438      * @param emailBoList
439      * @return true , if the entity email address has only one default in a single patron record, else false
440      */
441     public boolean checkEmailMultipleDefault(List<EntityEmailBo> emailBoList) {
442         boolean valid = true;
443         boolean isDefaultSet = false;
444         int i = 0;
445         for (EntityEmailBo email : emailBoList) {
446             if (email.isDefaultValue()) {
447                 if (isDefaultSet) {
448                     valid = false;
449                 } else {
450                     isDefaultSet = true;
451                 }
452             }
453             i++;
454         }
455         if (!emailBoList.isEmpty() && !isDefaultSet) {
456             valid = false;
457         }
458         return valid;
459     }
460 
461     public boolean isBorrowerTypeActive(OlePatronDocument olePatronDocument) {
462         boolean isPatronTypeActive = false;
463         HashMap<String, String> map = new HashMap<String, String>();
464         map.put(OLEConstants.BORROWER_TYPE_ID, olePatronDocument.getBorrowerType());
465         List<OleBorrowerType> borrowerTypes = (List<OleBorrowerType>) getBusinessObjectService().findMatching(OleBorrowerType.class, map);
466         if (borrowerTypes.size() > 0) {
467             if (borrowerTypes.get(0).isActive()) {
468                 isPatronTypeActive = true;
469             }
470         }
471         return isPatronTypeActive;
472     }
473 
474     public boolean validatePatron(OlePatronDocument patronDocument) {
475         boolean isValid = true;
476         isValid &= validateEntityInformation(patronDocument);
477         isValid &= validateAddress(patronDocument, "oleEntityAddressBo");
478         isValid &= validateRequiredField(patronDocument);
479         isValid &=checkDuplicateBarcode(patronDocument);
480         return isValid;
481     }
482 
483     private boolean validateEntityInformation( OlePatronDocument patronDoc) {
484         boolean valid = true;
485         List<OleEntityAddressBo> addressBoList = patronDoc.getOleEntityAddressBo();
486         List<EntityEmailBo> emailBoList = patronDoc.getEmails();
487         List<EntityPhoneBo> phoneBoList = patronDoc.getPhones();
488         if (addressBoList.size() == 1) {
489             OleEntityAddressBo oleEntityAddressBo = addressBoList.get(0);
490             oleEntityAddressBo.getEntityAddressBo().setDefaultValue(true);
491         }
492         if (emailBoList.size() == 1) {
493             EntityEmailBo entityEmailBo = emailBoList.get(0);
494             entityEmailBo.setDefaultValue(true);
495         }
496         if (phoneBoList.size() == 1) {
497             EntityPhoneBo entityPhoneBo = phoneBoList.get(0);
498             entityPhoneBo.setDefaultValue(true);
499         }
500         if (!checkPhoneMultipleDefault(patronDoc.getPhones())) {
501             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PHONE_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_PHONE);
502             valid &= false;
503         }
504         if (!checkAddressMultipleDefault(patronDoc.getOleEntityAddressBo())) {
505             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.ADDRESS_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_ADDRESS);
506             valid &= false;
507         }
508         if (!checkEmailMultipleDefault(patronDoc.getEmails())) {
509             GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.EMAIL_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_EMAIL);
510             valid &= false;
511         }
512         if (!checkAddressMultipleDeliverAddress(patronDoc.getOleEntityAddressBo(), "oleEntityAddressBo")) {
513            // GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.ADDRESS_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_DELIVER_ADDRESS);
514             valid &= false;
515         }
516         return valid;
517     }
518 
519     protected boolean checkAddressMultipleDeliverAddress(List<OleEntityAddressBo> addrBoList, String listName) {
520         boolean valid = true;
521         boolean isDefaultSet = false;
522         boolean isAtleastOneChecked=false;
523         int i = 0;
524         for (OleEntityAddressBo addr : addrBoList) {
525             OleAddressBo oleAddressBo = addr.getOleAddressBo();
526             if (oleAddressBo.isDeliverAddress()) {
527                 isAtleastOneChecked=true;
528                 if (isDefaultSet) {
529                     //this.putFieldError("dataObject." + listName + "[" + i + "].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_MULIT_DELIVER_ADDRESS);
530                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_MULIT_DELIVER_ADDRESS);
531                     valid = false;
532                 } else {
533                     isDefaultSet = true;
534                 }
535             }
536             i++;
537         }
538         if(!isAtleastOneChecked){
539                valid=true;
540         } else {
541             if (!addrBoList.isEmpty() && !isDefaultSet) {
542                 //this.putFieldError("dataObject."+listName+"[0].defaultValue",RiceKeyConstants.ERROR_NO_DEFAULT_SELETION);
543                 valid = false;
544             }
545         }
546         return valid;
547     }
548 
549     public boolean validateRequiredField(OlePatronDocument olePatronDocument) {
550         boolean valid = true;
551         List<OleEntityAddressBo> addressBoList = olePatronDocument.getOleEntityAddressBo();
552         List<EntityEmailBo> emailBoList = olePatronDocument.getEmails();
553         if (!(addressBoList.size() > 0 || emailBoList.size() > 0)) {
554             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS);
555             valid = false;
556         }
557         return valid;
558     }
559 
560     public boolean validateAddress(OlePatronDocument olePatronDocument, String addressBos) {
561         List<OleEntityAddressBo> addressBoList = olePatronDocument.getOleEntityAddressBo();
562         OleEntityAddressBo oleEntityAddressBo;
563         boolean valid = true;
564         boolean flag=true;
565         boolean dateExist = true;
566         Map<Date,Date> map=new HashMap<>();
567         for (int i = 0; i < addressBoList.size(); i++) {
568             oleEntityAddressBo = addressBoList.get(i);
569             if (oleEntityAddressBo.getEntityAddressBo().isDefaultValue()) {
570                 if (oleEntityAddressBo.getOleAddressBo().getAddressValidFrom() != null || oleEntityAddressBo.getOleAddressBo().getAddressValidTo() != null) {
571                     GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_DEFAULT_DATE);
572                     flag= false;
573                 }
574             }
575             if(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()!=null && oleEntityAddressBo.getOleAddressBo().getAddressValidTo()!=null && oleEntityAddressBo.getOleAddressBo().getAddressValidFrom().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidTo())>0){
576                 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_VALID_ADDRESS_TO_DATE);
577                 flag= false;
578             }
579 
580             if(!oleEntityAddressBo.getEntityAddressBo().isDefaultValue()){
581                 if(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()==null && oleEntityAddressBo.getOleAddressBo().getAddressValidTo()!=null){
582                     GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS_FROM_DATE, "ValidFrom");
583                     flag= false;
584                 }
585                 if(oleEntityAddressBo.getOleAddressBo().getAddressValidTo()==null && oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()!=null){
586                     GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS_FROM_DATE, "ValidTo");
587                     flag= false;
588                 }
589                 for(Map.Entry<Date,Date> entry:map.entrySet()){
590                     if(entry.getKey()!=null&&entry.getValue()!=null&&oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()!=null&&oleEntityAddressBo.getOleAddressBo().getAddressValidTo()!=null){
591                         if(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom().compareTo(entry.getKey())>=0 && oleEntityAddressBo.getOleAddressBo().getAddressValidFrom().compareTo(entry.getValue())<=0
592                                 || oleEntityAddressBo.getOleAddressBo().getAddressValidTo().compareTo(entry.getKey())>=0 && oleEntityAddressBo.getOleAddressBo().getAddressValidTo().compareTo(entry.getValue())<=0){
593                             GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue",OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_FROM_TO_DATE_OVERLAP);
594                             flag= false;
595                         }
596                         if(entry.getKey().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom())>=0 && entry.getKey().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidTo())<=0
597                                 || entry.getValue().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom())>=0 && entry.getValue().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidTo())<=0){
598                             GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_FROM_TO_DATE_OVERLAP);
599                             flag= false;
600                         }
601                     }
602                 }
603             }else if (oleEntityAddressBo.getOleAddressBo().getAddressValidFrom() != null || oleEntityAddressBo.getOleAddressBo().getAddressValidTo() != null) {
604 
605                 if (dateExist) {
606                     dateExist = false;
607                 } else {
608                     GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_SINGLE_DATE);
609                     flag= false;
610                 }
611             }
612             map.put(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom(),oleEntityAddressBo.getOleAddressBo().getAddressValidTo());
613         }
614         if(!flag){
615             return false;
616         }
617         return valid;
618     }
619 
620     private UiDocumentService uiDocumentService;
621     /**
622      * Gets the value of uiDocumentService property
623      *
624      * @return uiDocumentService(UiDocumentService)
625      */
626     public UiDocumentService getUIDocumentService() {
627         if (uiDocumentService == null) {
628             uiDocumentService = KIMServiceLocatorInternal.getUiDocumentService();
629         }
630         return uiDocumentService;
631     }
632 
633     protected boolean checkDuplicateBarcode(OlePatronDocument patronDoc) {
634         boolean valid = true;
635         Map barcodeMap = new HashMap();
636         barcodeMap.put(OLEConstants.OlePatron.BARCODE, patronDoc.getBarcode());
637         List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class, barcodeMap);
638         if (olePatronDocuments.size() > 0) {
639             for (OlePatronDocument olePatronDocument : olePatronDocuments) {
640                 if (patronDoc.getOlePatronId() == null || !(patronDoc.getOlePatronId().equals(olePatronDocument.getOlePatronId()))) {
641                     GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.PATRON_DUPLICATE_BARCODE);
642                     valid = false;
643                 }
644             }
645         }
646         return valid;
647     }
648 
649     /**
650      * Gets the instance of BusinessObjectService
651      *
652      * @return businessObjectService(BusinessObjectService)
653      */
654     public BusinessObjectService getBusinessObjectService() {
655         if (businessObjectService == null) {
656             businessObjectService = KRADServiceLocator.getBusinessObjectService();
657         }
658         return businessObjectService;
659     }
660 
661     public String getPatronPreferredAddress(EntityTypeContactInfoBo entityTypeContactInfoBo) throws Exception {
662         LOG.debug("Inside the getPatronPreferredAddress method");
663         String address = "";
664         if (entityTypeContactInfoBo.getAddresses() != null) {
665             for (int i = 0; i < entityTypeContactInfoBo.getAddresses().size(); i++) {
666                 if (entityTypeContactInfoBo.getAddresses().get(i).isDefaultValue()) {
667                     if (entityTypeContactInfoBo.getAddresses().get(i).getLine1() != null)
668                         if (!entityTypeContactInfoBo.getAddresses().get(i).getLine1().isEmpty())
669                             address += entityTypeContactInfoBo.getAddresses().get(i).getLine1() + ",";
670 
671                     if (entityTypeContactInfoBo.getAddresses().get(i).getLine2() != null)
672                         if (!entityTypeContactInfoBo.getAddresses().get(i).getLine2().isEmpty())
673                             address += entityTypeContactInfoBo.getAddresses().get(i).getLine2() + ",";
674 
675                     if (entityTypeContactInfoBo.getAddresses().get(i).getLine3() != null)
676                         if (!entityTypeContactInfoBo.getAddresses().get(i).getLine3().isEmpty())
677                             address += entityTypeContactInfoBo.getAddresses().get(i).getLine3() + ",";
678 
679                     if (entityTypeContactInfoBo.getAddresses().get(i).getCity() != null)
680                         if (!entityTypeContactInfoBo.getAddresses().get(i).getCity().isEmpty())
681                             address += entityTypeContactInfoBo.getAddresses().get(i).getCity() + ",";
682 
683                     if (entityTypeContactInfoBo.getAddresses().get(i).getStateProvinceCode() != null)
684                         if (!entityTypeContactInfoBo.getAddresses().get(i).getStateProvinceCode().isEmpty())
685                             address += entityTypeContactInfoBo.getAddresses().get(i).getStateProvinceCode() + ",";
686 
687                     if (entityTypeContactInfoBo.getAddresses().get(i).getCountryCode() != null)
688                         if (!entityTypeContactInfoBo.getAddresses().get(i).getCountryCode().isEmpty())
689                             address += entityTypeContactInfoBo.getAddresses().get(i).getCountryCode() + ",";
690 
691                     if (entityTypeContactInfoBo.getAddresses().get(i).getPostalCode() != null)
692                         if (!entityTypeContactInfoBo.getAddresses().get(i).getPostalCode().isEmpty())
693                             address += entityTypeContactInfoBo.getAddresses().get(i).getPostalCode();
694                 }
695             }
696         }
697 
698         return address;
699     }
700 
701     public String getPatronHomePhoneNumber(EntityTypeContactInfoBo entityTypeContactInfoBo) throws Exception {
702         LOG.debug("Inside the getPatronHomePhoneNumber method");
703         String phoneNumber = "";
704         if (entityTypeContactInfoBo.getPhoneNumbers() != null) {
705             for (int j = 0; j < entityTypeContactInfoBo.getPhoneNumbers().size(); j++) {
706                 if (entityTypeContactInfoBo.getPhoneNumbers().get(j).getPhoneTypeCode().equalsIgnoreCase("HM")) {
707                     phoneNumber = (entityTypeContactInfoBo.getPhoneNumbers().get(j).getPhoneNumber());
708                 }
709             }
710         }
711         return phoneNumber;
712     }
713 
714     public String getPatronHomeEmailId(EntityTypeContactInfoBo entityTypeContactInfoBo) throws Exception {
715         LOG.debug("Inside the getPatronHomeEmailId method");
716         String emailId = "";
717         if (entityTypeContactInfoBo.getEmailAddresses() != null) {
718             for (int j = 0; j < entityTypeContactInfoBo.getEmailAddresses().size(); j++) {
719                 if (entityTypeContactInfoBo.getEmailAddresses().get(j).getDefaultValue()) {
720                     emailId = (entityTypeContactInfoBo.getEmailAddresses().get(j).getEmailAddress());
721                     break;
722                 }
723             }
724         }
725         return emailId;
726     }
727 
728 
729 }