RecentTopicsAction.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 5k
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.  * Created on Oct 19, 2004
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum.view.forum;
  44. import java.util.ArrayList;
  45. import java.util.HashMap;
  46. import java.util.Iterator;
  47. import java.util.List;
  48. import java.util.Map;
  49. import net.jforum.Command;
  50. import net.jforum.JForumExecutionContext;
  51. import net.jforum.dao.DataAccessDriver;
  52. import net.jforum.dao.UserDAO;
  53. import net.jforum.entities.Forum;
  54. import net.jforum.entities.Topic;
  55. import net.jforum.entities.User;
  56. import net.jforum.repository.ForumRepository;
  57. import net.jforum.repository.TopicRepository;
  58. import net.jforum.util.I18n;
  59. import net.jforum.util.preferences.ConfigKeys;
  60. import net.jforum.util.preferences.SystemGlobals;
  61. import net.jforum.util.preferences.TemplateKeys;
  62. import net.jforum.view.forum.common.TopicsCommon;
  63. import net.jforum.view.forum.common.ViewCommon;
  64. /**
  65.  * Display a list of recent Topics
  66.  * 
  67.  * @author James Yong
  68.  * @author Rafael Steil
  69.  * @version $Id: RecentTopicsAction.java,v 1.20 2007/08/20 19:35:52 rafaelsteil Exp $
  70.  */
  71. public class RecentTopicsAction extends Command 
  72. {
  73. private List forums;
  74. public void list()
  75. {
  76. int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
  77. this.setTemplateName(TemplateKeys.RECENT_LIST);
  78. this.context.put("postsPerPage", new Integer(postsPerPage));
  79. this.context.put("topics", this.topics());
  80. this.context.put("forums", this.forums);
  81. this.context.put("pageTitle", I18n.getMessage("ForumBase.recentTopics"));
  82. TopicsCommon.topicListingBase();
  83. this.request.setAttribute("template", null);
  84. }
  85. private List topics()
  86. {
  87. int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
  88. List topics = TopicRepository.getRecentTopics();
  89. this.forums = new ArrayList(postsPerPage);
  90. for (Iterator iter = topics.iterator(); iter.hasNext(); ) {
  91. Topic t = (Topic)iter.next();
  92. if (TopicsCommon.isTopicAccessible(t.getForumId())) {
  93. Forum f = ForumRepository.getForum(t.getForumId());
  94. forums.add(f);
  95. }
  96. else {
  97. iter.remove();
  98. }
  99. }
  100. JForumExecutionContext.getRequest().removeAttribute("template");
  101. return TopicsCommon.prepareTopics(topics);
  102. }
  103. public void showTopicsByUser() 
  104. {
  105. DataAccessDriver da = DataAccessDriver.getInstance();
  106. UserDAO udao = da.newUserDAO();
  107. User u = udao.selectById(this.request.getIntParameter("user_id"));
  108. if (u.getId() == 0) {
  109. this.context.put("message", I18n.getMessage("User.notFound"));
  110. this.setTemplateName(TemplateKeys.USER_NOT_FOUND);
  111. return;
  112. TopicsCommon.topicListingBase();
  113. int start = ViewCommon.getStartPage();
  114. int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
  115. int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POSTS_PER_PAGE);
  116. this.setTemplateName(TemplateKeys.RECENT_USER_TOPICS_SHOW);
  117. int totalTopics = da.newTopicDAO().countUserTopics(u.getId());
  118. this.context.put("u", u);
  119. this.context.put("pageTitle", I18n.getMessage("ForumListing.userTopics") + " " + u.getUsername());
  120. this.context.put("postsPerPage", new Integer(postsPerPage));
  121. List topics = da.newTopicDAO().selectByUserByLimit(u.getId(),start,topicsPerPage);
  122. List l = TopicsCommon.prepareTopics(topics);
  123. Map forums = new HashMap();
  124. for (Iterator iter = l.iterator(); iter.hasNext(); ) {
  125. Topic t = (Topic)iter.next();
  126. Forum f = ForumRepository.getForum(t.getForumId());
  127. if (f == null) {
  128. iter.remove();
  129. totalTopics--;
  130. continue;
  131. }
  132. forums.put(new Integer(t.getForumId()), f);
  133. }
  134. this.context.put("topics", l);
  135. this.context.put("forums", forums);
  136. ViewCommon.contextToPagination(start, totalTopics, topicsPerPage);
  137. }
  138. }