View Javadoc

1   package net.sourceforge.blogentis.xmlrpc.impl;
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: MovableTypeExecutor.java,v 1.3 2004/10/31 07:16:22 tassos Exp $
23  //
24  
25  import java.sql.Connection;
26  import java.util.ArrayList;
27  import java.util.Hashtable;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Vector;
32  
33  import net.sourceforge.blogentis.om.Blog;
34  import net.sourceforge.blogentis.om.Post;
35  import net.sourceforge.blogentis.om.PostPeer;
36  import net.sourceforge.blogentis.om.PostSectionPeer;
37  import net.sourceforge.blogentis.om.Section;
38  import net.sourceforge.blogentis.om.SectionPeer;
39  import net.sourceforge.blogentis.utils.BlogConstants;
40  import net.sourceforge.blogentis.utils.DateUtils;
41  
42  import org.apache.torque.TorqueException;
43  import org.apache.torque.util.Transaction;
44  import org.apache.turbine.om.security.User;
45  import org.apache.xmlrpc.XmlRpcException;
46  
47  /***
48   * @author abas
49   */
50  public class MovableTypeExecutor
51          extends MetaWeblogExecutor {
52      protected Hashtable getSectionHash(Section s) {
53          Hashtable h = new Hashtable();
54          h.put("categoryId", s.getName());
55          h.put("categoryName", s.getTitle());
56          return h;
57      }
58  
59      public Vector getCategoryList(String blogid, String username,
60                                    String password)
61              throws XmlRpcException {
62          User u = haveUser(username, password);
63          Blog blog = isAuthenticatedForBlog(blogid, u, null);
64  
65          try {
66              List l;
67              l = blog.getSections();
68              Vector v = new Vector();
69              for(Iterator i = l.iterator(); i.hasNext();) {
70                  v.add(getSectionHash((Section)i.next()));
71              }
72              return v;
73          } catch (TorqueException e) {
74              throw new XmlRpcException(1,
75                  "Could not retrieve the category list!");
76          }
77      }
78  
79      public Vector getRecentPostTitles(String blogid, String username,
80                                        String password, int NumberOfPosts)
81              throws XmlRpcException {
82          User u = haveUser(username, password);
83          Blog blog = isAuthenticatedForBlog(blogid, u, null);
84          List l = getLatestPosts(blog, NumberOfPosts);
85          Vector v = new Vector();
86          for(Iterator i = l.iterator(); i.hasNext();) {
87              Post post = (Post)i.next();
88              Hashtable h = new Hashtable();
89              h.put("title", post.getTitle());
90              h.put("userid", post.getAuthorId());
91              h.put("dateCreated", DateUtils.formatIso8601(post.getPostedTime()));
92              h.put("postId", Integer.toString(post.getPostId()));
93              v.add(h);
94          }
95          return v;
96      }
97  
98      public Vector getPostCategories(String postid, String username,
99                                      String password)
100             throws XmlRpcException {
101         try {
102             User u = haveUser(username, password);
103             Post p = PostPeer.retrieveByPK(Integer.parseInt(postid));
104             isAuthenticatedForBlog(p.getBlog().getName(), u, null);
105 
106             Vector v = new Vector();
107             List l = p.getSections();
108             for(Iterator i = l.iterator(); i.hasNext();) {
109                 Hashtable h = getSectionHash((Section)i.next());
110                 h
111                     .put("isPrimary", v.size() == 0 ? Boolean.TRUE
112                             : Boolean.FALSE);
113                 v.add(h);
114             }
115             return v;
116         } catch (TorqueException e) {
117             throw new XmlRpcException(1,
118                 "Could not determine the post categories!");
119         }
120     }
121 
122     public boolean setPostCategories(String postid, String username,
123                                      String password, Vector categories)
124             throws XmlRpcException {
125         try {
126             User u = haveUser(username, password);
127             Post p = PostPeer.retrieveByPK(Integer.parseInt(postid));
128             isAuthenticatedForBlog(p.getBlog().getName(), u, BlogConstants.PERM_EDIT_POSTS);
129             List l = new ArrayList();
130             for(Iterator i = categories.iterator(); i.hasNext();) {
131                 String sectionId = (String)((Map)i.next()).get("categoryId");
132                 try {
133                     l.add(SectionPeer.retrieveSectionByPath(p.getBlogId(),
134                                                             sectionId));
135                 } catch (TorqueException e) {
136                     throw new XmlRpcException(1,
137                         "Could not find section named " + sectionId);
138                 }
139             }
140             Connection c = null;
141             try {
142                 c = Transaction.begin(PostSectionPeer.DATABASE_NAME);
143                 p.replaceSections(c, l);
144                 Transaction.commit(c);
145             } catch (TorqueException e) {
146                 if (c != null)
147                     Transaction.safeRollback(c);
148                 throw e;
149             }
150             return true;
151         } catch (TorqueException e) {
152             throw new XmlRpcException(1, "Could not set the post categories.");
153         }
154     }
155 
156     public Vector getSupportedMethods() {
157         Vector v = new Vector();
158         v.add("getSupportedMethods");
159         v.add("getRecentPostTitles");
160         v.add("getCategoryList");
161         v.add("getPostCategories");
162         v.add("setPostCategories");
163         return v;
164     }
165 }