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
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 List<EntityPhoneBo> phoneList = new ArrayList<>();
118 if(CollectionUtils.isNotEmpty(patronDocument.getOleEntityPhoneBo())) {
119 for(OleEntityPhoneBo oleEntityPhoneBo : patronDocument.getOleEntityPhoneBo()) {
120 if(oleEntityPhoneBo.getEntityPhoneBo() != null && oleEntityPhoneBo.getEntityPhoneBo().getPhoneType() != null) {
121 oleEntityPhoneBo.getEntityPhoneBo().setPhoneTypeCode(oleEntityPhoneBo.getEntityPhoneBo().getPhoneType().getCode());
122 }
123 phoneList.add(oleEntityPhoneBo.getEntityPhoneBo());
124 }
125 }
126 List<EntityEmailBo> emailList = new ArrayList<>();
127 if(CollectionUtils.isNotEmpty(patronDocument.getOleEntityEmailBo())) {
128 for(OleEntityEmailBo oleEntityEmailBo : patronDocument.getOleEntityEmailBo()) {
129 if(oleEntityEmailBo.getEntityEmailBo() != null && oleEntityEmailBo.getEntityEmailBo().getEmailType() != null) {
130 oleEntityEmailBo.getEntityEmailBo().setEmailTypeCode(oleEntityEmailBo.getEntityEmailBo().getEmailType().getCode());
131 }
132 emailList.add(oleEntityEmailBo.getEntityEmailBo());
133 }
134 }
135 patronDocument.setEmployments(employeeList);
136 entityTypes.clear();
137 entityType.setAddresses(addressList);
138 entityType.setEmailAddresses(emailList);
139 entityType.setPhoneNumbers(phoneList);
140 entityTypes.add(entityType);
141 kimEntity.setEntityTypeContactInfos(entityTypes);
142 try {
143 patronDocument.getIdentityService().updateEntity(EntityBo.to(kimEntity));
144 } catch (Exception e) {
145 LOG.error("Unable to save edited patron Record" + e);
146 }
147
148 return kimEntity;
149 }
150
151 public EntityBo copyAndSaveEntityBo(OlePatronDocument patronDocument) {
152 LOG.debug("Inside the copyAndSaveEntityBo method");
153
154 EntityBo kimEntity;
155 List<EntityTypeContactInfoBo> entityTypes;
156 EntityTypeContactInfoBo entityType;
157 kimEntity = patronDocument.getEntity();
158 kimEntity.setId(null);
159 kimEntity.setObjectId(null);
160 kimEntity.setVersionNumber(null);
161 boolean isUpdatePatron=false;
162 if (kimEntity.getId() == null) {
163 String kimEntityId=KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("krim_entity_id_s").toString();
164 kimEntity.setId(kimEntityId);
165 entityTypes = new ArrayList<EntityTypeContactInfoBo>();
166 entityType = new EntityTypeContactInfoBo();
167 kimEntity.setActive(true);
168 entityType.setEntityTypeCode(KimConstants.EntityTypes.PERSON);
169 entityType.setActive(true);
170 entityType.setEntityId(kimEntity.getId());
171 } else {
172 isUpdatePatron=true;
173 kimEntity.setActive(true);
174 entityTypes = kimEntity.getEntityTypeContactInfos();
175 if (!entityTypes.isEmpty()) {
176 entityType = entityTypes.get(0);
177 entityType.setActive(true);
178 } else {
179 entityType = new EntityTypeContactInfoBo();
180 entityType.setEntityTypeCode(KimConstants.EntityTypes.PERSON);
181 }
182 entityType.setEntityId(kimEntity.getId());
183 }
184
185 List<EntityAffiliationBo> affiliationBoList = new ArrayList<EntityAffiliationBo>();
186 List<EntityEmploymentBo> employeeList = new ArrayList<EntityEmploymentBo>();
187 String affiliationSeq = null;
188 if (patronDocument.getPatronAffiliations().size() > 0) {
189 for (OlePatronAffiliation patronAffiliation : patronDocument.getPatronAffiliations()) {
190 EntityAffiliationBo entityAffiliationBo = patronAffiliation.getEntityAffliationBo();
191 entityAffiliationBo.setEntityId(kimEntity.getId());
192 entityAffiliationBo.setObjectId(null);
193 entityAffiliationBo.setVersionNumber(null);
194 if (entityAffiliationBo.getId() == null) {
195 affiliationSeq = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("krim_entity_afltn_id_s").toString();
196 entityAffiliationBo.setId(affiliationSeq);
197 }
198 for (EntityEmploymentBo employmentBo : patronAffiliation.getEmployments()) {
199 employmentBo.setEntityAffiliation(entityAffiliationBo);
200 employmentBo.setEntityAffiliationId(entityAffiliationBo.getId());
201 employeeList.add(employmentBo);
202 }
203 affiliationBoList.add(entityAffiliationBo);
204 }
205 }
206 kimEntity.setAffiliations(affiliationBoList);
207 kimEntity.setEmploymentInformation(employeeList);
208 if (patronDocument.getName() != null) {
209 patronDocument.getName().setId(null);
210 patronDocument.getName().setVersionNumber(null);
211 patronDocument.getName().setObjectId(null);
212 patronDocument.getName().setEntityId(null);
213 }
214 kimEntity.setNames(Arrays.asList(patronDocument.getName()));
215
216 List<EntityAddressBo> addressList = new ArrayList<EntityAddressBo>();
217 if (patronDocument.getOleEntityAddressBo() != null) {
218 for (OleEntityAddressBo oleEntityAddress : patronDocument.getOleEntityAddressBo()) {
219 oleEntityAddress.getEntityAddressBo().setId(null);
220 oleEntityAddress.getEntityAddressBo().setVersionNumber(null);
221 oleEntityAddress.getEntityAddressBo().setObjectId(null);
222 oleEntityAddress.getEntityAddressBo().setEntityId(kimEntity.getId());
223 if(oleEntityAddress.getEntityAddressBo()!=null && oleEntityAddress.getEntityAddressBo().getAddressType()!=null){
224 oleEntityAddress.getEntityAddressBo().setAddressTypeCode(oleEntityAddress.getEntityAddressBo().getAddressType().getCode());
225 }
226 addressList.add(oleEntityAddress.getEntityAddressBo());
227 }
228 }
229 List<EntityPhoneBo> entityPhoneBoList = new ArrayList<>();
230 if(CollectionUtils.isNotEmpty(patronDocument.getOleEntityPhoneBo())) {
231 for(OleEntityPhoneBo oleEntityPhoneBo : patronDocument.getOleEntityPhoneBo()) {
232 oleEntityPhoneBo.getEntityPhoneBo().setId(null);
233 oleEntityPhoneBo.getEntityPhoneBo().setVersionNumber(null);
234 oleEntityPhoneBo.getEntityPhoneBo().setObjectId(null);
235 oleEntityPhoneBo.getEntityPhoneBo().setEntityId(kimEntity.getId());
236 if(oleEntityPhoneBo.getEntityPhoneBo() != null && oleEntityPhoneBo.getEntityPhoneBo().getPhoneType() != null) {
237 oleEntityPhoneBo.getEntityPhoneBo().setPhoneTypeCode(oleEntityPhoneBo.getEntityPhoneBo().getPhoneType().getCode());
238 }
239 entityPhoneBoList.add(oleEntityPhoneBo.getEntityPhoneBo());
240 }
241 }
242 List<EntityEmailBo> entityEmailBoList = new ArrayList<>();
243 if (CollectionUtils.isNotEmpty(patronDocument.getOleEntityEmailBo())) {
244 for (OleEntityEmailBo oleEntityEmailBo : patronDocument.getOleEntityEmailBo()) {
245 oleEntityEmailBo.getEntityEmailBo().setId(null);
246 oleEntityEmailBo.getEntityEmailBo().setVersionNumber(null);
247 oleEntityEmailBo.getEntityEmailBo().setObjectId(null);
248 oleEntityEmailBo.getEntityEmailBo().setEntityId(kimEntity.getId());
249 if(oleEntityEmailBo.getEntityEmailBo() != null && oleEntityEmailBo.getEntityEmailBo().getEmailType() != null) {
250 oleEntityEmailBo.getEntityEmailBo().setEmailTypeCode(oleEntityEmailBo.getEntityEmailBo().getEmailType().getCode());
251 }
252 entityEmailBoList.add(oleEntityEmailBo.getEntityEmailBo());
253 }
254 }
255 entityType.setAddresses(addressList);
256 entityType.setEmailAddresses(entityEmailBoList);
257 entityType.setPhoneNumbers(entityPhoneBoList);
258 entityTypes.add(entityType);
259 kimEntity.setEntityTypeContactInfos(entityTypes);
260 try {
261 if (isUpdatePatron) {
262 Entity entity = patronDocument.getIdentityService().updateEntity(EntityBo.to(kimEntity));
263 } else {
264 Entity entity = patronDocument.getIdentityService().createEntity(EntityBo.to(kimEntity));
265 }
266
267 } catch (Exception e) {
268 LOG.error("Copied to save edited patron Record" + e);
269 }
270 return kimEntity;
271 }
272
273 public List<OleAddressBo> retrieveOleAddressBo(EntityBo entityBo, OlePatronDocument patronDocument) {
274 List<EntityAddressBo> addressBos = new ArrayList<>();
275 List<OleEntityAddressBo> oleEntityAddressBos = new ArrayList<>();
276 if(CollectionUtils.isNotEmpty(entityBo.getEntityTypeContactInfos())){
277 addressBos = entityBo.getEntityTypeContactInfos().get(0).getAddresses();
278 }
279 boolean emptyOleEntityAddress = false;
280 List<OleAddressBo> oleAddressBos = new ArrayList<OleAddressBo>();
281 for (int i = 0; i < addressBos.size(); i++) {
282 EntityAddressBo entityAddressBo = addressBos.get(i);
283 Map<String, Object> criteria = new HashMap<String, Object>();
284 criteria.put(OLEConstants.OlePatron.ENTITY_BO_ID, entityAddressBo.getId());
285 OleAddressBo oleAddressBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleAddressBo.class, criteria);
286 if (oleAddressBo == null) {
287 oleAddressBo = new OleAddressBo();
288 }
289 oleAddressBo.setOlePatronId(entityBo.getId());
290 oleAddressBo.setOleAddressId(KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("OLE_DLVR_ADD_S").toString());
291 oleAddressBo.setEntityAddress(entityAddressBo);
292 if (CollectionUtils.isNotEmpty(patronDocument.getOleEntityAddressBo()) && !emptyOleEntityAddress) {
293 oleAddressBo.setAddressValidFrom(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().getAddressValidFrom());
294 oleAddressBo.setAddressValidTo(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().getAddressValidTo());
295 oleAddressBo.setAddressVerified(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().isAddressVerified());
296 oleAddressBo.setAddressSource(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().getAddressSource());
297 oleAddressBo.setId(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().getId());
298 oleAddressBo.setDeliverAddress(patronDocument.getOleEntityAddressBo().get(i).getOleAddressBo().isDeliverAddress());
299 }else{
300 emptyOleEntityAddress = true;
301 OleEntityAddressBo oleEntityAddressBo = new OleEntityAddressBo();
302 if(StringUtils.isBlank(oleAddressBo.getId())){
303 oleAddressBo.setId(entityAddressBo.getId());
304 if(CollectionUtils.isNotEmpty(patronDocument.getOleAddresses()) && patronDocument.getOleAddresses().size() > i && patronDocument.getOleAddresses().get(i) != null){
305 oleAddressBo.setAddressValidFrom(patronDocument.getOleAddresses().get(i).getAddressValidFrom());
306 oleAddressBo.setAddressValidTo(patronDocument.getOleAddresses().get(i).getAddressValidTo());
307 oleAddressBo.setAddressVerified(patronDocument.getOleAddresses().get(i).isAddressVerified());
308 oleAddressBo.setAddressSource(patronDocument.getOleAddresses().get(i).getAddressSource());
309 oleAddressBo.setDeliverAddress(patronDocument.getOleAddresses().get(i).isDeliverAddress());
310 }
311 }
312 oleEntityAddressBo.setOleAddressBo(oleAddressBo);
313 oleEntityAddressBo.setEntityAddressBo(entityAddressBo);
314 oleEntityAddressBos.add(oleEntityAddressBo);
315 }
316 oleAddressBos.add(oleAddressBo);
317
318 }
319
320 if(emptyOleEntityAddress){
321 patronDocument.setOleEntityAddressBo(oleEntityAddressBos);
322 }
323 return oleAddressBos;
324 }
325
326 public List<OlePhoneBo> retrieveOlePhoneBo(EntityBo entityBo, OlePatronDocument patronDocument) {
327 List<EntityPhoneBo> phoneBos = new ArrayList<>();
328 List<OleEntityPhoneBo> oleEntityPhoneBos = new ArrayList<>();
329 if(CollectionUtils.isNotEmpty(entityBo.getEntityTypeContactInfos())){
330 phoneBos = entityBo.getEntityTypeContactInfos().get(0).getPhoneNumbers();
331 }
332 boolean emptyOleEntityPhone = false;
333 List<OlePhoneBo> olePhoneBos = new ArrayList<>();
334 for (int i = 0; i < phoneBos.size(); i++) {
335 EntityPhoneBo entityPhoneBo = phoneBos.get(i);
336 Map<String, Object> criteria = new HashMap<String, Object>();
337 criteria.put(OLEConstants.OlePatron.ENTITY_BO_ID, entityPhoneBo.getId());
338 OlePhoneBo olePhoneBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePhoneBo.class, criteria);
339 if (olePhoneBo == null) {
340 olePhoneBo = new OlePhoneBo();
341 }
342 olePhoneBo.setOlePatronId(entityBo.getId());
343 olePhoneBo.setOlePhoneId(KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("OLE_DLVR_PHONE_S").toString());
344 olePhoneBo.setEntityPhoneBo(entityPhoneBo);
345 if (CollectionUtils.isNotEmpty(patronDocument.getOleEntityPhoneBo()) && !emptyOleEntityPhone) {
346 olePhoneBo.setPhoneSource(patronDocument.getOleEntityPhoneBo().get(i).getOlePhoneBo().getPhoneSource());
347 olePhoneBo.setId(patronDocument.getOleEntityPhoneBo().get(i).getOlePhoneBo().getId());
348 }else{
349 emptyOleEntityPhone = true;
350 OleEntityPhoneBo oleEntityPhoneBo = new OleEntityPhoneBo();
351 if(StringUtils.isBlank(olePhoneBo.getId())){
352 olePhoneBo.setId(entityPhoneBo.getId());
353 if(CollectionUtils.isNotEmpty(patronDocument.getOlePhones()) && patronDocument.getOlePhones().size() > i && patronDocument.getOlePhones().get(i) != null){
354 olePhoneBo.setPhoneSource(patronDocument.getOlePhones().get(i).getPhoneSource());
355 }
356 }
357 oleEntityPhoneBo.setOlePhoneBo(olePhoneBo);
358 oleEntityPhoneBo.setEntityPhoneBo(entityPhoneBo);
359 oleEntityPhoneBos.add(oleEntityPhoneBo);
360 }
361 olePhoneBos.add(olePhoneBo);
362
363 }
364
365 if(emptyOleEntityPhone){
366 patronDocument.setOleEntityPhoneBo(oleEntityPhoneBos);
367 }
368 return olePhoneBos;
369 }
370
371 public List<OleEmailBo> retrieveOleEmailBo(EntityBo entityBo, OlePatronDocument patronDocument) {
372 List<EntityEmailBo> emailBos = new ArrayList<>();
373 List<OleEntityEmailBo> oleEntityEmailBos = new ArrayList<>();
374 if(CollectionUtils.isNotEmpty(entityBo.getEntityTypeContactInfos())){
375 emailBos = entityBo.getEntityTypeContactInfos().get(0).getEmailAddresses();
376 }
377 boolean emptyOleEntityEmail = false;
378 List<OleEmailBo> oleEmailBos = new ArrayList<>();
379 for (int i = 0; i < emailBos.size(); i++) {
380 EntityEmailBo entityEmailBo = emailBos.get(i);
381 Map<String, Object> criteria = new HashMap<String, Object>();
382 criteria.put(OLEConstants.OlePatron.ENTITY_BO_ID, entityEmailBo.getId());
383 OleEmailBo oleEmailBo = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleEmailBo.class, criteria);
384 if (oleEmailBo == null) {
385 oleEmailBo = new OleEmailBo();
386 }
387 oleEmailBo.setOlePatronId(entityBo.getId());
388 oleEmailBo.setOleEmailId(KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber("OLE_DLVR_EMAIL_S").toString());
389 oleEmailBo.setEntityEmailBo(entityEmailBo);
390 if (CollectionUtils.isNotEmpty(patronDocument.getOleEntityEmailBo()) && !emptyOleEntityEmail) {
391 oleEmailBo.setEmailSource(patronDocument.getOleEntityEmailBo().get(i).getOleEmailBo().getEmailSource());
392 oleEmailBo.setId(patronDocument.getOleEntityEmailBo().get(i).getOleEmailBo().getId());
393 }else{
394 emptyOleEntityEmail = true;
395 OleEntityEmailBo oleEntityEmailBo = new OleEntityEmailBo();
396 if(StringUtils.isBlank(oleEmailBo.getId())){
397 oleEmailBo.setId(entityEmailBo.getId());
398 if(CollectionUtils.isNotEmpty(patronDocument.getOleEmails()) && patronDocument.getOleEmails().size() > i && patronDocument.getOleEmails().get(i) != null){
399 oleEmailBo.setEmailSource(patronDocument.getOleEmails().get(i).getEmailSource());
400 }
401 }
402 oleEntityEmailBo.setOleEmailBo(oleEmailBo);
403 oleEntityEmailBo.setEntityEmailBo(entityEmailBo);
404 oleEntityEmailBos.add(oleEntityEmailBo);
405 }
406 oleEmailBos.add(oleEmailBo);
407
408 }
409
410 if(emptyOleEntityEmail){
411 patronDocument.setOleEntityEmailBo(oleEntityEmailBos);
412 }
413 return oleEmailBos;
414 }
415
416
417
418
419
420
421
422
423 protected boolean isNameEntityCriteria(String propertyName) {
424 LOG.debug("Inside the isNameEntityCriteria method");
425 return propertyName.startsWith(ENTITY_NAME_PROPERTY_PREFIX);
426 }
427
428
429
430
431
432
433
434 protected boolean isEmailEntityCriteria(String propertyName) {
435 LOG.debug("Inside the isEmailEntityCriteria method");
436 return propertyName.startsWith(ENTITY_EMAIL_PROPERTY_PREFIX);
437 }
438
439
440 public boolean deletePatron(OlePatronDocument olePatronDocument) {
441 LOG.debug("Inside the deletePatron method");
442 boolean deleteFlag = false;
443 SimpleDateFormat fmt = new SimpleDateFormat(OLEConstants.OlePatron.PATRON_MAINTENANCE_DATE_FORMAT);
444 Map billMap = new HashMap();
445 billMap.put(OLEConstants.OlePatron.PAY_BILL_PATRON_ID, olePatronDocument.getOlePatronId());
446 List<PatronBillPayment> patronBillPayments = (List<PatronBillPayment>) KRADServiceLocator.getBusinessObjectService().findMatching(PatronBillPayment.class, billMap);
447
448 if (olePatronDocument != null && olePatronDocument.getOlePatronId() != null) {
449 if (olePatronDocument.getOleLoanDocuments() == null || olePatronDocument.getOleLoanDocuments().size() == 0) {
450 if ((olePatronDocument.getOleTemporaryCirculationHistoryRecords() == null || olePatronDocument
451 .getOleTemporaryCirculationHistoryRecords().size() == 0)) {
452 if ((olePatronDocument.getOleDeliverRequestBos() == null || olePatronDocument.getOleDeliverRequestBos().size() == 0)) {
453 if ((patronBillPayments == null || patronBillPayments.size() == 0)) {
454 olePatronDocument.setActiveIndicator(false);
455 List<OleAddressBo> addressBos = olePatronDocument.getOleAddresses();
456 if (addressBos.size() > 0) {
457 for (OleAddressBo addressBo : addressBos) {
458 if (addressBo.getAddressSource().isEmpty() || "".equals(addressBo.getAddressSource())) {
459 addressBo.setAddressSource(null);
460 }
461 }
462 }
463 List<OlePhoneBo> phoneBos = olePatronDocument.getOlePhones();
464 if(CollectionUtils.isNotEmpty(phoneBos)) {
465 for(OlePhoneBo phoneBo : phoneBos) {
466 if(StringUtils.isBlank(phoneBo.getPhoneSource())) {
467 phoneBo.setPhoneSource(null);
468 }
469 }
470 }
471 List<OleEmailBo> emailBos = olePatronDocument.getOleEmails();
472 if(CollectionUtils.isNotEmpty(emailBos)) {
473 for(OleEmailBo emailBo : emailBos) {
474 if(StringUtils.isBlank(emailBo.getEmailSource())) {
475 emailBo.setEmailSource(null);
476 }
477 }
478 }
479 KRADServiceLocator.getBusinessObjectService().save(olePatronDocument);
480 deleteFlag = true;
481 }
482 }
483 }
484 }
485 }
486 return deleteFlag;
487 }
488
489
490
491
492
493
494
495 public boolean checkAddressSource(List<OleAddressBo> oleAddresses) {
496
497 boolean valid = true;
498 for (OleAddressBo oleAddress : oleAddresses) {
499 if ((oleAddress.getAddressSource() != null) && (oleAddress.getAddressSource().equals(""))) {
500 valid = false;
501 }
502 }
503 return valid;
504 }
505
506 public boolean checkPhoneSource(List<OlePhoneBo> olePhones) {
507
508 boolean valid = true;
509 for (OlePhoneBo olePhone : olePhones) {
510 if (StringUtils.isBlank(olePhone.getPhoneSource())) {
511 valid = false;
512 }
513 }
514 return valid;
515 }
516
517 public boolean checkEmailSource(List<OleEmailBo> oleEmails) {
518
519 boolean valid = true;
520 for (OleEmailBo oleEmail : oleEmails) {
521 if (StringUtils.isBlank(oleEmail.getEmailSource())) {
522 valid = false;
523 }
524 }
525 return valid;
526 }
527
528
529
530
531
532
533
534 public boolean checkPhoneMultipleDefault(List<OleEntityPhoneBo> phoneBoList) {
535 boolean valid = true;
536 boolean isDefaultSet = false;
537 int i = 0;
538 for (OleEntityPhoneBo phone : phoneBoList) {
539 EntityPhoneBo entityPhoneBo = phone.getEntityPhoneBo();
540 if (null != entityPhoneBo && entityPhoneBo.isDefaultValue()) {
541 if (isDefaultSet) {
542 valid = false;
543 } else {
544 isDefaultSet = true;
545 }
546 }
547 i++;
548 }
549 if (!phoneBoList.isEmpty() && !isDefaultSet) {
550 valid = false;
551 }
552 return valid;
553 }
554
555
556
557
558
559
560
561 public boolean checkAddressMultipleDefault(List<OleEntityAddressBo> addrBoList) {
562 boolean valid = true;
563 boolean isDefaultSet = false;
564 int i = 0;
565 for (OleEntityAddressBo addr : addrBoList) {
566 EntityAddressBo entityAddressBo = addr.getEntityAddressBo();
567 if (entityAddressBo.isDefaultValue()) {
568 if (isDefaultSet) {
569 valid = false;
570 } else {
571 isDefaultSet = true;
572 }
573 }
574 i++;
575 }
576 if (!addrBoList.isEmpty() && !isDefaultSet) {
577 valid = false;
578 }
579 return valid;
580 }
581
582
583
584
585
586
587
588 public boolean checkEmailMultipleDefault(List<OleEntityEmailBo> emailBoList) {
589 boolean valid = true;
590 boolean isDefaultSet = false;
591 int i = 0;
592 for (OleEntityEmailBo email : emailBoList) {
593 EntityEmailBo entityEmailBo = email.getEntityEmailBo();
594 if (null != entityEmailBo && entityEmailBo.isDefaultValue()) {
595 if (isDefaultSet) {
596 valid = false;
597 } else {
598 isDefaultSet = true;
599 }
600 }
601 i++;
602 }
603 if (!emailBoList.isEmpty() && !isDefaultSet) {
604 valid = false;
605 }
606 return valid;
607 }
608
609 public boolean isBorrowerTypeActive(OlePatronDocument olePatronDocument) {
610 boolean isPatronTypeActive = false;
611 HashMap<String, String> map = new HashMap<String, String>();
612 map.put(OLEConstants.BORROWER_TYPE_ID, olePatronDocument.getBorrowerType());
613 List<OleBorrowerType> borrowerTypes = (List<OleBorrowerType>) getBusinessObjectService().findMatching(OleBorrowerType.class, map);
614 if (borrowerTypes.size() > 0) {
615 if (borrowerTypes.get(0).isActive()) {
616 isPatronTypeActive = true;
617 }
618 }
619 return isPatronTypeActive;
620 }
621
622 public boolean validatePatron(OlePatronDocument patronDocument) {
623 boolean isValid = true;
624 isValid &= validateEntityInformation(patronDocument);
625 isValid &= validateAddress(patronDocument, "oleEntityAddressBo");
626 isValid &= validateRequiredField(patronDocument);
627 isValid &=checkDuplicateBarcode(patronDocument);
628 return isValid;
629 }
630
631 private boolean validateEntityInformation( OlePatronDocument patronDoc) {
632 boolean valid = true;
633 List<OleEntityAddressBo> addressBoList = patronDoc.getOleEntityAddressBo();
634 List<OleEntityEmailBo> emailBoList = patronDoc.getOleEntityEmailBo();
635 List<OleEntityPhoneBo> phoneBoList = patronDoc.getOleEntityPhoneBo();
636 if (addressBoList.size() == 1) {
637 OleEntityAddressBo oleEntityAddressBo = addressBoList.get(0);
638 oleEntityAddressBo.getEntityAddressBo().setDefaultValue(true);
639 }
640 if (CollectionUtils.isNotEmpty(emailBoList) && emailBoList.size() == 1) {
641 OleEntityEmailBo oleEntityEmailBo = emailBoList.get(0);
642 oleEntityEmailBo.getEntityEmailBo().setDefaultValue(true);
643 }
644 if (CollectionUtils.isNotEmpty(phoneBoList) && phoneBoList.size() == 1) {
645 OleEntityPhoneBo oleEntityPhoneBo = phoneBoList.get(0);
646 oleEntityPhoneBo.getEntityPhoneBo().setDefaultValue(true);
647 }
648 if (!checkPhoneMultipleDefault(patronDoc.getOleEntityPhoneBo())) {
649 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PHONE_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_PHONE);
650 valid &= false;
651 }
652 if (!checkAddressMultipleDefault(patronDoc.getOleEntityAddressBo())) {
653 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.ADDRESS_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_ADDRESS);
654 valid &= false;
655 }
656 if (!checkEmailMultipleDefault(patronDoc.getOleEntityEmailBo())) {
657 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.EMAIL_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_EMAIL);
658 valid &= false;
659 }
660 if (!checkAddressMultipleDeliverAddress(patronDoc.getOleEntityAddressBo(), "oleEntityAddressBo")) {
661
662 valid &= false;
663 }
664 return valid;
665 }
666
667 protected boolean checkAddressMultipleDeliverAddress(List<OleEntityAddressBo> addrBoList, String listName) {
668 boolean valid = true;
669 boolean isDefaultSet = false;
670 boolean isAtleastOneChecked=false;
671 int i = 0;
672 for (OleEntityAddressBo addr : addrBoList) {
673 OleAddressBo oleAddressBo = addr.getOleAddressBo();
674 if (oleAddressBo.isDeliverAddress()) {
675 isAtleastOneChecked=true;
676 if (isDefaultSet) {
677
678 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_MULIT_DELIVER_ADDRESS);
679 valid = false;
680 } else {
681 isDefaultSet = true;
682 }
683 }
684 i++;
685 }
686 if(!isAtleastOneChecked){
687 valid=true;
688 } else {
689 if (!addrBoList.isEmpty() && !isDefaultSet) {
690
691 valid = false;
692 }
693 }
694 return valid;
695 }
696
697 public boolean validateRequiredField(OlePatronDocument olePatronDocument) {
698 boolean valid = true;
699 List<OleEntityAddressBo> addressBoList = olePatronDocument.getOleEntityAddressBo();
700 List<EntityEmailBo> emailBoList = olePatronDocument.getEmails();
701 if (!(addressBoList.size() > 0 || emailBoList.size() > 0)) {
702 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS);
703 valid = false;
704 }
705 return valid;
706 }
707
708 public boolean validateAddress(OlePatronDocument olePatronDocument, String addressBos) {
709 List<OleEntityAddressBo> addressBoList = olePatronDocument.getOleEntityAddressBo();
710 OleEntityAddressBo oleEntityAddressBo;
711 boolean valid = true;
712 boolean flag=true;
713 boolean dateExist = true;
714 Map<Date,Date> map=new HashMap<>();
715 for (int i = 0; i < addressBoList.size(); i++) {
716 oleEntityAddressBo = addressBoList.get(i);
717 if (oleEntityAddressBo.getEntityAddressBo().isDefaultValue()) {
718 if (oleEntityAddressBo.getOleAddressBo().getAddressValidFrom() != null || oleEntityAddressBo.getOleAddressBo().getAddressValidTo() != null) {
719 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_DEFAULT_DATE);
720 flag= false;
721 }
722 }
723 if(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()!=null && oleEntityAddressBo.getOleAddressBo().getAddressValidTo()!=null && oleEntityAddressBo.getOleAddressBo().getAddressValidFrom().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidTo())>0){
724 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_VALID_ADDRESS_TO_DATE);
725 flag= false;
726 }
727
728 if(!oleEntityAddressBo.getEntityAddressBo().isDefaultValue()){
729 if(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()==null && oleEntityAddressBo.getOleAddressBo().getAddressValidTo()!=null){
730 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS_FROM_DATE, "ValidFrom");
731 flag= false;
732 }
733 if(oleEntityAddressBo.getOleAddressBo().getAddressValidTo()==null && oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()!=null){
734 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS_FROM_DATE, "ValidTo");
735 flag= false;
736 }
737 for(Map.Entry<Date,Date> entry:map.entrySet()){
738 if(entry.getKey()!=null&&entry.getValue()!=null&&oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()!=null&&oleEntityAddressBo.getOleAddressBo().getAddressValidTo()!=null){
739 if(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom().compareTo(entry.getKey())>=0 && oleEntityAddressBo.getOleAddressBo().getAddressValidFrom().compareTo(entry.getValue())<=0
740 || oleEntityAddressBo.getOleAddressBo().getAddressValidTo().compareTo(entry.getKey())>=0 && oleEntityAddressBo.getOleAddressBo().getAddressValidTo().compareTo(entry.getValue())<=0){
741 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue",OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_FROM_TO_DATE_OVERLAP);
742 flag= false;
743 }
744 if(entry.getKey().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom())>=0 && entry.getKey().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidTo())<=0
745 || entry.getValue().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom())>=0 && entry.getValue().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidTo())<=0){
746 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_FROM_TO_DATE_OVERLAP);
747 flag= false;
748 }
749 }
750 }
751 }else if (oleEntityAddressBo.getOleAddressBo().getAddressValidFrom() != null || oleEntityAddressBo.getOleAddressBo().getAddressValidTo() != null) {
752
753 if (dateExist) {
754 dateExist = false;
755 } else {
756 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_SINGLE_DATE);
757 flag= false;
758 }
759 }
760 map.put(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom(),oleEntityAddressBo.getOleAddressBo().getAddressValidTo());
761 }
762 if(!flag){
763 return false;
764 }
765 return valid;
766 }
767
768 private UiDocumentService uiDocumentService;
769
770
771
772
773
774 public UiDocumentService getUIDocumentService() {
775 if (uiDocumentService == null) {
776 uiDocumentService = KIMServiceLocatorInternal.getUiDocumentService();
777 }
778 return uiDocumentService;
779 }
780
781 protected boolean checkDuplicateBarcode(OlePatronDocument patronDoc) {
782 boolean valid = true;
783 Map barcodeMap = new HashMap();
784 barcodeMap.put(OLEConstants.OlePatron.BARCODE, patronDoc.getBarcode());
785 List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class, barcodeMap);
786 if (olePatronDocuments.size() > 0) {
787 for (OlePatronDocument olePatronDocument : olePatronDocuments) {
788 if (patronDoc.getOlePatronId() == null || !(patronDoc.getOlePatronId().equals(olePatronDocument.getOlePatronId()))) {
789 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.PATRON_DUPLICATE_BARCODE);
790 valid = false;
791 }
792 }
793 }
794 return valid;
795 }
796
797
798
799
800
801
802 public BusinessObjectService getBusinessObjectService() {
803 if (businessObjectService == null) {
804 businessObjectService = KRADServiceLocator.getBusinessObjectService();
805 }
806 return businessObjectService;
807 }
808
809 public String getPatronPreferredAddress(EntityTypeContactInfoBo entityTypeContactInfoBo) throws Exception {
810 LOG.debug("Inside the getPatronPreferredAddress method");
811 String address = "";
812 if (entityTypeContactInfoBo!=null && entityTypeContactInfoBo.getAddresses() != null) {
813 for (int i = 0; i < entityTypeContactInfoBo.getAddresses().size(); i++) {
814 if (entityTypeContactInfoBo.getAddresses().get(i).isDefaultValue()) {
815 if (entityTypeContactInfoBo.getAddresses().get(i).getLine1() != null)
816 if (!entityTypeContactInfoBo.getAddresses().get(i).getLine1().isEmpty())
817 address += entityTypeContactInfoBo.getAddresses().get(i).getLine1() + ",";
818
819 if (entityTypeContactInfoBo.getAddresses().get(i).getLine2() != null)
820 if (!entityTypeContactInfoBo.getAddresses().get(i).getLine2().isEmpty())
821 address += entityTypeContactInfoBo.getAddresses().get(i).getLine2() + ",";
822
823 if (entityTypeContactInfoBo.getAddresses().get(i).getLine3() != null)
824 if (!entityTypeContactInfoBo.getAddresses().get(i).getLine3().isEmpty())
825 address += entityTypeContactInfoBo.getAddresses().get(i).getLine3() + ",";
826
827 if (entityTypeContactInfoBo.getAddresses().get(i).getCity() != null)
828 if (!entityTypeContactInfoBo.getAddresses().get(i).getCity().isEmpty())
829 address += entityTypeContactInfoBo.getAddresses().get(i).getCity() + ",";
830
831 if (entityTypeContactInfoBo.getAddresses().get(i).getStateProvinceCode() != null)
832 if (!entityTypeContactInfoBo.getAddresses().get(i).getStateProvinceCode().isEmpty())
833 address += entityTypeContactInfoBo.getAddresses().get(i).getStateProvinceCode() + ",";
834
835 if (entityTypeContactInfoBo.getAddresses().get(i).getCountryCode() != null)
836 if (!entityTypeContactInfoBo.getAddresses().get(i).getCountryCode().isEmpty())
837 address += entityTypeContactInfoBo.getAddresses().get(i).getCountryCode() + ",";
838
839 if (entityTypeContactInfoBo.getAddresses().get(i).getPostalCode() != null)
840 if (!entityTypeContactInfoBo.getAddresses().get(i).getPostalCode().isEmpty())
841 address += entityTypeContactInfoBo.getAddresses().get(i).getPostalCode();
842 }
843 }
844 }
845
846 return address;
847 }
848
849 public String getPatronHomePhoneNumber(EntityTypeContactInfoBo entityTypeContactInfoBo) throws Exception {
850 LOG.debug("Inside the getPatronHomePhoneNumber method");
851 String phoneNumber = "";
852 if (entityTypeContactInfoBo!=null && entityTypeContactInfoBo.getPhoneNumbers() != null) {
853 for (int j = 0; j < entityTypeContactInfoBo.getPhoneNumbers().size(); j++) {
854 if (entityTypeContactInfoBo.getPhoneNumbers().get(j).getPhoneTypeCode().equalsIgnoreCase("HM")) {
855 phoneNumber = (entityTypeContactInfoBo.getPhoneNumbers().get(j).getPhoneNumber());
856 }
857 }
858 }
859 return phoneNumber;
860 }
861
862 public String getPatronHomeEmailId(EntityTypeContactInfoBo entityTypeContactInfoBo) throws Exception {
863 LOG.debug("Inside the getPatronHomeEmailId method");
864 String emailId = "";
865 if (entityTypeContactInfoBo!=null && entityTypeContactInfoBo.getEmailAddresses() != null) {
866 for (int j = 0; j < entityTypeContactInfoBo.getEmailAddresses().size(); j++) {
867 if (entityTypeContactInfoBo.getEmailAddresses().get(j).getDefaultValue()) {
868 emailId = (entityTypeContactInfoBo.getEmailAddresses().get(j).getEmailAddress());
869 break;
870 }
871 }
872 }
873 return emailId;
874 }
875
876
877 }