KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > blog > BlogEntry


1 /*
2  * Created on Feb 18, 2005
3  */

4 package com.openedit.blog;
5
6 import java.io.Serializable JavaDoc;
7 import java.text.ParseException JavaDoc;
8 import java.text.SimpleDateFormat JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Date JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.openedit.blog.archive.CommentArchive;
17 import org.openedit.links.LinkTree;
18
19 import com.openedit.OpenEditException;
20 import com.openedit.OpenEditRuntimeException;
21 import com.openedit.page.manage.PageManager;
22 import com.openedit.users.User;
23 import com.sun.syndication.feed.synd.SyndContentImpl;
24 import com.sun.syndication.feed.synd.SyndEntryImpl;
25
26 /**
27  * @author cburkey
28  *
29  */

30 public class BlogEntry extends SyndEntryImpl implements Serializable JavaDoc, Comparable JavaDoc
31 {
32     protected String JavaDoc fieldPath;
33     protected List JavaDoc fieldComments;
34     transient protected PageManager fieldPageManager;
35     protected LinkTree fieldLinkTree;
36     protected boolean fieldVisible = false;
37     protected static SimpleDateFormat JavaDoc fieldGmtStandard;
38     protected User fieldUser;
39     protected List JavaDoc fieldNotify;
40     protected CommentArchive fieldCommentArchive;
41     private static final Log log = LogFactory.getLog(BlogEntry.class);
42     
43     public BlogEntry()
44     {
45     }
46     
47     
48     public String JavaDoc getPath()
49     {
50         return fieldPath;
51     }
52     
53     /**
54      * A path is just the end part of a link
55      * @param inPath
56      */

57     
58     public void setPath(String JavaDoc inPath)
59     {
60         fieldPath = inPath;
61     }
62
63     public String JavaDoc published(String JavaDoc inFormat)
64     {
65         SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(inFormat);
66         return format.format(getPublishedDate());
67     }
68     public String JavaDoc getId()
69     {
70         return getPath();
71     }
72
73     /**
74      * Returns the total number of comments on this blog entry, both visible
75      * (published) and invisible (unpublished).
76      *
77      * @return The number of comments
78      *
79      * @throws OpenEditException
80      * If the comments needed to be loaded and could not be
81      */

82     public int countComments() throws OpenEditException
83     {
84         return getComments().size();
85     }
86     
87     /**
88      * Returns the number of visible (published) comments on this blog entry.
89      *
90      * @return The number of visible comments
91      *
92      * @throws OpenEditException
93      * If the comments needed to be loaded and could not be
94      */

95     public int countVisibleComments() throws OpenEditException
96     {
97         return getVisibleComments().size();
98     }
99
100     /**
101      * Returns all the comments on this blog, published or unpublished.
102      *
103      * @return A {@link List} of {@link Comment}s
104      *
105      * @throws OpenEditException
106      * If the comments needed to be loaded and could not be
107      */

108     public List JavaDoc getComments() throws OpenEditException
109     {
110         if( fieldComments == null)
111         {
112             fieldComments = new ArrayList JavaDoc();
113             getCommentArchive().loadComments(this);
114         }
115         return fieldComments;
116     }
117     
118     /**
119      * Returns all comments on this blog entry that are visible (published).
120      *
121      * @return A {@link List} of {@link Comment}s
122      *
123      * @throws OpenEditException
124      * If the comments needed to be loaded and could not be
125      */

126     public List JavaDoc getVisibleComments() throws OpenEditException
127     {
128         List JavaDoc comments = getComments();
129         List JavaDoc visibleComments = new ArrayList JavaDoc();
130         for ( Iterator JavaDoc iter = comments.iterator(); iter.hasNext(); )
131         {
132             Comment comment = (Comment) iter.next();
133             if ( comment.isVisible() )
134             {
135                 visibleComments.add( comment );
136             }
137         }
138         return visibleComments;
139     }
140     
141     public List JavaDoc getAllowedComments(User inUser) throws OpenEditException
142     {
143         List JavaDoc comments = getComments();
144         List JavaDoc allowedComments = new ArrayList JavaDoc();
145         for ( Iterator JavaDoc iter = comments.iterator(); iter.hasNext(); )
146         {
147             Comment comment = (Comment) iter.next();
148             if ( comment.isVisible() || comment.canEdit(inUser))
149             {
150                 allowedComments.add( comment );
151             }
152         }
153         return allowedComments;
154     }
155     
156     public PageManager getPageManager()
157     {
158         return fieldPageManager;
159     }
160     public void setPageManager(PageManager inPageManager)
161     {
162         fieldPageManager = inPageManager;
163     }
164     public LinkTree getLinkTree()
165     {
166         return fieldLinkTree;
167     }
168     public void setLinkTree(LinkTree inLinkTree)
169     {
170         fieldLinkTree = inLinkTree;
171     }
172     /**
173      * Adds a comment to this blog entry. The comment's data, including the
174      * ID, must already be populated.
175      *
176      * @param inComment The new comment
177      */

178     public void addComment(Comment inComment) throws OpenEditException
179     {
180         // TODO: check for dups
181
getComments().add(inComment);
182     }
183     
184     /**
185      * Returns the comment with the given comment ID.
186      *
187      * @param inId The comment ID
188      *
189      * @return The comment, or <code>null</code> if this entry does not have a
190      * comment with the given ID
191      */

192     public Comment getComment(String JavaDoc inId) throws OpenEditException
193     {
194         for (Iterator JavaDoc iter = getComments().iterator(); iter.hasNext();)
195         {
196             Comment comment = (Comment) iter.next();
197             if (comment.getId().equals(inId))
198             {
199                 return comment;
200             }
201         }
202         return null;
203     }
204     
205     public void removeComment(String JavaDoc inId) throws OpenEditException
206     {
207         Comment comment = getComment(inId);
208         removeComment(comment);
209     }
210     
211     public void removeComment(Comment inComment) throws OpenEditException
212     {
213         if ( inComment != null )
214         {
215             getComments().remove( inComment );
216         }
217     }
218     
219     
220     /**
221      * @param inContent
222      */

223     public void setDescription(String JavaDoc inContent)
224     {
225         SyndContentImpl content = new SyndContentImpl();
226         content.setType("text/html");
227         content.setValue(inContent);
228         setDescription(content);
229     }
230     public boolean isVisible()
231     {
232         return fieldVisible;
233     }
234     public void setVisible(boolean invisible)
235     {
236         this.fieldVisible = invisible;
237     }
238
239     public SimpleDateFormat JavaDoc getGmtStandard()
240     {
241         if ( fieldGmtStandard == null)
242         {
243             fieldGmtStandard = new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
244         }
245         return fieldGmtStandard;
246     }
247     public Date JavaDoc parse(String JavaDoc inDate)
248     {
249         try
250         {
251             if ( inDate.startsWith("2"))
252             {
253                 return getGmtStandard().parse(inDate);
254             }
255             else
256             {
257                 return SimpleDateFormat.getDateTimeInstance().parse(inDate);
258             }
259         } catch( ParseException JavaDoc ex)
260         {
261             throw new OpenEditRuntimeException(ex);
262         }
263     }
264     public void setGmtStandard(SimpleDateFormat JavaDoc inGmtStandard)
265     {
266         fieldGmtStandard = inGmtStandard;
267     }
268
269     public User getUser()
270     {
271         return fieldUser;
272     }
273
274     public void setUser(User inUser)
275     {
276         fieldUser = inUser;
277     }
278
279     public List JavaDoc getNotify()
280     {
281         return fieldNotify;
282     }
283
284     public void setNotify(List JavaDoc inNotify)
285     {
286         fieldNotify = inNotify;
287     }
288
289     public CommentArchive getCommentArchive()
290     {
291         return fieldCommentArchive;
292     }
293
294     public void setCommentArchive(CommentArchive inCommentArchive)
295     {
296         fieldCommentArchive = inCommentArchive;
297     }
298     public int compareTo(Object JavaDoc inEntry)
299     {
300         BlogEntry in = (BlogEntry)inEntry;
301         return getPublishedDate().compareTo(in.getPublishedDate());
302     }
303 }
304
Popular Tags