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
15
16
17 public class BlogValidator implements Serializable {
18
19
20 public static final String DEFAULT_CODE_REGEX = "[0-9a-z]+";
21
22
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
64
65
66
67
68
69
70
71
72
73
74
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 }