/core/src/main/java/org/javabuilders/handler/validation/BuilderValidators.java

http://github.com/jacek99/javabuilders · Java · 503 lines · 280 code · 78 blank · 145 comment · 40 complexity · 38a89c7d34fdbcfd4e37440545af12b5 MD5 · raw file

  1. package org.javabuilders.handler.validation;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import org.javabuilders.BuildException;
  5. import org.javabuilders.BuildResult;
  6. import org.javabuilders.handler.validation.validator.DateFormatValidator;
  7. import org.javabuilders.handler.validation.validator.EmailAddressValidator;
  8. import org.javabuilders.handler.validation.validator.MandatoryValidator;
  9. import org.javabuilders.handler.validation.validator.MaxLengthValidator;
  10. import org.javabuilders.handler.validation.validator.MaxValueValidator;
  11. import org.javabuilders.handler.validation.validator.MinLengthValidator;
  12. import org.javabuilders.handler.validation.validator.MinValueValidator;
  13. import org.javabuilders.handler.validation.validator.RegexValidator;
  14. import org.javabuilders.handler.validation.validator.TypeValidator;
  15. import org.javabuilders.util.PropertyUtils;
  16. /**
  17. * A collection of validators for a build file
  18. * @author Jacek Furmankiewicz
  19. */
  20. @SuppressWarnings("serial")
  21. public class BuilderValidators extends LinkedList<PropertyValidations> {
  22. private BuildResult result;
  23. private static String defaultMandatoryMessage = "message.error.mandatory";
  24. private static String defaultMinLengthMessage = "message.error.minLength";
  25. private static String defaultMaxLengthMessage = "message.error.maxLength";
  26. private static String defaultDateFormatMessage = "message.error.dateFormat";
  27. private static String defaultMinValueMessage = "message.error.minValue";
  28. private static String defaultMaxValueMessage = "message.error.maxValue";
  29. private static String defaultNumericMessage = "message.error.numeric";
  30. private static String defaultEmailAddressMessage = "message.error.emailAddress";
  31. private static String defaultRegexMessage = "message.error.regex";
  32. private String mandatoryMessage = defaultMandatoryMessage;
  33. private String minLengthMessage = defaultMinLengthMessage;
  34. private String maxLengthMessage = defaultMaxLengthMessage;
  35. private String dateFormatMessage = defaultDateFormatMessage;
  36. private String minValueMessage = defaultMinValueMessage;
  37. private String maxValueMessage = defaultMaxValueMessage;
  38. private String numericMessage = defaultNumericMessage;
  39. private String emailAddressMessage = defaultEmailAddressMessage;
  40. private String regexMessage = defaultRegexMessage;
  41. private List<IValidator> validators = new LinkedList<IValidator>();
  42. private boolean validatorsCreated = false;
  43. /**
  44. * @param result Current build result
  45. */
  46. public BuilderValidators(BuildResult result) {
  47. this.result = result;
  48. }
  49. /**
  50. * Validates the properties as per the property validators
  51. * @param validationMessageHandler Current validation message handler
  52. */
  53. public ValidationMessageList getValidationMessages(IValidationMessageHandler validationMessageHandler) {
  54. ValidationMessageList list = new ValidationMessageList();
  55. createValidators(validationMessageHandler);
  56. for(PropertyValidations validator : this) {
  57. Object namedObject = result.get(validator.getProperty().getName());
  58. Object value;
  59. boolean isEmptyValue = false;
  60. try {
  61. value = PropertyUtils.getNestedProperty(namedObject, validator.getProperty().getPropertyExpression());
  62. if (value == null || String.valueOf(value).trim().length() == 0) {
  63. isEmptyValue = true;
  64. }
  65. } catch (Exception e) {
  66. throw new BuildException("Error while processing validator for \"{0}\": {1}",
  67. namedObject, e);
  68. }
  69. //validate standard validators first
  70. boolean isMandatory = (getValidator(validator.getProperty().getName(),
  71. validator.getProperty().getPropertyExpression(),MandatoryValidator.class) != null);
  72. //do not run the validatons if value is empty and mandatory validation is not activated
  73. if (isMandatory || !isEmptyValue) {
  74. for(IValidator routine : getValidators()) {
  75. if (routine instanceof IPropertyValidator) {
  76. IPropertyValidator pRoutine = (IPropertyValidator) routine;
  77. if (pRoutine.getProperty().equals(validator.getProperty())) {
  78. pRoutine.validate(value, list);
  79. }
  80. }
  81. }
  82. }
  83. }
  84. //execute the custom validator routines last
  85. for(IValidator routine : getValidators()) {
  86. if (routine instanceof ICustomValidator) {
  87. ((ICustomValidator)routine).validate(list);
  88. }
  89. }
  90. return list;
  91. }
  92. /**
  93. * @return the default Mandatory Message
  94. */
  95. public static String getDefaultMandatoryMessage() {
  96. return defaultMandatoryMessage;
  97. }
  98. /**
  99. * @param defaultMandatoryMessage the default Mandatory Message to set
  100. */
  101. public static void setDefaultMandatoryMessage(String defaultMandatoryMessage) {
  102. BuilderValidators.defaultMandatoryMessage = defaultMandatoryMessage;
  103. }
  104. /**
  105. * @return the mandatory Message
  106. */
  107. public String getMandatoryMessage() {
  108. return mandatoryMessage;
  109. }
  110. /**
  111. * @param mandatoryMessage the mandatory message to set (must have a {0} parameter for the field label)
  112. */
  113. public BuilderValidators setMandatoryMessage(String mandatoryMessage) {
  114. this.mandatoryMessage = mandatoryMessage;
  115. return this;
  116. }
  117. /**
  118. * @return the minLength message
  119. */
  120. public String getMinLengthMessage() {
  121. return minLengthMessage;
  122. }
  123. /**
  124. * @param minLengthMessage the minLength message to set (must have {0} for field label and {1} for min length value)
  125. */
  126. public BuilderValidators setMinLengthMessage(String minLengthMessage) {
  127. this.minLengthMessage = minLengthMessage;
  128. return this;
  129. }
  130. /**
  131. * @return the max length message
  132. */
  133. public String getMaxLengthMessage() {
  134. return maxLengthMessage;
  135. }
  136. /**
  137. * @param maxLengthMessage the maxLength message to set (must have {0} for field label and {1} for min length value)
  138. */
  139. public BuilderValidators setMaxLengthMessage(String maxLengthMessage) {
  140. this.maxLengthMessage = maxLengthMessage;
  141. return this;
  142. }
  143. /**
  144. * @return the default Min Length Message
  145. */
  146. public static String getDefaultMinLengthMessage() {
  147. return defaultMinLengthMessage;
  148. }
  149. /**
  150. * @param defaultMinLengthMessage the default Min Length Message to set
  151. */
  152. public static void setDefaultMinLengthMessage(String defaultMinLengthMessage) {
  153. BuilderValidators.defaultMinLengthMessage = defaultMinLengthMessage;
  154. }
  155. /**
  156. * @return the default Max Length Message
  157. */
  158. public static String getDefaultMaxLengthMessage() {
  159. return defaultMaxLengthMessage;
  160. }
  161. /**
  162. * @param defaultMaxLengthMessage the default Max Length Message to set
  163. */
  164. public static void setDefaultMaxLengthMessage(String defaultMaxLengthMessage) {
  165. BuilderValidators.defaultMaxLengthMessage = defaultMaxLengthMessage;
  166. }
  167. /**
  168. * @return the defaultDateFormatMessage
  169. */
  170. public static String getDefaultDateFormatMessage() {
  171. return defaultDateFormatMessage;
  172. }
  173. /**
  174. * @param defaultDateFormatMessage the defaultDateFormatMessage to set
  175. */
  176. public static void setDefaultDateFormatMessage(String defaultDateFormatMessage) {
  177. BuilderValidators.defaultDateFormatMessage = defaultDateFormatMessage;
  178. }
  179. /**
  180. * @return the dateFormatMessage
  181. */
  182. public String getDateFormatMessage() {
  183. return dateFormatMessage;
  184. }
  185. /**
  186. * @param dateFormatMessage the dateFormatMessage to set
  187. */
  188. public BuilderValidators setDateFormatMessage(String dateFormatMessage) {
  189. this.dateFormatMessage = dateFormatMessage;
  190. return this;
  191. }
  192. /**
  193. * @return the defaultNumericMessage
  194. */
  195. public static String getDefaultNumericMessage() {
  196. return defaultNumericMessage;
  197. }
  198. /**
  199. * @param defaultNumericMessage the defaultNumericMessage to set
  200. */
  201. public static void setDefaultNumericMessage(String defaultNumericMessage) {
  202. BuilderValidators.defaultNumericMessage = defaultNumericMessage;
  203. }
  204. /**
  205. * @return the numericMessage
  206. */
  207. public String getNumericMessage() {
  208. return numericMessage;
  209. }
  210. /**
  211. * @param numericMessage the numericMessage to set
  212. */
  213. public BuilderValidators setNumericMessage(String numericMessage) {
  214. this.numericMessage = numericMessage;
  215. return this;
  216. }
  217. /**
  218. * @return List of custom validators
  219. */
  220. public List<IValidator> getValidators() {
  221. return validators;
  222. }
  223. /**
  224. * Adds a custom validator
  225. * @param validator Custom validator
  226. * @return This
  227. */
  228. public BuilderValidators add(ICustomValidator validator) {
  229. getValidators().add(validator);
  230. return this;
  231. }
  232. /**
  233. * Adds a standard property validator
  234. * @param validator Property validator
  235. * @return This
  236. */
  237. public BuilderValidators add(IPropertyValidator validator) {
  238. getValidators().add(validator);
  239. return this;
  240. }
  241. //creates the validators
  242. private void createValidators(IValidationMessageHandler validationMessageHandler) {
  243. if (!validatorsCreated) {
  244. for(PropertyValidations validator : this) {
  245. Object namedObject = result.get(validator.getProperty().getName());
  246. String parsedLabel = validator.getLabel();
  247. if (parsedLabel == null || parsedLabel.length() == 0) {
  248. //no label defined .... try to get it in a domain-specific way
  249. parsedLabel = validationMessageHandler.getNamedObjectLabel(namedObject);
  250. } else {
  251. //attempt to localize it
  252. parsedLabel = result.getResource(parsedLabel);
  253. }
  254. if (validator.isMandatory()) {
  255. MandatoryValidator v = new MandatoryValidator(validator.getProperty(),parsedLabel,
  256. getMandatoryMessage(),result);
  257. validators.add(v);
  258. }
  259. if (validator.getType() != null) {
  260. TypeValidator v = new TypeValidator(validator.getProperty(),parsedLabel,
  261. null, result, validator.getType());
  262. validators.add(v);
  263. }
  264. if (validator.getMinLength() != null) {
  265. MinLengthValidator v = new MinLengthValidator(validator.getProperty(),parsedLabel,
  266. getMinLengthMessage(),result, validator.getMinLength());
  267. validators.add(v);
  268. }
  269. if (validator.getMaxLength() != null) {
  270. MaxLengthValidator v = new MaxLengthValidator(validator.getProperty(),parsedLabel,
  271. getMaxLengthMessage(),result, validator.getMaxLength());
  272. validators.add(v);
  273. }
  274. if (validator.getDateFormat() != null) {
  275. DateFormatValidator v = new DateFormatValidator(validator.getProperty(),parsedLabel,
  276. getDateFormatMessage(),result, validator.getDateFormat(),
  277. validator.getLocaleInstance());
  278. validators.add(v);
  279. }
  280. if (validator.isEmailAddress()) {
  281. EmailAddressValidator v = new EmailAddressValidator(validator.getProperty(),
  282. parsedLabel,
  283. getEmailAddressMessage(),result);
  284. validators.add(v);
  285. }
  286. if (validator.getRegex() != null) {
  287. String msgFormat = (validator.getRegexMessage() == null) ? getRegexMessage() : validator.getRegexMessage();
  288. RegexValidator v = new RegexValidator(validator.getProperty(),parsedLabel,
  289. msgFormat, result, validator.getRegex());
  290. validators.add(v);
  291. }
  292. if (validator.getMinValue() != null) {
  293. MinValueValidator v = new MinValueValidator(validator.getProperty(),parsedLabel,
  294. getMinValueMessage(),result,validator.getMinValue());
  295. validators.add(v);
  296. }
  297. if (validator.getMaxValue() != null) {
  298. MaxValueValidator v = new MaxValueValidator(validator.getProperty(),parsedLabel,
  299. getMaxValueMessage(),result,validator.getMaxValue());
  300. validators.add(v);
  301. }
  302. }
  303. //create validators only one
  304. validatorsCreated = true;
  305. }
  306. }
  307. /**
  308. * @return the defaultEmailAddressMessage
  309. */
  310. public static String getDefaultEmailAddressMessage() {
  311. return defaultEmailAddressMessage;
  312. }
  313. /**
  314. * @param defaultEmailAddressMessage the defaultEmailAddressMessage to set
  315. */
  316. public static void setDefaultEmailAddressMessage(
  317. String defaultEmailAddressMessage) {
  318. BuilderValidators.defaultEmailAddressMessage = defaultEmailAddressMessage;
  319. }
  320. /**
  321. * @return the emailAddress message
  322. */
  323. public String getEmailAddressMessage() {
  324. return emailAddressMessage;
  325. }
  326. /**
  327. * @param emailAddress the emailAddress message to set
  328. */
  329. public void setEmailAddress(String emailAddressMessage) {
  330. this.emailAddressMessage = emailAddressMessage;
  331. }
  332. /**
  333. * @return the defaultRegexMessage
  334. */
  335. public static String getDefaultRegexMessage() {
  336. return defaultRegexMessage;
  337. }
  338. /**
  339. * @param defaultRegexMessage the defaultRegexMessage to set
  340. */
  341. public static void setDefaultRegexMessage(String defaultRegexMessage) {
  342. BuilderValidators.defaultRegexMessage = defaultRegexMessage;
  343. }
  344. /**
  345. * @return the regexMessage
  346. */
  347. public String getRegexMessage() {
  348. return regexMessage;
  349. }
  350. /**
  351. * @param regexMessage the regexMessage to set
  352. */
  353. public void setRegexMessage(String regexMessage) {
  354. this.regexMessage = regexMessage;
  355. }
  356. /**
  357. * @return the defaultMaxValueMessage
  358. */
  359. public static String getDefaultMaxValueMessage() {
  360. return defaultMaxValueMessage;
  361. }
  362. /**
  363. * @param defaultMaxValueMessage the defaultMaxValueMessage to set
  364. */
  365. public static void setDefaultMaxValueMessage(String defaultMaxValueMessage) {
  366. BuilderValidators.defaultMaxValueMessage = defaultMaxValueMessage;
  367. }
  368. /**
  369. * @return the defaultMinValueMessage
  370. */
  371. public static String getDefaultMinValueMessage() {
  372. return defaultMinValueMessage;
  373. }
  374. /**
  375. * @param defaultMinValueMessage the defaultMinValueMessage to set
  376. */
  377. public static void setDefaultMinValueMessage(String defaultMinValueMessage) {
  378. BuilderValidators.defaultMinValueMessage = defaultMinValueMessage;
  379. }
  380. /**
  381. * @return the minValueMessage
  382. */
  383. public String getMinValueMessage() {
  384. return minValueMessage;
  385. }
  386. /**
  387. * @param minValueMessage the minValueMessage to set
  388. */
  389. public void setMinValueMessage(String minValueMessage) {
  390. this.minValueMessage = minValueMessage;
  391. }
  392. /**
  393. * @return the maxValueMessage
  394. */
  395. public String getMaxValueMessage() {
  396. return maxValueMessage;
  397. }
  398. /**
  399. * @param maxValueMessage the maxValueMessage to set
  400. */
  401. public void setMaxValueMessage(String maxValueMessage) {
  402. this.maxValueMessage = maxValueMessage;
  403. }
  404. /**
  405. * Looks for a particular validator in the list
  406. * @param propertyName Property name
  407. * @param validatorType Exact class of validator
  408. * @return Found validator or null
  409. */
  410. public IValidator getValidator(String objectName, String propertyExpression, Class<?> validatorType ) {
  411. IValidator v = null;
  412. for(IValidator validator : getValidators()) {
  413. if (validator instanceof IPropertyValidator) {
  414. IPropertyValidator pv = (IPropertyValidator) validator;
  415. if (objectName.equals(pv.getProperty().getName()) &&
  416. propertyExpression.equals(pv.getProperty().getPropertyExpression()) &&
  417. validator.getClass().equals(validatorType)) {
  418. v = validator;
  419. break;
  420. }
  421. }
  422. }
  423. return v;
  424. }
  425. }