KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/jdbc/MemberPermissionDAOImplJDBC.java,v 1.7 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.7 $
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 com.mvnforum.db.*;
48 import net.myvietnam.mvncore.db.DBUtils;
49 import net.myvietnam.mvncore.exception.*;
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52
53 public class MemberPermissionDAOImplJDBC implements MemberPermissionDAO {
54
55     private static Log log = LogFactory.getLog(MemberPermissionDAOImplJDBC.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 MemberPermissionDAOImplJDBC() {
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 memberID, 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 MemberID, Permission");
79         sql.append(" FROM " + TABLE_NAME);
80         sql.append(" WHERE MemberID = ? AND Permission = ?");
81         try {
82             connection = DBUtils.getConnection();
83             statement = connection.prepareStatement(sql.toString());
84             statement.setInt(1, memberID);
85             statement.setInt(2, permission);
86             resultSet = statement.executeQuery();
87             if (!resultSet.next()) {
88                 throw new ObjectNotFoundException("Cannot find the primary key (" + memberID + ", " + permission + ") in table 'MemberPermission'.");
89             }
90         } catch (SQLException sqle) {
91             log.error("Sql Execution Error!", sqle);
92             throw new DatabaseException("Error executing SQL in MemberPermissionDAOImplJDBC.findByPrimaryKey.");
93         } finally {
94             DBUtils.closeResultSet(resultSet);
95             DBUtils.closeStatement(statement);
96             DBUtils.closeConnection(connection);
97         }
98     }
99
100     /*
101      * Included columns: MemberID, Permission
102      * Excluded columns:
103      */

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

209     public Collection JavaDoc getBeans_inMember(int memberID)
210         throws DatabaseException {
211
212         Connection connection = null;
213         PreparedStatement statement = null;
214         ResultSet resultSet = null;
215         Collection JavaDoc retValue = new ArrayList JavaDoc();
216         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
217         sql.append("SELECT Permission");
218         sql.append(" FROM " + TABLE_NAME);
219         sql.append(" WHERE MemberID = ?");
220         try {
221             connection = DBUtils.getConnection();
222             statement = connection.prepareStatement(sql.toString());
223             statement.setInt(1, memberID);
224             resultSet = statement.executeQuery();
225             while (resultSet.next()) {
226                 MemberPermissionBean bean = new MemberPermissionBean();
227                 bean.setMemberID(memberID);
228                 bean.setPermission(resultSet.getInt("Permission"));
229                 retValue.add(bean);
230             }
231             return retValue;
232         } catch (SQLException sqle) {
233             log.error("Sql Execution Error!", sqle);
234             throw new DatabaseException("Error executing SQL in MemberPermissionDAOImplJDBC.getBeans_inMember.");
235         } finally {
236             DBUtils.closeResultSet(resultSet);
237             DBUtils.closeStatement(statement);
238             DBUtils.closeConnection(connection);
239         }
240     }
241
242 } // end of class MemberPermissionDAOImplJDBC
243
Popular Tags