KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > admin > ModerationAction


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * Created on Jan 30, 2005 2:49:29 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.admin;
44
45 import net.jforum.ActionServletRequest;
46 import net.jforum.dao.DataAccessDriver;
47 import net.jforum.dao.PostDAO;
48 import net.jforum.dao.TopicDAO;
49 import net.jforum.dao.UserDAO;
50 import net.jforum.entities.Post;
51 import net.jforum.entities.Topic;
52 import net.jforum.entities.User;
53 import net.jforum.repository.ForumRepository;
54 import net.jforum.repository.PostRepository;
55 import net.jforum.repository.TopicRepository;
56 import net.jforum.util.preferences.ConfigKeys;
57 import net.jforum.util.preferences.SystemGlobals;
58 import net.jforum.util.preferences.TemplateKeys;
59 import net.jforum.view.forum.common.AttachmentCommon;
60 import net.jforum.view.forum.common.PostCommon;
61 import net.jforum.view.forum.common.TopicsCommon;
62 import freemarker.template.SimpleHash;
63
64 /**
65  * @author Rafael Steil
66  * @version $Id: ModerationAction.java,v 1.21 2006/01/16 16:19:41 rafaelsteil Exp $
67  */

68 public class ModerationAction extends AdminCommand
69 {
70     public ModerationAction() {}
71     
72     public ModerationAction(SimpleHash context, ActionServletRequest request)
73     {
74         this.context = context;
75         this.request = request;
76     }
77     
78     /**
79      * @see net.jforum.Command#list()
80      */

81     public void list() throws Exception JavaDoc
82     {
83         this.setTemplateName(TemplateKeys.MODERATION_ADMIN_LIST);
84         this.context.put("infoList", DataAccessDriver.getInstance().newModerationDAO().categoryPendingModeration());
85     }
86     
87     public void view() throws Exception JavaDoc
88     {
89         int forumId = this.request.getIntParameter("forum_id");
90         
91         this.setTemplateName(TemplateKeys.MODERATION_ADMIN_VIEW);
92         this.context.put("forum", ForumRepository.getForum(forumId));
93         this.context.put("topics", DataAccessDriver.getInstance().newModerationDAO().topicsByForum(
94                 forumId));
95     }
96     
97     public void doSave() throws Exception JavaDoc
98     {
99         String JavaDoc[] posts = this.request.getParameterValues("post_id");
100
101         if (posts != null) {
102             TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
103             
104             for (int i = 0; i < posts.length; i++) {
105                 int postId = Integer.parseInt(posts[i]);
106                 
107                 String JavaDoc status = this.request.getParameter("status_" + postId);
108                 
109                 if ("defer".startsWith(status)) {
110                     continue;
111                 }
112                 
113                 if ("aprove".startsWith(status)) {
114                     Post p = DataAccessDriver.getInstance().newPostDAO().selectById(postId);
115                     
116                     UserDAO udao = DataAccessDriver.getInstance().newUserDAO();
117                     User u = udao.selectById(p.getUserId());
118                     
119                     // Check is the post is in fact waiting for moderation
120
if (!p.isModerationNeeded()) {
121                         continue;
122                     }
123                     
124                     boolean first = false;
125                     Topic t = TopicRepository.getTopic(new Topic(p.getTopicId()));
126                     
127                     if (t == null) {
128                         t = tm.selectById(p.getTopicId());
129                         
130                         if (t.getId() == 0) {
131                             first = true;
132                             t = tm.selectRaw(p.getTopicId());
133                         }
134                     }
135                     
136                     DataAccessDriver.getInstance().newModerationDAO().aprovePost(postId);
137                     
138                     boolean firstPost = (t.getFirstPostId() == postId);
139                     
140                     if (!firstPost) {
141                         t.setTotalReplies(t.getTotalReplies() + 1);
142                     }
143                     
144                     t.setLastPostId(postId);
145                     t.setLastPostBy(u);
146                     t.setLastPostDate(p.getTime());
147                     t.setLastPostTime(p.getFormatedTime());
148                     
149                     tm.update(t);
150                     
151                     if (first) {
152                         t = tm.selectById(t.getId());
153                     }
154
155                     TopicsCommon.updateBoardStatus(t, postId, firstPost,
156                             tm, DataAccessDriver.getInstance().newForumDAO());
157                     
158                     ForumRepository.updateForumStats(t, u, p);
159                     TopicsCommon.notifyUsers(t, tm);
160                     
161                     udao.incrementPosts(p.getUserId());
162                     
163                     if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
164                         PostRepository.append(p.getTopicId(), PostCommon.preparePostForDisplay(p));
165                     }
166                 }
167                 else {
168                     PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
169                     Post post = pm.selectById(postId);
170                     
171                     if (post == null || !post.isModerationNeeded()) {
172                         continue;
173                     }
174                     
175                     pm.delete(post);
176                     
177                     new AttachmentCommon(this.request, post.getForumId()).deleteAttachments(postId, post.getForumId());
178                     
179                     int totalPosts = tm.getTotalPosts(post.getTopicId());
180                     
181                     if (totalPosts == 0) {
182                         TopicsCommon.deleteTopic(post.getTopicId(), post.getForumId(), true);
183                     }
184                 }
185             }
186         }
187     }
188     
189     public void save() throws Exception JavaDoc
190     {
191         this.doSave();
192         this.view();
193     }
194 }
195
Popular Tags