⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postaction.java

📁 一个论坛程序的简单实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (c) 2003, Rafael Steil
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided 
 * that the following conditions are met:
 * 
 * 1) Redistributions of source code must retain the above 
 * copyright notice, this list of conditions and the 
 * following  disclaimer.
 * 2)  Redistributions in binary form must reproduce the 
 * above copyright notice, this list of conditions and 
 * the following disclaimer in the documentation and/or 
 * other materials provided with the distribution.
 * 3) Neither the name of "Rafael Steil" nor 
 * the names of its contributors may be used to endorse 
 * or promote products derived from this software without 
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
 * IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
 * 
 * This file creation date: May 3, 2003 / 5:05:18 PM
 * The JForum Project
 * http://www.jforum.net
 */
package net.jforum.view.forum;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import net.jforum.Command;
import net.jforum.JForum;
import net.jforum.SessionFacade;
import net.jforum.entities.Post;
import net.jforum.entities.Topic;
import net.jforum.entities.User;
import net.jforum.entities.UserSession;
import net.jforum.model.DataAccessDriver;
import net.jforum.model.ForumModel;
import net.jforum.model.PostModel;
import net.jforum.model.TopicModel;
import net.jforum.model.UserModel;
import net.jforum.repository.ForumRepository;
import net.jforum.repository.RankingRepository;
import net.jforum.repository.SecurityRepository;
import net.jforum.repository.SmiliesRepository;
import net.jforum.repository.TopicRepository;
import net.jforum.security.PermissionControl;
import net.jforum.security.SecurityConstants;
import net.jforum.util.I18n;
import net.jforum.util.concurrent.executor.QueuedExecutor;
import net.jforum.util.mail.EmailSenderTask;
import net.jforum.util.mail.TopicSpammer;
import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;

/**
 * @author Rafael Steil
 * @version $Id: PostAction.java,v 1.24 2004/11/08 01:03:21 rafaelsteil Exp $
 */
public class PostAction extends Command 
{
	private static final Logger logger = Logger.getLogger(PostAction.class);
	
    public void list() throws Exception 
	{
        PostModel pm = DataAccessDriver.getInstance().newPostModel();
        UserModel um = DataAccessDriver.getInstance().newUserModel();
        TopicModel tm = DataAccessDriver.getInstance().newTopicModel();

        int topicId = Integer.parseInt(JForum.getRequest().getParameter("topic_id"));
        Topic topic = tm.selectById(topicId);
        
        // The topic exists?
        if (topic.getId() == 0) {
            this.topicNotFound();
            return;
        }

        // Shall we proceed?
        if (!TopicsCommon.isTopicAccessible(topic.getForumId())) {
        	return;
        }

        tm.incrementTotalViews(topic.getId());

        ((HashMap) SessionFacade.getAttribute("topics_tracking")).put(new Integer(topic.getId()),
                new Long(topic.getLastPostTimeInMillis().getTime()));

        int count = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE);
        int start = ViewCommon.getStartPage();

        List posts = pm.selectAllByTopicByLimit(topicId, start, count);
        List helperList = new ArrayList();
        Map usersMap = new HashMap();

        int userId = SessionFacade.getUserSession().getUserId();
        int anonymousUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
        PermissionControl pc = SecurityRepository.get(userId);

        boolean canEdit = false;
        if (pc.canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT)) {
            canEdit = true;
        }

        Iterator iter = posts.iterator();
        while (iter.hasNext()) {
            Post p = (Post) iter.next();
            if (canEdit || (p.getUserId() != anonymousUser && p.getUserId() == userId)) {
                p.setCanEdit(true);
            }

            Integer posterId = new Integer(p.getUserId());
            if (!usersMap.containsKey(posterId)) {
                User u = um.selectById(p.getUserId());
                u.setSignature(PostCommon.processText(u.getSignature()));
                u.setSignature(PostCommon.processSmilies(u.getSignature(), 
                		SmiliesRepository.getSmilies()));

                usersMap.put(posterId, u);
            }

            helperList.add(PostCommon.preparePostForDisplay(p));
        }

        boolean isModerator = (pc.canAccess(SecurityConstants.PERM_MODERATION))
                && (pc.canAccess(SecurityConstants.PERM_MODERATION_FORUMS, Integer.toString(topic
                        .getForumId())));

        // Set the topic status as read
        tm.updateReadStatus(topic.getId(), userId, true);

        JForum.getContext().put("rssEnabled", SystemGlobals.getBoolValue(ConfigKeys.RSS_ENABLED));
        JForum.getContext().put("canRemove",
                SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE));
        JForum.getContext().put("canEdit",
                SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT));
        JForum.getContext().put("moduleAction", "post_show.htm");
        JForum.getContext().put("topic", topic);
        JForum.getContext().put("rank", new RankingRepository());
        JForum.getContext().put("posts", helperList);
        JForum.getContext().put("forum", ForumRepository.getForum(topic.getForumId()));
        JForum.getContext().put("users", usersMap);
        JForum.getContext().put("topicId", new Integer(topicId));
        JForum.getContext().put("watching",
                tm.isUserSubscribed(topicId, SessionFacade.getUserSession().getUserId()));
        JForum.getContext().put("pageTitle",
                SystemGlobals.getValue(ConfigKeys.FORUM_NAME) + " - " + topic.getTitle());
        JForum.getContext().put("isAdmin",
                SecurityRepository.canAccess(SecurityConstants.PERM_ADMINISTRATION));
        JForum.getContext().put(
                "readonly",
                !SecurityRepository.canAccess(SecurityConstants.PERM_READ_ONLY_FORUMS, Integer
                        .toString(topic.getForumId())));

        JForum.getContext().put(
                "isModerator",
                SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION)
                        && SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_FORUMS,
                                Integer.toString(topic.getForumId())));

        // Topic Status
        JForum.getContext().put("STATUS_LOCKED", new Integer(Topic.STATUS_LOCKED));
        JForum.getContext().put("STATUS_UNLOCKED", new Integer(Topic.STATUS_UNLOCKED));

        // Pagination
        int totalPosts = tm.getTotalPosts(topic.getId());
        JForum.getContext().put("totalPages", new Double(Math.ceil( (double)totalPosts / (double)count )));
        JForum.getContext().put("recordsPerPage", new Integer(count));
        JForum.getContext().put("totalRecords", new Integer(totalPosts));
        JForum.getContext().put("thisPage", new Double(Math.ceil( (double)(start+1) / (double)count )));
        JForum.getContext().put("start", new Integer(start));
    }

    private void topicNotFound() {
        JForum.getContext().put("moduleAction", "message.htm");
        JForum.getContext().put("message", I18n.getMessage("PostShow.TopicNotFound"));
    }

    private void postNotFound() {
        JForum.getContext().put("moduleAction", "message.htm");
        JForum.getContext().put("message", I18n.getMessage("PostShow.PostNotFound"));
    }

    public void insert() throws Exception {
        int forumId = Integer.parseInt(JForum.getRequest().getParameter("forum_id"));

        if (!this.anonymousPost(forumId)
                || this.isForumReadonly(forumId,
                        JForum.getRequest().getParameter("topic_id") != null)) {
            return;
        }

        ForumModel fm = DataAccessDriver.getInstance().newForumModel();

        if (JForum.getRequest().getParameter("topic_id") != null) {
            int topicId = Integer.parseInt(JForum.getRequest().getParameter("topic_id"));
            Topic t = DataAccessDriver.getInstance().newTopicModel().selectById(topicId);

            if (t.getStatus() == Topic.STATUS_LOCKED) {
                this.topicLocked();
                return;
            }

            JForum.getContext().put("topic", t);
            JForum.getContext().put("setType", false);
        } 
        else {
            JForum.getContext().put("setType", true);
        }

        JForum.getContext().put("forum", ForumRepository.getForum(forumId));
        JForum.getContext().put("action", "insertSave");
        JForum.getContext().put("moduleAction", "post_form.htm");
        JForum.getContext().put("start", JForum.getRequest().getParameter("start"));
        JForum.getContext().put("isNewPost", true);
        JForum.getContext().put("htmlAllowed", SecurityRepository.canAccess(
        		SecurityConstants.PERM_HTML_DISABLED, Integer.toString(forumId)));
        JForum.getContext().put("canCreateStickyOrAnnouncementTopics",
                SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_STICKY_ANNOUNCEMENT_TOPICS));

        int userId = SessionFacade.getUserSession().getUserId();
        User user = DataAccessDriver.getInstance().newUserModel().selectById(userId);
        
        if (JForum.getRequest().getParameter("preview") != null) {
        	user.setNotifyOnMessagesEnabled(JForum.getRequest().getParameter("notify") != null);
        }

        JForum.getContext().put("user", user);
    }

    public void edit() throws Exception {
        this.edit(false, null);
    }

    private void edit(boolean preview, Post p) throws Exception {
        int sUserId = SessionFacade.getUserSession().getUserId();
        int aId = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
        boolean canAccess = false;

        if (!preview) {
            PostModel pm = DataAccessDriver.getInstance().newPostModel();
            p = pm.selectById(Integer.parseInt(JForum.getRequest().getParameter("post_id")));

            // The post exist?
            if (p.getId() == 0) {
                this.postNotFound();
                return;
            }
        }

        boolean isModerator = SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT);
        canAccess = (isModerator || p.getUserId() == sUserId);

        if ((sUserId != aId) && canAccess) {
            Topic topic = DataAccessDriver.getInstance().newTopicModel().selectById(p.getTopicId());

            if (!TopicsCommon.isTopicAccessible(topic.getForumId())) {
                return;
            }

            if (topic.getStatus() == Topic.STATUS_LOCKED && !isModerator) {
                this.topicLocked();
                return;
            }

            if (preview && JForum.getRequest().getParameter("topic_type") != null) {
                topic.setType(Integer.parseInt(JForum.getRequest().getParameter("topic_type")));
            }

            JForum.getContext().put("forum", ForumRepository.getForum(p.getForumId()));
            JForum.getContext().put("action", "editSave");

            JForum.getContext().put("post", p);
            JForum.getContext().put("setType", p.getId() == topic.getFirstPostId());
            JForum.getContext().put("topic", topic);
            JForum.getContext().put("moduleAction", "post_form.htm");
            JForum.getContext().put("start", JForum.getRequest().getParameter("start"));
            JForum.getContext().put("htmlAllowed", SecurityRepository.canAccess(
            		SecurityConstants.PERM_HTML_DISABLED, Integer.toString(topic.getForumId())));
            JForum.getContext().put("canCreateStickyOrAnnouncementTopics",
                    SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_STICKY_ANNOUNCEMENT_TOPICS));
        } 
        else {
            JForum.getContext().put("moduleAction", "message.htm");
            JForum.getContext().put("message", I18n.getMessage("CannotEditPost"));
        }

        User u = DataAccessDriver.getInstance().newUserModel().selectById(sUserId);
        u.setSignature(PostCommon.processText(u.getSignature()));
        u.setSignature(PostCommon.processSmilies(u.getSignature(), SmiliesRepository.getSmilies()));
        
        if (preview) {
        	u.setNotifyOnMessagesEnabled(JForum.getRequest().getParameter("notify") != null);
        }

        JForum.getContext().put("user", u);
    }

    public void quote() throws Exception {
        PostModel pm = DataAccessDriver.getInstance().newPostModel();
        Post p = pm.selectById(Integer.parseInt(JForum.getRequest().getParameter("post_id")));

        if (!this.anonymousPost(p.getForumId())) {
            return;
        }

        Topic t = DataAccessDriver.getInstance().newTopicModel().selectById(p.getTopicId());

        if (!TopicsCommon.isTopicAccessible(t.getForumId())) {
            return;
        }

        if (t.getStatus() == Topic.STATUS_LOCKED) {
            this.topicLocked();
            return;
        }

        if (p.getId() == 0) {
            this.postNotFound();
            return;
        }

        JForum.getContext().put("forum", ForumRepository.getForum(p.getForumId()));
        JForum.getContext().put("action", "insertSave");
        JForum.getContext().put("post", p);

        UserModel um = DataAccessDriver.getInstance().newUserModel();
        User u = um.selectById(p.getUserId());
        
        Topic topic = DataAccessDriver.getInstance().newTopicModel().selectById(p.getTopicId());

        JForum.getContext().put("topic", topic);
        JForum.getContext().put("quote", "true");
        JForum.getContext().put("quoteUser", u.getUsername());
        JForum.getContext().put("moduleAction", "post_form.htm");
        JForum.getContext().put("setType", false);
        JForum.getContext().put("htmlAllowed", SecurityRepository.canAccess(
                		SecurityConstants.PERM_HTML_DISABLED, Integer.toString(topic.getForumId())));
        JForum.getContext().put("start", JForum.getRequest().getParameter("start"));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -