KickJava   Java API By Example, From Geeks To Geeks.

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


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 13/10/2004 23:47:06
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.forum;
44
45 import java.util.List JavaDoc;
46
47 import javax.servlet.http.HttpServletResponse JavaDoc;
48
49 import net.jforum.ActionServletRequest;
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.PostDAO;
55 import net.jforum.dao.TopicDAO;
56 import net.jforum.dao.UserDAO;
57 import net.jforum.entities.Forum;
58 import net.jforum.entities.Topic;
59 import net.jforum.entities.User;
60 import net.jforum.repository.ForumRepository;
61 import net.jforum.util.I18n;
62 import net.jforum.util.preferences.ConfigKeys;
63 import net.jforum.util.preferences.SystemGlobals;
64 import net.jforum.util.preferences.TemplateKeys;
65 import net.jforum.util.rss.ForumRSS;
66 import net.jforum.util.rss.RSSAware;
67 import net.jforum.util.rss.RecentTopicsRSS;
68 import net.jforum.util.rss.TopicPostsRSS;
69 import net.jforum.util.rss.TopicRSS;
70 import net.jforum.util.rss.UserPostsRSS;
71 import net.jforum.util.rss.UserTopicsRSS;
72 import net.jforum.view.forum.common.ForumCommon;
73 import net.jforum.view.forum.common.TopicsCommon;
74 import freemarker.template.SimpleHash;
75 import freemarker.template.Template;
76
77 /**
78  * @author Rafael Steil
79  * @version $Id: RSSAction.java,v 1.24 2006/03/01 13:17:22 rafaelsteil Exp $
80  */

81 public class RSSAction extends Command
82 {
83     /**
84      * RSS for all forums.
85      * Show rss syndication containing information about
86      * all available forums
87      * @throws Exception
88      */

89     public void forums() throws Exception JavaDoc
90     {
91         String JavaDoc title = I18n.getMessage("RSS.Forums.title");
92         String JavaDoc description = I18n.getMessage("RSS.Forums.description");
93         
94         RSSAware rss = new ForumRSS(title, description, ForumCommon.getAllCategoriesAndForums(false));
95         this.context.put("rssContents", rss.createRSS());
96     }
97     
98     /**
99      * RSS for all N first topics for some given forum
100      * @throws Exception
101      */

102     public void forumTopics() throws Exception JavaDoc
103     {
104         int forumId = this.request.getIntParameter("forum_id");
105         if (!TopicsCommon.isTopicAccessible(forumId)) {
106             JForumExecutionContext.requestBasicAuthentication();
107             return;
108         }
109         
110         List JavaDoc topics = TopicsCommon.topicsByForum(forumId, 0);
111         Forum forum = ForumRepository.getForum(forumId);
112         
113         String JavaDoc[] p = { forum.getName() };
114         
115         RSSAware rss = new TopicRSS(I18n.getMessage("RSS.ForumTopics.title", p),
116                 I18n.getMessage("RSS.ForumTopics.description", p),
117                 forumId,
118                 topics);
119         this.context.put("rssContents", rss.createRSS());
120     }
121     
122     /**
123      * RSS for all N first posts for some given topic
124      * @throws Exception
125      */

126     public void topicPosts() throws Exception JavaDoc
127     {
128         int topicId = this.request.getIntParameter("topic_id");
129
130         PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
131         TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO();
132         
133         Topic topic = tm.selectById(topicId);
134         
135         if (!TopicsCommon.isTopicAccessible(topic.getForumId()) || topic.getId() == 0) {
136             JForumExecutionContext.requestBasicAuthentication();
137             return;
138         }
139         
140         tm.incrementTotalViews(topic.getId());
141         
142         List JavaDoc posts = pm.selectAllByTopic(topicId);
143         
144         String JavaDoc[] p = { topic.getTitle() };
145         
146         String JavaDoc title = I18n.getMessage("RSS.TopicPosts.title", p);
147         String JavaDoc description = I18n.getMessage("RSS.TopicPosts.description", p);
148
149         RSSAware rss = new TopicPostsRSS(title, description, topic.getForumId(), posts);
150         this.context.put("rssContents", rss.createRSS());
151     }
152     
153     public void recentTopics() throws Exception JavaDoc
154     {
155         String JavaDoc title = I18n.getMessage("RSS.RecentTopics.title",
156             new Object JavaDoc[] { SystemGlobals.getValue(ConfigKeys.FORUM_NAME) });
157         String JavaDoc description = I18n.getMessage("RSS.RecentTopics.description");
158
159         RSSAware rss = new RecentTopicsRSS(title, description,
160             new RecentTopicsAction().topics());
161         this.context.put("rssContents", rss.createRSS());
162     }
163
164     public void showTopicsByUser() throws Exception JavaDoc
165     {
166         int uid = this.request.getIntParameter("user_id");
167         
168         UserDAO um = DataAccessDriver.getInstance().newUserDAO();
169         User u = um.selectById(uid);
170         
171         if (u == null || u.getId() == 0) {
172             return;
173         }
174         
175         List JavaDoc topics = DataAccessDriver.getInstance().newTopicDAO().selectByUserByLimit(u.getId(),
176                 0,
177                 SystemGlobals.getIntValue(ConfigKeys.TOPICS_PER_PAGE));
178         
179         String JavaDoc[] p = { u.getUsername() };
180         
181         String JavaDoc title = I18n.getMessage("RSS.TopicsByUser.title", p);
182         String JavaDoc description = I18n.getMessage("RSS.TopicsByUser.description", p);
183         
184         RSSAware rss = new UserTopicsRSS(title, description, u.getId(), topics);
185         this.context.put("rssContents", rss.createRSS());
186     }
187     
188     public void showPostsByUser() throws Exception JavaDoc
189     {
190         int uid = this.request.getIntParameter("user_id");
191         
192         UserDAO um = DataAccessDriver.getInstance().newUserDAO();
193         User u = um.selectById(uid);
194         
195         if (u == null || u.getId() == 0) {
196             return;
197         }
198         
199         List JavaDoc topics = DataAccessDriver.getInstance().newPostDAO().selectByUserByLimit(u.getId(),
200                 0,
201                 SystemGlobals.getIntValue(ConfigKeys.POST_PER_PAGE));
202         
203         String JavaDoc[] p = { u.getUsername() };
204         
205         String JavaDoc title = I18n.getMessage("RSS.PostsByPosts.title", p);
206         String JavaDoc description = I18n.getMessage("RSS.PostsByPosts.description", p);
207         
208         RSSAware rss = new UserPostsRSS(title, description, u.getId(), topics);
209         this.context.put("rssContents", rss.createRSS());
210     }
211     
212     /**
213      * @see net.jforum.Command#list()
214      */

215     public void list() throws Exception JavaDoc
216     {
217         this.forums();
218     }
219     
220     /**
221      * @see net.jforum.Command#process()
222      */

223     public Template process(ActionServletRequest request,
224             HttpServletResponse JavaDoc response,
225             SimpleHash context) throws Exception JavaDoc
226     {
227         if (!SessionFacade.isLogged() && UserAction.hasBasicAuthentication(request)) {
228             new UserAction().validateLogin(request);
229             JForumExecutionContext.setRedirect(null);
230         }
231
232         JForumExecutionContext.setContentType("text/xml");
233         super.setTemplateName(TemplateKeys.RSS);
234
235         return super.process(request, response, context);
236     }
237
238 }
239
Popular Tags