1 package net.sourceforge.blogentis.modules.screens;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
52
53
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
93
94
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 }