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

📄 postaction.java

📁 一个论坛程序的简单实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        int userId = SessionFacade.getUserSession().getUserId();
        JForum.getContext().put("user",
                DataAccessDriver.getInstance().newUserModel().selectById(userId));
    }

    public void editSave() throws Exception {
        PostModel pm = DataAccessDriver.getInstance().newPostModel();
        TopicModel tm = DataAccessDriver.getInstance().newTopicModel();

        Post p = pm.selectById(Integer.parseInt(JForum.getRequest().getParameter("post_id")));
        p = PostCommon.fillPostFromRequest(p);

        // The user wants to preview the message before posting it?
        if (JForum.getRequest().getParameter("preview") != null) {
            JForum.getContext().put("preview", true);

            Post postPreview = new Post(p);
            JForum.getContext().put("postPreview", PostCommon.preparePostForDisplay(postPreview));

            this.edit(true, p);
        } 
        else {
            Topic t = tm.selectById(p.getTopicId());

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

            if (t.getStatus() == Topic.STATUS_LOCKED
                    && !SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT)) {
                this.topicLocked();
                return;
            }

            pm.update(p);

            // Updates the topic title
            if (t.getFirstPostId() == p.getId()) {
                t.setTitle(p.getSubject());
                t.setType(Integer.parseInt(JForum.getRequest().getParameter("topic_type")));
                tm.update(t);
                ForumRepository.reloadForum(t.getForumId());
                TopicRepository.clearCache(t.getForumId());
            }

            if (JForum.getRequest().getParameter("notify") == null) {
                tm.removeSubscription(p.getTopicId(), SessionFacade.getUserSession().getUserId());
            }
            
            // Updates cache for latest topic
            TopicRepository.pushTopic(tm.selectById(t.getId()));

            String path = JForum.getRequest().getContextPath() + "/posts/list/";
            String start = JForum.getRequest().getParameter("start");
            if (start != null && !start.equals("0")) {
                path += start + "/";
            }

            path += p.getTopicId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) + "#"
                    + p.getId();
            JForum.setRedirect(path);
        }
    }

    public void insertSave() throws Exception {
        Topic t = new Topic();
        t.setId(-1);
        t.setForumId(Integer.parseInt(JForum.getRequest().getParameter("forum_id")));

        if (!TopicsCommon.isTopicAccessible(t.getForumId())
                || this.isForumReadonly(t.getForumId(), 
                		JForum.getRequest().getParameter("topic_id") != null)) {
            return;
        }

        TopicModel tm = DataAccessDriver.getInstance().newTopicModel();
        PostModel pm = DataAccessDriver.getInstance().newPostModel();
        ForumModel fm = DataAccessDriver.getInstance().newForumModel();

        if (JForum.getRequest().getParameter("topic_id") != null) {
            t = tm.selectById(Integer.parseInt(JForum.getRequest().getParameter("topic_id")));

            // Cannot insert new messages on locked topics
            if (t.getStatus() == Topic.STATUS_LOCKED) {
                this.topicLocked();
                return;
            }
        }

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

        UserSession us = SessionFacade.getUserSession();
        User u = new User();
        u.setId(us.getUserId());
        u.setUsername(us.getUsername());

        t.setPostedBy(u);

        // Set the Post
        Post p = PostCommon.fillPostFromRequest();
        p.setForumId(Integer.parseInt(JForum.getRequest().getParameter("forum_id")));

        boolean preview = (JForum.getRequest().getParameter("preview") != null);
        if (!preview) {
            // If topic_id is -1, then is the first post
            if (t.getId() == -1) {
                t.setTime(new Date());
                t.setTitle(JForum.getRequest().getParameter("subject"));

                int topicId = tm.addNew(t);
                t.setId(topicId);
                fm.incrementTotalTopics(t.getForumId(), 1);
            } 
            else {
                tm.incrementTotalReplies(t.getId());
                tm.incrementTotalViews(t.getId());
                
                t.setTotalReplies(t.getTotalReplies() + 1);

                // Ok, we have an answer. Time to notify the subscribed users
                if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_NOTIFY_ANSWERS)) {
                    try {
                        List usersToNotify = tm.notifyUsers(t);
                        
                        // we only have to send an email if there are users subscribed to the topic
                        if (usersToNotify != null && usersToNotify.size() > 0) {
                            QueuedExecutor.getInstance().execute(
                                    new EmailSenderTask(new TopicSpammer(t, usersToNotify)));
                        }
                    } 
                    catch (Exception e) {
                        logger.warn("Error while sending notification emails: " + e);
                    }
                }
            }

            // Topic watch
            if (JForum.getRequest().getParameter("notify") != null) {
                this.watch(tm, t.getId(), u.getId());
            }

            p.setTopicId(t.getId());

            // Save the remaining stuff
            int postId = pm.addNew(p);

            if (JForum.getRequest().getParameter("topic_id") == null) {
                t.setFirstPostId(postId);
            }

            t.setLastPostId(postId);
            tm.update(t);
            
            fm.setLastPost(t.getForumId(), postId);

            ForumRepository.reloadForum(t.getForumId());
            TopicRepository.clearCache(t.getForumId());

            // Updates cache for latest topic
            TopicRepository.pushTopic(tm.selectById(t.getId()));

            String path = JForum.getRequest().getContextPath() + "/posts/list/";

            String start = JForum.getRequest().getParameter("start");
            if (start == null || start.trim().equals("") || Integer.parseInt(start) < 0) {
            	start = "0";
            }

            path += this.startPage(t, Integer.parseInt(start)) + "/";
            path += t.getId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION) + "#" + postId;

            JForum.setRedirect(path);
            ((HashMap) SessionFacade.getAttribute("topics_tracking")).put(new Integer(t.getId()),
                    new Long(p.getTime().getTime()));
        } 
        else {
            JForum.getContext().put("preview", true);
            JForum.getContext().put("post", p);
            JForum.getContext().put("topic", t);
            JForum.getContext().put("start", JForum.getRequest().getParameter("start"));

            Post postPreview = new Post(p);
            JForum.getContext().put("postPreview", PostCommon.preparePostForDisplay(postPreview));

            this.insert();
        }
    }

	private int startPage(Topic t, int currentStart) {
		int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE);
	
		int newStart = ((t.getTotalReplies() / postsPerPage) * postsPerPage);
		if (newStart > currentStart) {
			return newStart;
		} 
		else {
		return currentStart;
		}
	}

    public void delete() throws Exception {
        if (!SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE)) {
            JForum.getContext().put("moduleAction", "message.htm");
            JForum.getContext().put("message", I18n.getMessage("CannotRemovePost"));

            return;
        }

        // Post
        PostModel pm = DataAccessDriver.getInstance().newPostModel();
        Post p = pm.selectById(Integer.parseInt(JForum.getRequest().getParameter("post_id")));

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

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

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

        pm.delete(p);

        // Topic
        tm.decrementTotalReplies(p.getTopicId());

        int maxPostId = tm.getMaxPostId(p.getTopicId());
        if (maxPostId > -1) {
            tm.setLastPostId(p.getTopicId(), maxPostId);
        }

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

        maxPostId = fm.getMaxPostId(p.getForumId());
        if (maxPostId > -1) {
            fm.setLastPost(p.getForumId(), maxPostId);
        }

        // It was the last remaining post in the topic?
        int totalPosts = tm.getTotalPosts(p.getTopicId());
        if (totalPosts > 0) {
            String page = JForum.getRequest().getParameter("start");
            String returnPath = JForum.getRequest().getContextPath() + "/posts/list/";

            if (page != null && !page.equals("") && !page.equals("0")) {
                int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE);
                int newPage = Integer.parseInt(page);

                if (totalPosts % postsPerPage == 0) {
                    newPage -= postsPerPage;
                }

                returnPath += newPage + "/";
            }

            JForum.setRedirect(returnPath + p.getTopicId()
                    + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
        } else {
            // Ok, all posts were removed. Time to say goodbye
            Topic topic = new Topic();
            topic.setId(p.getTopicId());
            tm.delete(topic);
            
            // Updates the Recent Topics if it contains this topic
            TopicRepository.popTopic(topic);
            TopicRepository.loadMostRecentTopics();

            tm.removeSubscriptionByTopic(p.getTopicId());

            fm.decrementTotalTopics(p.getForumId(), 1);

            JForum.setRedirect(JForum.getRequest().getContextPath() + "/forums/show/"
                    + p.getForumId() + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
        }

        ForumRepository.reloadForum(p.getForumId());
        TopicRepository.clearCache(p.getForumId());
    }

    private void watch(TopicModel tm, int topicId, int userId) throws Exception {
        if (!tm.isUserSubscribed(topicId, userId)) {
            tm.subscribeUser(topicId, userId);
        }
    }

    public void watch() throws Exception {
        int topicId = Integer.parseInt(JForum.getRequest().getParameter("topic_id"));
        int userId = SessionFacade.getUserSession().getUserId();

        this.watch(DataAccessDriver.getInstance().newTopicModel(), topicId, userId);
        this.list();
    }

    public void unwatch() throws Exception {
        if (this.isUserLogged()) {
            int topicId = Integer.parseInt(JForum.getRequest().getParameter("topic_id"));
            int userId = SessionFacade.getUserSession().getUserId();
            String start = JForum.getRequest().getParameter("start");

            DataAccessDriver.getInstance().newTopicModel().removeSubscription(topicId, userId);

            String returnPath = JForum.getRequest().getContextPath() + "/posts/list/";
            if (start != null && !start.equals("")) {
                returnPath += start + "/";
            }

            returnPath += topicId + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION);

            JForum.getContext().put("moduleAction", "message.htm");
            JForum.getContext().put("message",
                    I18n.getMessage("ForumBase.unwatched", new String[] { returnPath }));
        } else {
            ViewCommon.contextToLogin();
        }
    }

    private boolean isUserLogged() {
        return (SessionFacade.getAttribute("logged") != null && SessionFacade
                .getAttribute("logged").equals("1"));
    }

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

    private boolean isForumReadonly(int forumId, boolean isReply) throws Exception {
        if (!SecurityRepository.canAccess(SecurityConstants.PERM_READ_ONLY_FORUMS, Integer
                .toString(forumId))) {
            if (isReply) {
                this.list();
            } else {
                JForum.setRedirect(JForum.getRequest().getContextPath() + "/forums/show/" + forumId
                        + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
            }

            return true;
        }

        return false;
    }

    private boolean anonymousPost(int forumId) {
        // Check if anonymous posts are allowed
        if (!this.isUserLogged()
                && !SecurityRepository.canAccess(SecurityConstants.PERM_ANONYMOUS_POST, Integer
                        .toString(forumId))) {
            ViewCommon.contextToLogin();

            return false;
        }

        return true;
    }
}

⌨️ 快捷键说明

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