001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/phpbb2mvnforum/db/jdbc/ThreadDAOImplJDBC.java,v 1.5 2007/01/15 10:27:10 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.5 $
005: * $Date: 2007/01/15 10:27:10 $
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 net.myvietnam.mvncore.exception.CreateException;
050: import net.myvietnam.mvncore.exception.DatabaseException;
051: import net.myvietnam.mvncore.exception.DuplicateKeyException;
052: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
053:
054: import org.mvnforum.phpbb2mvnforum.db.ThreadDAO;
055: import org.mvnforum.util.DBUtils;
056:
057: public class ThreadDAOImplJDBC implements ThreadDAO {
058:
059: public void findByPrimaryKey(int threadID)
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 ThreadID");
067: sql.append(" FROM " + TABLE_NAME);
068: sql.append(" WHERE ThreadID = ?");
069: try {
070: connection = DBUtils.getMvnConnection();
071: statement = connection.prepareStatement(sql.toString());
072: statement.setInt(1, threadID);
073: resultSet = statement.executeQuery();
074: if (!resultSet.next()) {
075: throw new ObjectNotFoundException(
076: "Cannot find the primary key (" + threadID
077: + ") in table 'thread'.");
078: }
079: } catch (SQLException sqle) {
080: throw new DatabaseException(
081: "Error executing SQL in threadDAOImplJDBC.findByPrimaryKey.");
082: } finally {
083: DBUtils.closeResultSet(resultSet);
084: DBUtils.closeStatement(statement);
085: DBUtils.closeConnection(connection);
086: }
087: }
088:
089: /*
090: * Included columns: ThreadID, ForumID, MemberName, LastPostMemberName, ThreadTopic,
091: * ThreadBody, ThreadVoteCount, ThreadVoteTotalStars, ThreadCreationDate, ThreadLastPostDate,
092: * ThreadType, ThreadOption, ThreadStatus, ThreadHasPoll, ThreadViewCount,
093: * ThreadReplyCount, ThreadIcon, ThreadDuration, ThreadAttachCount
094: * Excluded columns:
095: */
096: public int createThread(int threadID, int forumID,
097: String memberName, String lastPostMemberName,
098: String threadTopic, String threadBody, int threadVoteCount,
099: int threadVoteTotalStars, Timestamp threadCreationDate,
100: Timestamp threadLastPostDate, int threadType,
101: int threadOption, int threadStatus, int threadHasPoll,
102: int threadViewCount, int threadReplyCount,
103: String threadIcon, int threadDuration, int threadAttachCount)
104: throws CreateException, DatabaseException,
105: DuplicateKeyException {
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(threadID);
113: //If so, then we have to throw an exception
114: throw new DuplicateKeyException(
115: "Primary key already exists. Cannot create new thread with the same [ThreadID] ("
116: + threadID + ").");
117: } catch (ObjectNotFoundException e) {
118: //Otherwise we can go ahead
119: }
120:
121: Connection connection = null;
122: PreparedStatement statement = null;
123: StringBuffer sql = new StringBuffer(512);
124: sql
125: .append("INSERT INTO "
126: + TABLE_NAME
127: + " (ThreadID, ForumID, MemberName, LastPostMemberName, ThreadTopic, ThreadBody, ThreadVoteCount, ThreadVoteTotalStars, ThreadCreationDate, ThreadLastPostDate, ThreadType, ThreadOption, ThreadStatus, ThreadHasPoll, ThreadViewCount, ThreadReplyCount, ThreadIcon, ThreadDuration, ThreadAttachCount)");
128: sql
129: .append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
130: try {
131: connection = DBUtils.getMvnConnection();
132: statement = connection.prepareStatement(sql.toString());
133:
134: statement.setInt(1, threadID);
135: statement.setInt(2, forumID);
136: statement.setString(3, memberName);
137: statement.setString(4, lastPostMemberName);
138: statement.setString(5, threadTopic);
139: statement.setString(6, threadBody);
140: statement.setInt(7, threadVoteCount);
141: statement.setInt(8, threadVoteTotalStars);
142: statement.setTimestamp(9, threadCreationDate);
143: statement.setTimestamp(10, threadLastPostDate);
144: statement.setInt(11, threadType);
145: statement.setInt(12, threadOption);
146: statement.setInt(13, threadStatus);
147: statement.setInt(14, threadHasPoll);
148: statement.setInt(15, threadViewCount);
149: statement.setInt(16, threadReplyCount);
150: statement.setString(17, threadIcon);
151: statement.setInt(18, threadDuration);
152: statement.setInt(19, threadAttachCount);
153:
154: if (statement.executeUpdate() != 1) {
155: throw new CreateException(
156: "Error adding a row into table 'thread'.");
157: }
158: return 0;
159: } catch (SQLException sqle) {
160: throw new DatabaseException(
161: "Error executing SQL in threadDAOImplJDBC.create.");
162: } finally {
163: DBUtils.closeStatement(statement);
164: DBUtils.closeConnection(connection);
165: }
166: }
167:
168: public void createMultiple(Collection beans) {
169: // TODO Auto-generated method stub
170:
171: }
172:
173: }
|