| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
package org.kuali.student.common.validator; |
| 10 | |
|
| 11 | |
import java.lang.reflect.InvocationTargetException; |
| 12 | |
import java.util.ArrayList; |
| 13 | |
import java.util.Collection; |
| 14 | |
import java.util.Date; |
| 15 | |
import java.util.HashMap; |
| 16 | |
import java.util.Iterator; |
| 17 | |
import java.util.List; |
| 18 | |
import java.util.Map; |
| 19 | |
import java.util.Stack; |
| 20 | |
|
| 21 | |
import org.apache.commons.beanutils.PropertyUtils; |
| 22 | |
import org.apache.log4j.Logger; |
| 23 | |
import org.kuali.student.common.dictionary.dto.CaseConstraint; |
| 24 | |
import org.kuali.student.common.dictionary.dto.CommonLookupParam; |
| 25 | |
import org.kuali.student.common.dictionary.dto.Constraint; |
| 26 | |
import org.kuali.student.common.dictionary.dto.DataType; |
| 27 | |
import org.kuali.student.common.dictionary.dto.FieldDefinition; |
| 28 | |
import org.kuali.student.common.dictionary.dto.LookupConstraint; |
| 29 | |
import org.kuali.student.common.dictionary.dto.MustOccurConstraint; |
| 30 | |
import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition; |
| 31 | |
import org.kuali.student.common.dictionary.dto.RequiredConstraint; |
| 32 | |
import org.kuali.student.common.dictionary.dto.ValidCharsConstraint; |
| 33 | |
import org.kuali.student.common.dictionary.dto.WhenConstraint; |
| 34 | |
import org.kuali.student.common.messages.dto.Message; |
| 35 | |
import org.kuali.student.common.messages.service.MessageService; |
| 36 | |
import org.kuali.student.common.search.dto.SearchParam; |
| 37 | |
import org.kuali.student.common.search.dto.SearchRequest; |
| 38 | |
import org.kuali.student.common.search.dto.SearchResult; |
| 39 | |
import org.kuali.student.common.search.service.SearchDispatcher; |
| 40 | |
import org.kuali.student.common.util.MessageUtils; |
| 41 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
| 42 | |
|
| 43 | 0 | public class DefaultValidatorImpl extends BaseAbstractValidator { |
| 44 | 0 | final static Logger LOG = Logger.getLogger(DefaultValidatorImpl.class); |
| 45 | |
|
| 46 | 0 | private MessageService messageService = null; |
| 47 | |
|
| 48 | |
private SearchDispatcher searchDispatcher; |
| 49 | |
|
| 50 | 0 | private String messageLocaleKey = "en"; |
| 51 | |
|
| 52 | 0 | private String messageGroupKey = "validation"; |
| 53 | |
|
| 54 | 0 | private DateParser dateParser = new ServerDateParser(); |
| 55 | |
|
| 56 | 0 | private boolean serverSide = true; |
| 57 | |
|
| 58 | |
public MessageService getMessageService() { |
| 59 | 0 | return messageService; |
| 60 | |
} |
| 61 | |
|
| 62 | |
public void setMessageService(MessageService messageService) { |
| 63 | 0 | this.messageService = messageService; |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
public String getMessageLocaleKey() { |
| 67 | 0 | return messageLocaleKey; |
| 68 | |
} |
| 69 | |
|
| 70 | |
public void setMessageLocaleKey(String messageLocaleKey) { |
| 71 | 0 | this.messageLocaleKey = messageLocaleKey; |
| 72 | 0 | } |
| 73 | |
|
| 74 | |
public String getMessageGroupKey() { |
| 75 | 0 | return messageGroupKey; |
| 76 | |
} |
| 77 | |
|
| 78 | |
public void setMessageGroupKey(String messageGroupKey) { |
| 79 | 0 | this.messageGroupKey = messageGroupKey; |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
public void setDateParser(DateParser dateParser) { |
| 83 | 0 | this.dateParser = dateParser; |
| 84 | 0 | } |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public boolean isServerSide() { |
| 90 | 0 | return serverSide; |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
public void setServerSide(boolean serverSide) { |
| 98 | 0 | this.serverSide = serverSide; |
| 99 | 0 | } |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public DateParser getDateParser() { |
| 105 | 0 | return dateParser; |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
public List<ValidationResultInfo> validateObject(Object data, ObjectStructureDefinition objStructure) { |
| 116 | |
|
| 117 | 0 | Stack<String> elementStack = new Stack<String>(); |
| 118 | |
|
| 119 | 0 | return validateObject(data, objStructure, elementStack, data, objStructure, true); |
| 120 | |
} |
| 121 | |
|
| 122 | |
private List<ValidationResultInfo> validateObject(Object data, ObjectStructureDefinition objStructure, Stack<String> elementStack, Object rootData, ObjectStructureDefinition rootObjStructure, boolean isRoot) { |
| 123 | |
|
| 124 | 0 | List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>(); |
| 125 | |
|
| 126 | 0 | ConstraintDataProvider dataProvider = new BeanConstraintDataProvider(); |
| 127 | 0 | dataProvider.initialize(data); |
| 128 | |
|
| 129 | |
|
| 130 | 0 | StringBuilder objXPathElement = new StringBuilder(dataProvider.getPath()); |
| 131 | |
|
| 132 | 0 | if(!isRoot && !objXPathElement.toString().isEmpty()){ |
| 133 | 0 | elementStack.push(objXPathElement.toString()); |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | 0 | if (null == objStructure) { |
| 141 | 0 | return results; |
| 142 | |
} |
| 143 | |
|
| 144 | 0 | for (FieldDefinition f : objStructure.getAttributes()) { |
| 145 | 0 | List<ValidationResultInfo> l = validateField(f, objStructure, dataProvider, elementStack, rootData, rootObjStructure); |
| 146 | |
|
| 147 | 0 | results.addAll(l); |
| 148 | |
|
| 149 | |
|
| 150 | 0 | if (f.getCustomValidatorClass() != null || f.isServerSide() && serverSide) { |
| 151 | 0 | Validator customValidator = validatorFactory.getValidator(f.getCustomValidatorClass()); |
| 152 | 0 | if(customValidator==null){ |
| 153 | 0 | throw new RuntimeException("Custom Validator "+f.getCustomValidatorClass()+" was not configured in this context"); |
| 154 | |
} |
| 155 | 0 | l = customValidator.validateObject(f,data, objStructure,elementStack); |
| 156 | 0 | results.addAll(l); |
| 157 | |
} |
| 158 | 0 | } |
| 159 | 0 | if(!isRoot && !objXPathElement.toString().isEmpty()){ |
| 160 | 0 | elementStack.pop(); |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | 0 | return results; |
| 173 | |
} |
| 174 | |
|
| 175 | |
public List<ValidationResultInfo> validateField(FieldDefinition field, ObjectStructureDefinition objStruct, ConstraintDataProvider dataProvider, Stack<String> elementStack, Object rootData, ObjectStructureDefinition rootObjectStructure) { |
| 176 | |
|
| 177 | 0 | Object value = dataProvider.getValue(field.getName()); |
| 178 | 0 | List<ValidationResultInfo> results = new ArrayList<ValidationResultInfo>(); |
| 179 | |
|
| 180 | |
|
| 181 | 0 | if (value == null || "".equals(value.toString().trim())) { |
| 182 | 0 | processConstraint(results, field, objStruct, value, dataProvider, elementStack, rootData, rootObjectStructure); |
| 183 | 0 | return results; |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | 0 | if (DataType.COMPLEX.equals(field.getDataType())) { |
| 190 | 0 | ObjectStructureDefinition nestedObjStruct = null; |
| 191 | |
|
| 192 | 0 | if (null != field.getDataObjectStructure()) { |
| 193 | 0 | nestedObjStruct = field.getDataObjectStructure(); |
| 194 | |
} |
| 195 | |
|
| 196 | 0 | elementStack.push(field.getName()); |
| 197 | |
|
| 198 | |
|
| 199 | 0 | if (value instanceof Collection) { |
| 200 | |
|
| 201 | 0 | String xPathForCollection = getElementXpath(elementStack) + "/*"; |
| 202 | |
|
| 203 | 0 | int i=0; |
| 204 | 0 | for (Object o : (Collection<?>) value) { |
| 205 | 0 | elementStack.push(Integer.toString(i)); |
| 206 | |
|
| 207 | 0 | processNestedObjectStructure(results, o, nestedObjStruct, field, elementStack, rootData, rootObjectStructure); |
| 208 | |
|
| 209 | |
|
| 210 | 0 | elementStack.pop(); |
| 211 | 0 | i++; |
| 212 | |
} |
| 213 | 0 | if (field.getMinOccurs() != null && field.getMinOccurs() > ((Collection<?>) value).size()) { |
| 214 | 0 | ValidationResultInfo valRes = new ValidationResultInfo(xPathForCollection, value); |
| 215 | 0 | valRes.setError(MessageUtils.interpolate(getMessage("validation.minOccurs"), toMap(field))); |
| 216 | 0 | results.add(valRes); |
| 217 | |
} |
| 218 | |
|
| 219 | 0 | Integer maxOccurs = tryParse(field.getMaxOccurs()); |
| 220 | 0 | if (maxOccurs != null && maxOccurs < ((Collection<?>) value).size()) { |
| 221 | 0 | ValidationResultInfo valRes = new ValidationResultInfo(xPathForCollection, value); |
| 222 | 0 | valRes.setError(MessageUtils.interpolate(getMessage("validation.maxOccurs"), toMap(field))); |
| 223 | 0 | results.add(valRes); |
| 224 | |
} |
| 225 | 0 | } else { |
| 226 | 0 | if (null != value) { |
| 227 | 0 | processNestedObjectStructure(results, value, nestedObjStruct, field, elementStack, rootData, rootObjectStructure); |
| 228 | |
} else { |
| 229 | 0 | if (field.getMinOccurs() != null && field.getMinOccurs() > 0) { |
| 230 | 0 | ValidationResultInfo val = new ValidationResultInfo(getElementXpath(elementStack), value); |
| 231 | 0 | val.setError(getMessage("validation.required")); |
| 232 | 0 | results.add(val); |
| 233 | |
} |
| 234 | |
} |
| 235 | |
} |
| 236 | |
|
| 237 | |
|
| 238 | 0 | elementStack.pop(); |
| 239 | |
|
| 240 | 0 | } else { |
| 241 | |
|
| 242 | 0 | if (value instanceof Collection) { |
| 243 | |
|
| 244 | 0 | if(((Collection<?>)value).isEmpty()){ |
| 245 | 0 | processConstraint(results, field, objStruct, "", dataProvider, elementStack, rootData, rootObjectStructure); |
| 246 | |
} |
| 247 | |
|
| 248 | 0 | int i = 0; |
| 249 | 0 | for (Object o : (Collection<?>) value) { |
| 250 | 0 | elementStack.push(Integer.toBinaryString(i)); |
| 251 | |
|
| 252 | 0 | processConstraint(results, field, objStruct, o, dataProvider, elementStack, rootData, rootObjectStructure); |
| 253 | |
|
| 254 | |
|
| 255 | 0 | elementStack.pop(); |
| 256 | 0 | i++; |
| 257 | |
} |
| 258 | |
|
| 259 | 0 | String xPath = getElementXpath(elementStack) + "/" + field.getName() + "/*"; |
| 260 | 0 | if (field.getMinOccurs() != null && field.getMinOccurs() > ((Collection<?>) value).size()) { |
| 261 | 0 | ValidationResultInfo valRes = new ValidationResultInfo(xPath, value); |
| 262 | 0 | valRes.setError(MessageUtils.interpolate(getMessage("validation.minOccurs"), toMap(field))); |
| 263 | 0 | results.add(valRes); |
| 264 | |
} |
| 265 | |
|
| 266 | 0 | Integer maxOccurs = tryParse(field.getMaxOccurs()); |
| 267 | 0 | if (maxOccurs != null && maxOccurs < ((Collection<?>) value).size()) { |
| 268 | 0 | ValidationResultInfo valRes = new ValidationResultInfo(xPath, value); |
| 269 | 0 | valRes.setError(MessageUtils.interpolate(getMessage("validation.maxOccurs"), toMap(field))); |
| 270 | 0 | results.add(valRes); |
| 271 | |
} |
| 272 | 0 | } else { |
| 273 | 0 | processConstraint(results, field, objStruct, value, dataProvider, elementStack, rootData, rootObjectStructure); |
| 274 | |
} |
| 275 | |
|
| 276 | |
} |
| 277 | 0 | return results; |
| 278 | |
} |
| 279 | |
|
| 280 | |
protected Integer tryParse(String s) { |
| 281 | 0 | Integer result = null; |
| 282 | 0 | if (s != null) { |
| 283 | |
try { |
| 284 | 0 | result = Integer.valueOf(s); |
| 285 | 0 | } catch (NumberFormatException e) { |
| 286 | |
|
| 287 | 0 | } |
| 288 | |
} |
| 289 | 0 | return result; |
| 290 | |
} |
| 291 | |
|
| 292 | |
protected void processNestedObjectStructure(List<ValidationResultInfo> results, Object value, ObjectStructureDefinition nestedObjStruct, FieldDefinition field, Stack<String> elementStack, Object rootData, ObjectStructureDefinition rootObjStructure) { |
| 293 | |
|
| 294 | 0 | results.addAll(validateObject(value, nestedObjStruct, elementStack, rootData, rootObjStructure, false)); |
| 295 | |
|
| 296 | 0 | } |
| 297 | |
|
| 298 | |
protected void processConstraint(List<ValidationResultInfo> valResults, FieldDefinition field, ObjectStructureDefinition objStructure, Object value, ConstraintDataProvider dataProvider, Stack<String> elementStack, Object rootData, ObjectStructureDefinition rootObjStructure) { |
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | 0 | Constraint caseConstraint = processCaseConstraint(valResults, field, objStructure, value, dataProvider, elementStack, rootData, rootObjStructure); |
| 303 | |
|
| 304 | 0 | Constraint constraint = (null != caseConstraint) ? caseConstraint : field; |
| 305 | |
|
| 306 | 0 | processBaseConstraints(valResults, constraint, field.getDataType(), field.getName(), value, elementStack); |
| 307 | |
|
| 308 | |
|
| 309 | 0 | if (value == null || "".equals(value.toString().trim())) { |
| 310 | 0 | return; |
| 311 | |
} |
| 312 | |
|
| 313 | 0 | String elementPath = getElementXpath(elementStack) + "/" + field.getName(); |
| 314 | |
|
| 315 | |
|
| 316 | 0 | if (null != constraint.getValidChars()) { |
| 317 | 0 | ValidationResultInfo val = processValidCharConstraint(elementPath, constraint.getValidChars(), dataProvider, value); |
| 318 | 0 | if (null != val) { |
| 319 | 0 | valResults.add(val); |
| 320 | |
} |
| 321 | |
} |
| 322 | |
|
| 323 | |
|
| 324 | 0 | if (value != null && !"".equals(value.toString().trim())) { |
| 325 | 0 | if (null != constraint.getRequireConstraint() && constraint.getRequireConstraint().size() > 0) { |
| 326 | 0 | for (RequiredConstraint rc : constraint.getRequireConstraint()) { |
| 327 | 0 | ValidationResultInfo val = processRequireConstraint(elementPath, rc, field, objStructure, dataProvider); |
| 328 | 0 | if (null != val) { |
| 329 | 0 | valResults.add(val); |
| 330 | |
} |
| 331 | 0 | } |
| 332 | |
} |
| 333 | |
} |
| 334 | |
|
| 335 | |
|
| 336 | 0 | if (null != constraint.getOccursConstraint() && constraint.getOccursConstraint().size() > 0) { |
| 337 | 0 | for (MustOccurConstraint oc : constraint.getOccursConstraint()) { |
| 338 | 0 | ValidationResultInfo val = processOccursConstraint(elementPath, oc, field, objStructure, dataProvider); |
| 339 | 0 | if (null != val) { |
| 340 | 0 | valResults.add(val); |
| 341 | |
} |
| 342 | 0 | } |
| 343 | |
} |
| 344 | |
|
| 345 | |
|
| 346 | 0 | if (null != constraint.getLookupDefinition()) { |
| 347 | 0 | processLookupConstraint(valResults, constraint.getLookupDefinition(), field, elementStack, dataProvider); |
| 348 | |
} |
| 349 | 0 | } |
| 350 | |
|
| 351 | |
protected ValidationResultInfo processRequireConstraint(String element, RequiredConstraint constraint, FieldDefinition field, ObjectStructureDefinition objStructure, ConstraintDataProvider dataProvider) { |
| 352 | |
|
| 353 | 0 | ValidationResultInfo val = null; |
| 354 | |
|
| 355 | 0 | String fieldName = constraint.getFieldPath(); |
| 356 | 0 | Object fieldValue = dataProvider.getValue(fieldName); |
| 357 | |
|
| 358 | 0 | boolean result = true; |
| 359 | |
|
| 360 | 0 | if (fieldValue instanceof java.lang.String) { |
| 361 | 0 | result = hasText((String) fieldValue); |
| 362 | 0 | } else if (fieldValue instanceof Collection) { |
| 363 | 0 | result = (((Collection<?>) fieldValue).size() > 0); |
| 364 | |
} else { |
| 365 | 0 | result = (null != fieldValue) ? true : false; |
| 366 | |
} |
| 367 | |
|
| 368 | 0 | if (!result) { |
| 369 | 0 | Map<String, Object> rMap = new HashMap<String, Object>(); |
| 370 | 0 | rMap.put("field1", field.getName()); |
| 371 | 0 | rMap.put("field2", fieldName); |
| 372 | 0 | val = new ValidationResultInfo(element, fieldValue); |
| 373 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.requiresField"), rMap)); |
| 374 | |
} |
| 375 | |
|
| 376 | 0 | return val; |
| 377 | |
} |
| 378 | |
|
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
|
| 386 | |
protected Constraint processCaseConstraint(List<ValidationResultInfo> valResults, FieldDefinition field, ObjectStructureDefinition objStructure, Object value, ConstraintDataProvider dataProvider, Stack<String> elementStack, Object rootData, ObjectStructureDefinition rootObjStructure) { |
| 387 | |
|
| 388 | 0 | CaseConstraint constraint = field.getCaseConstraint(); |
| 389 | |
|
| 390 | 0 | if (null == constraint) { |
| 391 | 0 | return null; |
| 392 | |
} |
| 393 | |
|
| 394 | 0 | String operator = (hasText(constraint.getOperator())) ? constraint.getOperator() : "EQUALS"; |
| 395 | 0 | FieldDefinition caseField = null; |
| 396 | 0 | boolean absolutePath = false; |
| 397 | 0 | if(hasText(constraint.getFieldPath())){ |
| 398 | 0 | if(constraint.getFieldPath().startsWith("/")){ |
| 399 | 0 | absolutePath = true; |
| 400 | 0 | caseField = ValidatorUtils.getField(constraint.getFieldPath().substring(1), rootObjStructure); |
| 401 | |
}else{ |
| 402 | 0 | caseField = ValidatorUtils.getField(constraint.getFieldPath(), objStructure); |
| 403 | |
} |
| 404 | |
} |
| 405 | |
|
| 406 | |
|
| 407 | 0 | Object fieldValue = value; |
| 408 | 0 | if(caseField!=null){ |
| 409 | 0 | if(absolutePath){ |
| 410 | |
try { |
| 411 | 0 | if(caseField.isDynamic()){ |
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | 0 | Map<String,String> attributes = (Map<String,String>) PropertyUtils.getNestedProperty(rootData, "attributes"); |
| 416 | 0 | if(attributes!=null){ |
| 417 | 0 | fieldValue = attributes.get(constraint.getFieldPath().substring(1)); |
| 418 | |
} |
| 419 | 0 | }else{ |
| 420 | 0 | fieldValue = PropertyUtils.getNestedProperty(rootData, constraint.getFieldPath().substring(1)); |
| 421 | |
} |
| 422 | 0 | } catch (IllegalAccessException e) { |
| 423 | 0 | } catch (InvocationTargetException e) { |
| 424 | 0 | } catch (NoSuchMethodException e) { |
| 425 | 0 | } |
| 426 | |
}else{ |
| 427 | 0 | fieldValue = dataProvider.getValue(caseField.getName()); |
| 428 | |
} |
| 429 | |
} |
| 430 | 0 | DataType fieldDataType = (null != caseField ? caseField.getDataType():null); |
| 431 | |
|
| 432 | |
|
| 433 | 0 | if(null == fieldValue) { |
| 434 | 0 | return null; |
| 435 | |
} |
| 436 | |
|
| 437 | |
|
| 438 | 0 | for (WhenConstraint wc : constraint.getWhenConstraint()) { |
| 439 | |
|
| 440 | 0 | if(hasText(wc.getValuePath())){ |
| 441 | 0 | Object whenValue = null; |
| 442 | 0 | if(wc.getValuePath().startsWith("/")){ |
| 443 | |
try { |
| 444 | 0 | whenValue = PropertyUtils.getNestedProperty(rootData, wc.getValuePath().substring(1)); |
| 445 | 0 | } catch (IllegalAccessException e) { |
| 446 | 0 | } catch (InvocationTargetException e) { |
| 447 | 0 | } catch (NoSuchMethodException e) { |
| 448 | 0 | } |
| 449 | |
}else{ |
| 450 | 0 | whenValue = dataProvider.getValue(wc.getValuePath()); |
| 451 | |
} |
| 452 | 0 | if (ValidatorUtils.compareValues(fieldValue, whenValue, fieldDataType, operator, constraint.isCaseSensitive(), dateParser) && null != wc.getConstraint()) { |
| 453 | 0 | return wc.getConstraint(); |
| 454 | |
} |
| 455 | 0 | }else{ |
| 456 | 0 | List<Object> whenValueList = wc.getValues(); |
| 457 | |
|
| 458 | 0 | for (Object whenValue : whenValueList) { |
| 459 | 0 | if (ValidatorUtils.compareValues(fieldValue, whenValue, fieldDataType, operator, constraint.isCaseSensitive(), dateParser) && null != wc.getConstraint()) { |
| 460 | 0 | return wc.getConstraint(); |
| 461 | |
} |
| 462 | |
} |
| 463 | 0 | } |
| 464 | |
} |
| 465 | |
|
| 466 | 0 | return null; |
| 467 | |
} |
| 468 | |
|
| 469 | |
public ValidationResultInfo processValidCharConstraint(String element, ValidCharsConstraint vcConstraint, ConstraintDataProvider dataProvider, Object value) { |
| 470 | |
|
| 471 | 0 | ValidationResultInfo val = null; |
| 472 | |
|
| 473 | 0 | StringBuilder fieldValue = new StringBuilder(); |
| 474 | 0 | String validChars = vcConstraint.getValue(); |
| 475 | |
|
| 476 | 0 | fieldValue.append(ValidatorUtils.getString(value)); |
| 477 | |
|
| 478 | 0 | int typIdx = validChars.indexOf(":"); |
| 479 | 0 | String processorType = "regex"; |
| 480 | 0 | if (-1 == typIdx) { |
| 481 | 0 | validChars = "[" + validChars + "]*"; |
| 482 | |
} else { |
| 483 | 0 | processorType = validChars.substring(0, typIdx); |
| 484 | 0 | validChars = validChars.substring(typIdx + 1); |
| 485 | |
} |
| 486 | |
|
| 487 | 0 | if ("regex".equalsIgnoreCase(processorType)) { |
| 488 | 0 | if (fieldValue == null || !fieldValue.toString().matches(validChars)) { |
| 489 | 0 | val = new ValidationResultInfo(element, fieldValue); |
| 490 | 0 | if(vcConstraint.getLabelKey()!=null){ |
| 491 | 0 | val.setError(getMessage(vcConstraint.getLabelKey())); |
| 492 | |
}else{ |
| 493 | 0 | val.setError(getMessage("validation.validCharsFailed")); |
| 494 | |
} |
| 495 | |
} |
| 496 | |
} |
| 497 | |
|
| 498 | 0 | return val; |
| 499 | |
} |
| 500 | |
|
| 501 | |
|
| 502 | |
|
| 503 | |
|
| 504 | |
|
| 505 | |
|
| 506 | |
|
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | |
|
| 511 | |
|
| 512 | |
|
| 513 | |
protected ValidationResultInfo processOccursConstraint(String element, MustOccurConstraint constraint, FieldDefinition field, ObjectStructureDefinition objStructure, ConstraintDataProvider dataProvider) { |
| 514 | |
|
| 515 | 0 | boolean result = false; |
| 516 | 0 | int trueCount = 0; |
| 517 | |
|
| 518 | 0 | ValidationResultInfo val = null; |
| 519 | |
|
| 520 | 0 | for (RequiredConstraint rc : constraint.getRequiredFields()) { |
| 521 | 0 | trueCount += (processRequireConstraint("", rc, field, objStructure, dataProvider) != null) ? 1 : 0; |
| 522 | |
} |
| 523 | |
|
| 524 | 0 | for (MustOccurConstraint oc : constraint.getOccurs()) { |
| 525 | 0 | trueCount += (processOccursConstraint("", oc, field, objStructure, dataProvider) != null) ? 1 : 0; |
| 526 | |
} |
| 527 | |
|
| 528 | 0 | result = (trueCount >= constraint.getMin() && trueCount <= constraint.getMax()) ? true : false; |
| 529 | |
|
| 530 | 0 | if (!result) { |
| 531 | |
|
| 532 | 0 | val = new ValidationResultInfo(element, null); |
| 533 | 0 | val.setError(getMessage("validation.occurs")); |
| 534 | |
} |
| 535 | |
|
| 536 | 0 | return val; |
| 537 | |
} |
| 538 | |
|
| 539 | |
|
| 540 | |
protected void processLookupConstraint(List<ValidationResultInfo> valResults, LookupConstraint lookupConstraint, FieldDefinition field, Stack<String> elementStack, ConstraintDataProvider dataProvider) { |
| 541 | 0 | if (lookupConstraint == null) { |
| 542 | 0 | return; |
| 543 | |
} |
| 544 | |
|
| 545 | |
|
| 546 | 0 | List<SearchParam> params = new ArrayList<SearchParam>(); |
| 547 | 0 | Object fieldValue = null; |
| 548 | 0 | for (CommonLookupParam paramMapping : lookupConstraint.getParams()) { |
| 549 | 0 | SearchParam param = new SearchParam(); |
| 550 | |
|
| 551 | 0 | param.setKey(paramMapping.getKey()); |
| 552 | |
|
| 553 | |
|
| 554 | 0 | if (paramMapping.getFieldPath() != null && !paramMapping.getFieldPath().isEmpty()) { |
| 555 | 0 | fieldValue = dataProvider.getValue(paramMapping.getFieldPath()); |
| 556 | 0 | if (fieldValue instanceof String) { |
| 557 | 0 | param.setValue((String) fieldValue); |
| 558 | 0 | } else if (fieldValue instanceof List<?>) { |
| 559 | 0 | param.setValue((List<String>) fieldValue); |
| 560 | |
} |
| 561 | 0 | } else if (paramMapping.getDefaultValueString() != null) { |
| 562 | 0 | param.setValue(paramMapping.getDefaultValueString()); |
| 563 | |
} else { |
| 564 | 0 | param.setValue(paramMapping.getDefaultValueList()); |
| 565 | |
} |
| 566 | 0 | params.add(param); |
| 567 | 0 | } |
| 568 | |
|
| 569 | 0 | SearchRequest searchRequest = new SearchRequest(); |
| 570 | 0 | searchRequest.setMaxResults(1); |
| 571 | 0 | searchRequest.setStartAt(0); |
| 572 | 0 | searchRequest.setNeededTotalResults(false); |
| 573 | 0 | searchRequest.setSearchKey(lookupConstraint.getSearchTypeId()); |
| 574 | 0 | searchRequest.setParams(params); |
| 575 | |
|
| 576 | 0 | SearchResult searchResult = null; |
| 577 | |
try { |
| 578 | 0 | searchResult = searchDispatcher.dispatchSearch(searchRequest); |
| 579 | 0 | } catch (Exception e) { |
| 580 | 0 | LOG.info("Error calling Search", e); |
| 581 | 0 | } |
| 582 | 0 | if (searchResult == null || searchResult.getRows() == null || searchResult.getRows().isEmpty()) { |
| 583 | 0 | ValidationResultInfo val = new ValidationResultInfo(getElementXpath(elementStack) + "/" + field.getName(), fieldValue); |
| 584 | 0 | val.setError(getMessage("validation.lookup")); |
| 585 | 0 | valResults.add(val); |
| 586 | |
} |
| 587 | 0 | } |
| 588 | |
|
| 589 | |
protected void processBaseConstraints(List<ValidationResultInfo> valResults, Constraint constraint, DataType dataType, String name, Object value, Stack<String> elementStack) { |
| 590 | |
|
| 591 | 0 | if (value == null || "".equals(value.toString().trim())) { |
| 592 | 0 | if (constraint.getMinOccurs() != null && constraint.getMinOccurs() > 0) { |
| 593 | 0 | ValidationResultInfo val = new ValidationResultInfo(getElementXpath(elementStack) + "/" + name, value); |
| 594 | 0 | val.setError(getMessage("validation.required")); |
| 595 | 0 | valResults.add(val); |
| 596 | |
} |
| 597 | 0 | return; |
| 598 | |
} |
| 599 | |
|
| 600 | 0 | String elementPath = getElementXpath(elementStack) + "/" + name; |
| 601 | |
|
| 602 | 0 | if (DataType.STRING.equals(dataType)) { |
| 603 | 0 | validateString(value, constraint, elementPath, valResults); |
| 604 | 0 | } else if (DataType.INTEGER.equals(dataType)) { |
| 605 | 0 | validateInteger(value, constraint, elementPath, valResults); |
| 606 | 0 | } else if (DataType.LONG.equals(dataType)) { |
| 607 | 0 | validateLong(value, constraint, elementPath, valResults); |
| 608 | 0 | } else if (DataType.DOUBLE.equals(dataType)) { |
| 609 | 0 | validateDouble(value, constraint, elementPath, valResults); |
| 610 | 0 | } else if (DataType.FLOAT.equals(dataType)) { |
| 611 | 0 | validateFloat(value, constraint, elementPath, valResults); |
| 612 | 0 | } else if (DataType.BOOLEAN.equals(dataType)) { |
| 613 | 0 | validateBoolean(value, constraint, elementPath, valResults); |
| 614 | 0 | } else if (DataType.DATE.equals(dataType)) { |
| 615 | 0 | validateDate(value, constraint, elementPath, valResults, dateParser); |
| 616 | |
} |
| 617 | 0 | } |
| 618 | |
|
| 619 | |
protected void validateBoolean(Object value, Constraint constraint, String element, List<ValidationResultInfo> results) { |
| 620 | 0 | if (!(value instanceof Boolean)) { |
| 621 | |
try { |
| 622 | 0 | Boolean.valueOf(value.toString()); |
| 623 | 0 | } catch (Exception e) { |
| 624 | 0 | ValidationResultInfo val = new ValidationResultInfo(element, value); |
| 625 | 0 | val.setError(getMessage("validation.mustBeBoolean")); |
| 626 | 0 | results.add(val); |
| 627 | 0 | } |
| 628 | |
} |
| 629 | 0 | } |
| 630 | |
|
| 631 | |
protected void validateDouble(Object value, Constraint constraint, String element, List<ValidationResultInfo> results) { |
| 632 | 0 | Double v = null; |
| 633 | |
|
| 634 | 0 | ValidationResultInfo val = new ValidationResultInfo(element, value); |
| 635 | |
|
| 636 | 0 | if (value instanceof Number) { |
| 637 | 0 | v = ((Number) value).doubleValue(); |
| 638 | |
} else { |
| 639 | |
try { |
| 640 | 0 | v = Double.valueOf(value.toString()); |
| 641 | 0 | } catch (Exception e) { |
| 642 | 0 | val.setError(getMessage("validation.mustBeDouble")); |
| 643 | 0 | } |
| 644 | |
} |
| 645 | |
|
| 646 | 0 | if (val.isOk()) { |
| 647 | 0 | Double maxValue = ValidatorUtils.getDouble(constraint.getInclusiveMax()); |
| 648 | 0 | Double minValue = ValidatorUtils.getDouble(constraint.getExclusiveMin()); |
| 649 | |
|
| 650 | 0 | if (maxValue != null && minValue != null) { |
| 651 | |
|
| 652 | 0 | if (v > maxValue || v < minValue) { |
| 653 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.outOfRange"), toMap(constraint))); |
| 654 | |
} |
| 655 | 0 | } else if (maxValue != null) { |
| 656 | 0 | if (v > maxValue) { |
| 657 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.maxValueFailed"), toMap(constraint))); |
| 658 | |
} |
| 659 | 0 | } else if (minValue != null) { |
| 660 | 0 | if (v < minValue) { |
| 661 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.minValueFailed"), toMap(constraint))); |
| 662 | |
} |
| 663 | |
} |
| 664 | |
} |
| 665 | |
|
| 666 | 0 | if (!val.isOk()) { |
| 667 | 0 | results.add(val); |
| 668 | |
} |
| 669 | 0 | } |
| 670 | |
|
| 671 | |
protected void validateFloat(Object value, Constraint constraint, String element, List<ValidationResultInfo> results) { |
| 672 | 0 | Float v = null; |
| 673 | |
|
| 674 | 0 | ValidationResultInfo val = new ValidationResultInfo(element, value); |
| 675 | 0 | if (value instanceof Number) { |
| 676 | 0 | v = ((Number) value).floatValue(); |
| 677 | |
} else { |
| 678 | |
try { |
| 679 | 0 | v = Float.valueOf(value.toString()); |
| 680 | 0 | } catch (Exception e) { |
| 681 | 0 | val.setError(getMessage("validation.mustBeFloat")); |
| 682 | 0 | } |
| 683 | |
} |
| 684 | |
|
| 685 | 0 | if (val.isOk()) { |
| 686 | 0 | Float maxValue = ValidatorUtils.getFloat(constraint.getInclusiveMax()); |
| 687 | 0 | Float minValue = ValidatorUtils.getFloat(constraint.getExclusiveMin()); |
| 688 | |
|
| 689 | 0 | if (maxValue != null && minValue != null) { |
| 690 | |
|
| 691 | 0 | if (v > maxValue || v < minValue) { |
| 692 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.outOfRange"), toMap(constraint))); |
| 693 | |
} |
| 694 | 0 | } else if (maxValue != null) { |
| 695 | 0 | if (v > maxValue) { |
| 696 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.maxValueFailed"), toMap(constraint))); |
| 697 | |
} |
| 698 | 0 | } else if (minValue != null) { |
| 699 | 0 | if (v < minValue) { |
| 700 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.minValueFailed"), toMap(constraint))); |
| 701 | |
} |
| 702 | |
} |
| 703 | |
} |
| 704 | |
|
| 705 | 0 | if (!val.isOk()) { |
| 706 | 0 | results.add(val); |
| 707 | |
} |
| 708 | 0 | } |
| 709 | |
|
| 710 | |
protected void validateLong(Object value, Constraint constraint, String element, List<ValidationResultInfo> results) { |
| 711 | 0 | Long v = null; |
| 712 | |
|
| 713 | 0 | ValidationResultInfo val = new ValidationResultInfo(element, value); |
| 714 | 0 | if (value instanceof Number) { |
| 715 | 0 | v = ((Number) value).longValue(); |
| 716 | |
} else { |
| 717 | |
try { |
| 718 | 0 | v = Long.valueOf(value.toString()); |
| 719 | 0 | } catch (Exception e) { |
| 720 | 0 | val.setError(getMessage("validation.mustBeLong")); |
| 721 | 0 | } |
| 722 | |
} |
| 723 | |
|
| 724 | 0 | if (val.isOk()) { |
| 725 | 0 | Long maxValue = ValidatorUtils.getLong(constraint.getInclusiveMax()); |
| 726 | 0 | Long minValue = ValidatorUtils.getLong(constraint.getExclusiveMin()); |
| 727 | |
|
| 728 | 0 | if (maxValue != null && minValue != null) { |
| 729 | |
|
| 730 | 0 | if (v > maxValue || v < minValue) { |
| 731 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.outOfRange"), toMap(constraint))); |
| 732 | |
} |
| 733 | 0 | } else if (maxValue != null) { |
| 734 | 0 | if (v > maxValue) { |
| 735 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.maxValueFailed"), toMap(constraint))); |
| 736 | |
} |
| 737 | 0 | } else if (minValue != null) { |
| 738 | 0 | if (v < minValue) { |
| 739 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.minValueFailed"), toMap(constraint))); |
| 740 | |
} |
| 741 | |
} |
| 742 | |
} |
| 743 | |
|
| 744 | 0 | if (!val.isOk()) { |
| 745 | 0 | results.add(val); |
| 746 | |
} |
| 747 | |
|
| 748 | 0 | } |
| 749 | |
|
| 750 | |
protected void validateInteger(Object value, Constraint constraint, String element, List<ValidationResultInfo> results) { |
| 751 | 0 | Integer v = null; |
| 752 | |
|
| 753 | 0 | ValidationResultInfo val = new ValidationResultInfo(element, value); |
| 754 | |
|
| 755 | 0 | if (value instanceof Number) { |
| 756 | 0 | v = ((Number) value).intValue(); |
| 757 | |
} else { |
| 758 | |
try { |
| 759 | 0 | v = Integer.valueOf(value.toString()); |
| 760 | 0 | } catch (Exception e) { |
| 761 | 0 | val.setError(getMessage("validation.mustBeInteger")); |
| 762 | 0 | } |
| 763 | |
} |
| 764 | |
|
| 765 | 0 | if (val.isOk()) { |
| 766 | 0 | Integer maxValue = ValidatorUtils.getInteger(constraint.getInclusiveMax()); |
| 767 | 0 | Integer minValue = ValidatorUtils.getInteger(constraint.getExclusiveMin()); |
| 768 | |
|
| 769 | 0 | if (maxValue != null && minValue != null) { |
| 770 | |
|
| 771 | 0 | if (v > maxValue || v < minValue) { |
| 772 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.outOfRange"), toMap(constraint))); |
| 773 | |
} |
| 774 | 0 | } else if (maxValue != null) { |
| 775 | 0 | if (v > maxValue) { |
| 776 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.maxValueFailed"), toMap(constraint))); |
| 777 | |
} |
| 778 | 0 | } else if (minValue != null) { |
| 779 | 0 | if (v < minValue) { |
| 780 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.minValueFailed"), toMap(constraint))); |
| 781 | |
} |
| 782 | |
} |
| 783 | |
} |
| 784 | |
|
| 785 | 0 | if (!val.isOk()) { |
| 786 | 0 | results.add(val); |
| 787 | |
} |
| 788 | 0 | } |
| 789 | |
|
| 790 | |
protected void validateDate(Object value, Constraint constraint, String element, List<ValidationResultInfo> results, DateParser dateParser) { |
| 791 | 0 | ValidationResultInfo val = new ValidationResultInfo(element, value); |
| 792 | |
|
| 793 | 0 | Date v = null; |
| 794 | |
|
| 795 | 0 | if (value instanceof Date) { |
| 796 | 0 | v = (Date) value; |
| 797 | |
} else { |
| 798 | |
try { |
| 799 | 0 | v = dateParser.parseDate(value.toString()); |
| 800 | 0 | } catch (Exception e) { |
| 801 | 0 | val.setError(getMessage("validation.mustBeDate")); |
| 802 | 0 | } |
| 803 | |
} |
| 804 | |
|
| 805 | 0 | if (val.isOk()) { |
| 806 | 0 | Date maxValue = ValidatorUtils.getDate(constraint.getInclusiveMax(), dateParser); |
| 807 | 0 | Date minValue = ValidatorUtils.getDate(constraint.getExclusiveMin(), dateParser); |
| 808 | |
|
| 809 | 0 | if (maxValue != null && minValue != null) { |
| 810 | |
|
| 811 | 0 | if (v.getTime() > maxValue.getTime() || v.getTime() < minValue.getTime()) { |
| 812 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.outOfRange"), toMap(constraint))); |
| 813 | |
} |
| 814 | 0 | } else if (maxValue != null) { |
| 815 | 0 | if (v.getTime() > maxValue.getTime()) { |
| 816 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.maxValueFailed"), toMap(constraint))); |
| 817 | |
} |
| 818 | 0 | } else if (minValue != null) { |
| 819 | 0 | if (v.getTime() < minValue.getTime()) { |
| 820 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.minValueFailed"), toMap(constraint))); |
| 821 | |
} |
| 822 | |
} |
| 823 | |
} |
| 824 | |
|
| 825 | 0 | if (!val.isOk()) { |
| 826 | 0 | results.add(val); |
| 827 | |
} |
| 828 | 0 | } |
| 829 | |
|
| 830 | |
protected void validateString(Object value, Constraint constraint, String element, List<ValidationResultInfo> results) { |
| 831 | |
|
| 832 | 0 | if (value == null) { |
| 833 | 0 | value = ""; |
| 834 | |
} |
| 835 | 0 | String s = value.toString().trim(); |
| 836 | |
|
| 837 | 0 | ValidationResultInfo val = new ValidationResultInfo(element, value); |
| 838 | |
|
| 839 | 0 | Integer maxLength = tryParse(constraint.getMaxLength()); |
| 840 | 0 | if (maxLength != null && constraint.getMinLength() != null && constraint.getMinLength() > 0) { |
| 841 | 0 | if (s.length() > maxLength || s.length() < constraint.getMinLength()) { |
| 842 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.lengthOutOfRange"), toMap(constraint))); |
| 843 | |
} |
| 844 | 0 | } else if (maxLength != null) { |
| 845 | 0 | if (s.length() > Integer.parseInt(constraint.getMaxLength())) { |
| 846 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.maxLengthFailed"), toMap(constraint))); |
| 847 | |
} |
| 848 | 0 | } else if (constraint.getMinLength() != null && constraint.getMinLength() > 0) { |
| 849 | 0 | if (s.length() < constraint.getMinLength()) { |
| 850 | 0 | val.setError(MessageUtils.interpolate(getMessage("validation.minLengthFailed"), toMap(constraint))); |
| 851 | |
} |
| 852 | |
} |
| 853 | |
|
| 854 | 0 | if (!val.isOk()) { |
| 855 | 0 | results.add(val); |
| 856 | |
} |
| 857 | 0 | } |
| 858 | |
|
| 859 | |
protected String getMessage(String messageId) { |
| 860 | 0 | if (null == messageService) { |
| 861 | 0 | return messageId; |
| 862 | |
} |
| 863 | |
|
| 864 | 0 | Message msg = messageService.getMessage(messageLocaleKey, messageGroupKey, messageId); |
| 865 | |
|
| 866 | 0 | return msg.getValue(); |
| 867 | |
} |
| 868 | |
|
| 869 | |
protected String getElementXpath(Stack<String> elementStack) { |
| 870 | 0 | StringBuilder xPath = new StringBuilder(); |
| 871 | 0 | Iterator<String> itr = elementStack.iterator(); |
| 872 | 0 | while (itr.hasNext()) { |
| 873 | 0 | xPath.append(itr.next()); |
| 874 | 0 | if(itr.hasNext()){ |
| 875 | 0 | xPath.append("/"); |
| 876 | |
} |
| 877 | |
} |
| 878 | |
|
| 879 | 0 | return xPath.toString(); |
| 880 | |
} |
| 881 | |
|
| 882 | |
|
| 883 | |
|
| 884 | |
|
| 885 | |
protected boolean hasText(String string) { |
| 886 | |
|
| 887 | 0 | if (string == null || string.length() < 1) { |
| 888 | 0 | return false; |
| 889 | |
} |
| 890 | 0 | int stringLength = string.length(); |
| 891 | |
|
| 892 | 0 | for (int i = 0; i < stringLength; i++) { |
| 893 | 0 | char currentChar = string.charAt(i); |
| 894 | 0 | if (' ' != currentChar || '\t' != currentChar || '\n' != currentChar) { |
| 895 | 0 | return true; |
| 896 | |
} |
| 897 | |
} |
| 898 | |
|
| 899 | 0 | return false; |
| 900 | |
} |
| 901 | |
|
| 902 | |
protected Map<String, Object> toMap(Constraint c) { |
| 903 | 0 | Map<String, Object> result = new HashMap<String, Object>(); |
| 904 | 0 | result.put("minOccurs", c.getMinOccurs()); |
| 905 | 0 | result.put("maxOccurs", c.getMaxOccurs()); |
| 906 | 0 | result.put("minLength", c.getMinLength()); |
| 907 | 0 | result.put("maxLength", c.getMaxLength()); |
| 908 | 0 | result.put("minValue", c.getExclusiveMin()); |
| 909 | 0 | result.put("maxValue", c.getInclusiveMax()); |
| 910 | |
|
| 911 | |
|
| 912 | 0 | return result; |
| 913 | |
} |
| 914 | |
|
| 915 | |
public SearchDispatcher getSearchDispatcher() { |
| 916 | 0 | return searchDispatcher; |
| 917 | |
} |
| 918 | |
|
| 919 | |
public void setSearchDispatcher(SearchDispatcher searchDispatcher) { |
| 920 | 0 | this.searchDispatcher = searchDispatcher; |
| 921 | 0 | } |
| 922 | |
|
| 923 | |
@Override |
| 924 | |
public List<ValidationResultInfo> validateObject(FieldDefinition field, |
| 925 | |
Object o, ObjectStructureDefinition objStructure,Stack<String> elementStack) { |
| 926 | 0 | return null; |
| 927 | |
} |
| 928 | |
} |