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