1 package org.mod4j.runtime.validation;
2
3 import org.springframework.util.Assert;
4 import org.springframework.validation.Errors;
5 import org.springframework.validation.ValidationUtils;
6
7 /**
8 * This class implements some common checks on fields. It uses the {@link ValidationUtils} class of the Spring framework
9 * to collect and report errors.
10 *
11 * @author Eric Jan Malotaux
12 *
13 * @deprecated
14 */
15 public class BusinessRuleUtils {
16
17 /**
18 * Reject the given field if the value is empty or just contains whitespace.
19 *
20 * An 'empty' value in this context means either null, the empty string "", or consisting wholly of whitespace.
21 *
22 * The object whose field is being validated does not need to be passed in because the Errors instance can resolve
23 * field values by itself (it will usually hold an internal reference to the target object).
24 *
25 * @param errors
26 * @param field
27 *
28 * @deprecated
29 */
30 public static void validateEmptyOrWhitespace(Errors errors, String field) {
31 ValidationUtils.rejectIfEmptyOrWhitespace(errors, field, "field.required");
32 }
33
34 /**
35 * Reject the given field if the string value is shorter than the minimum length given in <code>min</code>.
36 *
37 * The object whose field is being validated does not need to be passed in because the Errors instance can resolve
38 * field values by itself (it will usually hold an internal reference to the target object).
39 *
40 * @param errors
41 * @param field
42 * @param min
43 *
44 * @deprecated
45 */
46 public static void validateMinLength(Errors errors, String field, int min) {
47 String value = (String) errors.getFieldValue(field);
48 Assert.notNull(value);
49 int length = value.length();
50 if (length < min) {
51 errors.rejectValue(field, "field.length.min", new Integer[] { new Integer(min), new Integer(length) },
52 field + " should be at least " + min + " long, but was " + length);
53 }
54 }
55
56 /**
57 * Reject the given field if the string value is shorter than the maximum length given in <code>max</code>.
58 *
59 * The object whose field is being validated does not need to be passed in because the Errors instance can resolve
60 * field values by itself (it will usually hold an internal reference to the target object).
61 *
62 * @param errors
63 * @param field
64 * @param max
65 *
66 * @deprecated
67 */
68 public static void validateMaxLength(Errors errors, String field, int max) {
69 String value = (String) errors.getFieldValue(field);
70 Assert.notNull(value);
71 int length = value.length();
72 if (length > max) {
73 errors.rejectValue(field, "field.length.max", new Integer[] { new Integer(max), new Integer(length) },
74 field + " should be at most " + max + " long, but was " + length);
75 }
76 }
77
78 /**
79 * Reject the given field if the <code>long</code> value is more than the maximum value given in <code>max</code>.
80 *
81 * The object whose field is being validated does not need to be passed in because the Errors instance can resolve
82 * field values by itself (it will usually hold an internal reference to the target object).
83 *
84 * @param errors
85 * @param field
86 * @param max
87 *
88 * @deprecated
89 */
90 public static void validateMaxValue(Errors errors, String field, long max) {
91
92 Long value = null;
93
94 if (errors.getFieldValue(field) instanceof Integer) {
95 value = ((Integer) errors.getFieldValue(field)).longValue();
96 } else if (errors.getFieldValue(field) instanceof Long) {
97 value = ((Long) errors.getFieldValue(field));
98 }
99 Assert.notNull(value);
100 if (value > max) {
101 errors.rejectValue(field, "field.value.max", new Long[] { new Long(max), new Long(value) }, field
102 + " should be at most " + max + ", but was " + value);
103 }
104 }
105
106 /**
107 * Reject the given field if the <code>long</code> value is less than the minimum value given in <code>min</code>.
108 *
109 * The object whose field is being validated does not need to be passed in because the Errors instance can resolve
110 * field values by itself (it will usually hold an internal reference to the target object).
111 *
112 * @param errors
113 * @param field
114 * @param min
115 * @deprecated
116 */
117 public static void validateMinValue(Errors errors, String field, long min) {
118
119 Long value = null;
120
121 if (errors.getFieldValue(field) instanceof Integer) {
122 value = ((Integer) errors.getFieldValue(field)).longValue();
123 } else if (errors.getFieldValue(field) instanceof Long) {
124 value = ((Long) errors.getFieldValue(field));
125 }
126
127 if (value != null) {
128 Assert.notNull(value);
129 if (value < min) {
130 errors.rejectValue(field, "field.value.min", new Long[] { new Long(min), new Long(value) }, field
131 + " should be at least " + min + ", but was " + value);
132 }
133 }
134 }
135 }