1 package org.unitedfront2.domain;
2
3 import java.io.Serializable;
4 import java.util.Date;
5
6 import org.apache.commons.lang.builder.EqualsBuilder;
7 import org.apache.commons.lang.builder.HashCodeBuilder;
8 import org.apache.commons.lang.builder.ToStringBuilder;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.unitedfront2.dao.SampleDomainDao;
12
13 /**
14 * サンプルドメインのドメインモデルです。
15 *
16 * @invariant ${this.d} > 0
17 * @author kurokkie
18 *
19 */
20 public class SampleDomain implements Identifiable<SampleDomain>, Storable,
21 Deletable, Serializable, Domain {
22
23 /** 状態の定義 */
24 public static enum State {
25
26 /** 利用可能 */
27 AVAILABLE,
28
29 /** 利用不可能 */
30 DISABLE
31 }
32
33 /** シリアル番号 */
34 private static final long serialVersionUID = -2784958436581567548L;
35
36 /** ログ */
37 protected final transient Log logger = LogFactory.getLog(getClass());
38
39 /** ID */
40 private Integer id;
41
42 /**
43 * コード
44 *
45 * @invariant 一意な値
46 */
47 private String code;
48
49 /** 浮動点小数 */
50 private Double d;
51
52 /** テキスト */
53 private String text;
54
55 /** 真なら <code>true</code> 、偽なら <code>false</code> */
56 private Boolean bool;
57
58 /**
59 * 日時
60 *
61 * @invariant not null
62 */
63 private Date date;
64
65 /**
66 * 状態
67 *
68 * @invariant not null
69 */
70 private State state;
71
72 /** サンプルドメインデータアクセスオブジェクト */
73 private transient SampleDomainDao sampleDomainDao;
74
75 public SampleDomain() {
76 super();
77 }
78
79 public SampleDomain(String code, Double d, String text, Boolean bool,
80 Date date, State state) {
81 super();
82 this.code = code;
83 this.d = d;
84 this.text = text;
85 this.bool = bool;
86 this.date = (Date) date.clone();
87 this.state = state;
88 }
89
90 public SampleDomain(Integer id, String code, Double d, String text,
91 Boolean bool, Date date, State state) {
92 this(code, d, text, bool, date, state);
93 this.id = id;
94 }
95
96 @Override
97 public String toString() {
98 return new ToStringBuilder(this)
99 .append("id", id)
100 .append("code", code)
101 .append("d", d)
102 .append("text", text)
103 .append("bool", bool)
104 .append("date", date)
105 .append("state", state).toString();
106 }
107
108 @Override
109 public boolean equals(final Object other) {
110 if (!(other instanceof SampleDomain)) {
111 return false;
112 }
113 SampleDomain castOther = (SampleDomain) other;
114 return new EqualsBuilder()
115 .append(id, castOther.id)
116 .append(code, castOther.code)
117 .append(d, castOther.d)
118 .append(text, castOther.text)
119 .append(bool, castOther.bool)
120 .append(date, castOther.date)
121 .append(state, castOther.state).isEquals();
122 }
123
124 @Override
125 public int hashCode() {
126 return new HashCodeBuilder()
127 .append(id)
128 .append(code)
129 .append(d)
130 .append(text)
131 .append(bool)
132 .append(date)
133 .append(state).toHashCode();
134 }
135
136 @Override
137 public boolean identify(SampleDomain other) {
138 if (id == null) {
139 return false;
140 }
141 return id.equals(other.getId());
142 }
143
144 @Override
145 public void store() throws DuplicateSampleDomainCodeException {
146 if (id == null) {
147 if (sampleDomainDao.findByCode(code) != null) {
148 String message = "The code '" + code + "' already exists.";
149 logger.warn(message);
150 throw new DuplicateSampleDomainCodeException(message);
151 }
152 sampleDomainDao.register(this);
153 } else {
154 SampleDomain found = sampleDomainDao.findByCode(code);
155 if (found != null && !identify(found)) {
156 String message = "The code '" + code + "' already exists.";
157 logger.warn(message);
158 throw new DuplicateSampleDomainCodeException(message);
159 }
160 sampleDomainDao.update(this);
161 }
162 }
163
164 @Override
165 public void delete() {
166 sampleDomainDao.delete(id);
167 }
168
169 /**
170 * @return テキスト文字数と浮動小数点の掛け算の結果
171 */
172 public double multipleTextLengthAndD() {
173 int textLength;
174 if (text == null) {
175 textLength = 0;
176 } else {
177 textLength = text.length();
178 }
179 return textLength * d;
180 }
181
182 public Integer getId() {
183 return id;
184 }
185
186 public void setId(Integer id) {
187 this.id = id;
188 }
189
190 public String getCode() {
191 return code;
192 }
193
194 public void setCode(String code) {
195 this.code = code;
196 }
197
198 public Double getD() {
199 return d;
200 }
201
202 public void setD(Double d) {
203 this.d = d;
204 }
205
206 public String getText() {
207 return text;
208 }
209
210 public void setText(String text) {
211 this.text = text;
212 }
213
214 public Boolean getBool() {
215 return bool;
216 }
217
218 public void setBool(Boolean bool) {
219 this.bool = bool;
220 }
221
222 public Date getDate() {
223 if (date == null) {
224 return null;
225 } else {
226 return (Date) date.clone();
227 }
228 }
229
230 public void setDate(Date date) {
231 if (date == null) {
232 this.date = null;
233 } else {
234 this.date = (Date) date.clone();
235 }
236 }
237
238 public State getState() {
239 return state;
240 }
241
242 public void setState(State state) {
243 this.state = state;
244 }
245
246 public void setSampleDomainDao(SampleDomainDao sampleDomainDao) {
247 this.sampleDomainDao = sampleDomainDao;
248 }
249 }