KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portlet > forums > commands > admin > DeleteMoveForumCommand


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under GPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portlet.forums.commands.admin;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.jboss.portal.common.command.result.Result;
16 import org.jboss.portal.core.modules.ModuleException;
17 import org.jboss.portlet.forums.ForumsModule;
18 import org.jboss.portlet.forums.commands.AbstractCommand;
19 import org.jboss.portlet.forums.model.Forum;
20 import org.jboss.portlet.JBossActionRequest;
21 import org.jboss.portlet.JBossActionResponse;
22
23 /**
24  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
25  * @author <a HREF="mailto:theute@jboss.org">Thomas Heute</a>
26  * @version $Revision: 1.4 $
27  */

28 public class DeleteMoveForumCommand
29    extends AbstractCommand
30 {
31    /** DOCUMENT_ME */
32    public Forum source;
33
34    /** DOCUMENT_ME */
35    public Forum target;
36
37    /** DOCUMENT_ME */
38    public ForumsModule module;
39
40    public DeleteMoveForumCommand(JBossActionRequest request, JBossActionResponse response)
41    {
42       super(request, response);
43    }
44
45    /**
46     * DOCUMENT_ME
47     *
48     * @return DOCUMENT_ME
49     */

50    public Result execute()
51    {
52       Iterator JavaDoc topics = source.getTopics().iterator();
53
54       // TODO This is not good, it makes N requests instead of an atomic:
55
// UPDATE jbp_forums_topics SET jbp_forum_id = 'targetId' WHERE jbp_forum_id = 'sourceId'
56

57       /*
58          while (topics.hasNext())
59          {
60             Topic topic = (Topic)topics.next();
61             topic.setForum(target);
62          }
63        */

64       try
65       {
66          target.setLastPost(source.getLastPost());
67          target.setPostCount(target.getPostCount() + source.getPostCount());
68          target.setTopicCount(target.getTopicCount() + source.getTopicCount());
69          module.removeForum(source);
70       }
71       catch (ModuleException e)
72       {
73          target.setLastPost(null);
74       }
75
76       return TYPE_FORUM_MOVED_AND_REMOVED;
77    }
78
79    /*
80       public Integer sourceId;
81       public Integer targetId;
82       public DataSource postDataSource;
83       public String postTableName;
84       public String postForumFKColumnName;
85       public DataSource topicDataSource;
86       public String topicTableName;
87       public String topicForumFKColumnName;
88       public ForumEJBLocalHome forumHome;
89       public TopicEJBLocalHome topicHome;
90       public PostEJBLocalHome postHome;
91       public Result execute()
92       {
93          Connection conn1 = null;
94          Connection conn2 = null;
95          PreparedStatement ps1 = null;
96          PreparedStatement ps2 = null;
97          try
98          {
99             // get the source and target EJBs
100             ForumEJBLocal source = forumHome.findByPrimaryKey(sourceId);
101             ForumEJBLocal target = forumHome.findByPrimaryKey(targetId);
102             // update the topics
103             conn1 = topicDataSource.getConnection();
104             ps1 = conn1.prepareStatement("UPDATE " + topicTableName + " SET " + topicForumFKColumnName + "=? WHERE " + topicForumFKColumnName + "=?");
105             ps1.setInt(1, target.getId().intValue());
106             ps1.setInt(2, source.getId().intValue());
107             ps1.executeUpdate();
108             // update the posts
109             conn2 = postDataSource.getConnection();
110             ps2 = conn1.prepareStatement("UPDATE " + postTableName + " SET " + postForumFKColumnName + "=? WHERE " + postForumFKColumnName + "=?");
111             ps2.setInt(1, target.getId().intValue());
112             ps2.setInt(2, source.getId().intValue());
113             ps2.executeUpdate();
114             // here we must invalidate the EJB caches
115             // update values cached in EJBs
116             target.setTopicSize(target.getTopicSize() + source.getTopicSize());
117             target.setPostSize(target.getPostSize() + source.getPostSize());
118             try
119             {
120                target.setLastPost(postHome.findLastPost(target));
121             }
122             catch(ObjectNotFoundException e)
123             {
124                target.setLastPost(null);
125             }
126             // finally delete source forum
127             source.remove();
128          }
129          catch(Exception e)
130          {
131             log.error("", e);
132             return TYPE_CANNOT_MOVE_AND_REMOVE_FORUM;
133          }
134          finally
135          {
136             Tools.safeClose(ps2);
137             Tools.safeClose(ps1);
138             Tools.safeClose(conn1);
139             Tools.safeClose(conn2);
140          }
141          return TYPE_FORUM_MOVED_AND_REMOVED;
142       }
143     */

144 }
Popular Tags