001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on Jan 30, 2005 2:49:29 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.admin;
044:
045: import net.jforum.context.RequestContext;
046: import net.jforum.dao.DataAccessDriver;
047: import net.jforum.dao.PostDAO;
048: import net.jforum.dao.TopicDAO;
049: import net.jforum.dao.UserDAO;
050: import net.jforum.entities.Post;
051: import net.jforum.entities.Topic;
052: import net.jforum.entities.User;
053: import net.jforum.repository.ForumRepository;
054: import net.jforum.repository.PostRepository;
055: import net.jforum.repository.TopicRepository;
056: import net.jforum.util.preferences.ConfigKeys;
057: import net.jforum.util.preferences.SystemGlobals;
058: import net.jforum.util.preferences.TemplateKeys;
059: import net.jforum.view.forum.common.AttachmentCommon;
060: import net.jforum.view.forum.common.PostCommon;
061: import net.jforum.view.forum.common.TopicsCommon;
062: import freemarker.template.SimpleHash;
063:
064: /**
065: * @author Rafael Steil
066: * @version $Id: ModerationAction.java,v 1.27 2007/09/10 23:07:00 rafaelsteil Exp $
067: */
068: public class ModerationAction extends AdminCommand {
069: public ModerationAction() {
070: }
071:
072: public ModerationAction(SimpleHash context, RequestContext request) {
073: this .context = context;
074: this .request = request;
075: }
076:
077: /**
078: * @see net.jforum.Command#list()
079: */
080: public void list() {
081: this .setTemplateName(TemplateKeys.MODERATION_ADMIN_LIST);
082: this .context.put("infoList", DataAccessDriver.getInstance()
083: .newModerationDAO().categoryPendingModeration());
084: }
085:
086: public void view() {
087: int forumId = this .request.getIntParameter("forum_id");
088:
089: this .setTemplateName(TemplateKeys.MODERATION_ADMIN_VIEW);
090: this .context.put("forum", ForumRepository.getForum(forumId));
091: this .context.put("topics", DataAccessDriver.getInstance()
092: .newModerationDAO().topicsByForum(forumId));
093: }
094:
095: public void doSave() {
096: String[] posts = this .request.getParameterValues("post_id");
097:
098: if (posts != null) {
099: TopicDAO topicDao = DataAccessDriver.getInstance()
100: .newTopicDAO();
101:
102: for (int i = 0; i < posts.length; i++) {
103: int postId = Integer.parseInt(posts[i]);
104:
105: String status = this .request.getParameter("status_"
106: + postId);
107:
108: if ("defer".startsWith(status)) {
109: continue;
110: }
111:
112: if ("aprove".startsWith(status)) {
113: Post p = DataAccessDriver.getInstance()
114: .newPostDAO().selectById(postId);
115:
116: // Check is the post is in fact waiting for moderation
117: if (!p.isModerationNeeded()) {
118: continue;
119: }
120:
121: UserDAO userDao = DataAccessDriver.getInstance()
122: .newUserDAO();
123: User u = userDao.selectById(p.getUserId());
124:
125: boolean first = false;
126: Topic t = TopicRepository.getTopic(new Topic(p
127: .getTopicId()));
128:
129: if (t == null) {
130: t = topicDao.selectById(p.getTopicId());
131:
132: if (t.getId() == 0) {
133: first = true;
134: t = topicDao.selectRaw(p.getTopicId());
135: }
136: }
137:
138: DataAccessDriver.getInstance().newModerationDAO()
139: .aprovePost(postId);
140:
141: boolean firstPost = (t.getFirstPostId() == postId);
142:
143: if (!firstPost) {
144: t.setTotalReplies(t.getTotalReplies() + 1);
145: }
146:
147: t.setLastPostId(postId);
148: t.setLastPostBy(u);
149: t.setLastPostDate(p.getTime());
150: t.setLastPostTime(p.getFormatedTime());
151:
152: topicDao.update(t);
153:
154: if (first) {
155: t = topicDao.selectById(t.getId());
156: }
157:
158: TopicsCommon.updateBoardStatus(t, postId,
159: firstPost, topicDao, DataAccessDriver
160: .getInstance().newForumDAO());
161:
162: ForumRepository.updateForumStats(t, u, p);
163: TopicsCommon.notifyUsers(t, p);
164:
165: userDao.incrementPosts(p.getUserId());
166:
167: if (SystemGlobals
168: .getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
169: PostRepository.append(p.getTopicId(),
170: PostCommon.preparePostForDisplay(p));
171: }
172: } else {
173: PostDAO pm = DataAccessDriver.getInstance()
174: .newPostDAO();
175: Post post = pm.selectById(postId);
176:
177: if (post == null || !post.isModerationNeeded()) {
178: continue;
179: }
180:
181: pm.delete(post);
182:
183: new AttachmentCommon(this .request, post
184: .getForumId()).deleteAttachments(postId,
185: post.getForumId());
186:
187: int totalPosts = topicDao.getTotalPosts(post
188: .getTopicId());
189:
190: if (totalPosts == 0) {
191: TopicsCommon.deleteTopic(post.getTopicId(),
192: post.getForumId(), true);
193: }
194: }
195: }
196: }
197: }
198:
199: public void save() {
200: this.doSave();
201: this.view();
202: }
203: }
|