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: BlogSectionMod.java,v 1.2 2004/10/31 07:16:22 tassos Exp $
23  //
24  
25  import net.sourceforge.blogentis.om.Blog;
26  import net.sourceforge.blogentis.om.Section;
27  import net.sourceforge.blogentis.om.SectionPeer;
28  import net.sourceforge.blogentis.turbine.BlogParameterParser;
29  import net.sourceforge.blogentis.turbine.SecureBlogAction;
30  import net.sourceforge.blogentis.utils.BlogConstants;
31  
32  import org.apache.turbine.util.RunData;
33  import org.apache.velocity.context.Context;
34  
35  public class BlogSectionMod
36          extends SecureBlogAction {
37      private boolean isConformingName(String sectionName) {
38          return sectionName != null && sectionName.length() > 0
39                 && sectionName.matches("^[a-zA-Z0-9_]+$");
40      }
41  
42      public Section getSectionId(RunData data) {
43          BlogParameterParser pp = (BlogParameterParser)data.getParameters();
44          Blog blog = pp.getBlog();
45  
46          Section section = null;
47          try {
48              section = SectionPeer.retrieveByPK(pp.getInt("sectionId", -1));
49          } catch (Exception e) {
50              data.setMessage("Given Section does not exist");
51          }
52          if (section != null && section.getBlogId() != blog.getBlogId()) {
53              data.setMessage("Given Section does not exist");
54              return null;
55          }
56          return section;
57      }
58  
59      public void doRemove(RunData data, Context context)
60              throws Exception {
61          Section section = getSectionId(data);
62          if (section != null)
63              SectionPeer.doDelete(section);
64      }
65  
66      public void doUpdate(RunData data, Context context)
67              throws Exception {
68          Section section = getSectionId(data);
69          if (section == null)
70              return;
71          BlogParameterParser pp = (BlogParameterParser)data.getParameters();
72          if (!isConformingName(pp.getString("name", null))) {
73              data.setMessage("The Name of the section must be a single word");
74              return;
75          }
76          section.setName(pp.getString("name", section.getName()));
77          section.setTitle(pp.getString("title", section.getTitle()));
78          section.setDescription(pp.getString("description", section
79              .getDescription()));
80          section.save();
81      }
82  
83      public void doAdd(RunData data, Context context)
84              throws Exception {
85          BlogParameterParser pp = (BlogParameterParser)data.getParameters();
86  
87          if (!isConformingName(pp.getString("name", null))) {
88              data.setMessage("The Name of the section must be a single word");
89              return;
90          }
91          Section section = new Section();
92          section.setName(pp.getString("name", null));
93          section.setTitle(pp.getString("title", null));
94          section.setDescription(pp.getString("description", null));
95          section.setBlogId(pp.getBlog().getBlogId());
96          section.save();
97      }
98  
99      public void doPerform(RunData data, Context context)
100             throws Exception {
101         data.setMessage("Hmm... Unknown action requested!");
102     }
103     
104     protected String[] getPermissions() {
105         return new String[]{BlogConstants.PERM_ADMIN_BLOG};
106     }
107 }