1 43 package net.jforum.view.forum; 44 45 46 import javax.servlet.http.HttpServletResponse ; 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 73 public class BookmarkAction extends Command 74 { 75 public void insert() throws Exception 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 93 { 94 Forum f = ForumRepository.getForum(this.request.getIntParameter("relation_id")); 95 String title = f.getName(); 96 String 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 (BookmarkType.FORUM)); 116 this.context.put("relationId", new Integer (f.getId())); 117 } 118 119 private void addTopic() throws Exception 120 { 121 Topic t = DataAccessDriver.getInstance().newTopicDAO().selectById( 122 this.request.getIntParameter("relation_id")); 123 String 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 (BookmarkType.TOPIC)); 139 this.context.put("relationId", new Integer (t.getId())); 140 } 141 142 private void addUser() throws Exception 143 { 144 User u = DataAccessDriver.getInstance().newUserDAO().selectById( 145 this.request.getIntParameter("relation_id")); 146 String 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 (BookmarkType.USER)); 162 this.context.put("relationId", new Integer (u.getId())); 163 } 164 165 public void insertSave() throws Exception 166 { 167 Bookmark b = new Bookmark(); 168 b.setDescription(this.request.getParameter("description")); 169 b.setTitle(this.request.getParameter("title")); 170 171 String 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 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 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 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 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 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 266 public void list() throws Exception 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 (BookmarkType.FORUM)); 273 this.context.put("userType", new Integer (BookmarkType.USER)); 274 this.context.put("topicType", new Integer (BookmarkType.TOPIC)); 275 User u=DataAccessDriver.getInstance().newUserDAO().selectById(userId); 276 this.context.put("user", u); 277 this.context.put("loggedUserId", new Integer (SessionFacade.getUserSession().getUserId())); 278 this.context.put("pageTitle", I18n.getMessage("Bookmarks.for")+" "+u.getUsername()); 279 } 280 281 284 public Template process(ActionServletRequest request, HttpServletResponse response, SimpleHash context) throws Exception 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 |