View Javadoc

1   package org.unitedfront2.domain.communication;
2   
3   import java.io.Serializable;
4   import java.util.Date;
5   import java.util.List;
6   
7   import org.apache.commons.lang.builder.EqualsBuilder;
8   import org.apache.commons.lang.builder.HashCodeBuilder;
9   import org.apache.commons.lang.builder.ToStringBuilder;
10  import org.apache.commons.logging.Log;
11  import org.apache.commons.logging.LogFactory;
12  import org.unitedfront2.dao.BlogDao;
13  import org.unitedfront2.domain.Deletable;
14  import org.unitedfront2.domain.Domain;
15  import org.unitedfront2.domain.Identifiable;
16  import org.unitedfront2.domain.Storable;
17  import org.unitedfront2.domain.User;
18  import org.unitedfront2.domain.accesscontrol.AccessControl;
19  import org.unitedfront2.domain.accesscontrol.AccessDeniedException;
20  
21  /**
22   * ブログの記事を表すドメインクラスです。
23   *
24   * @author kurokkie
25   *
26   */
27  public class BlogEntry implements Serializable, Identifiable<BlogEntry>,
28      Storable, Deletable, Domain {
29  
30      /** ログ */
31      protected final transient Log logger = LogFactory.getLog(getClass());
32  
33      /** シリアル番号 */
34      private static final long serialVersionUID = 8962368517170431378L;
35  
36      /** 記事 */
37      private Message entry;
38  
39      /** コメントリスト */
40      private transient List<Comment> comments;
41  
42      /** コメント数 */
43      private transient int commentCount;
44  
45      /** ブログデータアクセスオブジェクト */
46      private transient BlogDao blogDao;
47  
48      public BlogEntry() {
49          super();
50      }
51  
52      public BlogEntry(Message entry) {
53          super();
54          this.entry = entry;
55      }
56  
57      @Override
58      public boolean equals(final Object other) {
59          if (!(other instanceof BlogEntry)) {
60              return false;
61          }
62          BlogEntry castOther = (BlogEntry) other;
63          return new EqualsBuilder()
64              .append(entry, castOther.entry).isEquals();
65      }
66  
67      @Override
68      public int hashCode() {
69          return new HashCodeBuilder().append(entry).toHashCode();
70      }
71  
72      @Override
73      public String toString() {
74          return new ToStringBuilder(this).append("entry", entry).toString();
75      }
76  
77      @Override
78      public boolean identify(BlogEntry other) {
79          if (entry == null) {
80              return false;
81          }
82          return entry.identify(other.entry);
83      }
84  
85      /**
86       * 記事の新規投稿は {@link Blog#post(BlogEntry)} を利用してください。
87       */
88      @Override
89      public void store() {
90          try {
91              entry.store();
92          } catch (MessageCodeUsedByOtherException e) {
93              // 起こらない。記事のコードは自動生成される。
94              logger.error(e.getMessage(), e);
95              throw new IllegalStateException(e);
96          }
97      }
98  
99      /**
100      * 記事とコメントを全て削除します。
101      */
102     @Override
103     public void delete() {
104         entry.delete();
105     }
106 
107     //**************************************************************************
108     // Message から委譲したメソッド
109     //**************************************************************************
110     public boolean canReadAccess() {
111         return entry.canRead();
112     }
113 
114     public boolean canReadAccess(int userId) {
115         return entry.canRead(userId);
116     }
117 
118     public boolean canWriteAccess() {
119         return entry.canWrite();
120     }
121 
122     public boolean canWriteAccess(int userId) {
123         return entry.canWrite(userId);
124     }
125 
126     public User getAuthor() {
127         return entry.getAuthor();
128     }
129 
130     public Integer getAuthorId() {
131         return entry.getAuthorId();
132     }
133 
134     public String getCode() {
135         return entry.getCode();
136     }
137 
138     public Integer getId() {
139         return entry.getId();
140     }
141 
142     public Date getRegistrationDate() {
143         return entry.getRegistrationDate();
144     }
145 
146     public Date getLastUpdateDate() {
147         return entry.getLastUpdateDate();
148     }
149 
150     public User getOwner() {
151         return entry.getOwner();
152     }
153 
154     public Integer getOwnerId() {
155         return entry.getOwnerId();
156     }
157 
158     public AccessControl getReadAccessControl() {
159         return entry.getReadAccessControl();
160     }
161 
162     public MessageEntry getRequiredEntry() {
163         return entry.getRequiredEntry();
164     }
165 
166     public AccessControl getWriteAccessControl() {
167         return entry.getWriteAccessControl();
168     }
169 
170     public void readAccess() throws AccessDeniedException {
171         entry.readAccess();
172     }
173 
174     public void readAccess(int userId) throws AccessDeniedException {
175         entry.readAccess(userId);
176     }
177 
178     public void retrieveAuthor() {
179         entry.retrieveAuthor();
180     }
181 
182     public void retrieveOwner() {
183         entry.retrieveOwner();
184     }
185 
186     public void writeAccess() throws AccessDeniedException {
187         entry.writeAccess();
188     }
189 
190     public void writeAccess(int userId) throws AccessDeniedException {
191         entry.writeAccess(userId);
192     }
193 
194     /**
195      * このブログ記事のブログコメントを復元します。
196      */
197     public void retrieveComments() {
198         comments = blogDao.findComments(getId());
199     }
200 
201     /**
202      * ブログコメントの投稿者を復元します。ブログコメントが復元されていない場合は復元します。
203      */
204     public void retrieveCommenters() {
205         if (comments == null) {
206             retrieveComments();
207         }
208         for (Comment c : comments) {
209             c.retrieveCommenter();
210         }
211     }
212 
213     /**
214      * ブログコメントの投稿者のプロフィールを復元します。ブログコメントや投稿者が復元されていない場合は復元
215      * します。
216      */
217     public void retrieveCommenterProfiles() {
218         if (comments == null) {
219             retrieveComments();
220         }
221         for (Comment c : comments) {
222             if (c.getCommenter() == null) {
223                 c.retrieveCommenter();
224             }
225             if (c.getCommenter() != null) {
226                 // 匿名でない場合
227                 c.getCommenter().retrieveProfile();
228             }
229         }
230     }
231 
232     /**
233      * このブログ記事のコメント数を復元します。
234      */
235     public void retrieveCommentCount() {
236         commentCount = blogDao.countComment(getId());
237     }
238 
239     /**
240      * コメントを投稿します。コメントを保存し、このブログ記事と関連付けます。
241      *
242      * @param comment コメント
243      */
244     public void post(Comment comment) {
245         comment.store();
246         blogDao.registerComment(getId(), comment.getId());
247     }
248 
249     /**
250      * コメントを取得します。コメントが復元されていなければ復元します。
251      *
252      * @param id コメント ID
253      * @return コメント、見つからなければ <code>null</code>
254      */
255     public Comment findComment(int id) {
256         retrieveComments();
257         for (Comment c : comments) {
258             if (c.getId() == id) {
259                 return c;
260             }
261         }
262         return null;
263     }
264 
265     public Message getEntry() {
266         return entry;
267     }
268 
269     public void setEntry(Message entry) {
270         this.entry = entry;
271     }
272 
273     public List<Comment> getComments() {
274         return comments;
275     }
276 
277     public int getCommentCount() {
278         return commentCount;
279     }
280 
281     public void setBlogDao(BlogDao blogDao) {
282         this.blogDao = blogDao;
283     }
284 }