KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > forum > RecentTopicsAction


1 /*
2  * Copyright (c) Rafael Steil
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
45 import java.util.ArrayList JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.Map JavaDoc;
50
51 import net.jforum.Command;
52 import net.jforum.JForumExecutionContext;
53 import net.jforum.dao.DataAccessDriver;
54 import net.jforum.dao.UserDAO;
55 import net.jforum.entities.Forum;
56 import net.jforum.entities.Topic;
57 import net.jforum.entities.User;
58 import net.jforum.repository.ForumRepository;
59 import net.jforum.repository.TopicRepository;
60 import net.jforum.util.I18n;
61 import net.jforum.util.preferences.ConfigKeys;
62 import net.jforum.util.preferences.SystemGlobals;
63 import net.jforum.util.preferences.TemplateKeys;
64 import net.jforum.view.forum.common.TopicsCommon;
65 import net.jforum.view.forum.common.ViewCommon;
66
67 /**
68  * Display a list of recent Topics
69  *
70  * @author James Yong
71  * @author Rafael Steil
72  * @version $Id: RecentTopicsAction.java,v 1.17 2006/03/01 13:17:22 rafaelsteil Exp $
73  */

74 public class RecentTopicsAction extends Command
75 {
76     private List JavaDoc forums;
77
78     public void list() throws Exception JavaDoc
79     {
80         int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE);
81
82         this.setTemplateName(TemplateKeys.RECENT_LIST);
83         
84         this.context.put("postsPerPage", new Integer JavaDoc(postsPerPage));
85         this.context.put("topics", this.topics());
86         this.context.put("forums", this.forums);
87         this.context.put("pageTitle", I18n.getMessage("ForumBase.recentTopics"));
88
89         TopicsCommon.topicListingBase();
90         this.request.setAttribute("template", null);
91     }
92     
93     List JavaDoc topics() throws Exception JavaDoc
94     {
95         int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE);
96         List JavaDoc tmpTopics = TopicRepository.getRecentTopics();
97         
98         this.forums = new ArrayList JavaDoc(postsPerPage);
99
100         for (Iterator JavaDoc iter = tmpTopics.iterator(); iter.hasNext(); ) {
101             Topic t = (Topic)iter.next();
102             
103             if (TopicsCommon.isTopicAccessible(t.getForumId())) {
104                 // Get name of forum that the topic refers to
105
Forum f = ForumRepository.getForum(t.getForumId());
106                 forums.add(f);
107             }
108             else {
109                 iter.remove();
110             }
111         }
112         
113         JForumExecutionContext.getRequest().removeAttribute("template");
114         
115         return TopicsCommon.prepareTopics(tmpTopics);
116     }
117
118     public void showTopicsByUser() throws Exception JavaDoc
119     {
120         DataAccessDriver da = DataAccessDriver.getInstance();
121         
122         UserDAO udao = da.newUserDAO();
123         User u = udao.selectById(this.request.getIntParameter("user_id"));
124         
125         if (u.getId() == 0) {
126             this.context.put("message", I18n.getMessage("User.notFound"));
127             this.setTemplateName(TemplateKeys.USER_NOT_FOUND);
128             return;
129         }
130             
131         TopicsCommon.topicListingBase();
132         
133         int start = ViewCommon.getStartPage();
134         int topicsPerPage = SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE);
135         int postsPerPage = SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE);
136         
137         this.setTemplateName(TemplateKeys.RECENT_USER_TOPICS_SHOW);
138         
139         int totalTopics = da.newTopicDAO().countUserTopics(u.getId());
140         
141         this.context.put("u", u);
142         this.context.put("pageTitle", I18n.getMessage("ForumListing.userTopics") + " " + u.getUsername());
143         
144         this.context.put("postsPerPage", new Integer JavaDoc(postsPerPage));
145         
146         List JavaDoc topics = da.newTopicDAO().selectByUserByLimit(u.getId(),start,topicsPerPage);
147         
148         List JavaDoc l = TopicsCommon.prepareTopics(topics);
149         Map JavaDoc forums = new HashMap JavaDoc();
150         
151         for (Iterator JavaDoc iter = l.iterator(); iter.hasNext(); ) {
152             Topic t = (Topic)iter.next();
153             
154             Forum f = ForumRepository.getForum(t.getForumId());
155             
156             if (f == null) {
157                 iter.remove();
158                 totalTopics--;
159                 continue;
160             }
161             
162             forums.put(new Integer JavaDoc(t.getForumId()), f);
163         }
164         
165         this.context.put("topics", l);
166         this.context.put("forums", forums);
167         
168         ViewCommon.contextToPagination(start, totalTopics, topicsPerPage);
169     }
170 }
171
Popular Tags