KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > dao > generic > GenericBookmarkDAO


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  * 1) Redistributions of source code must retain the above
9  * copyright notice, this list of conditions and the
10  * following disclaimer.
11  * 2) Redistributions in binary form must reproduce the
12  * above copyright notice, this list of conditions and
13  * the following disclaimer in the documentation and/or
14  * other materials provided with the distribution.
15  * 3) Neither the name of "Rafael Steil" nor
16  * the names of its contributors may be used to endorse
17  * or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
21  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
23  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
31  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
33  * IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
37  *
38  * Created on Jan 16, 2005 12:47:31 PM
39  * The JForum Project
40  * http://www.jforum.net
41  */

42 package net.jforum.dao.generic;
43
44 import java.sql.PreparedStatement JavaDoc;
45 import java.sql.ResultSet JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.List JavaDoc;
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 /**
56  * @author Rafael Steil
57  * @version $Id: GenericBookmarkDAO.java,v 1.5 2006/01/29 15:06:24 rafaelsteil Exp $
58  */

59 public class GenericBookmarkDAO implements net.jforum.dao.BookmarkDAO
60 {
61     /**
62      * @see net.jforum.dao.BookmarkDAO#add(net.jforum.entities.Bookmark)
63      */

64     public void add(Bookmark b) throws Exception JavaDoc
65     {
66         PreparedStatement JavaDoc 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     /**
79      * @see net.jforum.dao.BookmarkDAO#update(net.jforum.entities.Bookmark)
80      */

81     public void update(Bookmark b) throws Exception JavaDoc
82     {
83         PreparedStatement JavaDoc 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     /**
94      * @see net.jforum.dao.BookmarkDAO#remove(int)
95      */

96     public void remove(int bookmarkId) throws Exception JavaDoc
97     {
98         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
99                 SystemGlobals.getSql("BookmarkModel.remove"));
100         p.setInt(1, bookmarkId);
101         p.executeUpdate();
102         p.close();
103     }
104
105     /**
106      * @see net.jforum.dao.BookmarkDAO#selectByUser(int, int)
107      */

108     public List JavaDoc selectByUser(int userId, int relationType) throws Exception JavaDoc
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     /**
126      * @see net.jforum.dao.BookmarkDAO#selectByUser(int)
127      */

128     public List JavaDoc selectByUser(int userId) throws Exception JavaDoc
129     {
130         List JavaDoc l = new ArrayList JavaDoc();
131         
132         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
133                 SystemGlobals.getSql("BookmarkModel.selectAllFromUser"));
134         p.setInt(1, userId);
135         
136         ResultSet JavaDoc 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     /**
148      * @see net.jforum.dao.BookmarkDAO#selectById(int)
149      */

150     public Bookmark selectById(int bookmarkId) throws Exception JavaDoc
151     {
152         Bookmark b = null;
153         
154         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
155                 SystemGlobals.getSql("BookmarkModel.selectById"));
156         p.setInt(1, bookmarkId);
157         
158         ResultSet JavaDoc 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     /**
170      * @see net.jforum.dao.BookmarkDAO#selectForUpdate(int, int, int)
171      */

172     public Bookmark selectForUpdate(int relationId, int relationType, int userId) throws Exception JavaDoc
173     {
174         PreparedStatement JavaDoc 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 JavaDoc 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 JavaDoc getUsers(int userId) throws Exception JavaDoc
193     {
194         List JavaDoc l = new ArrayList JavaDoc();
195         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
196                 SystemGlobals.getSql("BookmarkModel.selectUserBookmarks"));
197         p.setInt(1, userId);
198         
199         ResultSet JavaDoc 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 JavaDoc getTopics(int userId) throws Exception JavaDoc
217     {
218         List JavaDoc l = new ArrayList JavaDoc();
219         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
220                 SystemGlobals.getSql("BookmarkModel.selectTopicBookmarks"));
221         p.setInt(1, userId);
222         
223         ResultSet JavaDoc 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 JavaDoc getForums(int userId) throws Exception JavaDoc
241     {
242         List JavaDoc l = new ArrayList JavaDoc();
243         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(
244                 SystemGlobals.getSql("BookmarkModel.selectForumBookmarks"));
245         p.setInt(1, userId);
246         
247         ResultSet JavaDoc 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 JavaDoc rs) throws Exception JavaDoc
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