ForumAction.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 12k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * 
  5.  * Redistribution and use in source and binary forms, 
  6.  * with or without modification, are permitted provided 
  7.  * that the following conditions are met:
  8.  * 
  9.  * 1) Redistributions of source code must retain the above 
  10.  * copyright notice, this list of conditions and the 
  11.  * following  disclaimer.
  12.  * 2)  Redistributions in binary form must reproduce the 
  13.  * above copyright notice, this list of conditions and 
  14.  * the following disclaimer in the documentation and/or 
  15.  * other materials provided with the distribution.
  16.  * 3) Neither the name of "Rafael Steil" nor 
  17.  * the names of its contributors may be used to endorse 
  18.  * or promote products derived from this software without 
  19.  * specific prior written permission.
  20.  * 
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
  22.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  24.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  26.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  27.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  32.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  33.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
  34.  * IN CONTRACT, STRICT LIABILITY, OR TORT 
  35.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  38.  * 
  39.  * This file creation date: Apr 24, 2003 / 10:15:07 PM
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum.view.forum;
  44. import java.text.SimpleDateFormat;
  45. import java.util.Date;
  46. import java.util.GregorianCalendar;
  47. import java.util.HashMap;
  48. import java.util.List;
  49. import java.util.Map;
  50. import net.jforum.Command;
  51. import net.jforum.JForumExecutionContext;
  52. import net.jforum.SessionFacade;
  53. import net.jforum.dao.DataAccessDriver;
  54. import net.jforum.dao.ForumDAO;
  55. import net.jforum.dao.ModerationDAO;
  56. import net.jforum.entities.Forum;
  57. import net.jforum.entities.MostUsersEverOnline;
  58. import net.jforum.entities.UserSession;
  59. import net.jforum.repository.ForumRepository;
  60. import net.jforum.repository.SecurityRepository;
  61. import net.jforum.security.SecurityConstants;
  62. import net.jforum.util.I18n;
  63. import net.jforum.util.preferences.ConfigKeys;
  64. import net.jforum.util.preferences.SystemGlobals;
  65. import net.jforum.util.preferences.TemplateKeys;
  66. import net.jforum.view.admin.ModerationAction;
  67. import net.jforum.view.forum.common.ForumCommon;
  68. import net.jforum.view.forum.common.PostCommon;
  69. import net.jforum.view.forum.common.TopicsCommon;
  70. import net.jforum.view.forum.common.ViewCommon;
  71. /**
  72.  * @author Rafael Steil
  73.  * @version $Id: ForumAction.java,v 1.76 2007/09/03 12:24:32 rafaelsteil Exp $
  74.  */
  75. public class ForumAction extends Command
  76. {
  77. /**
  78.  * List all the forums (first page of forum index)?
  79.  */
  80. public void list()
  81. {
  82. this.setTemplateName(TemplateKeys.FORUMS_LIST);
  83. this.context.put("allCategories", ForumCommon.getAllCategoriesAndForums(true));
  84. this.context.put("topicsPerPage", new Integer(SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE)));
  85. this.context.put("rssEnabled", SystemGlobals.getBoolValue(ConfigKeys.RSS_ENABLED));
  86. this.context.put("totalMessages", new Integer(ForumRepository.getTotalMessages()));
  87. this.context.put("totalRegisteredUsers", ForumRepository .totalUsers());
  88. this.context.put("lastUser", ForumRepository.lastRegisteredUser());
  89. SimpleDateFormat df = new SimpleDateFormat(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
  90. GregorianCalendar gc = new GregorianCalendar();
  91. this.context.put("now", df.format(gc.getTime()));
  92. this.context.put("lastVisit", df.format(SessionFacade.getUserSession().getLastVisit()));
  93. this.context.put("forumRepository", new ForumRepository());
  94. // Online Users
  95. this.context.put("totalOnlineUsers", new Integer(SessionFacade.size()));
  96. int aid = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID);
  97. List onlineUsersList = SessionFacade.getLoggedSessions();
  98. // Check for an optional language parameter
  99. UserSession currentUser = SessionFacade.getUserSession();
  100. if (currentUser.getUserId() == aid) {
  101. String lang = this.request.getParameter("lang");
  102. if (lang != null && I18n.languageExists(lang)) {
  103. currentUser.setLang(lang);
  104. }
  105. }
  106. // If there are only guest users, then just register
  107. // a single one. In any other situation, we do not
  108. // show the "guest" username
  109. if (onlineUsersList.size() == 0) {
  110. UserSession us = new UserSession();
  111. us.setUserId(aid);
  112. us.setUsername(I18n.getMessage("Guest"));
  113. onlineUsersList.add(us);
  114. }
  115. int registeredSize = SessionFacade.registeredSize();
  116. int anonymousSize = SessionFacade.anonymousSize();
  117. int totalOnlineUsers = registeredSize + anonymousSize;
  118. this.context.put("userSessions", onlineUsersList);
  119. this.context.put("totalOnlineUsers", new Integer(totalOnlineUsers));
  120. this.context.put("totalRegisteredOnlineUsers", new Integer(registeredSize));
  121. this.context.put("totalAnonymousUsers", new Integer(anonymousSize));
  122. // Most users ever online
  123. MostUsersEverOnline mostUsersEverOnline = ForumRepository.getMostUsersEverOnline();
  124. if (totalOnlineUsers > mostUsersEverOnline.getTotal()) {
  125. mostUsersEverOnline.setTotal(totalOnlineUsers);
  126. mostUsersEverOnline.setTimeInMillis(System.currentTimeMillis());
  127. ForumRepository.updateMostUsersEverOnline(mostUsersEverOnline);
  128. }
  129. this.context.put("mostUsersEverOnline", mostUsersEverOnline);
  130. }
  131. public void moderation()
  132. {
  133. this.context.put("openModeration", true);
  134. this.show();
  135. }
  136. /**
  137.  * Display all topics in a forum
  138.  */
  139. public void show()
  140. {
  141. int forumId = this.request.getIntParameter("forum_id");
  142. ForumDAO fm = DataAccessDriver.getInstance().newForumDAO();
  143. // The user can access this forum?
  144. Forum forum = ForumRepository.getForum(forumId);
  145. if (forum == null || !ForumRepository.isCategoryAccessible(forum.getCategoryId())) {
  146. new ModerationHelper().denied(I18n.getMessage("ForumListing.denied"));
  147. return;
  148. }
  149. int start = ViewCommon.getStartPage();
  150. List tmpTopics = TopicsCommon.topicsByForum(forumId, start);
  151. this.setTemplateName(TemplateKeys.FORUMS_SHOW);
  152. // Moderation
  153. UserSession userSession = SessionFacade.getUserSession();
  154. boolean isLogged = SessionFacade.isLogged();
  155. boolean isModerator = userSession.isModerator(forumId);
  156. boolean canApproveMessages = (isLogged && isModerator 
  157. && SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_APPROVE_MESSAGES));
  158. Map topicsToApprove = new HashMap();
  159. if (canApproveMessages) {
  160. ModerationDAO mdao = DataAccessDriver.getInstance().newModerationDAO();
  161. topicsToApprove = mdao.topicsByForum(forumId);
  162. this.context.put("postFormatter", new PostCommon());
  163. }
  164. this.context.put("topicsToApprove", topicsToApprove);
  165. this.context.put("attachmentsEnabled", SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_ENABLED,
  166.         Integer.toString(forumId))
  167.         || SecurityRepository.canAccess(SecurityConstants.PERM_ATTACHMENTS_DOWNLOAD));
  168. this.context.put("topics", TopicsCommon.prepareTopics(tmpTopics));
  169. this.context.put("allCategories", ForumCommon.getAllCategoriesAndForums(false));
  170. this.context.put("forum", forum);
  171. this.context.put("rssEnabled", SystemGlobals.getBoolValue(ConfigKeys.RSS_ENABLED));
  172. this.context.put("pageTitle", forum.getName());
  173. this.context.put("canApproveMessages", canApproveMessages);
  174. this.context.put("replyOnly", !SecurityRepository.canAccess(SecurityConstants.PERM_REPLY_ONLY, Integer
  175.         .toString(forum.getId())));
  176. this.context.put("readonly", !SecurityRepository.canAccess(SecurityConstants.PERM_READ_ONLY_FORUMS, Integer
  177.         .toString(forumId)));
  178. this.context.put("watching", fm.isUserSubscribed(forumId, userSession.getUserId()));
  179. // Pagination
  180. int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
  181. int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
  182. int totalTopics = forum.getTotalTopics();
  183. ViewCommon.contextToPagination(start, totalTopics, topicsPerPage);
  184. this.context.put("postsPerPage", new Integer(postsPerPage));
  185. TopicsCommon.topicListingBase();
  186. this.context.put("moderator", isLogged && isModerator);
  187. }
  188. // Make an URL to some action
  189. private String makeRedirect(String action)
  190. {
  191. String path = this.request.getContextPath() + "/forums/" + action + "/";
  192. String thisPage = this.request.getParameter("start");
  193. if (thisPage != null && !thisPage.equals("0")) {
  194. path += thisPage + "/";
  195. }
  196. String forumId = this.request.getParameter("forum_id");
  197. if (forumId == null) {
  198. forumId = this.request.getParameter("persistData");
  199. }
  200. path += forumId + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION);
  201. return path;
  202. }
  203. // Mark all topics as read
  204. public void readAll()
  205. {
  206. String forumId = this.request.getParameter("forum_id");
  207. if (forumId != null) {
  208. Map tracking = SessionFacade.getTopicsReadTimeByForum();
  209. if (tracking == null) {
  210. tracking = new HashMap();
  211. }
  212. tracking.put(new Integer(forumId), new Long(System.currentTimeMillis()));
  213. SessionFacade.setAttribute(ConfigKeys.TOPICS_READ_TIME_BY_FORUM, tracking);
  214. }
  215. if (forumId != null) {
  216. JForumExecutionContext.setRedirect(this.makeRedirect("show"));
  217. }
  218. else {
  219. JForumExecutionContext.setRedirect(this.request.getContextPath() + "/forums/list"
  220.         + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
  221. }
  222. }
  223. // Messages since last visit
  224. public void newMessages()
  225. {
  226. this.request.addParameter("from_date", SessionFacade.getUserSession().getLastVisit());
  227. this.request.addParameter("to_date", new Date());
  228. SearchAction searchAction = new SearchAction(this.request, this.response, this.context);
  229. searchAction.newMessages();
  230. this.setTemplateName(TemplateKeys.SEARCH_NEW_MESSAGES);
  231. }
  232. public void approveMessages()
  233. {
  234. if (SessionFacade.getUserSession().isModerator(this.request.getIntParameter("forum_id"))) {
  235. new ModerationAction(this.context, this.request).doSave();
  236. }
  237. JForumExecutionContext.setRedirect(this.request.getContextPath() + "/forums/show/"
  238. + this.request.getParameter("forum_id") + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
  239. }
  240. /**
  241.  * Action when users click on "watch this forum"
  242.  * It gets teh forum_id and userId, and put them into a watch_forum table in the database;
  243.  */
  244. public void watchForum()
  245. {
  246. int forumId = this.request.getIntParameter("forum_id");
  247. int userId = SessionFacade.getUserSession().getUserId();
  248. this.watchForum(DataAccessDriver.getInstance().newForumDAO(), forumId, userId);
  249. JForumExecutionContext.setRedirect(this.redirectLinkToShowAction(forumId));
  250. }
  251. public void banned()
  252. {
  253. this.setTemplateName(TemplateKeys.FORUMS_BANNED);
  254. this.context.put("message", I18n.getMessage("ForumBanned.banned"));
  255. }
  256. private String redirectLinkToShowAction(int forumId)
  257. {
  258. int start = ViewCommon.getStartPage();
  259. return this.request.getContextPath() + "/forums/show/" + (start > 0 ? start + "/" : "") + forumId
  260. + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION);
  261. }
  262. /**
  263.  * 
  264.  * @param dao ForumDAO
  265.  * @param forumId int
  266.  * @param userId int
  267.  */
  268. private void watchForum(ForumDAO dao, int forumId, int userId)
  269. {
  270. if (SessionFacade.isLogged() && !dao.isUserSubscribed(forumId, userId)) {
  271. dao.subscribeUser(forumId, userId);
  272. }
  273. }
  274. /**
  275.  * Unwatch the forum watched.
  276.  */
  277. public void unwatchForum()
  278. {
  279. if (SessionFacade.isLogged()) {
  280. int forumId = this.request.getIntParameter("forum_id");
  281. int userId = SessionFacade.getUserSession().getUserId();
  282. DataAccessDriver.getInstance().newForumDAO().removeSubscription(forumId, userId);
  283. String returnPath = this.redirectLinkToShowAction(forumId);
  284. this.setTemplateName(TemplateKeys.POSTS_UNWATCH);
  285. this.context.put("message", I18n.getMessage("ForumBase.forumUnwatched", new String[] { returnPath }));
  286. }
  287. else {
  288. this.setTemplateName(ViewCommon.contextToLogin());
  289. }
  290. }
  291. }