📄 postaction.java
字号:
/*
* 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.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.jforum.Command;
import net.jforum.JForum;
import net.jforum.SessionFacade;
import net.jforum.entities.Attachment;
import net.jforum.entities.Post;
import net.jforum.entities.QuotaLimit;
import net.jforum.entities.Topic;
import net.jforum.entities.User;
import net.jforum.entities.UserSession;
import net.jforum.exceptions.AttachmentException;
import net.jforum.model.AttachmentModel;
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.PostRepository;
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.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;
import net.jforum.view.forum.common.AttachmentCommon;
import net.jforum.view.forum.common.ForumCommon;
import net.jforum.view.forum.common.PostCommon;
import net.jforum.view.forum.common.TopicsCommon;
import net.jforum.view.forum.common.ViewCommon;
import org.apache.log4j.Logger;
/**
* @author Rafael Steil
* @version $Id: PostAction.java,v 1.68 2005/03/04 14:14:48 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 userId = SessionFacade.getUserSession().getUserId();
int anonymousUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
int topicId = this.request.getIntParameter("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;
}
int count = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE);
int start = ViewCommon.getStartPage();
PermissionControl pc = SecurityRepository.get(userId);
boolean canEdit = false;
if (pc.canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT)) {
canEdit = true;
}
Map usersMap = new HashMap();
List helperList = PostCommon.topicPosts(pm, um, usersMap, canEdit, userId, topic.getId(), start, count);
// Ugly assumption:
// Is moderation pending for the topic?
if (topic.isModerated() && helperList.size() == 0) {
this.notModeratedYet();
return;
}
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);
tm.incrementTotalViews(topic.getId());
if (userId != anonymousUser) {
((HashMap) SessionFacade.getAttribute(ConfigKeys.TOPICS_TRACKING)).put(new Integer(topic.getId()),
new Long(topic.getLastPostTimeInMillis().getTime()));
}
this.context.put("attachmentsEnabled", SecurityRepository.canAccess(
SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(topic.getForumId())));
this.context.put("canDownloadAttachments", SecurityRepository.canAccess(
SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD));
this.context.put("am", new AttachmentCommon(this.request));
this.context.put("karmaVotes", DataAccessDriver.getInstance().newKarmaModel().getUserVotes(topic.getId(), userId));
this.context.put("rssEnabled", SystemGlobals.getBoolValue(ConfigKeys.RSS_ENABLED));
this.context.put("canRemove",
SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_REMOVE));
this.context.put("canEdit", canEdit);
this.context.put("moduleAction", "post_show.htm");
this.context.put("allCategories", ForumCommon.getAllCategoriesAndForums(false));
this.context.put("topic", topic);
this.context.put("rank", new RankingRepository());
this.context.put("posts", helperList);
this.context.put("karmaEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_KARMA_ENABLED));
this.context.put("forum", ForumRepository.getForum(topic.getForumId()));
this.context.put("users", usersMap);
this.context.put("topicId", new Integer(topicId));
this.context.put("anonymousPosts", SecurityRepository.canAccess(SecurityConstants.PERM_ANONYMOUS_POST,
Integer.toString(topic.getForumId())));
this.context.put("watching", tm.isUserSubscribed(topicId, SessionFacade.getUserSession().getUserId()));
this.context.put("pageTitle", SystemGlobals.getValue(ConfigKeys.FORUM_NAME) + " - " + topic.getTitle());
this.context.put("isAdmin", SecurityRepository.canAccess(SecurityConstants.PERM_ADMINISTRATION));
this.context.put("readonly", !SecurityRepository.canAccess(SecurityConstants.PERM_READ_ONLY_FORUMS,
Integer.toString(topic.getForumId())));
this.context.put("replyOnly", !SecurityRepository.canAccess(SecurityConstants.PERM_REPLY_ONLY,
Integer.toString(topic.getForumId())));
this.context.put("isModerator", SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION)
&& SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_FORUMS,
Integer.toString(topic.getForumId())));
// Topic Status
this.context.put("STATUS_LOCKED", new Integer(Topic.STATUS_LOCKED));
this.context.put("STATUS_UNLOCKED", new Integer(Topic.STATUS_UNLOCKED));
// Pagination
int totalPosts = tm.getTotalPosts(topic.getId());
this.context.put("totalPages", new Double(Math.ceil((double) totalPosts / (double) count)));
this.context.put("recordsPerPage", new Integer(count));
this.context.put("totalRecords", new Integer(totalPosts));
this.context.put("thisPage", new Double(Math.ceil((double) (start + 1) / (double) count)));
this.context.put("start", new Integer(start));
}
public void review() throws Exception {
PostModel pm = DataAccessDriver.getInstance().newPostModel();
UserModel um = DataAccessDriver.getInstance().newUserModel();
TopicModel tm = DataAccessDriver.getInstance().newTopicModel();
int userId = SessionFacade.getUserSession().getUserId();
int topicId = this.request.getIntParameter("topic_id");
Topic topic = tm.selectById(topicId);
if (!TopicsCommon.isTopicAccessible(topic.getForumId())) {
return;
}
int count = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE);
int start = ViewCommon.getStartPage();
Map usersMap = new HashMap();
List helperList = PostCommon.topicPosts(pm, um, usersMap, false, userId, topic.getId(), start, count);
Collections.reverse(helperList);
this.setTemplateName(SystemGlobals.getValue(ConfigKeys.TEMPLATE_NAME) + "/empty.htm");
this.context.put("moduleAction", "topic_review.htm");
this.context.put("posts", helperList);
this.context.put("users", usersMap);
}
private void topicNotFound() {
this.context.put("moduleAction", "message.htm");
this.context.put("message", I18n.getMessage("PostShow.TopicNotFound"));
}
private void postNotFound() {
this.context.put("moduleAction", "message.htm");
this.context.put("message", I18n.getMessage("PostShow.PostNotFound"));
}
private void replyOnly()
{
this.context.put("moduleAction", "message.htm");
this.context.put("message", I18n.getMessage("PostShow.replyOnly"));
}
private boolean isReplyOnly(int forumId) throws Exception
{
return !SecurityRepository.canAccess(SecurityConstants.PERM_REPLY_ONLY,
Integer.toString(forumId));
}
public void insert() throws Exception {
int forumId = this.request.getIntParameter("forum_id");
if (!this.anonymousPost(forumId)
|| this.isForumReadonly(forumId, this.request.getParameter("topic_id") != null)) {
return;
}
ForumModel fm = DataAccessDriver.getInstance().newForumModel();
if (this.request.getParameter("topic_id") != null) {
int topicId = this.request.getIntParameter("topic_id");
Topic t = DataAccessDriver.getInstance().newTopicModel().selectById(topicId);
if (t.getStatus() == Topic.STATUS_LOCKED) {
this.topicLocked();
return;
}
this.context.put("topic", t);
this.context.put("setType", false);
}
else {
if (this.isReplyOnly(forumId)) {
this.replyOnly();
return;
}
this.context.put("setType", true);
}
int userId = SessionFacade.getUserSession().getUserId();
this.context.put("attachmentsEnabled", SecurityRepository.canAccess(
SecurityConstants.PERM_ATTACHMENTS_ENABLED, Integer.toString(forumId)));
QuotaLimit ql = new AttachmentCommon(this.request).getQuotaLimit(userId);
this.context.put("maxAttachmentsSize", new Long(ql != null ? ql.getSizeInBytes() : 1));
this.context.put("maxAttachments", SystemGlobals.getValue(ConfigKeys.ATTACHMENTS_MAX_POST));
this.context.put("forum", ForumRepository.getForum(forumId));
this.context.put("action", "insertSave");
this.context.put("moduleAction", "post_form.htm");
this.context.put("start", this.request.getParameter("start"));
this.context.put("isNewPost", true);
this.context.put("htmlAllowed",
SecurityRepository.canAccess(SecurityConstants.PERM_HTML_DISABLED, Integer.toString(forumId)));
this.context.put("canCreateStickyOrAnnouncementTopics",
SecurityRepository.canAccess(SecurityConstants.PERM_CREATE_STICKY_ANNOUNCEMENT_TOPICS));
User user = DataAccessDriver.getInstance().newUserModel().selectById(userId);
user.setSignature(PostCommon.processText(user.getSignature()));
user.setSignature(PostCommon.processSmilies(user.getSignature(), SmiliesRepository.getSmilies()));
if (this.request.getParameter("preview") != null) {
user.setNotifyOnMessagesEnabled(this.request.getParameter("notify") != null);
}
this.context.put("user", user);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -