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: PostEdit.java,v 1.3 2004/10/31 07:16:22 tassos Exp $
23  //
24  
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  import net.sourceforge.blogentis.om.Blog;
30  import net.sourceforge.blogentis.om.Post;
31  import net.sourceforge.blogentis.om.Section;
32  import net.sourceforge.blogentis.om.SectionPeer;
33  import net.sourceforge.blogentis.plugins.BlogPluginService;
34  import net.sourceforge.blogentis.plugins.base.IPostEditExtensionPoint;
35  import net.sourceforge.blogentis.turbine.BlogParameterParser;
36  import net.sourceforge.blogentis.turbine.BlogRunData;
37  import net.sourceforge.blogentis.turbine.SecureBlogScreen;
38  import net.sourceforge.blogentis.utils.BlogConstants;
39  import net.sourceforge.blogentis.utils.BlogPropertyCache;
40  
41  import org.apache.torque.util.Criteria;
42  import org.apache.turbine.util.RunData;
43  import org.apache.velocity.context.Context;
44  
45  public class PostEdit
46          extends SecureBlogScreen {
47      public static class PluginHelper {
48          IPostEditExtensionPoint point;
49          Post p;
50          BlogRunData data;
51  
52          PluginHelper(BlogRunData data, Blog b, Post p) {
53              point = (IPostEditExtensionPoint)BlogPluginService
54                  .locateExtensionPoint(b, IPostEditExtensionPoint.class);
55              this.p = p;
56              this.data = data;
57          }
58  
59          public String getOptions() {
60              return point.buildOptionsHTML(data, p);
61          }
62      }
63  
64      protected Post getPost(RunData data)
65              throws Exception {
66          BlogParameterParser bpp = (BlogParameterParser)data.getParameters();
67          Post p = bpp.getPost();
68          if (p == null) {
69              data.setMessage("No post specified");
70              return null;
71          }
72          if (p.getBlogId() != bpp.getBlog().getBlogId()) {
73              data.setMessage("This post is from an other blog!");
74              return null;
75          }
76          return p;
77      }
78  
79      protected List makeSectionList(List sections) {
80          ArrayList l = new ArrayList();
81          for(Iterator i = sections.iterator(); i.hasNext();) {
82              Section s = (Section)i.next();
83              l.add(new Integer(s.getSectionId()));
84          }
85          return l;
86      }
87  
88      protected void doEdit(BlogRunData data, Context context)
89              throws Exception {
90          Post post = getPost(data);
91          if (post == null)
92              return;
93          context.put("post", post);
94          context.put("postSections", makeSectionList(post.getSections()));
95          context.put("pluginHelper", new PluginHelper(data, data.getBlog(), post));
96      }
97  
98      protected void doAdd(BlogRunData data, Context context)
99              throws Exception {
100         context.put("pluginHelper", new PluginHelper(data, data.getBlog(), null));
101     }
102 
103     protected void doBuildTemplate(RunData data, Context context)
104             throws Exception {
105         super.doBuildTemplate(data, context);
106         BlogParameterParser bpp = (BlogParameterParser)data.getParameters();
107         Criteria c = new Criteria()
108             .addAscendingOrderByColumn(SectionPeer.TITLE);
109         context.put("blogSections", makeSectionList(bpp.getBlog()
110             .getSections()));
111         context.put("properties", BlogPropertyCache.getInstance()
112             .getEditProperties(bpp.getBlog()));
113         if (bpp.getString("create") != null) {
114             doAdd((BlogRunData)data, context);
115         } else {
116             doEdit((BlogRunData)data, context);
117         }
118     }
119 
120     protected String[] getPermissions() {
121         // FIXME: need to write proper authentication here :-/
122         return new String[] {BlogConstants.PERM_WRITE_POSTS};
123     }
124 }