001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/jdbc/MemberPermissionDAOImplJDBC.java,v 1.9 2007/10/09 11:09:20 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.9 $
005: * $Date: 2007/10/09 11:09:20 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Mai Nguyen
040: */
041: package com.mvnforum.db.jdbc;
042:
043: import java.sql.*;
044: import java.util.ArrayList;
045: import java.util.Collection;
046:
047: import com.mvnforum.db.*;
048: import net.myvietnam.mvncore.db.DBUtils;
049: import net.myvietnam.mvncore.exception.*;
050: import org.apache.commons.logging.Log;
051: import org.apache.commons.logging.LogFactory;
052:
053: public class MemberPermissionDAOImplJDBC implements MemberPermissionDAO {
054:
055: private static Log log = LogFactory
056: .getLog(MemberPermissionDAOImplJDBC.class);
057:
058: // this variable will support caching if cache for this class is needed
059: private static boolean m_dirty = true;
060:
061: public MemberPermissionDAOImplJDBC() {
062: }
063:
064: protected static boolean isDirty() {
065: return m_dirty;
066: }
067:
068: protected static void setDirty(boolean dirty) {
069: m_dirty = dirty;
070: }
071:
072: public void findByPrimaryKey(int memberID, int permission)
073: throws ObjectNotFoundException, DatabaseException {
074:
075: Connection connection = null;
076: PreparedStatement statement = null;
077: ResultSet resultSet = null;
078: StringBuffer sql = new StringBuffer(512);
079: sql.append("SELECT MemberID, Permission");
080: sql.append(" FROM " + TABLE_NAME);
081: sql.append(" WHERE MemberID = ? AND Permission = ?");
082: try {
083: connection = DBUtils.getConnection();
084: statement = connection.prepareStatement(sql.toString());
085: statement.setInt(1, memberID);
086: statement.setInt(2, permission);
087: resultSet = statement.executeQuery();
088: if (!resultSet.next()) {
089: throw new ObjectNotFoundException(
090: "Cannot find the primary key (" + memberID
091: + ", " + permission
092: + ") in table 'MemberPermission'.");
093: }
094: } catch (SQLException sqle) {
095: log.error("Sql Execution Error!", sqle);
096: throw new DatabaseException(
097: "Error executing SQL in MemberPermissionDAOImplJDBC.findByPrimaryKey.");
098: } finally {
099: DBUtils.closeResultSet(resultSet);
100: DBUtils.closeStatement(statement);
101: DBUtils.closeConnection(connection);
102: }
103: }
104:
105: /*
106: * Included columns: MemberID, Permission
107: * Excluded columns:
108: */
109: public void create(int memberID, int permission)
110: throws CreateException, DatabaseException,
111: DuplicateKeyException, ForeignKeyNotFoundException {
112:
113: // @todo: comment this try-catch block if the needed columns dont have attribute 'include'
114: // If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
115: // However, if primary key is a auto_increment column, then you can safely delete this block
116: try {
117: //Check if primary key already exists
118: findByPrimaryKey(memberID, permission);
119: //If so, then we have to throw an exception
120: throw new DuplicateKeyException(
121: "Primary key already exists. Cannot create new MemberPermission with the same [MemberID, Permission] ("
122: + memberID + ", " + permission + ").");
123: } catch (ObjectNotFoundException e) {
124: //Otherwise we can go ahead
125: }
126:
127: if (memberID != 0) {
128: try {
129: // @todo: modify the parameter list as needed
130: // You may have to regenerate this method if the needed columns dont have attribute 'include'
131: DAOFactory.getMemberDAO().findByPrimaryKey(memberID);
132: } catch (ObjectNotFoundException e) {
133: throw new ForeignKeyNotFoundException(
134: "Foreign key refers to table 'mvnforumMember' does not exist. Cannot create new MemberPermission.");
135: }
136: }
137:
138: Connection connection = null;
139: PreparedStatement statement = null;
140: StringBuffer sql = new StringBuffer(512);
141: sql.append("INSERT INTO " + TABLE_NAME
142: + " (MemberID, Permission)");
143: sql.append(" VALUES (?, ?)");
144: try {
145: connection = DBUtils.getConnection();
146: statement = connection.prepareStatement(sql.toString());
147:
148: statement.setInt(1, memberID);
149: statement.setInt(2, permission);
150:
151: if (statement.executeUpdate() != 1) {
152: throw new CreateException(
153: "Error adding a row into table 'MemberPermission'.");
154: }
155: m_dirty = true;
156: } catch (SQLException sqle) {
157: log.error("Sql Execution Error!", sqle);
158: throw new DatabaseException(
159: "Error executing SQL in MemberPermissionDAOImplJDBC.create.");
160: } finally {
161: DBUtils.closeStatement(statement);
162: DBUtils.closeConnection(connection);
163: }
164: }
165:
166: public void delete(int memberID, int permission)
167: throws DatabaseException, ObjectNotFoundException {
168:
169: Connection connection = null;
170: PreparedStatement statement = null;
171: StringBuffer sql = new StringBuffer(512);
172: sql.append("DELETE FROM " + TABLE_NAME);
173: sql.append(" WHERE MemberID = ? AND Permission = ?");
174:
175: try {
176: connection = DBUtils.getConnection();
177: statement = connection.prepareStatement(sql.toString());
178: statement.setInt(1, memberID);
179: statement.setInt(2, permission);
180: if (statement.executeUpdate() != 1) {
181: throw new ObjectNotFoundException(
182: "Cannot delete a row in table MemberPermission where primary key = ("
183: + memberID + ", " + permission + ").");
184: }
185: m_dirty = true;
186: } catch (SQLException sqle) {
187: log.error("Sql Execution Error!", sqle);
188: throw new DatabaseException(
189: "Error executing SQL in MemberPermissionDAOImplJDBC.delete.");
190: } finally {
191: DBUtils.closeStatement(statement);
192: DBUtils.closeConnection(connection);
193: }
194: }
195:
196: public void delete_inMember(int memberID) throws DatabaseException {
197:
198: Connection connection = null;
199: PreparedStatement statement = null;
200: StringBuffer sql = new StringBuffer(512);
201: sql.append("DELETE FROM " + TABLE_NAME);
202: sql.append(" WHERE MemberID = ?");
203:
204: try {
205: connection = DBUtils.getConnection();
206: statement = connection.prepareStatement(sql.toString());
207: statement.setInt(1, memberID);
208: statement.executeUpdate();
209: m_dirty = true;
210: } catch (SQLException sqle) {
211: log.error("Sql Execution Error!", sqle);
212: throw new DatabaseException(
213: "Error executing SQL in MemberPermissionDAOImplJDBC.delete_inMember.");
214: } finally {
215: DBUtils.closeStatement(statement);
216: DBUtils.closeConnection(connection);
217: }
218: }
219:
220: /*
221: * Included columns: MemberID, Permission
222: * Excluded columns:
223: */
224: public Collection getBeans_inMember(int memberID)
225: throws DatabaseException {
226:
227: Connection connection = null;
228: PreparedStatement statement = null;
229: ResultSet resultSet = null;
230: Collection retValue = new ArrayList();
231: StringBuffer sql = new StringBuffer(512);
232: sql.append("SELECT Permission");
233: sql.append(" FROM " + TABLE_NAME);
234: sql.append(" WHERE MemberID = ?");
235: try {
236: connection = DBUtils.getConnection();
237: statement = connection.prepareStatement(sql.toString());
238: statement.setInt(1, memberID);
239: resultSet = statement.executeQuery();
240: while (resultSet.next()) {
241: MemberPermissionBean bean = new MemberPermissionBean();
242: bean.setMemberID(memberID);
243: bean.setPermission(resultSet.getInt("Permission"));
244: retValue.add(bean);
245: }
246: return retValue;
247: } catch (SQLException sqle) {
248: log.error("Sql Execution Error!", sqle);
249: throw new DatabaseException(
250: "Error executing SQL in MemberPermissionDAOImplJDBC.getBeans_inMember.");
251: } finally {
252: DBUtils.closeResultSet(resultSet);
253: DBUtils.closeStatement(statement);
254: DBUtils.closeConnection(connection);
255: }
256: }
257:
258: } // end of class MemberPermissionDAOImplJDBC
|