GenericGroupDAO.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 6k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  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. import java.sql.PreparedStatement;
  45. import java.sql.ResultSet;
  46. import java.sql.SQLException;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49. import net.jforum.JForumExecutionContext;
  50. import net.jforum.dao.DataAccessDriver;
  51. import net.jforum.dao.GroupSecurityDAO;
  52. import net.jforum.entities.Group;
  53. import net.jforum.exceptions.DatabaseException;
  54. import net.jforum.util.DbUtils;
  55. import net.jforum.util.preferences.SystemGlobals;
  56. /**
  57.  * @author Rafael Steil
  58.  * @version $Id: GenericGroupDAO.java,v 1.8 2007/08/24 23:11:35 rafaelsteil Exp $
  59.  */
  60. public class GenericGroupDAO implements net.jforum.dao.GroupDAO
  61. {
  62. /**
  63.  * @see net.jforum.dao.GroupDAO#selectById(int)
  64.  */
  65. public Group selectById(int groupId)
  66. {
  67. PreparedStatement p = null;
  68. ResultSet rs = null;
  69. try {
  70. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.selectById"));
  71. p.setInt(1, groupId);
  72. rs = p.executeQuery();
  73. Group g = new Group();
  74. if (rs.next()) {
  75. g = this.getGroup(rs);
  76. }
  77. return g;
  78. }
  79. catch (SQLException e) {
  80. throw new DatabaseException(e);
  81. }
  82. finally {
  83. DbUtils.close(rs, p);
  84. }
  85. }
  86. /**
  87.  * @see net.jforum.dao.GroupDAO#canDelete(int)
  88.  */
  89. public boolean canDelete(int groupId)
  90. {
  91. PreparedStatement p = null;
  92. ResultSet rs = null;
  93. try {
  94. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.canDelete"));
  95. p.setInt(1, groupId);
  96. boolean status = false;
  97. rs = p.executeQuery();
  98. if (!rs.next() || rs.getInt("total") < 1) {
  99. status = true;
  100. }
  101. return status;
  102. }
  103. catch (SQLException e) {
  104. throw new DatabaseException(e);
  105. }
  106. finally {
  107. DbUtils.close(rs, p);
  108. }
  109. }
  110. /**
  111.  * @see net.jforum.dao.GroupDAO#delete(int)
  112.  */
  113. public void delete(int groupId)
  114. {
  115. PreparedStatement p = null;
  116. try {
  117. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.delete"));
  118. p.setInt(1, groupId);
  119. p.executeUpdate();
  120. GroupSecurityDAO securityDao = DataAccessDriver.getInstance().newGroupSecurityDAO();
  121. securityDao.deleteAllRoles(groupId);
  122. }
  123. catch (SQLException e) {
  124. throw new DatabaseException(e);
  125. }
  126. finally {
  127. DbUtils.close(p);
  128. }
  129. }
  130. /**
  131.  * @see net.jforum.dao.GroupDAO#update(net.jforum.entities.Group)
  132.  */
  133. public void update(Group group)
  134. {
  135. PreparedStatement p = null;
  136. try {
  137. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.update"));
  138. p.setString(1, group.getName());
  139. p.setInt(2, group.getParentId());
  140. p.setString(3, group.getDescription());
  141. p.setInt(4, group.getId());
  142. p.executeUpdate();
  143. }
  144. catch (SQLException e) {
  145. throw new DatabaseException(e);
  146. }
  147. finally {
  148. DbUtils.close(p);
  149. }
  150. }
  151. /**
  152.  * @see net.jforum.dao.GroupDAO#addNew(net.jforum.entities.Group)
  153.  */
  154. public void addNew(Group group)
  155. {
  156. PreparedStatement p = null;
  157. try {
  158. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.addNew"));
  159. p.setString(1, group.getName());
  160. p.setString(2, group.getDescription());
  161. p.setInt(3, group.getParentId());
  162. p.executeUpdate();
  163. }
  164. catch (SQLException e) {
  165. throw new DatabaseException(e);
  166. }
  167. finally {
  168. DbUtils.close(p);
  169. }
  170. }
  171. /**
  172.  * @see net.jforum.dao.GroupDAO#selectUsersIds(int)
  173.  */
  174. public List selectUsersIds(int groupId)
  175. {
  176. ArrayList l = new ArrayList();
  177. PreparedStatement p = null;
  178. ResultSet rs = null;
  179. try {
  180. p = JForumExecutionContext.getConnection().prepareStatement(
  181. SystemGlobals.getSql("GroupModel.selectUsersIds"));
  182. p.setInt(1, groupId);
  183. rs = p.executeQuery();
  184. while (rs.next()) {
  185. l.add(new Integer(rs.getInt("user_id")));
  186. }
  187. return l;
  188. }
  189. catch (SQLException e) {
  190. throw new DatabaseException(e);
  191. }
  192. finally {
  193. DbUtils.close(rs, p);
  194. }
  195. }
  196. protected List fillGroups(ResultSet rs) throws SQLException
  197. {
  198. List l = new ArrayList();
  199. while (rs.next()) {
  200. l.add(this.getGroup(rs));
  201. }
  202. return l;
  203. }
  204. protected Group getGroup(ResultSet rs) throws SQLException
  205. {
  206. Group g = new Group();
  207. g.setId(rs.getInt("group_id"));
  208. g.setDescription(rs.getString("group_description"));
  209. g.setName(rs.getString("group_name"));
  210. g.setParentId(rs.getInt("parent_id"));
  211. return g;
  212. }
  213. /**
  214.  * @see net.jforum.dao.GroupDAO#selectAll()
  215.  */
  216. public List selectAll()
  217. {
  218. PreparedStatement p = null;
  219. ResultSet rs = null;
  220. try {
  221. p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("GroupModel.selectAll"));
  222. rs = p.executeQuery();
  223. return this.fillGroups(rs);
  224. }
  225. catch (SQLException e) {
  226. throw new DatabaseException(e);
  227. }
  228. finally {
  229. DbUtils.close(rs, p);
  230. }
  231. }
  232. }