1 package org.kuali.ole.deliver.rule;
2
3 import org.apache.cxf.common.util.StringUtils;
4 import org.kuali.ole.OLEConstants;
5 import org.kuali.ole.deliver.bo.OleAddressBo;
6 import org.kuali.ole.deliver.bo.OleEntityAddressBo;
7 import org.kuali.ole.deliver.bo.OlePatronDocument;
8 import org.kuali.rice.core.api.util.RiceKeyConstants;
9 import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
10 import org.kuali.rice.coreservice.api.parameter.Parameter;
11 import org.kuali.rice.coreservice.api.parameter.ParameterKey;
12 import org.kuali.rice.kim.api.identity.IdentityService;
13 import org.kuali.rice.kim.api.identity.entity.EntityDefault;
14 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
15 import org.kuali.rice.kim.impl.identity.address.EntityAddressBo;
16 import org.kuali.rice.kim.impl.identity.affiliation.EntityAffiliationBo;
17 import org.kuali.rice.kim.impl.identity.email.EntityEmailBo;
18 import org.kuali.rice.kim.impl.identity.employment.EntityEmploymentBo;
19 import org.kuali.rice.kim.impl.identity.entity.EntityBo;
20 import org.kuali.rice.kim.impl.identity.name.EntityNameBo;
21 import org.kuali.rice.kim.impl.identity.phone.EntityPhoneBo;
22 import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
23 import org.kuali.rice.kim.service.UiDocumentService;
24 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
25 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
26 import org.kuali.rice.krad.service.KRADServiceLocator;
27 import org.kuali.rice.krad.util.GlobalVariables;
28 import org.kuali.rice.krad.util.KRADConstants;
29 import java.util.Date;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
35
36
37
38
39 public class OlePatronDocumentRule extends MaintenanceDocumentRuleBase {
40
41 protected UiDocumentService uiDocumentService;
42 protected IdentityService identityService;
43
44
45
46
47
48
49
50
51 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
52 LOG.debug(" Inside processCustomSaveDocumentBusinessRule");
53 boolean isValid = true;
54 OlePatronDocument patronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
55 isValid &= validateBorrowerType(patronDocument);
56 isValid &= checkName(patronDocument);
57 EntityDefault origEntity = null;
58 if (patronDocument.getOlePatronId() != null) {
59 origEntity = getIdentityService().getEntityDefault(patronDocument.getOlePatronId());
60 }
61 boolean isCreatingNew = origEntity == null ? true : false;
62
63 isValid &= validateEntityInformation(isCreatingNew, patronDocument);
64 isValid &= validateBarcode(patronDocument);
65 isValid &= checkDuplicateBarcode(patronDocument);
66 isValid &= validateAddress(patronDocument, "oleEntityAddressBo");
67 isValid &= validateRequiredField(patronDocument);
68 isValid &= validatePatronName(patronDocument);
69 if (LOG.isDebugEnabled()) {
70 LOG.debug(" DocumentBusinessRule for patron record is " + isValid);
71 }
72 return isValid;
73 }
74
75
76
77
78
79
80
81 protected boolean validateBorrowerType(OlePatronDocument patronDoc) {
82 boolean valid = true;
83 if (StringUtils.isEmpty(patronDoc.getBorrowerType())) {
84 this.putFieldError("dataObject.borrowerType", "error.field.required");
85 valid = false;
86 }
87 return valid;
88 }
89
90
91
92
93
94
95
96 protected boolean checkName(OlePatronDocument patronDoc) {
97 boolean valid = true;
98 if (StringUtils.isEmpty(patronDoc.getName().getFirstName())) {
99 this.putFieldError("dataObject.name.firstName", "error.field.required");
100 valid = false;
101 }
102 if (StringUtils.isEmpty(patronDoc.getName().getLastName())) {
103 this.putFieldError("dataObject.name.lastName", "error.field.required");
104 valid = false;
105 }
106
107 return valid;
108 }
109
110
111
112
113
114
115
116
117 protected boolean validateEntityInformation(boolean isCreatingNew, OlePatronDocument patronDoc) {
118 boolean valid = true;
119 boolean canOverridePrivacyPreferences = getUIDocumentService().canOverrideEntityPrivacyPreferences(GlobalVariables.getUserSession().getPrincipalId(), null);
120
121 List<OleEntityAddressBo> addressBoList = patronDoc.getOleEntityAddressBo();
122 List<EntityEmailBo> emailBoList = patronDoc.getEmails();
123 List<EntityPhoneBo> phoneBoList = patronDoc.getPhones();
124 if (addressBoList.size() == 1) {
125 OleEntityAddressBo oleEntityAddressBo = addressBoList.get(0);
126 oleEntityAddressBo.getEntityAddressBo().setDefaultValue(true);
127 }
128 if (emailBoList.size() == 1) {
129 EntityEmailBo entityEmailBo = emailBoList.get(0);
130 entityEmailBo.setDefaultValue(true);
131 }
132 if (phoneBoList.size() == 1) {
133 EntityPhoneBo entityPhoneBo = phoneBoList.get(0);
134 entityPhoneBo.setDefaultValue(true);
135 }
136 if (!checkPhoneMultipleDefault(patronDoc.getPhones(), "phones")) {
137 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PHONE_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_PHONE);
138 valid &= false;
139 }
140 if (!checkAddressMultipleDefault(patronDoc.getOleEntityAddressBo(), "oleEntityAddressBo")) {
141 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.ADDRESS_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_ADDRESS);
142 valid &= false;
143 }
144
145 if (!checkAddressMultipleDeliverAddress(patronDoc.getOleEntityAddressBo(), "oleEntityAddressBo")) {
146
147 valid &= false;
148 }
149
150 if (!checkEmailMultipleDefault(patronDoc.getEmails(), "emails")) {
151 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.EMAIL_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_EMAIL);
152 valid &= false;
153 }
154 return valid;
155 }
156
157
158
159
160
161
162
163 protected boolean validateBarcode(OlePatronDocument patronDoc) {
164 boolean valid = true;
165 if (StringUtils.isEmpty(patronDoc.getBarcode())) {
166 this.putFieldError("dataObject.barcode", "error.field.required");
167 valid = false;
168 }
169 return valid;
170 }
171
172
173
174
175
176
177
178 protected boolean checkDuplicateBarcode(OlePatronDocument patronDoc) {
179 boolean valid = true;
180 Map barcodeMap = new HashMap();
181 barcodeMap.put(OLEConstants.OlePatron.BARCODE, patronDoc.getBarcode());
182 List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class, barcodeMap);
183 if (olePatronDocuments.size() > 0) {
184 for (OlePatronDocument olePatronDocument : olePatronDocuments) {
185 if (patronDoc.getOlePatronId() == null || !(patronDoc.getOlePatronId().equals(olePatronDocument.getOlePatronId()))) {
186 this.putFieldError("dataObject.barcode", "error.barcode.duplicate");
187 valid = false;
188 }
189 }
190 }
191 return valid;
192 }
193
194
195
196
197
198
199
200 public UiDocumentService getUIDocumentService() {
201 if (uiDocumentService == null) {
202 uiDocumentService = KIMServiceLocatorInternal.getUiDocumentService();
203 }
204 return uiDocumentService;
205 }
206
207
208
209
210
211
212
213
214 protected boolean checkPhoneMultipleDefault(List<EntityPhoneBo> phoneBoList, String listName) {
215 boolean valid = true;
216 boolean isDefaultSet = false;
217 int i = 0;
218 for (EntityPhoneBo phone : phoneBoList) {
219 if (phone.isDefaultValue()) {
220 if (isDefaultSet) {
221 this.putFieldError("dataObject." + listName + "[" + i + "].defaultValue", RiceKeyConstants.ERROR_MULTIPLE_DEFAULT_SELETION);
222 valid = false;
223 } else {
224 isDefaultSet = true;
225 }
226 }
227 i++;
228 }
229 if (!phoneBoList.isEmpty() && !isDefaultSet) {
230
231 valid = false;
232 }
233 return valid;
234 }
235
236
237
238
239
240
241
242
243 protected boolean checkAddressMultipleDefault(List<OleEntityAddressBo> addrBoList, String listName) {
244 boolean valid = true;
245 boolean isDefaultSet = false;
246 int i = 0;
247 for (OleEntityAddressBo addr : addrBoList) {
248 EntityAddressBo entityAddressBo = addr.getEntityAddressBo();
249 if (entityAddressBo.isDefaultValue()) {
250 if (isDefaultSet) {
251 this.putFieldError("dataObject." + listName + "[" + i + "].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_MULIT_PREFERRED_ADDRESS);
252 valid = false;
253 } else {
254 isDefaultSet = true;
255 }
256 }
257 i++;
258 }
259 if (!addrBoList.isEmpty() && !isDefaultSet) {
260
261 valid = false;
262 }
263 return valid;
264 }
265
266
267 protected boolean checkAddressMultipleDeliverAddress(List<OleEntityAddressBo> addrBoList, String listName) {
268 boolean valid = true;
269 boolean isDefaultSet = false;
270 int i = 0;
271 boolean isAtleastOneChecked=false;
272 for (OleEntityAddressBo addr : addrBoList) {
273 OleAddressBo oleAddressBo = addr.getOleAddressBo();
274 if (oleAddressBo.isDeliverAddress()) {
275 isAtleastOneChecked=true;
276 if (isDefaultSet) {
277 this.putFieldError("dataObject." + listName + "[" + i + "].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_MULIT_DELIVER_ADDRESS);
278 valid = false;
279 } else {
280 isDefaultSet = true;
281 }
282 }
283 i++;
284 }
285 if(!isAtleastOneChecked){
286 valid=true;
287 } else {
288 if (!addrBoList.isEmpty() && !isDefaultSet) {
289
290 valid = false;
291 }
292 }
293 return valid;
294 }
295
296
297
298
299
300
301
302 protected boolean checkEmailMultipleDefault(List<EntityEmailBo> emailBoList, String listName) {
303 boolean valid = true;
304 boolean isDefaultSet = false;
305 int i = 0;
306 for (EntityEmailBo email : emailBoList) {
307 if (email.isDefaultValue()) {
308 if (isDefaultSet) {
309 this.putFieldError("dataObject." + listName + "[" + i + "].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_MULIT_PREFERRED_EMAIL);
310 valid = false;
311 } else {
312 isDefaultSet = true;
313 }
314 }
315 i++;
316 }
317 if (!emailBoList.isEmpty() && !isDefaultSet) {
318
319 valid = false;
320 }
321 return valid;
322 }
323
324
325
326
327
328
329 public IdentityService getIdentityService() {
330 if (identityService == null) {
331 identityService = KimApiServiceLocator.getIdentityService();
332 }
333 return identityService;
334 }
335
336
337 public boolean validateAddress(OlePatronDocument olePatronDocument, String addressBos) {
338 List<OleEntityAddressBo> addressBoList = olePatronDocument.getOleEntityAddressBo();
339 OleEntityAddressBo oleEntityAddressBo;
340 boolean valid = true;
341 boolean flag=true;
342 boolean dateExist = true;
343 Map<Date,Date> map=new HashMap<>();
344 for (int i = 0; i < addressBoList.size(); i++) {
345 oleEntityAddressBo = addressBoList.get(i);
346 if (oleEntityAddressBo.getEntityAddressBo().isDefaultValue()) {
347 if (oleEntityAddressBo.getOleAddressBo().getAddressValidFrom() != null || oleEntityAddressBo.getOleAddressBo().getAddressValidTo() != null) {
348 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_DEFAULT_DATE);
349 flag= false;
350 }
351 }
352 if(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()!=null && oleEntityAddressBo.getOleAddressBo().getAddressValidTo()!=null && oleEntityAddressBo.getOleAddressBo().getAddressValidFrom().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidTo())>0){
353 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_VALID_ADDRESS_TO_DATE);
354 flag= false;
355 }
356
357 if(!oleEntityAddressBo.getEntityAddressBo().isDefaultValue()){
358 if(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()==null && oleEntityAddressBo.getOleAddressBo().getAddressValidTo()!=null){
359 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS_FROM_DATE, "ValidFrom");
360 flag= false;
361 }
362 if(oleEntityAddressBo.getOleAddressBo().getAddressValidTo()==null && oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()!=null){
363 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS_FROM_DATE, "ValidTo");
364 flag= false;
365 }
366 for(Map.Entry<Date,Date> entry:map.entrySet()){
367 if(entry.getKey()!=null&&entry.getValue()!=null&&oleEntityAddressBo.getOleAddressBo().getAddressValidFrom()!=null&&oleEntityAddressBo.getOleAddressBo().getAddressValidTo()!=null){
368 if(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom().compareTo(entry.getKey())>=0 && oleEntityAddressBo.getOleAddressBo().getAddressValidFrom().compareTo(entry.getValue())<=0
369 || oleEntityAddressBo.getOleAddressBo().getAddressValidTo().compareTo(entry.getKey())>=0 && oleEntityAddressBo.getOleAddressBo().getAddressValidTo().compareTo(entry.getValue())<=0){
370 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue",OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_FROM_TO_DATE_OVERLAP);
371 flag= false;
372 }
373 if(entry.getKey().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom())>=0 && entry.getKey().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidTo())<=0
374 || entry.getValue().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom())>=0 && entry.getValue().compareTo(oleEntityAddressBo.getOleAddressBo().getAddressValidTo())<=0){
375 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_FROM_TO_DATE_OVERLAP);
376 flag= false;
377 }
378 }
379 }
380 }else if (oleEntityAddressBo.getOleAddressBo().getAddressValidFrom() != null || oleEntityAddressBo.getOleAddressBo().getAddressValidTo() != null) {
381
382 if (dateExist) {
383 dateExist = false;
384 } else {
385 GlobalVariables.getMessageMap().putError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_SINGLE_DATE);
386 flag= false;
387 }
388 }
389 map.put(oleEntityAddressBo.getOleAddressBo().getAddressValidFrom(),oleEntityAddressBo.getOleAddressBo().getAddressValidTo());
390 }
391 if(!flag){
392 return false;
393 }
394 return valid;
395 }
396
397 public boolean validateRequiredField(OlePatronDocument olePatronDocument) {
398 boolean valid = true;
399 List<OleEntityAddressBo> addressBoList = olePatronDocument.getOleEntityAddressBo();
400 List<EntityEmailBo> emailBoList = olePatronDocument.getEmails();
401 if (!(addressBoList.size() > 0 || emailBoList.size() > 0)) {
402 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS);
403 valid = false;
404 }
405
406
407
408
409
410
411
412
413
414
415
416 return valid;
417 }
418
419 public boolean validatePatronName(OlePatronDocument olePatronDocument) {
420 boolean valid = true;
421 EntityNameBo names = olePatronDocument.getName();
422 String firstName = names.getFirstName();
423 String middleName = names.getMiddleName();
424 String lastName = names.getLastName();
425 String parameterResult = "";
426 Pattern pattern;
427 Matcher matcher;
428 boolean matchName;
429 if (firstName != null && !firstName.equals("")) {
430 parameterResult = getParameter(OLEConstants.OlePatron.PATRON_NAME_VALIDATION);
431 if (parameterResult != null) {
432 pattern = Pattern.compile(parameterResult);
433 matcher = pattern.matcher(firstName);
434 if (matcher.find()) {
435 this.putFieldError(OLEConstants.OlePatron.FIRST_NAME, OLEConstants.OlePatron.ERROR_PATRON_FIRST_NAME);
436 valid = false;
437 }
438 }
439 }
440 if (middleName != null && !middleName.equals("")) {
441 parameterResult = getParameter(OLEConstants.OlePatron.PATRON_NAME_VALIDATION);
442 if (parameterResult != null) {
443 pattern = Pattern.compile(parameterResult);
444 matcher = pattern.matcher(middleName);
445 if (matcher.find()) {
446 this.putFieldError(OLEConstants.OlePatron.MIDDLE_NAME, OLEConstants.OlePatron.ERROR_PATRON_MIDDLE_NAME);
447 valid = false;
448 }
449 }
450 }
451 if (lastName != null && !lastName.equals("")) {
452 parameterResult = getParameter(OLEConstants.OlePatron.PATRON_NAME_VALIDATION);
453 if (parameterResult != null) {
454 pattern = Pattern.compile(parameterResult);
455 matcher = pattern.matcher(lastName);
456 if (matcher.find()) {
457 this.putFieldError(OLEConstants.OlePatron.LAST_NAME, OLEConstants.OlePatron.ERROR_PATRON_LAST_NAME);
458 valid = false;
459 }
460 }
461 }
462 return valid;
463 }
464
465 public String getParameter(String name) {
466 ParameterKey parameterKey = ParameterKey.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, name);
467 Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
468 return parameter != null ? parameter.getValue() : null;
469 }
470 }