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.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
122 return new String[] {BlogConstants.PERM_WRITE_POSTS};
123 }
124 }