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: * 1) Redistributions of source code must retain the above
009: * copyright notice, this list of conditions and the
010: * following disclaimer.
011: * 2) Redistributions in binary form must reproduce the
012: * above copyright notice, this list of conditions and
013: * the following disclaimer in the documentation and/or
014: * other materials provided with the distribution.
015: * 3) Neither the name of "Rafael Steil" nor
016: * the names of its contributors may be used to endorse
017: * or promote products derived from this software without
018: * specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
021: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
022: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
023: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
024: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
025: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
026: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
027: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
028: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
029: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
030: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
031: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
032: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
033: * IN CONTRACT, STRICT LIABILITY, OR TORT
034: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
035: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
036: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
037: *
038: * Created on Jan 16, 2005 12:47:31 PM
039: * The JForum Project
040: * http://www.jforum.net
041: */
042: package net.jforum.dao.generic;
043:
044: import java.sql.PreparedStatement;
045: import java.sql.ResultSet;
046: import java.sql.SQLException;
047: import java.util.ArrayList;
048: import java.util.List;
049:
050: import net.jforum.JForumExecutionContext;
051: import net.jforum.entities.Bookmark;
052: import net.jforum.entities.BookmarkType;
053: import net.jforum.exceptions.DatabaseException;
054: import net.jforum.exceptions.InvalidBookmarkTypeException;
055: import net.jforum.util.DbUtils;
056: import net.jforum.util.preferences.SystemGlobals;
057:
058: /**
059: * @author Rafael Steil
060: * @version $Id: GenericBookmarkDAO.java,v 1.8 2006/08/23 02:13:42 rafaelsteil Exp $
061: */
062: public class GenericBookmarkDAO implements net.jforum.dao.BookmarkDAO {
063: /**
064: * @see net.jforum.dao.BookmarkDAO#add(net.jforum.entities.Bookmark)
065: */
066: public void add(Bookmark b) {
067: PreparedStatement p = null;
068: try {
069: p = JForumExecutionContext.getConnection()
070: .prepareStatement(
071: SystemGlobals.getSql("BookmarkModel.add"));
072: p.setInt(1, b.getUserId());
073: p.setInt(2, b.getRelationId());
074: p.setInt(3, b.getRelationType());
075: p.setInt(4, b.isPublicVisible() ? 1 : 0);
076: p.setString(5, b.getTitle());
077: p.setString(6, b.getDescription());
078: p.executeUpdate();
079: } catch (SQLException e) {
080: throw new DatabaseException(e);
081: } finally {
082: DbUtils.close(p);
083: }
084: }
085:
086: /**
087: * @see net.jforum.dao.BookmarkDAO#update(net.jforum.entities.Bookmark)
088: */
089: public void update(Bookmark b) {
090: PreparedStatement p = null;
091: try {
092: p = JForumExecutionContext.getConnection()
093: .prepareStatement(
094: SystemGlobals
095: .getSql("BookmarkModel.update"));
096: p.setInt(1, b.isPublicVisible() ? 1 : 0);
097: p.setString(2, b.getTitle());
098: p.setString(3, b.getDescription());
099: p.setInt(4, b.getId());
100: p.executeUpdate();
101: } catch (SQLException e) {
102: throw new DatabaseException(e);
103: } finally {
104: DbUtils.close(p);
105: }
106: }
107:
108: /**
109: * @see net.jforum.dao.BookmarkDAO#remove(int)
110: */
111: public void remove(int bookmarkId) {
112: PreparedStatement p = null;
113: try {
114: p = JForumExecutionContext.getConnection()
115: .prepareStatement(
116: SystemGlobals
117: .getSql("BookmarkModel.remove"));
118: p.setInt(1, bookmarkId);
119: p.executeUpdate();
120: } catch (SQLException e) {
121: throw new DatabaseException(e);
122: } finally {
123: DbUtils.close(p);
124: }
125: }
126:
127: /**
128: * @see net.jforum.dao.BookmarkDAO#selectByUser(int, int)
129: */
130: public List selectByUser(int userId, int relationType) {
131: if (relationType == BookmarkType.FORUM) {
132: return this .getForums(userId);
133: } else if (relationType == BookmarkType.TOPIC) {
134: return this .getTopics(userId);
135: } else if (relationType == BookmarkType.USER) {
136: return this .getUsers(userId);
137: } else {
138: throw new InvalidBookmarkTypeException("The type "
139: + relationType + " is not a valid bookmark type");
140: }
141: }
142:
143: /**
144: * @see net.jforum.dao.BookmarkDAO#selectByUser(int)
145: */
146: public List selectByUser(int userId) {
147: List l = new ArrayList();
148:
149: PreparedStatement p = null;
150: ResultSet rs = null;
151: try {
152: p = JForumExecutionContext
153: .getConnection()
154: .prepareStatement(
155: SystemGlobals
156: .getSql("BookmarkModel.selectAllFromUser"));
157: p.setInt(1, userId);
158:
159: rs = p.executeQuery();
160: while (rs.next()) {
161: l.add(this .getBookmark(rs));
162: }
163:
164: return l;
165: } catch (SQLException e) {
166: throw new DatabaseException(e);
167: } finally {
168: DbUtils.close(rs, p);
169: }
170: }
171:
172: /**
173: * @see net.jforum.dao.BookmarkDAO#selectById(int)
174: */
175: public Bookmark selectById(int bookmarkId) {
176: Bookmark b = null;
177:
178: PreparedStatement p = null;
179: ResultSet rs = null;
180: try {
181: p = JForumExecutionContext
182: .getConnection()
183: .prepareStatement(
184: SystemGlobals
185: .getSql("BookmarkModel.selectById"));
186: p.setInt(1, bookmarkId);
187:
188: rs = p.executeQuery();
189: if (rs.next()) {
190: b = this .getBookmark(rs);
191: }
192:
193: return b;
194: } catch (SQLException e) {
195: throw new DatabaseException(e);
196: } finally {
197: DbUtils.close(rs, p);
198: }
199: }
200:
201: /**
202: * @see net.jforum.dao.BookmarkDAO#selectForUpdate(int, int, int)
203: */
204: public Bookmark selectForUpdate(int relationId, int relationType,
205: int userId) {
206: PreparedStatement p = null;
207: ResultSet rs = null;
208: try {
209: p = JForumExecutionContext
210: .getConnection()
211: .prepareStatement(
212: SystemGlobals
213: .getSql("BookmarkModel.selectForUpdate"));
214: p.setInt(1, relationId);
215: p.setInt(2, relationType);
216: p.setInt(3, userId);
217:
218: Bookmark b = null;
219: rs = p.executeQuery();
220: if (rs.next()) {
221: b = this .getBookmark(rs);
222: }
223:
224: return b;
225: } catch (SQLException e) {
226: throw new DatabaseException(e);
227: } finally {
228: DbUtils.close(rs, p);
229: }
230: }
231:
232: protected List getUsers(int userId) {
233: List l = new ArrayList();
234: PreparedStatement p = null;
235: ResultSet rs = null;
236: try {
237: p = JForumExecutionContext
238: .getConnection()
239: .prepareStatement(
240: SystemGlobals
241: .getSql("BookmarkModel.selectUserBookmarks"));
242: p.setInt(1, userId);
243:
244: rs = p.executeQuery();
245: while (rs.next()) {
246: Bookmark b = this .getBookmark(rs);
247:
248: if (b.getTitle() == null || "".equals(b.getTitle())) {
249: b.setTitle(rs.getString("username"));
250: }
251:
252: l.add(b);
253: }
254:
255: return l;
256: } catch (SQLException e) {
257: throw new DatabaseException(e);
258: } finally {
259: DbUtils.close(rs, p);
260: }
261: }
262:
263: protected List getTopics(int userId) {
264: PreparedStatement p = null;
265: ResultSet rs = null;
266: try {
267: List l = new ArrayList();
268: p = JForumExecutionContext
269: .getConnection()
270: .prepareStatement(
271: SystemGlobals
272: .getSql("BookmarkModel.selectTopicBookmarks"));
273: p.setInt(1, userId);
274:
275: rs = p.executeQuery();
276: while (rs.next()) {
277: Bookmark b = this .getBookmark(rs);
278:
279: if (b.getTitle() == null || "".equals(b.getTitle())) {
280: b.setTitle(rs.getString("topic_title"));
281: }
282:
283: l.add(b);
284: }
285:
286: return l;
287: } catch (SQLException e) {
288: throw new DatabaseException(e);
289: } finally {
290: DbUtils.close(rs, p);
291: }
292: }
293:
294: protected List getForums(int userId) {
295: PreparedStatement p = null;
296: ResultSet rs = null;
297: try {
298: List l = new ArrayList();
299: p = JForumExecutionContext
300: .getConnection()
301: .prepareStatement(
302: SystemGlobals
303: .getSql("BookmarkModel.selectForumBookmarks"));
304: p.setInt(1, userId);
305:
306: rs = p.executeQuery();
307: while (rs.next()) {
308: Bookmark b = this .getBookmark(rs);
309:
310: if (b.getTitle() == null || "".equals(b.getTitle())) {
311: b.setTitle(rs.getString("forum_name"));
312: }
313:
314: if (b.getDescription() == null
315: || "".equals(b.getDescription())) {
316: b.setDescription(rs.getString("forum_desc"));
317: }
318:
319: l.add(b);
320: }
321:
322: return l;
323: } catch (SQLException e) {
324: throw new DatabaseException(e);
325: } finally {
326: DbUtils.close(rs, p);
327: }
328: }
329:
330: protected Bookmark getBookmark(ResultSet rs) throws SQLException {
331: Bookmark b = new Bookmark();
332: b.setId(rs.getInt("bookmark_id"));
333: b.setDescription(rs.getString("description"));
334: b.setPublicVisible(rs.getInt("public_visible") == 1);
335: b.setRelationId(rs.getInt("relation_id"));
336: b.setTitle(rs.getString("title"));
337: b.setDescription(rs.getString("description"));
338: b.setUserId(rs.getInt("user_id"));
339: b.setRelationType(rs.getInt("relation_type"));
340:
341: return b;
342: }
343: }
|