View Javadoc

1   package net.sourceforge.blogentis.modules.actions;
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: CommentAdd.java,v 1.1 2004/10/22 17:34:10 tassos Exp $
23  //
24  
25  import java.util.Date;
26  
27  import net.sourceforge.blogentis.om.Blog;
28  import net.sourceforge.blogentis.om.Comment;
29  import net.sourceforge.blogentis.om.Post;
30  import net.sourceforge.blogentis.plugins.BlogPluginService;
31  import net.sourceforge.blogentis.plugins.base.ICommentExtensionPoint;
32  import net.sourceforge.blogentis.turbine.BlogParameterParser;
33  import net.sourceforge.blogentis.turbine.BlogRunData;
34  import net.sourceforge.blogentis.utils.BlogPropertyCache;
35  import net.sourceforge.blogentis.utils.MappedConfiguration;
36  import net.sourceforge.blogentis.utils.StringUtils;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.apache.turbine.modules.actions.VelocityAction;
41  import org.apache.turbine.util.RunData;
42  import org.apache.velocity.context.Context;
43  
44  public class CommentAdd
45          extends VelocityAction {
46      private static Log log = LogFactory.getLog(CommentAdd.class);
47  
48      public void doAdd(RunData data, Context context)
49              throws Exception {
50          BlogParameterParser bpp = (BlogParameterParser)(data.getParameters());
51          Post post = bpp.getPost();
52  
53          Comment c = new Comment();
54          c.setPostedTime(new Date());
55          c.setPost(post);
56          c.setName(StringUtils.escapeHTML(bpp.getString("author",
57                                                         "Anonymous Coward")));
58          c.setText(bpp.getString("comment", ""));
59          c.setUrl(StringUtils.escapeHTML(bpp.getString("url", "")));
60          if (c.getText().length() == 0)
61              c.setText(null);
62          if (c.getUrl().length() == 0)
63              c.setUrl(null);
64          if (c.getText() == null || c.getText().equals("")) {
65              data.setMessage("Please don't leave the comment field empty.");
66              return;
67          }
68  
69          ICommentExtensionPoint cext = (ICommentExtensionPoint)BlogPluginService
70              .locateExtensionPoint(bpp.getBlog(), ICommentExtensionPoint.class);
71          if (cext == null || cext.commentCreated((BlogRunData)data, c))
72              c.save();
73  
74          if (bpp.getBoolean("save", false)) {
75              int age = 60 * 60 * 24 * 365; // one year retention;
76              data.getCookies().set("bauth", c.getName(), age);
77              data.getCookies().set("burl", c.getUrl(), age);
78          }
79      }
80  
81      protected boolean isAuthorized(RunData data)
82              throws Exception {
83          BlogParameterParser bpp = (BlogParameterParser)(data.getParameters());
84          Blog blog = bpp.getBlog();
85          if (blog == null)
86              return false; // we allways post a comment to a blog.
87          Post post = bpp.getPost();
88          if (post == null || post.getBlogId() != blog.getBlogId())
89              return false;
90  
91          return new MappedConfiguration(post).getBoolean("commentsDisabled",
92                                                          false)
93                 || BlogPropertyCache.getInstance().getBlogProperties(blog)
94                     .isAllowComments();
95      }
96  
97      public void doPerform(RunData data, Context context)
98              throws Exception {
99          log.debug("Unknown action requested");
100         data.setMessage("Unknown action requested");
101     }
102 }