1 package org.unitedfront2.domain;
2
3 import java.io.Serializable;
4
5 import org.apache.commons.lang.builder.EqualsBuilder;
6 import org.apache.commons.lang.builder.HashCodeBuilder;
7 import org.apache.commons.lang.builder.ToStringBuilder;
8 import org.unitedfront2.validation.Validate;
9 import org.unitedfront2.validation.ValidationException;
10
11
12
13
14
15
16
17 public class SampleDomainValidator implements Serializable {
18
19
20 public static final int DEFAULT_CODE_MIN_LENGTH = 2;
21
22
23 public static final int DEFAULT_CODE_MAX_LENGTH = 32;
24
25
26 public static final String DEFAULT_CODE_REGEX = "[\\p{Alnum}]*";
27
28
29 public static final int DEFAULT_TEXT_MAX_LENGTH = 128;
30
31
32 private static final long serialVersionUID = 8383063072303370633L;
33
34
35 private int codeMinLength = DEFAULT_CODE_MIN_LENGTH;
36
37
38 private int codeMaxLength = DEFAULT_CODE_MAX_LENGTH;
39
40
41 private String codeRegex = DEFAULT_CODE_REGEX;
42
43
44 private int textMaxLength = DEFAULT_TEXT_MAX_LENGTH;
45
46
47 private transient SampleDomainTable sampleDomainTable;
48
49 @Override
50 public boolean equals(final Object other) {
51 if (!(other instanceof SampleDomainValidator)) {
52 return false;
53 }
54 SampleDomainValidator castOther = (SampleDomainValidator) other;
55 return new EqualsBuilder().append(codeMinLength,
56 castOther.codeMinLength).append(codeMaxLength,
57 castOther.codeMaxLength).append(codeRegex, castOther.codeRegex)
58 .append(textMaxLength, castOther.textMaxLength).isEquals();
59 }
60
61 @Override
62 public int hashCode() {
63 return new HashCodeBuilder().append(codeMinLength)
64 .append(codeMaxLength).append(codeRegex).append(textMaxLength)
65 .toHashCode();
66 }
67
68 @Override
69 public String toString() {
70 return new ToStringBuilder(this).append("codeMinLength", codeMinLength)
71 .append("codeMaxLength", codeMaxLength).append("codeRegex",
72 codeRegex).append("textMaxLength", textMaxLength)
73 .toString();
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public void validateCode(SampleDomain sampleDomain)
92 throws ValidationException {
93 String code = sampleDomain.getCode();
94 Validate.notBlank(code);
95 Validate.lengthInRange(code, codeMinLength, codeMaxLength);
96 Validate.match(code, codeRegex);
97 SampleDomain found = sampleDomainTable.findByCode(code);
98 if (found != null && !sampleDomain.identify(found)) {
99 throw new ValidationException("sample.validation.codeUsedByOther",
100 new Object[] {code},
101 "The code '" + code + "' is used by other.");
102 }
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117 public void validateText(String text) throws ValidationException {
118 Validate.notBlank(text);
119 Validate.maxLength(text, textMaxLength);
120 }
121
122
123
124
125
126
127
128 public void validateText(SampleDomain sampleDomain)
129 throws ValidationException {
130
131 validateText(sampleDomain.getText());
132 }
133
134 public int getCodeMinLength() {
135 return codeMinLength;
136 }
137
138 public void setCodeMinLength(int codeMinLength) {
139 this.codeMinLength = codeMinLength;
140 }
141
142 public int getCodeMaxLength() {
143 return codeMaxLength;
144 }
145
146 public void setCodeMaxLength(int codeMaxLength) {
147 this.codeMaxLength = codeMaxLength;
148 }
149
150 public String getCodeRegex() {
151 return codeRegex;
152 }
153
154 public void setCodeRegex(String codeRegex) {
155 this.codeRegex = codeRegex;
156 }
157
158 public int getTextMaxLength() {
159 return textMaxLength;
160 }
161
162 public void setTextMaxLength(int textMaxLength) {
163 this.textMaxLength = textMaxLength;
164 }
165
166 public void setSampleDomainTable(SampleDomainTable sampleDomainTable) {
167 this.sampleDomainTable = sampleDomainTable;
168 }
169 }