001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/phpbb2mvnforum/db/jdbc/ForumDAOImplJDBC.java,v 1.5 2007/01/15 10:27:12 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.5 $
005: * $Date: 2007/01/15 10:27:12 $
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:
039: */
040: package org.mvnforum.phpbb2mvnforum.db.jdbc;
041:
042: import java.sql.Connection;
043: import java.sql.PreparedStatement;
044: import java.sql.ResultSet;
045: import java.sql.SQLException;
046: import java.sql.Timestamp;
047: import java.util.Collection;
048:
049: import org.mvnforum.phpbb2mvnforum.db.ForumDAO;
050: import org.mvnforum.util.DBUtils;
051:
052: import net.myvietnam.mvncore.exception.CreateException;
053: import net.myvietnam.mvncore.exception.DatabaseException;
054: import net.myvietnam.mvncore.exception.DuplicateKeyException;
055: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
056:
057: public class ForumDAOImplJDBC implements ForumDAO {
058:
059: public void findByPrimaryKey(int forumID)
060: throws ObjectNotFoundException, DatabaseException {
061:
062: Connection connection = null;
063: PreparedStatement statement = null;
064: ResultSet resultSet = null;
065: StringBuffer sql = new StringBuffer(512);
066: sql.append("SELECT ForumID");
067: sql.append(" FROM " + TABLE_NAME);
068: sql.append(" WHERE ForumID = ?");
069: try {
070: connection = DBUtils.getMvnConnection();
071: statement = connection.prepareStatement(sql.toString());
072: statement.setInt(1, forumID);
073: resultSet = statement.executeQuery();
074: if (!resultSet.next()) {
075: throw new ObjectNotFoundException(
076: "Cannot find the primary key (" + forumID
077: + ") in table 'forum'.");
078: }
079: } catch (SQLException sqle) {
080: throw new DatabaseException(
081: "Error executing SQL in forumDAOImplJDBC.findByPrimaryKey.");
082: } finally {
083: DBUtils.closeResultSet(resultSet);
084: DBUtils.closeStatement(statement);
085: DBUtils.closeConnection(connection);
086: }
087: }
088:
089: public void findByAlternateKey_ForumName_CategoryID(
090: String forumName, int categoryID)
091: throws ObjectNotFoundException, DatabaseException {
092:
093: Connection connection = null;
094: PreparedStatement statement = null;
095: ResultSet resultSet = null;
096: StringBuffer sql = new StringBuffer(512);
097: sql.append("SELECT ForumName, CategoryID");
098: sql.append(" FROM " + TABLE_NAME);
099: sql.append(" WHERE ForumName = ? AND CategoryID = ?");
100: try {
101: connection = DBUtils.getMvnConnection();
102: statement = connection.prepareStatement(sql.toString());
103: statement.setString(1, forumName);
104: statement.setInt(2, categoryID);
105: resultSet = statement.executeQuery();
106: if (!resultSet.next()) {
107: throw new ObjectNotFoundException(
108: "Cannot find the alternate key [ForumName, CategoryID] ("
109: + forumName + ", " + categoryID
110: + ") in table 'forum'.");
111: }
112: } catch (SQLException sqle) {
113: throw new DatabaseException(
114: "Error executing SQL in forumDAOImplJDBC.findByAlternateKey_ForumName_CategoryID.");
115: } finally {
116: DBUtils.closeResultSet(resultSet);
117: DBUtils.closeStatement(statement);
118: DBUtils.closeConnection(connection);
119: }
120: }
121:
122: /*
123: * Included columns: ForumID, CategoryID, LastPostMemberName, ForumName, ForumDesc,
124: * ForumCreationDate, ForumModifiedDate, ForumLastPostDate, ForumOrder, ForumType,
125: * ForumFormatOption, ForumOption, ForumStatus, ForumModerationMode, ForumPassword,
126: * ForumThreadCount, ForumPostCount
127: * Excluded columns:
128: */
129: public void create(int forumID, int categoryID,
130: String lastPostMemberName, String forumName,
131: String forumDesc, Timestamp forumCreationDate,
132: Timestamp forumModifiedDate, Timestamp forumLastPostDate,
133: int forumOrder, int forumType, int forumFormatOption,
134: int forumOption, int forumStatus, int forumModerationMode,
135: String forumPassword, int forumThreadCount,
136: int forumPostCount) throws CreateException,
137: DatabaseException/*, DuplicateKeyException*/,
138: DuplicateKeyException {
139:
140: // @todo: comment this try-catch block if the needed columns dont have attribute 'include'
141: // If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
142: // However, if primary key is a auto_increament column, then you can safely delete this block
143: try {
144: //Check if primary key already exists
145: findByPrimaryKey(forumID);
146: //If so, then we have to throw an exception
147: throw new DuplicateKeyException(
148: "Primary key already exists. Cannot create new forum with the same [ForumID] ("
149: + forumID + ").");
150: } catch (ObjectNotFoundException e) {
151: //Otherwise we can go ahead
152: }
153:
154: // @todo: Comment this try-catch block if the needed columns dont have attribute 'include'
155: // If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
156: try {
157: //Check if alternate key already exists
158: findByAlternateKey_ForumName_CategoryID(forumName,
159: categoryID);
160: //If so, then we have to throw an exception
161: throw new DuplicateKeyException(
162: "Alternate key already exists. Cannot create new forum with the same [ForumName, CategoryID] ("
163: + forumName + ", " + categoryID + ").");
164: } catch (ObjectNotFoundException e) {
165: //Otherwise we can go ahead
166: }
167:
168: Connection connection = null;
169: PreparedStatement statement = null;
170: StringBuffer sql = new StringBuffer(512);
171: sql
172: .append("INSERT INTO "
173: + TABLE_NAME
174: + " (ForumID, CategoryID, LastPostMemberName, ForumName, ForumDesc, ForumCreationDate, ForumModifiedDate, ForumLastPostDate, ForumOrder, ForumType, ForumFormatOption, ForumOption, ForumStatus, ForumModerationMode, ForumPassword, ForumThreadCount, ForumPostCount)");
175: sql
176: .append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
177: try {
178: connection = DBUtils.getMvnConnection();
179: statement = connection.prepareStatement(sql.toString());
180:
181: statement.setInt(1, forumID);
182: statement.setInt(2, categoryID);
183: statement.setString(3, lastPostMemberName);
184: statement.setString(4, forumName);
185: statement.setString(5, forumDesc);
186: statement.setTimestamp(6, forumCreationDate);
187: statement.setTimestamp(7, forumModifiedDate);
188: statement.setTimestamp(8, forumLastPostDate);
189: statement.setInt(9, forumOrder);
190: statement.setInt(10, forumType);
191: statement.setInt(11, forumFormatOption);
192: statement.setInt(12, forumOption);
193: statement.setInt(13, forumStatus);
194: statement.setInt(14, forumModerationMode);
195: statement.setString(15, forumPassword);
196: statement.setInt(16, forumThreadCount);
197: statement.setInt(17, forumPostCount);
198:
199: if (statement.executeUpdate() != 1) {
200: throw new CreateException(
201: "Error adding a row into table 'forum'.");
202: }
203: } catch (SQLException sqle) {
204: throw new DatabaseException(
205: "Error executing SQL in forumDAOImplJDBC.create.");
206: } finally {
207: DBUtils.closeStatement(statement);
208: DBUtils.closeConnection(connection);
209: }
210: }
211:
212: public void createMultiple(Collection beans) {
213: // TODO Auto-generated method stub
214:
215: }
216:
217: }
|