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 * {@link SampleDomain} の検証クラスです。
13 *
14 * @author kurokkie
15 *
16 */
17 public class SampleDomainValidator implements Serializable {
18
19 /** デフォルトのコードの最小文字数のデフォルト (2) */
20 public static final int DEFAULT_CODE_MIN_LENGTH = 2;
21
22 /** デフォルトのコードの最大文字数のデフォルト (32) */
23 public static final int DEFAULT_CODE_MAX_LENGTH = 32;
24
25 /** デフォルトのコードの正規表現 ([\p{Alnum}]*) */
26 public static final String DEFAULT_CODE_REGEX = "[\\p{Alnum}]*";
27
28 /** デフォルトのテキストの最大文字数 (128) */
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 * コードを検証します。<p>
78 *
79 * 検証項目
80 * <ul>
81 * <li>必須項目</li>
82 * <li>最小文字数</li>
83 * <li>最大文字数</li>
84 * <li>正規表現</li>
85 * <li>一意</li>
86 * </ul>
87 *
88 * @param sampleDomain サンプルドメイン
89 * @throws ValidationException {@link ValidationException}
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 * テキストを検証します。<p>
107 *
108 * 検証項目
109 * <ul>
110 * <li>必須項目</li>
111 * <li>最大文字数</li>
112 * </ul>
113 *
114 * @param text テキスト
115 * @throws ValidationException {@link ValidationException}
116 */
117 public void validateText(String text) throws ValidationException {
118 Validate.notBlank(text);
119 Validate.maxLength(text, textMaxLength);
120 }
121
122 /**
123 * テキストを検証します。
124 *
125 * @param sampleDomain {@link SampleDomain}
126 * @throws ValidationException {@link ValidationException}
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 }