KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > db > jdbc > GroupPermissionDAOImplJDBC


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/jdbc/GroupPermissionDAOImplJDBC.java,v 1.8 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.8 $
5  * $Date: 2006/04/14 17:05:26 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.db.jdbc;
42
43 import java.sql.*;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Collection JavaDoc;
46
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49 import com.mvnforum.db.*;
50 import net.myvietnam.mvncore.db.DBUtils;
51 import net.myvietnam.mvncore.exception.*;
52
53 public class GroupPermissionDAOImplJDBC implements GroupPermissionDAO {
54
55     private static Log log = LogFactory.getLog(GroupPermissionDAOImplJDBC.class);
56
57     // this variable will support caching if cache for this class is needed
58
private static boolean m_dirty = true;
59
60     public GroupPermissionDAOImplJDBC() {
61     }
62
63     protected static boolean isDirty() {
64         return m_dirty;
65     }
66
67     protected static void setDirty(boolean dirty) {
68         m_dirty = dirty;
69     }
70
71     public void findByPrimaryKey(int groupID, int permission)
72         throws ObjectNotFoundException, DatabaseException {
73
74         Connection connection = null;
75         PreparedStatement statement = null;
76         ResultSet resultSet = null;
77         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
78         sql.append("SELECT GroupID, Permission");
79         sql.append(" FROM " + TABLE_NAME);
80         sql.append(" WHERE GroupID = ? AND Permission = ?");
81         try {
82             connection = DBUtils.getConnection();
83             statement = connection.prepareStatement(sql.toString());
84             statement.setInt(1, groupID);
85             statement.setInt(2, permission);
86             resultSet = statement.executeQuery();
87             if (!resultSet.next()) {
88                 throw new ObjectNotFoundException("Cannot find the primary key (" + groupID + ", " + permission + ") in table 'GroupPermission'.");
89             }
90         } catch(SQLException sqle) {
91             log.error("Sql Execution Error!", sqle);
92             throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.findByPrimaryKey.");
93         } finally {
94             DBUtils.closeResultSet(resultSet);
95             DBUtils.closeStatement(statement);
96             DBUtils.closeConnection(connection);
97         }
98     }
99
100     /*
101      * Included columns: GroupID, Permission
102      * Excluded columns:
103      */

104     public void create(int groupID, int permission)
105         throws CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException {
106
107         // @todo: comment this try-catch block if the needed columns dont have attribute 'include'
108
// If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
109
// However, if primary key is a auto_increament column, then you can safely delete this block
110
try {
111             //Check if primary key already exists
112
findByPrimaryKey(groupID, permission);
113             //If so, then we have to throw an exception
114
throw new DuplicateKeyException("Primary key already exists. Cannot create new GroupPermission with the same [GroupID, Permission] (" + groupID + ", " + permission + ").");
115         } catch(ObjectNotFoundException e) {
116             //Otherwise we can go ahead
117
}
118
119         try {
120             // @todo: modify the parameter list as needed
121
// You may have to regenerate this method if the needed columns dont have attribute 'include'
122
DAOFactory.getGroupsDAO().findByPrimaryKey(groupID);
123         } catch(ObjectNotFoundException e) {
124             throw new ForeignKeyNotFoundException("Foreign key refers to table 'Groups' does not exist. Cannot create new GroupPermission.");
125         }
126
127         Connection connection = null;
128         PreparedStatement statement = null;
129         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
130         sql.append("INSERT INTO " + TABLE_NAME + " (GroupID, Permission)");
131         sql.append(" VALUES (?, ?)");
132         try {
133             connection = DBUtils.getConnection();
134             statement = connection.prepareStatement(sql.toString());
135
136             statement.setInt(1, groupID);
137             statement.setInt(2, permission);
138
139             if (statement.executeUpdate() != 1) {
140                 throw new CreateException("Error adding a row into table 'GroupPermission'.");
141             }
142             m_dirty = true;
143         } catch(SQLException sqle) {
144             log.error("Sql Execution Error!", sqle);
145             throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.create.");
146         } finally {
147             DBUtils.closeStatement(statement);
148             DBUtils.closeConnection(connection);
149         }
150     }
151
152     public void delete(int groupID, int permission)
153         throws DatabaseException, ObjectNotFoundException {
154
155         Connection connection = null;
156         PreparedStatement statement = null;
157         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
158         sql.append("DELETE FROM " + TABLE_NAME);
159         sql.append(" WHERE GroupID = ? AND Permission = ?");
160
161         try {
162             connection = DBUtils.getConnection();
163             statement = connection.prepareStatement(sql.toString());
164             statement.setInt(1, groupID);
165             statement.setInt(2, permission);
166             if (statement.executeUpdate() != 1) {
167                 throw new ObjectNotFoundException("Cannot delete a row in table GroupPermission where primary key = (" + groupID + ", " + permission + ").");
168             }
169             m_dirty = true;
170         } catch(SQLException sqle) {
171             log.error("Sql Execution Error!", sqle);
172             throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.delete.");
173         } finally {
174             DBUtils.closeStatement(statement);
175             DBUtils.closeConnection(connection);
176         }
177     }
178
179     public void delete_inGroup(int groupID)
180         throws DatabaseException {
181
182         Connection connection = null;
183         PreparedStatement statement = null;
184         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
185         sql.append("DELETE FROM " + TABLE_NAME);
186         sql.append(" WHERE GroupID = ? ");
187
188         try {
189             connection = DBUtils.getConnection();
190             statement = connection.prepareStatement(sql.toString());
191             statement.setInt(1, groupID);
192
193             statement.executeUpdate();
194             m_dirty = true;
195         } catch(SQLException sqle) {
196             log.error("Sql Execution Error!", sqle);
197             throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.delete_inGroup.");
198         } finally {
199             DBUtils.closeStatement(statement);
200             DBUtils.closeConnection(connection);
201         }
202     }
203
204
205 /************************************************
206  * Customized methods come below
207  ************************************************/

208
209
210     /*
211      * Included columns: Permission
212      * Excluded columns: GroupID
213      */

214     public Collection JavaDoc getBeans_inGroup(int groupID)
215         throws DatabaseException {
216
217         Connection connection = null;
218         PreparedStatement statement = null;
219         ResultSet resultSet = null;
220         Collection JavaDoc retValue = new ArrayList JavaDoc();
221         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
222         sql.append("SELECT Permission");
223         sql.append(" FROM " + TABLE_NAME);
224         sql.append(" WHERE GroupID = ?"); // @todo: uncomment as needed
225
//sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
226
try {
227             connection = DBUtils.getConnection();
228             statement = connection.prepareStatement(sql.toString());
229             statement.setInt(1, groupID);
230
231             resultSet = statement.executeQuery();
232             while (resultSet.next()) {
233                 GroupPermissionBean bean = new GroupPermissionBean();
234                 bean.setGroupID(groupID);
235                 bean.setPermission(resultSet.getInt("Permission"));
236                 retValue.add(bean);
237             }
238             return retValue;
239         } catch(SQLException sqle) {
240             log.error("Sql Execution Error!", sqle);
241             throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.getBeans_inGroup.");
242         } finally {
243             DBUtils.closeResultSet(resultSet);
244             DBUtils.closeStatement(statement);
245             DBUtils.closeConnection(connection);
246         }
247     }
248
249     /*
250      * Included columns: GroupID
251      * Excluded columns: Permission
252      */

253     public Collection JavaDoc getDistinctGroups()
254         throws DatabaseException {
255
256         Connection connection = null;
257         PreparedStatement statement = null;
258         ResultSet resultSet = null;
259         Collection JavaDoc retValue = new ArrayList JavaDoc();
260         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
261         sql.append("SELECT DISTINCT GroupID");
262         sql.append(" FROM " + TABLE_NAME);
263         sql.append(" ORDER BY GroupID ASC ");
264         try {
265             connection = DBUtils.getConnection();
266             statement = connection.prepareStatement(sql.toString());
267             resultSet = statement.executeQuery();
268             while (resultSet.next()) {
269                 GroupPermissionBean bean = new GroupPermissionBean();
270                 bean.setGroupID(resultSet.getInt("GroupID"));
271                 retValue.add(bean);
272             }
273             return retValue;
274         } catch(SQLException sqle) {
275             log.error("Sql Execution Error!", sqle);
276             throw new DatabaseException("Error executing SQL in GroupPermissionDAOImplJDBC.getDistinctGroups.");
277         } finally {
278             DBUtils.closeResultSet(resultSet);
279             DBUtils.closeStatement(statement);
280             DBUtils.closeConnection(connection);
281         }
282     }
283
284 }// end of class GroupPermissionDAOImplJDBC
285
Popular Tags