1 package net.sourceforge.blogentis.xmlrpc.impl;
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.Date;
26 import java.util.Hashtable;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Vector;
30
31 import net.sourceforge.blogentis.om.Blog;
32 import net.sourceforge.blogentis.om.StoredBlogPeer;
33 import net.sourceforge.blogentis.om.Post;
34 import net.sourceforge.blogentis.om.PostPeer;
35 import net.sourceforge.blogentis.om.TurbineGroupPeer;
36 import net.sourceforge.blogentis.om.TurbineUserGroupRolePeer;
37 import net.sourceforge.blogentis.om.TurbineUserPeer;
38 import net.sourceforge.blogentis.utils.AbsoluteLinkURL;
39 import net.sourceforge.blogentis.utils.BlogConstants;
40 import net.sourceforge.blogentis.utils.DateUtils;
41
42 import org.apache.torque.util.Criteria;
43 import org.apache.turbine.om.security.User;
44 import org.apache.xmlrpc.XmlRpcException;
45
46 /***
47 * @author abas
48 */
49 public class BloggerExecutor
50 extends AuthenticationExecutor {
51 public Vector getUsersBlogs(String appkey, String username, String password)
52 throws Exception {
53 haveUser(username, password);
54 Criteria c = new Criteria();
55 c.addJoin(TurbineUserGroupRolePeer.GROUP_ID, TurbineGroupPeer.GROUP_ID);
56 c.addJoin(TurbineUserGroupRolePeer.USER_ID, TurbineUserPeer.USER_ID);
57 c.addJoin(TurbineGroupPeer.GROUP_NAME, StoredBlogPeer.NAME);
58 c.add(TurbineUserPeer.LOGIN_NAME, username);
59 Vector v = new Vector(StoredBlogPeer.doSelect(c));
60 AbsoluteLinkURL l = new AbsoluteLinkURL();
61 for(int i = 0; i < v.size(); i++) {
62 Blog b = (Blog)v.get(i);
63 Hashtable h = new Hashtable();
64 h.put("url", l.permaLink(b).toString());
65 h.put("blogid", b.getName());
66 h.put("blogName", b.getTitle());
67 v.set(i, h);
68 }
69 return v;
70 }
71
72 public Hashtable getUserInfo(String appkey, String username, String password)
73 throws XmlRpcException {
74 User u = haveUser(username, password);
75 Hashtable h = new Hashtable();
76 h.put("nickname", u.getName());
77 h.put("userid", u.getName());
78
79 h.put("url", "http://www.localhost.com/");
80 h.put("email", u.getEmail());
81 h.put("firstname", u.getFirstName());
82 h.put("lastname", u.getLastName());
83 return h;
84 }
85
86 protected void setPostContent(Post post, String content) {
87 int pos = content.indexOf('\n');
88 String firstLine = content.substring(0, pos);
89 content = content.substring(pos + 1);
90 if (content.startsWith("<p>"))
91 content = content.substring(3);
92 if (content.endsWith("</p>"))
93 content = content.substring(0, content.length() - 4);
94 post.setTitle(firstLine);
95 post.setShortDescription("");
96 post.setFullText(content);
97 }
98
99 protected Hashtable getPostContents(Post post) {
100 Hashtable h = new Hashtable();
101 h.put("postid", Integer.toString(post.getPostId()));
102 h.put("content", post.getTitle() + "\n" + post.getShortDescription()
103 + post.getFullText());
104 h.put("dateCreated", DateUtils.formatIso8601(post.getPostedTime()));
105 return h;
106 }
107
108 public String newPost(String appkey, String blogid, String username,
109 String password, String content, boolean publish)
110 throws XmlRpcException {
111
112 User u = haveUser(username, password);
113 Blog blog = isAuthenticatedForBlog(blogid, u, BlogConstants.PERM_WRITE_POSTS);
114 Post p = new Post();
115 try {
116 p.setBlogId(blog.getBlogId());
117 setPostContent(p, content);
118 setAuthorID(p, u);
119 if (publish) {
120 p.setPostedTime(new Date());
121 }
122 p.save();
123 return String.valueOf(p.getPostId());
124 } catch (Exception e) {
125 throw new XmlRpcException(1, "Could not save the new post.");
126 }
127 }
128
129 public boolean editPost(String appkey, String postid, String username,
130 String password, String content, boolean publish)
131 throws XmlRpcException {
132 try {
133 User u = haveUser(username, password);
134 Post p = PostPeer.retrieveByPK(Integer.parseInt(postid));
135 isAuthenticatedForBlog(p.getBlog().getName(), u, BlogConstants.PERM_EDIT_POSTS);
136
137 setPostContent(p, content);
138 setAuthorID(p, u);
139 if (publish && p.getPostedTime() == null) {
140 p.setPostedTime(new Date());
141 } else if (!publish && p.getPostedTime() != null) {
142 p.setPostedTime(null);
143 }
144 p.save();
145 return true;
146 } catch (XmlRpcException e1) {
147 throw e1;
148 } catch (Exception e) {
149 throw new XmlRpcException(1, "Could not save the new post.");
150 }
151 }
152
153 public boolean deletePost(String appkey, String postid, String username,
154 String password, boolean publish)
155 throws XmlRpcException {
156 try {
157 User u = haveUser(username, password);
158 Post p = PostPeer.retrieveByPK(Integer.parseInt(postid));
159 isAuthenticatedForBlog(p.getBlog().getName(), u, BlogConstants.PERM_EDIT_POSTS);
160
161 PostPeer.doDelete(p);
162 return true;
163 } catch (XmlRpcException e1) {
164 throw e1;
165 } catch (Exception e) {
166 throw new XmlRpcException(1, "Could not save the new post.");
167 }
168 }
169
170 public Vector getRecentPosts(String appkey, String blogid, String username,
171 String password, int numberOfPosts)
172 throws XmlRpcException {
173
174 User u = haveUser(username, password);
175 Blog blog = isAuthenticatedForBlog(blogid, u, null);
176 List l = getLatestPosts(blog, numberOfPosts);
177 Vector v = new Vector(l.size());
178 int i = 0;
179 for(Iterator it = l.iterator(); it.hasNext(); i++) {
180 v.add(getPostContents((Post)it.next()));
181 }
182 return v;
183 }
184 }