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 Jan 16, 2005 4:48:39 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.forum;
044:
045: import net.jforum.*;
046: import net.jforum.context.RequestContext;
047: import net.jforum.context.ResponseContext;
048: import net.jforum.dao.BookmarkDAO;
049: import net.jforum.dao.DataAccessDriver;
050: import net.jforum.entities.Bookmark;
051: import net.jforum.entities.BookmarkType;
052: import net.jforum.entities.Forum;
053: import net.jforum.entities.Topic;
054: import net.jforum.entities.User;
055: import net.jforum.repository.ForumRepository;
056: import net.jforum.repository.SecurityRepository;
057: import net.jforum.security.SecurityConstants;
058: import net.jforum.util.I18n;
059: import net.jforum.util.preferences.ConfigKeys;
060: import net.jforum.util.preferences.SystemGlobals;
061: import net.jforum.util.preferences.TemplateKeys;
062: import freemarker.template.SimpleHash;
063: import freemarker.template.Template;
064: import org.apache.commons.lang.StringUtils;
065:
066: /**
067: * @author Rafael Steil
068: * @version $Id: BookmarkAction.java,v 1.17 2006/08/23 02:13:53 rafaelsteil Exp $
069: */
070: public class BookmarkAction extends Command {
071: public void insert() {
072: int type = this .request.getIntParameter("relation_type");
073: if (type == BookmarkType.FORUM) {
074: this .addForum();
075: } else if (type == BookmarkType.TOPIC) {
076: this .addTopic();
077: } else if (type == BookmarkType.USER) {
078: this .addUser();
079: } else {
080: this .error("Bookmarks.invalidType");
081: }
082: }
083:
084: private void addForum() {
085: Forum f = ForumRepository.getForum(this .request
086: .getIntParameter("relation_id"));
087: String title = f.getName();
088: String description = f.getDescription();
089:
090: Bookmark b = DataAccessDriver.getInstance().newBookmarkDAO()
091: .selectForUpdate(f.getId(), BookmarkType.FORUM,
092: SessionFacade.getUserSession().getUserId());
093: if (b != null) {
094: if (b.getTitle() != null) {
095: title = b.getTitle();
096: }
097:
098: if (b.getDescription() != null) {
099: description = b.getDescription();
100: }
101:
102: this .context.put("bookmark", b);
103: }
104:
105: this .setTemplateName(TemplateKeys.BOOKMARKS_ADD_FORUM);
106: this .context.put("title", title);
107: this .context.put("description", description);
108: this .context.put("relationType",
109: new Integer(BookmarkType.FORUM));
110: this .context.put("relationId", new Integer(f.getId()));
111: }
112:
113: private void addTopic() {
114: Topic t = DataAccessDriver
115: .getInstance()
116: .newTopicDAO()
117: .selectById(this .request.getIntParameter("relation_id"));
118: String title = t.getTitle();
119:
120: Bookmark b = DataAccessDriver.getInstance().newBookmarkDAO()
121: .selectForUpdate(t.getId(), BookmarkType.TOPIC,
122: SessionFacade.getUserSession().getUserId());
123: if (b != null) {
124: if (b.getTitle() != null) {
125: title = b.getTitle();
126: }
127:
128: this .context.put("description", b.getDescription());
129: this .context.put("bookmark", b);
130: }
131:
132: this .setTemplateName(TemplateKeys.BOOKMARS_ADD_TOPIC);
133: this .context.put("title", title);
134: this .context.put("relationType",
135: new Integer(BookmarkType.TOPIC));
136: this .context.put("relationId", new Integer(t.getId()));
137: }
138:
139: private void addUser() {
140: User u = DataAccessDriver
141: .getInstance()
142: .newUserDAO()
143: .selectById(this .request.getIntParameter("relation_id"));
144: String title = u.getUsername();
145:
146: Bookmark b = DataAccessDriver.getInstance().newBookmarkDAO()
147: .selectForUpdate(u.getId(), BookmarkType.USER,
148: SessionFacade.getUserSession().getUserId());
149: if (b != null) {
150: if (b.getTitle() != null) {
151: title = b.getTitle();
152: }
153:
154: this .context.put("description", b.getDescription());
155: this .context.put("bookmark", b);
156: }
157:
158: this .setTemplateName(TemplateKeys.BOOKMARKS_ADD_USER);
159: this .context.put("title", title);
160: this .context
161: .put("relationType", new Integer(BookmarkType.USER));
162: this .context.put("relationId", new Integer(u.getId()));
163: }
164:
165: public void insertSave() {
166: Bookmark b = new Bookmark();
167: b.setDescription(this .request.getParameter("description"));
168: b.setTitle(this .request.getParameter("title"));
169:
170: String publicVisible = this .request.getParameter("visible");
171: b.setPublicVisible(publicVisible != null
172: && publicVisible.length() > 0);
173:
174: b.setRelationId(this .request.getIntParameter("relation_id"));
175: b
176: .setRelationType(this .request
177: .getIntParameter("relation_type"));
178: b.setUserId(SessionFacade.getUserSession().getUserId());
179:
180: DataAccessDriver.getInstance().newBookmarkDAO().add(b);
181: this .setTemplateName(TemplateKeys.BOOKMARKS_INSERT_SAVE);
182: }
183:
184: public void updateSave() {
185: int id = this .request.getIntParameter("bookmark_id");
186: BookmarkDAO bm = DataAccessDriver.getInstance()
187: .newBookmarkDAO();
188: Bookmark b = bm.selectById(id);
189:
190: if (!this .sanityCheck(b)) {
191: return;
192: }
193:
194: b.setTitle(this .request.getParameter("title"));
195: b.setDescription(this .request.getParameter("description"));
196:
197: String visible = this .request.getParameter("visible");
198: b.setPublicVisible(StringUtils.isNotBlank(visible));
199:
200: bm.update(b);
201: this .setTemplateName(TemplateKeys.BOOKMARKS_UPDATE_SAVE);
202: }
203:
204: public void edit() {
205: int id = this .request.getIntParameter("bookmark_id");
206: BookmarkDAO bm = DataAccessDriver.getInstance()
207: .newBookmarkDAO();
208: Bookmark b = bm.selectById(id);
209:
210: if (!this .sanityCheck(b)) {
211: return;
212: }
213:
214: this .setTemplateName(TemplateKeys.BOOKMARKS_EDIT);
215: this .context.put("bookmark", b);
216: }
217:
218: public void delete() {
219: int id = this .request.getIntParameter("bookmark_id");
220: BookmarkDAO bm = DataAccessDriver.getInstance()
221: .newBookmarkDAO();
222: Bookmark b = bm.selectById(id);
223:
224: if (!this .sanityCheck(b)) {
225: return;
226: }
227:
228: bm.remove(id);
229:
230: JForumExecutionContext.setRedirect(this .request
231: .getContextPath()
232: + "/bookmarks/list/"
233: + b.getUserId()
234: + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
235: }
236:
237: private boolean sanityCheck(Bookmark b) {
238: if (b == null) {
239: this .error("Bookmarks.notFound");
240: return false;
241: }
242:
243: if (b.getUserId() != SessionFacade.getUserSession().getUserId()) {
244: this .error("Bookmarks.notOwner");
245: return false;
246: }
247:
248: return true;
249: }
250:
251: private void error(String message) {
252: this .setTemplateName(TemplateKeys.BOOKMARKS_ERROR);
253: this .context.put("message", I18n.getMessage(message));
254: }
255:
256: public void disabled() {
257: this .error("Bookmarks.featureDisabled");
258: }
259:
260: public void anonymousIsDenied() {
261: this .error("Bookmarks.anonymousIsDenied");
262: }
263:
264: /**
265: * @see net.jforum.Command#list()
266: */
267: public void list() {
268: int userId = this .request.getIntParameter("user_id");
269:
270: this .setTemplateName(TemplateKeys.BOOKMARKS_LIST);
271: this .context.put("bookmarks", DataAccessDriver.getInstance()
272: .newBookmarkDAO().selectByUser(userId));
273: this .context.put("forumType", new Integer(BookmarkType.FORUM));
274: this .context.put("userType", new Integer(BookmarkType.USER));
275: this .context.put("topicType", new Integer(BookmarkType.TOPIC));
276: User u = DataAccessDriver.getInstance().newUserDAO()
277: .selectById(userId);
278: this .context.put("user", u);
279: this .context.put("loggedUserId", new Integer(SessionFacade
280: .getUserSession().getUserId()));
281: this .context.put("pageTitle", I18n.getMessage("Bookmarks.for")
282: + " " + u.getUsername());
283: }
284:
285: /**
286: * @see net.jforum.Command#process(net.jforum.context.RequestContext, net.jforum.context.ResponseContext, freemarker.template.SimpleHash)
287: */
288: public Template process(RequestContext request,
289: ResponseContext response, SimpleHash context) {
290: if (SessionFacade.getUserSession().getUserId() == SystemGlobals
291: .getIntValue(ConfigKeys.ANONYMOUS_USER_ID)
292: && !request.getAction().equals("list")) {
293: request.addParameter("action", "anonymousIsDenied");
294: } else if (!SecurityRepository
295: .canAccess(SecurityConstants.PERM_BOOKMARKS_ENABLED)) {
296: request.addParameter("action", "disabled");
297: }
298:
299: return super.process(request, response, context);
300: }
301: }
|