View Javadoc

1   package net.sourceforge.blogentis.modules.screens;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: ViewPost.java,v 1.2 2004/10/28 10:41:40 tassos Exp $
23  //
24  
25  import java.util.Date;
26  import java.util.List;
27  
28  import net.sourceforge.blogentis.feed.Feed;
29  import net.sourceforge.blogentis.feed.FeedFactory;
30  import net.sourceforge.blogentis.om.Blog;
31  import net.sourceforge.blogentis.om.Post;
32  import net.sourceforge.blogentis.om.PostPeer;
33  import net.sourceforge.blogentis.plugins.BlogPluginService;
34  import net.sourceforge.blogentis.plugins.base.IPostViewExtensionPoint;
35  import net.sourceforge.blogentis.trackback.TrackbackPostEditExtension;
36  import net.sourceforge.blogentis.turbine.BaseBlogScreen;
37  import net.sourceforge.blogentis.turbine.BlogParameterParser;
38  import net.sourceforge.blogentis.turbine.BlogRunData;
39  import net.sourceforge.blogentis.utils.MappedConfiguration;
40  
41  import org.apache.torque.TorqueException;
42  import org.apache.torque.util.Criteria;
43  import org.apache.turbine.util.RunData;
44  import org.apache.velocity.context.Context;
45  
46  public class ViewPost
47          extends BaseBlogScreen {
48      public static class CommentHelper {
49          public int isCommentable(Blog blog, Post post) {
50              /*
51               * 0: comments allowed and open 1: comments disallowed, no comments
52               * shown 2: comments shown but no additions possible due to
53               * archival.
54               */
55              MappedConfiguration blogConf = blog.getConfiguration();
56              if (!blogConf.getBoolean("blog.allowComments", true))
57                  return 1;
58              if (!post.isAllowComments())
59                  return 1;
60              if (blogConf.getBoolean("blog.commentTimeLimit", false)) {
61                  long days = blogConf.getInt("blog.commentTimeLimitValue", 30);
62                  Date postedDate = new Date(post.getPostedTime().getTime());
63                  postedDate.setTime(postedDate.getTime() + days * 3600 * 24
64                                     * 1000);
65                  if (new Date().after(postedDate))
66                      return 2;
67              }
68              return 0;
69          }
70  
71          public boolean canReceiveTrackbacks(Blog blog, Post post) {
72              return blog.getConfiguration()
73                  .getBoolean(TrackbackPostEditExtension.T_BLOG_RECEIVE, false);
74          }
75      }
76  
77      protected void doBuildTemplate(RunData data, Context context)
78              throws Exception {
79          BlogParameterParser bpp = (BlogParameterParser)data.getParameters();
80          Post p = bpp.getPost();
81          if (p == null) {
82              doRedirect(data, "Default.vm");
83              return;
84          }
85          super.doBuildTemplate(data, context);
86          if (p.getBlogId() != bpp.getBlog().getBlogId()) {
87              throw new Exception("This post is from an other blog!");
88          }
89  
90          Feed f = FeedFactory.getFeed(data);
91          f.setContextProperties(context);
92          // We don't use the comment feed here beacuse a) it will show the
93          // comments in the reverse chronological order and
94          // b) it will cut off the number of comments.
95          context.put("comments", p.getComments());
96  
97          if (p.isPublished()) {
98              context.put("nextPost", getNextPost(p));
99              context.put("prevPost", getPreviousPost(p));
100         }
101         IPostViewExtensionPoint pExt = (IPostViewExtensionPoint)BlogPluginService
102             .locateExtensionPoint(bpp.getBlog(), IPostViewExtensionPoint.class);
103         if (pExt != null)
104             context
105                 .put("post", pExt.doPostViewExtensions((BlogRunData)data, p));
106         context.put("commentHelper", new CommentHelper());
107     }
108 
109     public Post getNextPost(Post p) {
110         try {
111             Criteria c = new Criteria();
112             c.add(PostPeer.BLOG_ID, p.getBlogId());
113             c.add(PostPeer.POSTED_TIME, p.getPostedTime(),
114                   Criteria.GREATER_THAN);
115             c.add(PostPeer.POST_TYPE, PostPeer.PUBLISHED_TYPE);
116             c.addAscendingOrderByColumn(PostPeer.POSTED_TIME);
117             c.setLimit(1);
118             List l = PostPeer.doSelect(c);
119             if (l.size() > 0)
120                 return (Post)l.get(0);
121         } catch (TorqueException ignored) {}
122         return null;
123     }
124 
125     public Post getPreviousPost(Post p) {
126         try {
127             Criteria c = new Criteria();
128             c.add(PostPeer.BLOG_ID, p.getBlogId());
129             c.add(PostPeer.POSTED_TIME, p.getPostedTime(), Criteria.LESS_THAN);
130             c.add(PostPeer.POST_TYPE, PostPeer.PUBLISHED_TYPE);
131             c.addDescendingOrderByColumn(PostPeer.POSTED_TIME);
132             c.setLimit(1);
133             List l = PostPeer.doSelect(c);
134             if (l.size() > 0)
135                 return (Post)l.get(0);
136         } catch (TorqueException ignored) {}
137         return null;
138     }
139 }