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
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.info(" 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.info(" 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 valid &= checkPhoneMultipleDefault(patronDoc.getPhones(), "phones");
136 valid &= checkAddressMultipleDefault(patronDoc.getOleEntityAddressBo(), "oleEntityAddressBo");
137 valid &= checkEmailMultipleDefault(patronDoc.getEmails(), "emails");
138 if (!valid) {
139 GlobalVariables.getMessageMap().putError("OlePatronDocument-ContactsSection", OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_DEFAULT_FIELD_CONTACT);
140
141
142 }
143 return valid;
144 }
145
146
147
148
149
150
151
152 protected boolean validateBarcode(OlePatronDocument patronDoc) {
153 boolean valid = true;
154 if (StringUtils.isEmpty(patronDoc.getBarcode())) {
155 this.putFieldError("dataObject.barcode", "error.field.required");
156 valid = false;
157 }
158 return valid;
159 }
160
161
162
163
164
165
166
167 protected boolean checkDuplicateBarcode(OlePatronDocument patronDoc) {
168 boolean valid = true;
169 Map barcodeMap = new HashMap();
170 barcodeMap.put(OLEConstants.OlePatron.BARCODE, patronDoc.getBarcode());
171 List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OlePatronDocument.class, barcodeMap);
172 if (olePatronDocuments.size() > 0) {
173 for (OlePatronDocument olePatronDocument : olePatronDocuments) {
174 if (patronDoc.getOlePatronId() == null || !(patronDoc.getOlePatronId().equals(olePatronDocument.getOlePatronId()))) {
175 this.putFieldError("dataObject.barcode", "error.barcode.duplicate");
176 valid = false;
177 }
178 }
179 }
180 return valid;
181 }
182
183
184
185
186
187
188
189 public UiDocumentService getUIDocumentService() {
190 if (uiDocumentService == null) {
191 uiDocumentService = KIMServiceLocatorInternal.getUiDocumentService();
192 }
193 return uiDocumentService;
194 }
195
196
197
198
199
200
201
202
203 protected boolean checkPhoneMultipleDefault(List<EntityPhoneBo> phoneBoList, String listName) {
204 boolean valid = true;
205 boolean isDefaultSet = false;
206 int i = 0;
207 for (EntityPhoneBo phone : phoneBoList) {
208 if (phone.isDefaultValue()) {
209 if (isDefaultSet) {
210 this.putFieldError("dataObject." + listName + "[" + i + "].defaultValue", RiceKeyConstants.ERROR_MULTIPLE_DEFAULT_SELETION);
211 valid = false;
212 } else {
213 isDefaultSet = true;
214 }
215 }
216 i++;
217 }
218 if (!phoneBoList.isEmpty() && !isDefaultSet) {
219
220 valid = false;
221 }
222 return valid;
223 }
224
225
226
227
228
229
230
231
232 protected boolean checkAddressMultipleDefault(List<OleEntityAddressBo> addrBoList, String listName) {
233 boolean valid = true;
234 boolean isDefaultSet = false;
235 int i = 0;
236 for (OleEntityAddressBo addr : addrBoList) {
237 EntityAddressBo entityAddressBo = addr.getEntityAddressBo();
238 if (entityAddressBo.isDefaultValue()) {
239 if (isDefaultSet) {
240 this.putFieldError("dataObject." + listName + "[" + i + "].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_MULIT_PREFERRED_ADDRESS);
241 valid = false;
242 } else {
243 isDefaultSet = true;
244 }
245 }
246 i++;
247 }
248 if (!addrBoList.isEmpty() && !isDefaultSet) {
249
250 valid = false;
251 }
252 return valid;
253 }
254
255
256
257
258
259
260
261
262 protected boolean checkEmailMultipleDefault(List<EntityEmailBo> emailBoList, String listName) {
263 boolean valid = true;
264 boolean isDefaultSet = false;
265 int i = 0;
266 for (EntityEmailBo email : emailBoList) {
267 if (email.isDefaultValue()) {
268 if (isDefaultSet) {
269 this.putFieldError("dataObject." + listName + "[" + i + "].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_MULIT_PREFERRED_EMAIL);
270 valid = false;
271 } else {
272 isDefaultSet = true;
273 }
274 }
275 i++;
276 }
277 if (!emailBoList.isEmpty() && !isDefaultSet) {
278
279 valid = false;
280 }
281 return valid;
282 }
283
284
285
286
287
288
289 public IdentityService getIdentityService() {
290 if (identityService == null) {
291 identityService = KimApiServiceLocator.getIdentityService();
292 }
293 return identityService;
294 }
295
296
297 public boolean validateAddress(OlePatronDocument olePatronDocument, String addressBos) {
298 List<OleEntityAddressBo> addressBoList = olePatronDocument.getOleEntityAddressBo();
299 OleEntityAddressBo oleEntityAddressBo;
300 boolean valid = true;
301 boolean dateExist = true;
302 for (int i = 0; i < addressBoList.size(); i++) {
303 oleEntityAddressBo = addressBoList.get(i);
304 if (oleEntityAddressBo.getEntityAddressBo().isDefaultValue()) {
305 if (oleEntityAddressBo.getOleAddressBo().getAddressValidFrom() != null && oleEntityAddressBo.getOleAddressBo().getAddressValidTo() != null) {
306 this.putFieldError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_DEFAULT_DATE);
307 return false;
308 }
309
310 }
311
312
313 else if (oleEntityAddressBo.getOleAddressBo().getAddressValidFrom() != null || oleEntityAddressBo.getOleAddressBo().getAddressValidTo() != null) {
314
315 if (dateExist) {
316 dateExist = false;
317 } else {
318 this.putFieldError("dataObject." + addressBos + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_SINGLE_DATE);
319 return false;
320 }
321 }
322
323 }
324 return valid;
325 }
326
327 public boolean validateRequiredField(OlePatronDocument olePatronDocument) {
328 boolean valid = true;
329 List<OleEntityAddressBo> addressBoList = olePatronDocument.getOleEntityAddressBo();
330 List<EntityEmailBo> emailBoList = olePatronDocument.getEmails();
331 EntityBo entity = olePatronDocument.getEntity();
332 List<EntityAffiliationBo> affiliationBoList = entity.getAffiliations();
333 List<EntityEmploymentBo> employeeList = entity.getEmploymentInformation();
334 if (!(addressBoList.size() > 0 || emailBoList.size() > 0)) {
335 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS);
336 valid = false;
337 }
338
339
340
341
342
343
344
345
346
347
348
349 return valid;
350 }
351
352 public boolean validatePatronName(OlePatronDocument olePatronDocument) {
353 boolean valid = true;
354 EntityNameBo names = olePatronDocument.getName();
355 String firstName = names.getFirstName();
356 String middleName = names.getMiddleName();
357 String lastName = names.getLastName();
358 String parameterResult = "";
359 Pattern pattern;
360 Matcher matcher;
361 boolean matchName;
362 if (firstName != null && !firstName.equals("")) {
363 parameterResult = getParameter(OLEConstants.OlePatron.PATRON_NAME_VALIDATION);
364 if (parameterResult != null) {
365 pattern = Pattern.compile(parameterResult);
366 matcher = pattern.matcher(firstName);
367 if (matcher.find()) {
368 this.putFieldError(OLEConstants.OlePatron.FIRST_NAME, OLEConstants.OlePatron.ERROR_PATRON_FIRST_NAME);
369 valid = false;
370 }
371 }
372 }
373 if (middleName != null && !middleName.equals("")) {
374 parameterResult = getParameter(OLEConstants.OlePatron.PATRON_NAME_VALIDATION);
375 if (parameterResult != null) {
376 pattern = Pattern.compile(parameterResult);
377 matcher = pattern.matcher(middleName);
378 if (matcher.find()) {
379 this.putFieldError(OLEConstants.OlePatron.MIDDLE_NAME, OLEConstants.OlePatron.ERROR_PATRON_MIDDLE_NAME);
380 valid = false;
381 }
382 }
383 }
384 if (lastName != null && !lastName.equals("")) {
385 parameterResult = getParameter(OLEConstants.OlePatron.PATRON_NAME_VALIDATION);
386 if (parameterResult != null) {
387 pattern = Pattern.compile(parameterResult);
388 matcher = pattern.matcher(lastName);
389 if (matcher.find()) {
390 this.putFieldError(OLEConstants.OlePatron.LAST_NAME, OLEConstants.OlePatron.ERROR_PATRON_LAST_NAME);
391 valid = false;
392 }
393 }
394 }
395 return valid;
396 }
397
398 public String getParameter(String name) {
399 ParameterKey parameterKey = ParameterKey.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, name);
400 Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
401 return parameter != null ? parameter.getValue() : null;
402 }
403 }