1 package org.kuali.ole.deliver.rule;
2
3 import org.apache.commons.collections.CollectionUtils;
4 import org.apache.cxf.common.util.StringUtils;
5 import org.kuali.ole.OLEConstants;
6 import org.kuali.ole.deliver.bo.*;
7 import org.kuali.ole.service.OlePatronHelperService;
8 import org.kuali.rice.core.api.criteria.QueryByCriteria;
9 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
10 import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
11 import org.kuali.rice.coreservice.api.parameter.Parameter;
12 import org.kuali.rice.coreservice.api.parameter.ParameterKey;
13 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
14 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
15 import org.kuali.rice.krad.service.KRADServiceLocator;
16 import org.kuali.rice.krad.util.GlobalVariables;
17 import org.kuali.rice.krad.util.KRADConstants;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25
26
27
28 public class OlePatronDocumentRule extends MaintenanceDocumentRuleBase {
29
30 private OlePatronHelperService olePatronHelperService;
31
32 public OlePatronHelperService getOlePatronHelperService(){
33 if(olePatronHelperService==null){
34 olePatronHelperService = GlobalResourceLoader.getService("olePatronHelperService");
35 }
36 return olePatronHelperService;
37 }
38
39
40
41
42
43
44
45 @Override
46 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
47 OlePatronDocument patronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
48 boolean isValid = true;
49 isValid &= validatePatronName(patronDocument);
50 isValid &= validateBorrowerType(patronDocument);
51 isValid &= validateBarcode(patronDocument);
52 isValid &= checkDuplicateBarcode(patronDocument);
53 isValid &= validateRequiredField(patronDocument);
54 isValid &= validatePatronInformation(patronDocument);
55 isValid &= validateAddress(patronDocument);
56 return isValid;
57 }
58
59 @Override
60 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
61 OlePatronDocument patronDocument = (OlePatronDocument) document.getNewMaintainableObject().getDataObject();
62 boolean isValid = true;
63 isValid &= validatePatronName(patronDocument);
64 isValid &= validateBorrowerType(patronDocument);
65 isValid &= validateBarcode(patronDocument);
66 isValid &= checkDuplicateBarcode(patronDocument);
67 isValid &= validateRequiredField(patronDocument);
68 isValid &= validatePatronInformation(patronDocument);
69 isValid &= validateAddress(patronDocument);
70 return isValid;
71 }
72
73 protected boolean validateBorrowerType(OlePatronDocument patronDoc) {
74 boolean valid = true;
75 if (StringUtils.isEmpty(patronDoc.getBorrowerType())) {
76 this.putFieldError("dataObject.borrowerType", "error.field.required");
77 valid = false;
78 }
79 return valid;
80 }
81
82 protected boolean validateBarcode(OlePatronDocument patronDoc) {
83 boolean valid = true;
84 if (StringUtils.isEmpty(patronDoc.getBarcode())) {
85 this.putFieldError("dataObject.barcode", "error.field.required");
86 valid = false;
87 }
88 return valid;
89 }
90
91 protected boolean checkDuplicateBarcode(OlePatronDocument patronDoc) {
92 boolean valid = true;
93 Map barcodeMap = new HashMap();
94 barcodeMap.put(OLEConstants.OlePatron.BARCODE, patronDoc.getBarcode());
95 List<OlePatronDocument> olePatronDocuments = (List<OlePatronDocument>) KRADServiceLocator.getDataObjectService().findMatching(OlePatronDocument.class, QueryByCriteria.Builder.andAttributes(barcodeMap).build()).getResults();
96 if (olePatronDocuments.size() > 0) {
97 for (OlePatronDocument olePatronDocument : olePatronDocuments) {
98 if (patronDoc.getOlePatronId() == null || !(patronDoc.getOlePatronId().equals(olePatronDocument.getOlePatronId()))) {
99 this.putFieldError("dataObject.barcode", "error.barcode.duplicate");
100 valid = false;
101 }
102 }
103 }
104 return valid;
105 }
106
107 public boolean validateAddress(OlePatronDocument olePatronDocument) {
108 List<OLEPatronDocumentAddress> addressBoList = olePatronDocument.getOlePatronDocumentAddresses();
109 boolean valid = true;
110 boolean flag = true;
111 boolean dateExist = true;
112 Map<Date, Date> map = new HashMap<>();
113 for (OLEPatronDocumentAddress address : addressBoList) {
114 if (address.isDefaultValue()) {
115 if (address.getAddressValidFrom() != null || address.getAddressValidTo() != null) {
116 GlobalVariables.getMessageMap().putError("dataObject."+"olePatronDocumentAddresses" + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_DEFAULT_DATE);
117 flag = false;
118 }
119 }
120 if (address.getAddressValidFrom() != null && address.getAddressValidTo() != null && address.getAddressValidFrom().compareTo(address.getAddressValidTo()) > 0) {
121 GlobalVariables.getMessageMap().putError("dataObject." +"olePatronDocumentAddresses" + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_VALID_ADDRESS_TO_DATE);
122 flag = false;
123 }
124
125 if (!address.isDefaultValue()) {
126 if (address.getAddressValidFrom() == null && address.getAddressValidTo() != null) {
127 GlobalVariables.getMessageMap().putError("dataObject."+"olePatronDocumentAddresses" + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS_FROM_DATE, "ValidFrom");
128 flag = false;
129 }
130 if (address.getAddressValidTo() == null && address.getAddressValidFrom() != null) {
131 GlobalVariables.getMessageMap().putError("dataObject."+"olePatronDocumentAddresses" + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS_FROM_DATE, "ValidTo");
132 flag = false;
133 }
134 for (Map.Entry<Date, Date> entry : map.entrySet()) {
135 if (entry.getKey() != null && entry.getValue() != null && address.getAddressValidFrom() != null && address.getAddressValidTo() != null) {
136 if (address.getAddressValidFrom().compareTo(entry.getKey()) >= 0 && address.getAddressValidFrom().compareTo(entry.getValue()) <= 0
137 || address.getAddressValidTo().compareTo(entry.getKey()) >= 0 && address.getAddressValidTo().compareTo(entry.getValue()) <= 0) {
138 GlobalVariables.getMessageMap().putError("dataObject." +"olePatronDocumentAddresses" + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_FROM_TO_DATE_OVERLAP);
139 flag = false;
140 }
141 if (entry.getKey().compareTo(address.getAddressValidFrom()) >= 0 && entry.getKey().compareTo(address.getAddressValidTo()) <= 0
142 || entry.getValue().compareTo(address.getAddressValidFrom()) >= 0 && entry.getValue().compareTo(address.getAddressValidTo()) <= 0) {
143 GlobalVariables.getMessageMap().putError("dataObject." +"olePatronDocumentAddresses" + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_FROM_TO_DATE_OVERLAP);
144 flag = false;
145 }
146 }
147 }
148 } else if (address.getAddressValidFrom() != null || address.getAddressValidTo() != null) {
149 if (dateExist) {
150 dateExist = false;
151 } else {
152 GlobalVariables.getMessageMap().putError("dataObject." +"olePatronDocumentAddresses" + "[0].defaultValue", OLEConstants.OlePatron.ERROR_PATRON_ADDRESS_SINGLE_DATE);
153 flag = false;
154 }
155 }
156 map.put(address.getAddressValidFrom(), address.getAddressValidTo());
157 }
158 if (!flag) {
159 return false;
160 }
161 return valid;
162 }
163
164 public boolean validatePatronName(OlePatronDocument olePatronDocument) {
165 boolean valid = true;
166 OLEPatronDocumentName names = olePatronDocument.getOlePatronDocumentName();
167 String firstName = names.getFirstName();
168 String middleName = names.getMiddleName();
169 String lastName = names.getLastName();
170 String parameterResult = "";
171 Pattern pattern;
172 Matcher matcher;
173 boolean matchName;
174 if (firstName != null && !firstName.equals("")) {
175 parameterResult = getParameter(OLEConstants.OlePatron.PATRON_NAME_VALIDATION);
176 if (parameterResult != null) {
177 pattern = Pattern.compile(parameterResult);
178 matcher = pattern.matcher(firstName);
179 if (matcher.find()) {
180 this.putFieldError(OLEConstants.OlePatron.FIRST_NAME, OLEConstants.OlePatron.ERROR_PATRON_FIRST_NAME);
181 valid = false;
182 }
183 }
184 }
185 if (middleName != null && !middleName.equals("")) {
186 parameterResult = getParameter(OLEConstants.OlePatron.PATRON_NAME_VALIDATION);
187 if (parameterResult != null) {
188 pattern = Pattern.compile(parameterResult);
189 matcher = pattern.matcher(middleName);
190 if (matcher.find()) {
191 this.putFieldError(OLEConstants.OlePatron.MIDDLE_NAME, OLEConstants.OlePatron.ERROR_PATRON_MIDDLE_NAME);
192 valid = false;
193 }
194 }
195 }
196 if (lastName != null && !lastName.equals("")) {
197 parameterResult = getParameter(OLEConstants.OlePatron.PATRON_NAME_VALIDATION);
198 if (parameterResult != null) {
199 pattern = Pattern.compile(parameterResult);
200 matcher = pattern.matcher(lastName);
201 if (matcher.find()) {
202 this.putFieldError(OLEConstants.OlePatron.LAST_NAME, OLEConstants.OlePatron.ERROR_PATRON_LAST_NAME);
203 valid = false;
204 }
205 }
206 }
207 return valid;
208 }
209
210 public boolean validateDefaultOneAsActive(OlePatronDocument patronDoc){
211 boolean isValid = true;
212 if (CollectionUtils.isNotEmpty(patronDoc.getOlePatronDocumentPhones())) {
213 for (OLEPatronDocumentPhone olePatronDocumentPhone : patronDoc.getOlePatronDocumentPhones()) {
214 if (olePatronDocumentPhone.isDefaultValue() && !olePatronDocumentPhone.isActive()) {
215 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PHONE_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_PHONE_ACTIVE);
216 isValid=false;
217 break;
218 }
219 }
220 }
221 if (CollectionUtils.isNotEmpty(patronDoc.getOlePatronDocumentAddresses())) {
222 for (OLEPatronDocumentAddress olePatronDocumentAddress : patronDoc.getOlePatronDocumentAddresses()) {
223 if (olePatronDocumentAddress.isDefaultValue() && !olePatronDocumentAddress.isActive()) {
224 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.ADDRESS_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_ADDRESS_ACTIVE);
225 isValid=false;
226 break;
227 }
228 }
229 }
230 if (CollectionUtils.isNotEmpty(patronDoc.getOlePatronDocumentEmails())) {
231 for (OLEPatronDocumentEmail olePatronDocumentEmail : patronDoc.getOlePatronDocumentEmails()) {
232 if (olePatronDocumentEmail.isDefaultValue() && !olePatronDocumentEmail.isActive()) {
233 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.EMAIL_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_EMAIL_ACTIVE);
234 isValid=false;
235 break;
236 }
237 }
238 }
239 return isValid;
240 }
241
242 public String getParameter(String name) {
243 ParameterKey parameterKey = ParameterKey.create(OLEConstants.APPL_ID, OLEConstants.DLVR_NMSPC, OLEConstants.DLVR_CMPNT, name);
244 Parameter parameter = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(parameterKey);
245 return parameter != null ? parameter.getValue() : null;
246 }
247
248
249 private boolean validatePatronInformation(OlePatronDocument patronDoc) {
250 boolean valid = true;
251 List<OLEPatronDocumentAddress> addressBoList = patronDoc.getOlePatronDocumentAddresses();
252 List<OLEPatronDocumentEmail> emailBoList = patronDoc.getOlePatronDocumentEmails();
253 List<OLEPatronDocumentPhone> phoneBoList = patronDoc.getOlePatronDocumentPhones();
254 if (addressBoList.size() == 1) {
255 OLEPatronDocumentAddress olePatronDocumentAddress = addressBoList.get(0);
256 olePatronDocumentAddress.setDefaultValue(true);
257 }
258 if (emailBoList.size() == 1) {
259 OLEPatronDocumentEmail entityEmailBo = emailBoList.get(0);
260 entityEmailBo.setDefaultValue(true);
261 }
262 if (phoneBoList.size() == 1) {
263 OLEPatronDocumentPhone entityPhoneBo = phoneBoList.get(0);
264 entityPhoneBo.setDefaultValue(true);
265 }
266 if (CollectionUtils.isNotEmpty(patronDoc.getOlePatronDocumentPhones())) {
267 if (!getOlePatronHelperService().checkPatronPhoneMultipleDefault(patronDoc.getOlePatronDocumentPhones())) {
268 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.PHONE_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_PHONE);
269 valid = false;
270 }
271 }
272 if (CollectionUtils.isNotEmpty(patronDoc.getOlePatronDocumentAddresses())) {
273 if (!getOlePatronHelperService().checkPatronAddressMultipleDefault(patronDoc.getOlePatronDocumentAddresses())) {
274 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.ADDRESS_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_ADDRESS);
275 valid = false;
276 }
277 }
278 if (CollectionUtils.isNotEmpty(patronDoc.getOlePatronDocumentEmails())){
279 if (!getOlePatronHelperService().checkPatronEmailMultipleDefault(patronDoc.getOlePatronDocumentEmails())) {
280 GlobalVariables.getMessageMap().putErrorForSectionId(OLEConstants.OlePatron.EMAIL_SECTION_ID, OLEConstants.OlePatron.ERROR_SELECTION_PREFERRED_EMAIL);
281 valid = false;
282 }
283 }
284 return valid;
285
286 }
287
288 public boolean validateRequiredField(OlePatronDocument olePatronDocument) {
289 boolean valid = true;
290 List<OLEPatronDocumentAddress> addressBoList = olePatronDocument.getOlePatronDocumentAddresses();
291 List<OLEPatronDocumentEmail> emailBoList = olePatronDocument.getOlePatronDocumentEmails();
292 if (!(addressBoList.size() > 0 || emailBoList.size() > 0)) {
293 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.OlePatron.ERROR_PATRON_REQUIRED_ADDRESS);
294
295 valid = false;
296 }
297 return valid;
298 }
299 }