KickJava   Java API By Example, From Geeks To Geeks.

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


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

43 package net.jforum.dao.generic;
44
45 import java.sql.PreparedStatement JavaDoc;
46 import java.sql.ResultSet JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.List JavaDoc;
49
50 import net.jforum.JForumExecutionContext;
51 import net.jforum.entities.Group;
52 import net.jforum.util.preferences.SystemGlobals;
53
54 /**
55  * @author Rafael Steil
56  * @version $Id: GenericGroupDAO.java,v 1.4 2006/01/29 15:06:25 rafaelsteil Exp $
57  */

58 public class GenericGroupDAO implements net.jforum.dao.GroupDAO
59 {
60     /**
61      * @see net.jforum.dao.GroupDAO#selectById(int)
62      */

63     public Group selectById(int groupId) throws Exception JavaDoc
64     {
65         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.selectById"));
66         p.setInt(1, groupId);
67         
68         ResultSet JavaDoc rs = p.executeQuery();
69         
70         Group g = new Group();
71         
72         if (rs.next()) {
73             g = this.getGroup(rs);
74         }
75         
76         rs.close();
77         p.close();
78
79         return g;
80     }
81
82     /**
83      * @see net.jforum.dao.GroupDAO#canDelete(int)
84      */

85     public boolean canDelete(int groupId) throws Exception JavaDoc
86     {
87         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.canDelete"));
88         p.setInt(1, groupId);
89         
90         boolean status = false;
91         
92         ResultSet JavaDoc rs = p.executeQuery();
93         if (!rs.next() || rs.getInt("total") < 1) {
94             status = true;
95         }
96         
97         rs.close();
98         p.close();
99         
100         return status;
101     }
102
103     /**
104      * @see net.jforum.dao.GroupDAO#delete(int)
105      */

106     public void delete(int groupId) throws Exception JavaDoc
107     {
108         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.delete"));
109         p.setInt(1, groupId);
110         
111         p.executeUpdate();
112         p.close();
113     }
114
115     /**
116      * @see net.jforum.dao.GroupDAO#update(net.jforum.Group)
117      */

118     public void update(Group group) throws Exception JavaDoc
119     {
120         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.update"));
121         p.setString(1, group.getName());
122         p.setInt(2, group.getParentId());
123         p.setString(3, group.getDescription());
124         p.setInt(4, group.getId());
125         
126         p.executeUpdate();
127         p.close();
128     }
129
130     /**
131      * @see net.jforum.dao.GroupDAO#addNew(net.jforum.Group)
132      */

133     public void addNew(Group group) throws Exception JavaDoc
134     {
135         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.addNew"));
136         p.setString(1, group.getName());
137         p.setString(2, group.getDescription());
138         p.setInt(3, group.getParentId());
139         
140         p.executeUpdate();
141         p.close();
142     }
143
144     /**
145      * @see net.jforum.dao.GroupDAO#selectUsersIds(int)
146      */

147     public List JavaDoc selectUsersIds(int groupId) throws Exception JavaDoc
148     {
149         ArrayList JavaDoc l = new ArrayList JavaDoc();
150         
151         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.selectUsersIds"));
152         p.setInt(1, groupId);
153         
154         ResultSet JavaDoc rs = p.executeQuery();
155         while (rs.next()) {
156             l.add(new Integer JavaDoc(rs.getInt("user_id")));
157         }
158         
159         rs.close();
160         p.close();
161
162         return l;
163     }
164     
165     protected List JavaDoc fillGroups(ResultSet JavaDoc rs) throws Exception JavaDoc
166     {
167         List JavaDoc l = new ArrayList JavaDoc();
168         
169         while (rs.next()) {
170             l.add(this.getGroup(rs));
171         }
172         
173         return l;
174     }
175     
176     protected Group getGroup(ResultSet JavaDoc rs) throws Exception JavaDoc
177     {
178         Group g = new Group();
179         
180         g.setId(rs.getInt("group_id"));
181         g.setDescription(rs.getString("group_description"));
182         g.setName(rs.getString("group_name"));
183         g.setParentId(rs.getInt("parent_id"));
184         
185         return g;
186     }
187
188     /**
189      * @see net.jforum.dao.GroupDAO#selectAll()
190      */

191     public List JavaDoc selectAll() throws Exception JavaDoc
192     {
193         PreparedStatement JavaDoc p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.selectAll"));
194         ResultSet JavaDoc rs = p.executeQuery();
195         
196         List JavaDoc l = this.fillGroups(rs);
197         
198         rs.close();
199         p.close();
200         
201         return l;
202     }
203
204 }
205
Popular Tags