1 package net.sourceforge.blogentis.modules.actions;
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 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 }