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: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: Mar 3, 2003 / 1:35:30 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.dao.generic;
044:
045: import java.sql.PreparedStatement;
046: import java.sql.ResultSet;
047: import java.sql.SQLException;
048: import java.util.ArrayList;
049: import java.util.List;
050:
051: import net.jforum.JForumExecutionContext;
052: import net.jforum.dao.DataAccessDriver;
053: import net.jforum.dao.GroupSecurityDAO;
054: import net.jforum.entities.Group;
055: import net.jforum.exceptions.DatabaseException;
056: import net.jforum.util.DbUtils;
057: import net.jforum.util.preferences.SystemGlobals;
058:
059: /**
060: * @author Rafael Steil
061: * @version $Id: GenericGroupDAO.java,v 1.8 2007/08/24 23:11:35 rafaelsteil Exp $
062: */
063: public class GenericGroupDAO implements net.jforum.dao.GroupDAO {
064: /**
065: * @see net.jforum.dao.GroupDAO#selectById(int)
066: */
067: public Group selectById(int groupId) {
068: PreparedStatement p = null;
069: ResultSet rs = null;
070: try {
071: p = JForumExecutionContext.getConnection()
072: .prepareStatement(
073: SystemGlobals
074: .getSql("GroupModel.selectById"));
075: p.setInt(1, groupId);
076:
077: rs = p.executeQuery();
078:
079: Group g = new Group();
080:
081: if (rs.next()) {
082: g = this .getGroup(rs);
083: }
084:
085: return g;
086: } catch (SQLException e) {
087: throw new DatabaseException(e);
088: } finally {
089: DbUtils.close(rs, p);
090: }
091: }
092:
093: /**
094: * @see net.jforum.dao.GroupDAO#canDelete(int)
095: */
096: public boolean canDelete(int groupId) {
097: PreparedStatement p = null;
098: ResultSet rs = null;
099: try {
100: p = JForumExecutionContext.getConnection()
101: .prepareStatement(
102: SystemGlobals
103: .getSql("GroupModel.canDelete"));
104: p.setInt(1, groupId);
105:
106: boolean status = false;
107:
108: rs = p.executeQuery();
109: if (!rs.next() || rs.getInt("total") < 1) {
110: status = true;
111: }
112:
113: return status;
114: } catch (SQLException e) {
115: throw new DatabaseException(e);
116: } finally {
117: DbUtils.close(rs, p);
118: }
119: }
120:
121: /**
122: * @see net.jforum.dao.GroupDAO#delete(int)
123: */
124: public void delete(int groupId) {
125: PreparedStatement p = null;
126: try {
127: p = JForumExecutionContext.getConnection()
128: .prepareStatement(
129: SystemGlobals.getSql("GroupModel.delete"));
130: p.setInt(1, groupId);
131:
132: p.executeUpdate();
133:
134: GroupSecurityDAO securityDao = DataAccessDriver
135: .getInstance().newGroupSecurityDAO();
136: securityDao.deleteAllRoles(groupId);
137: } catch (SQLException e) {
138: throw new DatabaseException(e);
139: } finally {
140: DbUtils.close(p);
141: }
142: }
143:
144: /**
145: * @see net.jforum.dao.GroupDAO#update(net.jforum.entities.Group)
146: */
147: public void update(Group group) {
148: PreparedStatement p = null;
149: try {
150: p = JForumExecutionContext.getConnection()
151: .prepareStatement(
152: SystemGlobals.getSql("GroupModel.update"));
153: p.setString(1, group.getName());
154: p.setInt(2, group.getParentId());
155: p.setString(3, group.getDescription());
156: p.setInt(4, group.getId());
157:
158: p.executeUpdate();
159: } catch (SQLException e) {
160: throw new DatabaseException(e);
161: } finally {
162: DbUtils.close(p);
163: }
164: }
165:
166: /**
167: * @see net.jforum.dao.GroupDAO#addNew(net.jforum.entities.Group)
168: */
169: public void addNew(Group group) {
170: PreparedStatement p = null;
171: try {
172: p = JForumExecutionContext.getConnection()
173: .prepareStatement(
174: SystemGlobals.getSql("GroupModel.addNew"));
175: p.setString(1, group.getName());
176: p.setString(2, group.getDescription());
177: p.setInt(3, group.getParentId());
178:
179: p.executeUpdate();
180: } catch (SQLException e) {
181: throw new DatabaseException(e);
182: } finally {
183: DbUtils.close(p);
184: }
185: }
186:
187: /**
188: * @see net.jforum.dao.GroupDAO#selectUsersIds(int)
189: */
190: public List selectUsersIds(int groupId) {
191: ArrayList l = new ArrayList();
192:
193: PreparedStatement p = null;
194: ResultSet rs = null;
195: try {
196: p = JForumExecutionContext
197: .getConnection()
198: .prepareStatement(
199: SystemGlobals
200: .getSql("GroupModel.selectUsersIds"));
201: p.setInt(1, groupId);
202:
203: rs = p.executeQuery();
204: while (rs.next()) {
205: l.add(new Integer(rs.getInt("user_id")));
206: }
207:
208: return l;
209: } catch (SQLException e) {
210: throw new DatabaseException(e);
211: } finally {
212: DbUtils.close(rs, p);
213: }
214: }
215:
216: protected List fillGroups(ResultSet rs) throws SQLException {
217: List l = new ArrayList();
218:
219: while (rs.next()) {
220: l.add(this .getGroup(rs));
221: }
222:
223: return l;
224: }
225:
226: protected Group getGroup(ResultSet rs) throws SQLException {
227: Group g = new Group();
228:
229: g.setId(rs.getInt("group_id"));
230: g.setDescription(rs.getString("group_description"));
231: g.setName(rs.getString("group_name"));
232: g.setParentId(rs.getInt("parent_id"));
233:
234: return g;
235: }
236:
237: /**
238: * @see net.jforum.dao.GroupDAO#selectAll()
239: */
240: public List selectAll() {
241: PreparedStatement p = null;
242: ResultSet rs = null;
243: try {
244: p = JForumExecutionContext.getConnection()
245: .prepareStatement(
246: SystemGlobals
247: .getSql("GroupModel.selectAll"));
248: rs = p.executeQuery();
249:
250: return this .fillGroups(rs);
251: } catch (SQLException e) {
252: throw new DatabaseException(e);
253: } finally {
254: DbUtils.close(rs, p);
255: }
256: }
257: }
|