001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on Oct 19, 2004
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.forum;
044:
045: import java.util.ArrayList;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.Map;
050:
051: import net.jforum.Command;
052: import net.jforum.JForumExecutionContext;
053: import net.jforum.dao.DataAccessDriver;
054: import net.jforum.dao.UserDAO;
055: import net.jforum.entities.Forum;
056: import net.jforum.entities.Topic;
057: import net.jforum.entities.User;
058: import net.jforum.repository.ForumRepository;
059: import net.jforum.repository.TopicRepository;
060: import net.jforum.util.I18n;
061: import net.jforum.util.preferences.ConfigKeys;
062: import net.jforum.util.preferences.SystemGlobals;
063: import net.jforum.util.preferences.TemplateKeys;
064: import net.jforum.view.forum.common.TopicsCommon;
065: import net.jforum.view.forum.common.ViewCommon;
066:
067: /**
068: * Display a list of recent Topics
069: *
070: * @author James Yong
071: * @author Rafael Steil
072: * @version $Id: RecentTopicsAction.java,v 1.20 2007/08/20 19:35:52 rafaelsteil Exp $
073: */
074: public class RecentTopicsAction extends Command {
075: private List forums;
076:
077: public void list() {
078: int postsPerPage = SystemGlobals
079: .getIntValue(ConfigKeys.POSTS_PER_PAGE);
080:
081: this .setTemplateName(TemplateKeys.RECENT_LIST);
082:
083: this .context.put("postsPerPage", new Integer(postsPerPage));
084: this .context.put("topics", this .topics());
085: this .context.put("forums", this .forums);
086: this .context.put("pageTitle", I18n
087: .getMessage("ForumBase.recentTopics"));
088:
089: TopicsCommon.topicListingBase();
090: this .request.setAttribute("template", null);
091: }
092:
093: private List topics() {
094: int postsPerPage = SystemGlobals
095: .getIntValue(ConfigKeys.POSTS_PER_PAGE);
096: List topics = TopicRepository.getRecentTopics();
097:
098: this .forums = new ArrayList(postsPerPage);
099:
100: for (Iterator iter = topics.iterator(); iter.hasNext();) {
101: Topic t = (Topic) iter.next();
102:
103: if (TopicsCommon.isTopicAccessible(t.getForumId())) {
104: Forum f = ForumRepository.getForum(t.getForumId());
105: forums.add(f);
106: } else {
107: iter.remove();
108: }
109: }
110:
111: JForumExecutionContext.getRequest().removeAttribute("template");
112:
113: return TopicsCommon.prepareTopics(topics);
114: }
115:
116: public void showTopicsByUser() {
117: DataAccessDriver da = DataAccessDriver.getInstance();
118:
119: UserDAO udao = da.newUserDAO();
120: User u = udao.selectById(this .request
121: .getIntParameter("user_id"));
122:
123: if (u.getId() == 0) {
124: this .context.put("message", I18n
125: .getMessage("User.notFound"));
126: this .setTemplateName(TemplateKeys.USER_NOT_FOUND);
127: return;
128: }
129:
130: TopicsCommon.topicListingBase();
131:
132: int start = ViewCommon.getStartPage();
133: int topicsPerPage = SystemGlobals
134: .getIntValue(ConfigKeys.TOPICS_PER_PAGE);
135: int postsPerPage = SystemGlobals
136: .getIntValue(ConfigKeys.POSTS_PER_PAGE);
137:
138: this .setTemplateName(TemplateKeys.RECENT_USER_TOPICS_SHOW);
139:
140: int totalTopics = da.newTopicDAO().countUserTopics(u.getId());
141:
142: this .context.put("u", u);
143: this .context.put("pageTitle", I18n
144: .getMessage("ForumListing.userTopics")
145: + " " + u.getUsername());
146:
147: this .context.put("postsPerPage", new Integer(postsPerPage));
148:
149: List topics = da.newTopicDAO().selectByUserByLimit(u.getId(),
150: start, topicsPerPage);
151:
152: List l = TopicsCommon.prepareTopics(topics);
153: Map forums = new HashMap();
154:
155: for (Iterator iter = l.iterator(); iter.hasNext();) {
156: Topic t = (Topic) iter.next();
157:
158: Forum f = ForumRepository.getForum(t.getForumId());
159:
160: if (f == null) {
161: iter.remove();
162: totalTopics--;
163: continue;
164: }
165:
166: forums.put(new Integer(t.getForumId()), f);
167: }
168:
169: this .context.put("topics", l);
170: this .context.put("forums", forums);
171:
172: ViewCommon.contextToPagination(start, totalTopics,
173: topicsPerPage);
174: }
175: }
|