1 42 package net.jforum.dao.generic; 43 44 import java.sql.PreparedStatement ; 45 import java.sql.ResultSet ; 46 import java.util.ArrayList ; 47 import java.util.List ; 48 49 import net.jforum.JForumExecutionContext; 50 import net.jforum.entities.Bookmark; 51 import net.jforum.entities.BookmarkType; 52 import net.jforum.exceptions.InvalidBookmarkTypeException; 53 import net.jforum.util.preferences.SystemGlobals; 54 55 59 public class GenericBookmarkDAO implements net.jforum.dao.BookmarkDAO 60 { 61 64 public void add(Bookmark b) throws Exception 65 { 66 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 67 SystemGlobals.getSql("BookmarkModel.add")); 68 p.setInt(1, b.getUserId()); 69 p.setInt(2, b.getRelationId()); 70 p.setInt(3, b.getRelationType()); 71 p.setInt(4, b.isPublicVisible() ? 1 : 0); 72 p.setString(5, b.getTitle()); 73 p.setString(6, b.getDescription()); 74 p.executeUpdate(); 75 p.close(); 76 } 77 78 81 public void update(Bookmark b) throws Exception 82 { 83 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 84 SystemGlobals.getSql("BookmarkModel.update")); 85 p.setInt(1, b.isPublicVisible() ? 1 : 0); 86 p.setString(2, b.getTitle()); 87 p.setString(3, b.getDescription()); 88 p.setInt(4, b.getId()); 89 p.executeUpdate(); 90 p.close(); 91 } 92 93 96 public void remove(int bookmarkId) throws Exception 97 { 98 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 99 SystemGlobals.getSql("BookmarkModel.remove")); 100 p.setInt(1, bookmarkId); 101 p.executeUpdate(); 102 p.close(); 103 } 104 105 108 public List selectByUser(int userId, int relationType) throws Exception 109 { 110 if (relationType == BookmarkType.FORUM) { 111 return this.getForums(userId); 112 } 113 else if (relationType == BookmarkType.TOPIC) { 114 return this.getTopics(userId); 115 } 116 else if (relationType == BookmarkType.USER) { 117 return this.getUsers(userId); 118 } 119 else { 120 throw new InvalidBookmarkTypeException("The type " + relationType 121 + " is not a valid bookmark type"); 122 } 123 } 124 125 128 public List selectByUser(int userId) throws Exception 129 { 130 List l = new ArrayList (); 131 132 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 133 SystemGlobals.getSql("BookmarkModel.selectAllFromUser")); 134 p.setInt(1, userId); 135 136 ResultSet rs = p.executeQuery(); 137 while (rs.next()) { 138 l.add(this.getBookmark(rs)); 139 } 140 141 rs.close(); 142 p.close(); 143 144 return l; 145 } 146 147 150 public Bookmark selectById(int bookmarkId) throws Exception 151 { 152 Bookmark b = null; 153 154 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 155 SystemGlobals.getSql("BookmarkModel.selectById")); 156 p.setInt(1, bookmarkId); 157 158 ResultSet rs = p.executeQuery(); 159 if (rs.next()) { 160 b = this.getBookmark(rs); 161 } 162 163 rs.close(); 164 p.close(); 165 166 return b; 167 } 168 169 172 public Bookmark selectForUpdate(int relationId, int relationType, int userId) throws Exception 173 { 174 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 175 SystemGlobals.getSql("BookmarkModel.selectForUpdate")); 176 p.setInt(1, relationId); 177 p.setInt(2, relationType); 178 p.setInt(3, userId); 179 180 Bookmark b = null; 181 ResultSet rs = p.executeQuery(); 182 if (rs.next()) { 183 b = this.getBookmark(rs); 184 } 185 186 rs.close(); 187 p.close(); 188 189 return b; 190 } 191 192 protected List getUsers(int userId) throws Exception 193 { 194 List l = new ArrayList (); 195 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 196 SystemGlobals.getSql("BookmarkModel.selectUserBookmarks")); 197 p.setInt(1, userId); 198 199 ResultSet rs = p.executeQuery(); 200 while (rs.next()) { 201 Bookmark b = this.getBookmark(rs); 202 203 if (b.getTitle() == null || "".equals(b.getTitle())) { 204 b.setTitle(rs.getString("username")); 205 } 206 207 l.add(b); 208 } 209 210 rs.close(); 211 p.close(); 212 213 return l; 214 } 215 216 protected List getTopics(int userId) throws Exception 217 { 218 List l = new ArrayList (); 219 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 220 SystemGlobals.getSql("BookmarkModel.selectTopicBookmarks")); 221 p.setInt(1, userId); 222 223 ResultSet rs = p.executeQuery(); 224 while (rs.next()) { 225 Bookmark b = this.getBookmark(rs); 226 227 if (b.getTitle() == null || "".equals(b.getTitle())) { 228 b.setTitle(rs.getString("topic_title")); 229 } 230 231 l.add(b); 232 } 233 234 rs.close(); 235 p.close(); 236 237 return l; 238 } 239 240 protected List getForums(int userId) throws Exception 241 { 242 List l = new ArrayList (); 243 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 244 SystemGlobals.getSql("BookmarkModel.selectForumBookmarks")); 245 p.setInt(1, userId); 246 247 ResultSet rs = p.executeQuery(); 248 while (rs.next()) { 249 Bookmark b = this.getBookmark(rs); 250 251 if (b.getTitle() == null || "".equals(b.getTitle())) { 252 b.setTitle(rs.getString("forum_name")); 253 } 254 255 if (b.getDescription() == null || "".equals(b.getDescription())) { 256 b.setDescription(rs.getString("forum_desc")); 257 } 258 259 l.add(b); 260 } 261 262 rs.close(); 263 p.close(); 264 265 return l; 266 } 267 268 protected Bookmark getBookmark(ResultSet rs) throws Exception 269 { 270 Bookmark b = new Bookmark(); 271 b.setId(rs.getInt("bookmark_id")); 272 b.setDescription(rs.getString("description")); 273 b.setPublicVisible(rs.getInt("public_visible") == 1 ? true : false); 274 b.setRelationId(rs.getInt("relation_id")); 275 b.setTitle(rs.getString("title")); 276 b.setDescription(rs.getString("description")); 277 b.setUserId(rs.getInt("user_id")); 278 b.setRelationType(rs.getInt("relation_type")); 279 280 return b; 281 } 282 } 283 | Popular Tags |