1 package org.unitedfront2.domain.communication;
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 * @author kurokkie
15 *
16 */
17 public class BlogValidator implements Serializable {
18
19 /** コードの正規表現のデフォルト ([0-9a-z]+) */
20 public static final String DEFAULT_CODE_REGEX = "[0-9a-z]+";
21
22 /** コード最大文字列長のデフォルト (32) */
23 public static final int DEFAULT_CODE_MAX_LENGTH = 32;
24
25 /** シリアル番号 */
26 private static final long serialVersionUID = -7938436516527392518L;
27
28 /** コードの正規表現 */
29 private String codeRegex = DEFAULT_CODE_REGEX;
30
31 /** コードの最大文字列長 */
32 private int codeMaxLength = DEFAULT_CODE_MAX_LENGTH;
33
34 /** ブログテーブル */
35 private transient BlogTable blogTable;
36
37 @Override
38 public boolean equals(final Object other) {
39 if (!(other instanceof BlogValidator)) {
40 return false;
41 }
42 BlogValidator castOther = (BlogValidator) other;
43 return new EqualsBuilder()
44 .append(codeRegex, castOther.codeRegex)
45 .append(codeMaxLength, castOther.codeMaxLength).isEquals();
46 }
47
48 @Override
49 public int hashCode() {
50 return new HashCodeBuilder()
51 .append(codeRegex)
52 .append(codeMaxLength).toHashCode();
53 }
54
55 @Override
56 public String toString() {
57 return new ToStringBuilder(this)
58 .append("codeRegex", codeRegex)
59 .append("codeMaxLength", codeMaxLength).toString();
60 }
61
62 /**
63 * コードを検証します。<p>
64 *
65 * 検証内容
66 * <ul>
67 * <li>必須項目</li>
68 * <li>最大文字数</li>
69 * <li>正規表現</li>
70 * <li>一意</li>
71 * </ul>
72 *
73 * @param blog ブログ
74 * @throws ValidationException {@link ValidationException}
75 */
76 public void validateCode(Blog blog) throws ValidationException {
77 String code = blog.getCode();
78 Validate.notBlank(code);
79 Validate.maxLength(code, codeMaxLength);
80 Validate.match(code, codeRegex);
81 Blog found = blogTable.findByCode(code);
82 if (found != null && !blog.identify(found)) {
83 throw new ValidationException("blog.BlogCodeUsedByOtherException",
84 null, "The code '" + code + "' is used by other.");
85 }
86 }
87
88 public String getCodeRegex() {
89 return codeRegex;
90 }
91
92 public void setCodeRegex(String codeRegex) {
93 this.codeRegex = codeRegex;
94 }
95
96 public int getCodeMaxLength() {
97 return codeMaxLength;
98 }
99
100 public void setCodeMaxLength(int codeMaxLength) {
101 this.codeMaxLength = codeMaxLength;
102 }
103
104 public void setBlogTable(BlogTable blogTable) {
105 this.blogTable = blogTable;
106 }
107 }