KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003, 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 Jan 16, 2005 4:48:39 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.forum;
44
45
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47
48 import net.jforum.ActionServletRequest;
49 import net.jforum.Command;
50 import net.jforum.JForumExecutionContext;
51 import net.jforum.SessionFacade;
52 import net.jforum.dao.BookmarkDAO;
53 import net.jforum.dao.DataAccessDriver;
54 import net.jforum.entities.Bookmark;
55 import net.jforum.entities.BookmarkType;
56 import net.jforum.entities.Forum;
57 import net.jforum.entities.Topic;
58 import net.jforum.entities.User;
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 freemarker.template.SimpleHash;
67 import freemarker.template.Template;
68
69 /**
70  * @author Rafael Steil
71  * @version $Id: BookmarkAction.java,v 1.13 2006/01/29 15:06:55 rafaelsteil Exp $
72  */

73 public class BookmarkAction extends Command
74 {
75     public void insert() throws Exception JavaDoc
76     {
77         int type = this.request.getIntParameter("relation_type");
78         if (type == BookmarkType.FORUM) {
79             this.addForum();
80         }
81         else if (type == BookmarkType.TOPIC) {
82             this.addTopic();
83         }
84         else if (type == BookmarkType.USER) {
85             this.addUser();
86         }
87         else {
88             this.error("Bookmarks.invalidType");
89         }
90     }
91     
92     private void addForum() throws Exception JavaDoc
93     {
94         Forum f = ForumRepository.getForum(this.request.getIntParameter("relation_id"));
95         String JavaDoc title = f.getName();
96         String JavaDoc description = f.getDescription();
97         
98         Bookmark b = DataAccessDriver.getInstance().newBookmarkDAO().selectForUpdate(
99                 f.getId(), BookmarkType.FORUM, SessionFacade.getUserSession().getUserId());
100         if (b != null) {
101             if (b.getTitle() != null) {
102                 title = b.getTitle();
103             }
104             
105             if (b.getDescription() != null) {
106                 description = b.getDescription();
107             }
108
109             this.context.put("bookmark", b);
110         }
111         
112         this.setTemplateName(TemplateKeys.BOOKMARKS_ADD_FORUM);
113         this.context.put("title", title);
114         this.context.put("description", description);
115         this.context.put("relationType", new Integer JavaDoc(BookmarkType.FORUM));
116         this.context.put("relationId", new Integer JavaDoc(f.getId()));
117     }
118     
119     private void addTopic() throws Exception JavaDoc
120     {
121         Topic t = DataAccessDriver.getInstance().newTopicDAO().selectById(
122                 this.request.getIntParameter("relation_id"));
123         String JavaDoc title = t.getTitle();
124         
125         Bookmark b = DataAccessDriver.getInstance().newBookmarkDAO().selectForUpdate(
126                 t.getId(), BookmarkType.TOPIC, SessionFacade.getUserSession().getUserId());
127         if (b != null) {
128             if (b.getTitle() != null) {
129                 title = b.getTitle();
130             }
131             
132             this.context.put("description", b.getDescription());
133             this.context.put("bookmark", b);
134         }
135         
136         this.setTemplateName(TemplateKeys.BOOKMARS_ADD_TOPIC);
137         this.context.put("title", title);
138         this.context.put("relationType", new Integer JavaDoc(BookmarkType.TOPIC));
139         this.context.put("relationId", new Integer JavaDoc(t.getId()));
140     }
141     
142     private void addUser() throws Exception JavaDoc
143     {
144         User u = DataAccessDriver.getInstance().newUserDAO().selectById(
145                 this.request.getIntParameter("relation_id"));
146         String JavaDoc title = u.getUsername();
147         
148         Bookmark b = DataAccessDriver.getInstance().newBookmarkDAO().selectForUpdate(
149                 u.getId(), BookmarkType.USER, SessionFacade.getUserSession().getUserId());
150         if (b != null) {
151             if (b.getTitle() != null) {
152                 title = b.getTitle();
153             }
154             
155             this.context.put("description", b.getDescription());
156             this.context.put("bookmark", b);
157         }
158         
159         this.setTemplateName(TemplateKeys.BOOKMARKS_ADD_USER);
160         this.context.put("title", title);
161         this.context.put("relationType", new Integer JavaDoc(BookmarkType.USER));
162         this.context.put("relationId", new Integer JavaDoc(u.getId()));
163     }
164     
165     public void insertSave() throws Exception JavaDoc
166     {
167         Bookmark b = new Bookmark();
168         b.setDescription(this.request.getParameter("description"));
169         b.setTitle(this.request.getParameter("title"));
170         
171         String JavaDoc publicVisible = this.request.getParameter("visible");
172         b.setPublicVisible(publicVisible != null && publicVisible.length() > 0);
173         
174         b.setRelationId(this.request.getIntParameter("relation_id"));
175         b.setRelationType(this.request.getIntParameter("relation_type"));
176         b.setUserId(SessionFacade.getUserSession().getUserId());
177         
178         DataAccessDriver.getInstance().newBookmarkDAO().add(b);
179         this.setTemplateName(TemplateKeys.BOOKMARKS_INSERT_SAVE);
180     }
181     
182     public void updateSave() throws Exception JavaDoc
183     {
184         int id = this.request.getIntParameter("bookmark_id");
185         BookmarkDAO bm = DataAccessDriver.getInstance().newBookmarkDAO();
186         Bookmark b = bm.selectById(id);
187         
188         if (!this.sanityCheck(b)) {
189             return;
190         }
191         
192         b.setTitle(this.request.getParameter("title"));
193         b.setDescription(this.request.getParameter("description"));
194         
195         String JavaDoc visible = this.request.getParameter("visible");
196         b.setPublicVisible(visible != null && !"".equals(visible) ? true : false);
197         
198         bm.update(b);
199         this.setTemplateName(TemplateKeys.BOOKMARKS_UPDATE_SAVE);
200     }
201     
202     public void edit() throws Exception JavaDoc
203     {
204         int id = this.request.getIntParameter("bookmark_id");
205         BookmarkDAO bm = DataAccessDriver.getInstance().newBookmarkDAO();
206         Bookmark b = bm.selectById(id);
207         
208         if (!this.sanityCheck(b)) {
209             return;
210         }
211         
212         this.setTemplateName(TemplateKeys.BOOKMARKS_EDIT);
213         this.context.put("bookmark", b);
214     }
215     
216     public void delete() throws Exception JavaDoc
217     {
218         int id = this.request.getIntParameter("bookmark_id");
219         BookmarkDAO bm = DataAccessDriver.getInstance().newBookmarkDAO();
220         Bookmark b = bm.selectById(id);
221         
222         if (!this.sanityCheck(b)) {
223             return;
224         }
225         
226         bm.remove(id);
227         
228         JForumExecutionContext.setRedirect(this.request.getContextPath() + "/bookmarks/list/" + b.getUserId()
229                 + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
230     }
231     
232     private boolean sanityCheck(Bookmark b)
233     {
234         if (b == null) {
235             this.error("Bookmarks.notFound");
236             return false;
237         }
238         
239         if (b.getUserId() != SessionFacade.getUserSession().getUserId()) {
240             this.error("Bookmarks.notOwner");
241             return false;
242         }
243         
244         return true;
245     }
246     
247     private void error(String JavaDoc message)
248     {
249         this.setTemplateName(TemplateKeys.BOOKMARKS_ERROR);
250         this.context.put("message", I18n.getMessage(message));
251     }
252     
253     public void disabled()
254     {
255         this.error("Bookmarks.featureDisabled");
256     }
257     
258     public void anonymousIsDenied()
259     {
260         this.error("Bookmarks.anonymousIsDenied");
261     }
262
263     /**
264      * @see net.jforum.Command#list()
265      */

266     public void list() throws Exception JavaDoc
267     {
268         int userId = this.request.getIntParameter("user_id");
269         
270         this.setTemplateName(TemplateKeys.BOOKMARKS_LIST);
271         this.context.put("bookmarks", DataAccessDriver.getInstance().newBookmarkDAO().selectByUser(userId));
272         this.context.put("forumType", new Integer JavaDoc(BookmarkType.FORUM));
273         this.context.put("userType", new Integer JavaDoc(BookmarkType.USER));
274         this.context.put("topicType", new Integer JavaDoc(BookmarkType.TOPIC));
275         User u=DataAccessDriver.getInstance().newUserDAO().selectById(userId);
276         this.context.put("user", u);
277         this.context.put("loggedUserId", new Integer JavaDoc(SessionFacade.getUserSession().getUserId()));
278         this.context.put("pageTitle", I18n.getMessage("Bookmarks.for")+" "+u.getUsername());
279     }
280     
281     /**
282      * @see net.jforum.Command#process(net.jforum.ActionServletRequest, javax.servlet.http.HttpServletResponse, freemarker.template.SimpleHash)
283      */

284     public Template process(ActionServletRequest request, HttpServletResponse JavaDoc response, SimpleHash context) throws Exception JavaDoc
285     {
286         if (SessionFacade.getUserSession().getUserId() == SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)
287                 && !request.getAction().equals("list")) {
288             request.addParameter("action", "anonymousIsDenied");
289         }
290         else if (!SecurityRepository.canAccess(SecurityConstants.PERM_BOOKMARKS_ENABLED)) {
291             request.addParameter("action", "disabled");
292         }
293
294         return super.process(request, response, context);
295     }
296 }
297
Popular Tags