001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/jdbc/MemberForumDAOImplJDBC.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 MemberForumDAOImplJDBC implements MemberForumDAO {
054:
055: private static Log log = LogFactory
056: .getLog(MemberForumDAOImplJDBC.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 MemberForumDAOImplJDBC() {
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 forumID,
073: int permission) throws ObjectNotFoundException,
074: DatabaseException {
075:
076: Connection connection = null;
077: PreparedStatement statement = null;
078: ResultSet resultSet = null;
079: StringBuffer sql = new StringBuffer(512);
080: sql.append("SELECT MemberID, ForumID, Permission");
081: sql.append(" FROM " + TABLE_NAME);
082: sql
083: .append(" WHERE MemberID = ? AND ForumID = ? AND Permission = ?");
084: try {
085: connection = DBUtils.getConnection();
086: statement = connection.prepareStatement(sql.toString());
087: statement.setInt(1, memberID);
088: statement.setInt(2, forumID);
089: statement.setInt(3, permission);
090: resultSet = statement.executeQuery();
091: if (!resultSet.next()) {
092: throw new ObjectNotFoundException(
093: "Cannot find the primary key (" + memberID
094: + ", " + forumID + ", " + permission
095: + ") in table 'MemberForum'.");
096: }
097: } catch (SQLException sqle) {
098: log.error("Sql Execution Error!", sqle);
099: throw new DatabaseException(
100: "Error executing SQL in MemberForumDAOImplJDBC.findByPrimaryKey.");
101: } finally {
102: DBUtils.closeResultSet(resultSet);
103: DBUtils.closeStatement(statement);
104: DBUtils.closeConnection(connection);
105: }
106: }
107:
108: /*
109: * Included columns: MemberID, ForumID, Permission
110: * Excluded columns:
111: */
112: public void create(int memberID, int forumID, int permission)
113: throws CreateException, DatabaseException,
114: DuplicateKeyException, ForeignKeyNotFoundException {
115:
116: // @todo: comment this try-catch block if the needed columns dont have attribute 'include'
117: // If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
118: // However, if primary key is a auto_increment column, then you can safely delete this block
119: try {
120: //Check if primary key already exists
121: findByPrimaryKey(memberID, forumID, permission);
122: //If so, then we have to throw an exception
123: throw new DuplicateKeyException(
124: "Primary key already exists. Cannot create new MemberForum with the same [MemberID, ForumID, Permission] ("
125: + memberID
126: + ", "
127: + forumID
128: + ", "
129: + permission + ").");
130: } catch (ObjectNotFoundException e) {
131: //Otherwise we can go ahead
132: }
133:
134: if (memberID != 0) {
135: try {
136: // @todo: modify the parameter list as needed
137: // You may have to regenerate this method if the needed columns dont have attribute 'include'
138: DAOFactory.getMemberDAO().findByPrimaryKey(memberID);
139: } catch (ObjectNotFoundException e) {
140: throw new ForeignKeyNotFoundException(
141: "Foreign key refers to table 'mvnforumMember' does not exist. Cannot create new MemberForum.");
142: }
143: }
144:
145: try {
146: // @todo: modify the parameter list as needed
147: // You may have to regenerate this method if the needed columns dont have attribute 'include'
148: DAOFactory.getForumDAO().findByPrimaryKey(forumID);
149: } catch (ObjectNotFoundException e) {
150: throw new ForeignKeyNotFoundException(
151: "Foreign key refers to table 'mvnforumForum' does not exist. Cannot create new MemberForum.");
152: }
153:
154: Connection connection = null;
155: PreparedStatement statement = null;
156: StringBuffer sql = new StringBuffer(512);
157: sql.append("INSERT INTO " + TABLE_NAME
158: + " (MemberID, ForumID, Permission)");
159: sql.append(" VALUES (?, ?, ?)");
160: try {
161: connection = DBUtils.getConnection();
162: statement = connection.prepareStatement(sql.toString());
163:
164: statement.setInt(1, memberID);
165: statement.setInt(2, forumID);
166: statement.setInt(3, permission);
167:
168: if (statement.executeUpdate() != 1) {
169: throw new CreateException(
170: "Error adding a row into table 'MemberForum'.");
171: }
172: m_dirty = true;
173: } catch (SQLException sqle) {
174: log.error("Sql Execution Error!", sqle);
175: throw new DatabaseException(
176: "Error executing SQL in MemberForumDAOImplJDBC.create.");
177: } finally {
178: DBUtils.closeStatement(statement);
179: DBUtils.closeConnection(connection);
180: }
181: }
182:
183: public void delete(int memberID, int forumID, int permission)
184: throws DatabaseException, ObjectNotFoundException {
185:
186: Connection connection = null;
187: PreparedStatement statement = null;
188: StringBuffer sql = new StringBuffer(512);
189: sql.append("DELETE FROM " + TABLE_NAME);
190: sql
191: .append(" WHERE MemberID = ? AND ForumID = ? AND Permission = ?");
192:
193: try {
194: connection = DBUtils.getConnection();
195: statement = connection.prepareStatement(sql.toString());
196: statement.setInt(1, memberID);
197: statement.setInt(2, forumID);
198: statement.setInt(3, permission);
199: if (statement.executeUpdate() != 1) {
200: throw new ObjectNotFoundException(
201: "Cannot delete a row in table MemberForum where primary key = ("
202: + memberID + ", " + forumID + ", "
203: + permission + ").");
204: }
205: m_dirty = true;
206: } catch (SQLException sqle) {
207: log.error("Sql Execution Error!", sqle);
208: throw new DatabaseException(
209: "Error executing SQL in MemberForumDAOImplJDBC.delete.");
210: } finally {
211: DBUtils.closeStatement(statement);
212: DBUtils.closeConnection(connection);
213: }
214: }
215:
216: public void delete_inMember(int memberID) throws DatabaseException {
217:
218: Connection connection = null;
219: PreparedStatement statement = null;
220: StringBuffer sql = new StringBuffer(512);
221: sql.append("DELETE FROM " + TABLE_NAME);
222: sql.append(" WHERE MemberID = ?");
223:
224: try {
225: connection = DBUtils.getConnection();
226: statement = connection.prepareStatement(sql.toString());
227: statement.setInt(1, memberID);
228: statement.executeUpdate();
229: m_dirty = true;
230: } catch (SQLException sqle) {
231: log.error("Sql Execution Error!", sqle);
232: throw new DatabaseException(
233: "Error executing SQL in MemberForumDAOImplJDBC.delete_inMember.");
234: } finally {
235: DBUtils.closeStatement(statement);
236: DBUtils.closeConnection(connection);
237: }
238: }
239:
240: public void delete_inForum(int forumID) throws DatabaseException {
241:
242: Connection connection = null;
243: PreparedStatement statement = null;
244: StringBuffer sql = new StringBuffer(512);
245: sql.append("DELETE FROM " + TABLE_NAME);
246: sql.append(" WHERE ForumID = ? ");
247:
248: try {
249: connection = DBUtils.getConnection();
250: statement = connection.prepareStatement(sql.toString());
251: statement.setInt(1, forumID);
252:
253: statement.executeUpdate();
254: m_dirty = true;
255: } catch (SQLException sqle) {
256: log.error("Sql Execution Error!", sqle);
257: throw new DatabaseException(
258: "Error executing SQL in MemberForumDAOImplJDBC.delete_inForum.");
259: } finally {
260: DBUtils.closeStatement(statement);
261: DBUtils.closeConnection(connection);
262: }
263: }
264:
265: /*
266: * Included columns: Permission
267: * Excluded columns: MemberID, ForumID
268: */
269: public Collection getBeans_inMemberForum(int memberID, int forumID)
270: throws DatabaseException {
271:
272: Connection connection = null;
273: PreparedStatement statement = null;
274: ResultSet resultSet = null;
275: Collection retValue = new ArrayList();
276: StringBuffer sql = new StringBuffer(512);
277: sql.append("SELECT Permission");
278: sql.append(" FROM " + TABLE_NAME);
279: sql.append(" WHERE MemberID = ? AND ForumID = ?");
280: try {
281: connection = DBUtils.getConnection();
282: statement = connection.prepareStatement(sql.toString());
283: statement.setInt(1, memberID);
284: statement.setInt(2, forumID);
285:
286: resultSet = statement.executeQuery();
287: while (resultSet.next()) {
288: MemberForumBean bean = new MemberForumBean();
289: bean.setMemberID(memberID);
290: bean.setForumID(forumID);
291: bean.setPermission(resultSet.getInt("Permission"));
292: retValue.add(bean);
293: }
294: return retValue;
295: } catch (SQLException sqle) {
296: log.error("Sql Execution Error!", sqle);
297: throw new DatabaseException(
298: "Error executing SQL in MemberForumDAOImplJDBC.getBeans_inMemberForum.");
299: } finally {
300: DBUtils.closeResultSet(resultSet);
301: DBUtils.closeStatement(statement);
302: DBUtils.closeConnection(connection);
303: }
304: }
305:
306: /*
307: * Included columns: MemberID, ForumID, Permission
308: * Excluded columns:
309: */
310: public Collection getBeans_inMember(int memberID)
311: throws DatabaseException {
312:
313: Connection connection = null;
314: PreparedStatement statement = null;
315: ResultSet resultSet = null;
316: Collection retValue = new ArrayList();
317:
318: StringBuffer sql = new StringBuffer(512);
319: sql.append("SELECT MemberID, ForumID, Permission");
320: sql.append(" FROM " + TABLE_NAME);
321: sql.append(" WHERE MemberID = ?");
322: try {
323: connection = DBUtils.getConnection();
324: statement = connection.prepareStatement(sql.toString());
325: statement.setInt(1, memberID);
326: resultSet = statement.executeQuery();
327: while (resultSet.next()) {
328: MemberForumBean bean = new MemberForumBean();
329: bean.setMemberID(resultSet.getInt("MemberID"));
330: bean.setForumID(resultSet.getInt("ForumID"));
331: bean.setPermission(resultSet.getInt("Permission"));
332: retValue.add(bean);
333: }
334: return retValue;
335: } catch (SQLException sqle) {
336: log.error("Sql Execution Error!", sqle);
337: throw new DatabaseException(
338: "Error executing SQL in MemberForumDAOImplJDBC.getBeans_inMember.");
339: } finally {
340: DBUtils.closeResultSet(resultSet);
341: DBUtils.closeStatement(statement);
342: DBUtils.closeConnection(connection);
343: }
344: }
345:
346: /*
347: * Included columns: MemberID, ForumID, Permission
348: * Excluded columns:
349: */
350: public Collection getBeans_inForum(int forumID)
351: throws DatabaseException {
352:
353: Connection connection = null;
354: PreparedStatement statement = null;
355: ResultSet resultSet = null;
356: Collection retValue = new ArrayList();
357:
358: StringBuffer sql = new StringBuffer(512);
359: sql.append("SELECT MemberID, ForumID, Permission");
360: sql.append(" FROM " + TABLE_NAME);
361: sql.append(" WHERE ForumID = ?");
362: sql.append(" ORDER BY MemberID ");
363:
364: try {
365: connection = DBUtils.getConnection();
366: statement = connection.prepareStatement(sql.toString());
367: statement.setInt(1, forumID);
368: resultSet = statement.executeQuery();
369: while (resultSet.next()) {
370: MemberForumBean bean = new MemberForumBean();
371: bean.setMemberID(resultSet.getInt("MemberID"));
372: bean.setForumID(resultSet.getInt("ForumID"));
373: bean.setPermission(resultSet.getInt("Permission"));
374: retValue.add(bean);
375: }
376: return retValue;
377: } catch (SQLException sqle) {
378: log.error("Sql Execution Error!", sqle);
379: throw new DatabaseException(
380: "Error executing SQL in MemberForumDAOImplJDBC.getBeans_inForum.");
381: } finally {
382: DBUtils.closeResultSet(resultSet);
383: DBUtils.closeStatement(statement);
384: DBUtils.closeConnection(connection);
385: }
386: }
387: }// end of class MemberForumDAOImplJDBC
|