1 package org.unitedfront2.domain.communication;
2
3 import java.io.Serializable;
4 import java.util.List;
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.unitedfront2.dao.BlogDao;
10 import org.unitedfront2.domain.Deletable;
11 import org.unitedfront2.domain.Domain;
12 import org.unitedfront2.domain.Identifiable;
13 import org.unitedfront2.domain.Storable;
14 import org.unitedfront2.domain.User;
15 import org.unitedfront2.domain.accesscontrol.AbstractResource;
16 import org.unitedfront2.domain.accesscontrol.AccessControl;
17 import org.unitedfront2.domain.accesscontrol.AccessDeniedException;
18
19
20
21
22
23
24
25
26
27
28
29
30 public class Blog extends AbstractResource implements Serializable,
31 Identifiable<Blog>, Storable, Deletable, Domain {
32
33
34 private static final long serialVersionUID = 5187863597884559866L;
35
36
37 private Integer id;
38
39
40
41
42
43
44 private String code;
45
46
47 private Message overview;
48
49
50 private AccessControl commentAccessControl;
51
52
53 private transient List<BlogEntry> entries;
54
55
56 private transient Integer count;
57
58
59 private transient BlogDao blogDao;
60
61 public Blog() {
62 super();
63 }
64
65 public Blog(String code, Message overview, Integer ownerId,
66 AccessControl readAccessControl, AccessControl writeAccessControl,
67 AccessControl commeAccessControl) {
68 super(ownerId, readAccessControl, writeAccessControl);
69 this.code = code;
70 this.overview = overview;
71 this.commentAccessControl = commeAccessControl;
72 }
73
74 public Blog(Integer id, String code, Message overview, Integer ownerId,
75 AccessControl readAccessControl, AccessControl writeAccessControl,
76 AccessControl commeAccessControl) {
77 this(code, overview, ownerId, readAccessControl, writeAccessControl,
78 commeAccessControl);
79 this.id = id;
80 }
81
82 @Override
83 protected boolean buildEqualsBuilder(EqualsBuilder eb, Object other) {
84 if (!(other instanceof Blog)) {
85 return false;
86 }
87 Blog castOther = (Blog) other;
88 eb.append(id, castOther.id)
89 .append(code, castOther.code)
90 .append(overview, castOther.overview);
91 if (!super.buildEqualsBuilder(eb, other)) {
92 return false;
93 }
94 eb.append(commentAccessControl, castOther.commentAccessControl);
95 return true;
96 }
97
98 @Override
99 protected void buildHashCodeBuilder(HashCodeBuilder hcb) {
100 hcb.append(id).append(code).append(overview);
101 super.buildHashCodeBuilder(hcb);
102 hcb.append(commentAccessControl);
103 }
104
105 @Override
106 protected void buildToStringBuilder(ToStringBuilder tsb) {
107 tsb.append("id", id).append("code", code).append("overview", overview);
108 super.buildToStringBuilder(tsb);
109 tsb.append("commentAccessControl", commentAccessControl);
110 }
111
112
113
114
115 @Override
116 public void store() throws BlogCodeUsedByOtherException {
117 Blog foundByCode = blogDao.findByCode(code);
118 if (foundByCode != null && !foundByCode.identify(this)) {
119 String message = "The code '" + code
120 + "' has already been used by other.";
121 logger.warn(message);
122 throw new BlogCodeUsedByOtherException(message);
123 }
124 if (this.id == null) {
125 blogDao.register(this);
126 } else {
127 blogDao.update(this);
128 }
129 }
130
131 @Override
132 public boolean identify(Blog blog) {
133 if (id == null) {
134 return false;
135 }
136 return id.equals(blog.getId());
137 }
138
139
140
141
142
143
144
145 public void retrieveEntries(int no, int num) {
146 entries = blogDao.findBlogEntries(id, no, num);
147 }
148
149
150
151
152
153
154 public void post(BlogEntry blogEntry) {
155 blogEntry.store();
156 blogDao.registerBlogEntry(id, blogEntry.getId());
157 }
158
159
160
161
162
163
164 public void commentAccess() throws AccessDeniedException {
165 commentAccessControl.access(this);
166 }
167
168
169
170
171
172
173
174 public void commentAccess(int userId) throws AccessDeniedException {
175 commentAccessControl.access(this, userId);
176 }
177
178
179
180
181
182
183
184 public void commentAccess(User user) throws AccessDeniedException {
185 commentAccessControl.access(this, user);
186 }
187
188
189
190
191
192
193 public boolean canComment() {
194 try {
195 commentAccess();
196 return true;
197 } catch (AccessDeniedException e) {
198 return false;
199 }
200 }
201
202
203
204
205
206
207
208 public boolean canComment(int userId) {
209 try {
210 commentAccess(userId);
211 return true;
212 } catch (AccessDeniedException e) {
213 return false;
214 }
215 }
216
217
218
219
220
221
222
223 public boolean canComment(User user) {
224 try {
225 commentAccess(user);
226 return true;
227 } catch (AccessDeniedException e) {
228 return false;
229 }
230 }
231
232
233
234
235 @Override
236 public void delete() throws BlogEntryExistException {
237 int count = blogDao.countBlogEntry(id);
238 if (count > 0) {
239 String message = "The blog [ID=" + id + "] has " + count
240 + " entries.";
241 logger.warn(message);
242 throw new BlogEntryExistException(message);
243 }
244 blogDao.delete(id);
245 }
246
247 public Integer getId() {
248 return id;
249 }
250
251 public void setId(Integer id) {
252 this.id = id;
253 }
254
255 public String getCode() {
256 return code;
257 }
258
259 public void setCode(String code) {
260 this.code = code;
261 }
262
263 public Message getOverview() {
264 return overview;
265 }
266
267 public void setOverview(Message overview) {
268 this.overview = overview;
269 }
270
271 public AccessControl getCommentAccessControl() {
272 return commentAccessControl;
273 }
274
275 public void setCommentAccessControl(AccessControl commentAccessControl) {
276 this.commentAccessControl = commentAccessControl;
277 }
278
279
280
281
282
283
284
285 public List<BlogEntry> getEntries() {
286 if (entries == null && blogDao != null && getCount() != null) {
287 entries = blogDao.findBlogEntries(id, 0, getCount());
288 }
289 return entries;
290 }
291
292 public Integer getCount() {
293 if (count == null && blogDao != null && id != null) {
294 count = blogDao.countBlogEntry(id);
295 }
296 return count;
297 }
298
299 public Profile getOwnerProfile() {
300 if (getOwner() == null) {
301 return null;
302 } else {
303 return getOwner().getProfile();
304 }
305 }
306
307 public void setBlogDao(BlogDao blogDao) {
308 this.blogDao = blogDao;
309 }
310 }