/*
* 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.co