View Javadoc

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   * @invariant ${this.writeAccessControl} is
23   * {@link org.unitedfront2.domain.accesscontrol.OwnerOnly}
24   * @invariant ${this.readAccessControl} is ${this.overview.readAccessControl}
25   * @invariant ${this.writeAccessControl} is ${this.overview.writeAccessControl}
26   * @invariant ブログ記事が存在するブログは削除できない。
27   * @author kurokkie
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      /** ID */
37      private Integer id;
38  
39      /**
40       * コード
41       *
42       * @invariant 一意な値
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      * @throws BlogCodeUsedByOtherException 指定したコードが他のブログで既に使用されている
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      * 指定した範囲のブログ記事を復元します。記事番号は0から始まる整数で、ID の降順になります。
141      *
142      * @param no 開始点となる記事番号
143      * @param num 件数
144      */
145     public void retrieveEntries(int no, int num) {
146         entries = blogDao.findBlogEntries(id, no, num);
147     }
148 
149     /**
150      * ブログ記事を投稿します。指定したブログ記事を保存し、このブログと関連付けます。
151      *
152      * @param blogEntry ブログ記事
153      */
154     public void post(BlogEntry blogEntry) {
155         blogEntry.store();
156         blogDao.registerBlogEntry(id, blogEntry.getId());
157     }
158 
159     /**
160      * 匿名ユーザで投稿アクセスを試みます。
161      *
162      * @throws AccessDeniedException アクセス拒否
163      */
164     public void commentAccess() throws AccessDeniedException {
165         commentAccessControl.access(this);
166     }
167 
168     /**
169      * 投稿アクセスを試みます。
170      *
171      * @param userId ユーザ ID
172      * @throws AccessDeniedException アクセス拒否
173      */
174     public void commentAccess(int userId) throws AccessDeniedException {
175         commentAccessControl.access(this, userId);
176     }
177 
178     /**
179      * 投稿アクセスを試みます。
180      *
181      * @param user ユーザ
182      * @throws AccessDeniedException アクセス拒否
183      */
184     public void commentAccess(User user) throws AccessDeniedException {
185         commentAccessControl.access(this, user);
186     }
187 
188     /**
189      * 匿名ユーザに投稿アクセス権限があるか判定します。
190      *
191      * @return 権限があれば <code>true</code> 、なければ <code>false</code>
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      * @param userId ユーザ ID
206      * @return 権限があれば <code>true</code> 、なければ <code>false</code>
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      * @param user ユーザ
221      * @return 権限があれば <code>true</code> 、なければ <code>false</code>
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      * @throws BlogEntryExistException ブログ記事が存在している
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      * 記事リストを取得します。呼び出す前に {@link #retrieveEntries(int, int)} を実行して
281      * ください。実行しなかった場合、全ての記事リストが返ります。
282      *
283      * @return 記事リスト
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 }