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 Integer 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 void setReadAccessControl(AccessControl readAccessControl) {
163         entry.setReadAccessControl(readAccessControl);
164     }
165 
166     public AccessControl getWriteAccessControl() {
167         return entry.getWriteAccessControl();
168     }
169 
170     public void setWriteAccessControl(AccessControl writeAccessControl) {
171         entry.setWriteAccessControl(writeAccessControl);
172     }
173 
174     public MessageEntry getRequiredEntry() {
175         return entry.getRequiredEntry();
176     }
177 
178     public void readAccess() throws AccessDeniedException {
179         entry.readAccess();
180     }
181 
182     public void readAccess(int userId) throws AccessDeniedException {
183         entry.readAccess(userId);
184     }
185 
186     public void readAccess(User user) throws AccessDeniedException {
187         entry.readAccess(user);
188     }
189 
190     public void writeAccess() throws AccessDeniedException {
191         entry.writeAccess();
192     }
193 
194     public void writeAccess(int userId) throws AccessDeniedException {
195         entry.writeAccess(userId);
196     }
197 
198     public void writeAccess(User user) throws AccessDeniedException {
199         entry.writeAccess(user);
200     }
201 
202     /**
203      * コメントを投稿します。コメントを保存し、このブログ記事と関連付けます。
204      *
205      * @param comment コメント
206      */
207     public void post(Comment comment) {
208         comment.store();
209         blogDao.registerComment(getId(), comment.getId());
210     }
211 
212     /**
213      * コメントを取得します。
214      *
215      * @param id コメント ID
216      * @return コメント、見つからなければ <code>null</code>
217      */
218     public Comment findComment(int id) {
219         for (Comment c : getComments()) {
220             if (c.getId() == id) {
221                 return c;
222             }
223         }
224         return null;
225     }
226 
227     public Message getEntry() {
228         return entry;
229     }
230 
231     public void setEntry(Message entry) {
232         this.entry = entry;
233     }
234 
235     public List<Comment> getComments() {
236         if (comments == null && blogDao != null && getId() != null) {
237             comments = blogDao.findComments(getId());
238         }
239         return comments;
240     }
241 
242     public Integer getCommentCount() {
243         if (commentCount == null && blogDao != null && getId() != null) {
244             commentCount = blogDao.countComment(getId());
245         }
246         return commentCount;
247     }
248 
249     public void setBlogDao(BlogDao blogDao) {
250         this.blogDao = blogDao;
251     }
252 }