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