001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/phpbb2mvnforum/db/jdbc/PostDAOImplJDBC.java,v 1.8 2007/01/15 10:27:11 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.8 $
005: * $Date: 2007/01/15 10:27:11 $
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.ForeignKeyNotFoundException;
053: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
054:
055: import org.mvnforum.phpbb2mvnforum.db.PostDAO;
056: import org.mvnforum.util.DBUtils;
057:
058: public class PostDAOImplJDBC implements PostDAO {
059:
060: public void findByPrimaryKey(int postID)
061: throws ObjectNotFoundException, DatabaseException {
062:
063: Connection connection = null;
064: PreparedStatement statement = null;
065: ResultSet resultSet = null;
066: StringBuffer sql = new StringBuffer(512);
067: sql.append("SELECT PostID");
068: sql.append(" FROM " + TABLE_NAME);
069: sql.append(" WHERE PostID = ?");
070: try {
071: connection = DBUtils.getMvnConnection();
072: statement = connection.prepareStatement(sql.toString());
073: statement.setInt(1, postID);
074: resultSet = statement.executeQuery();
075: if (!resultSet.next()) {
076: throw new ObjectNotFoundException(
077: "Cannot find the primary key (" + postID
078: + ") in table 'post'.");
079: }
080: } catch (SQLException sqle) {
081: throw new DatabaseException(
082: "Error executing SQL in postDAOImplJDBC.findByPrimaryKey.");
083: } finally {
084: DBUtils.closeResultSet(resultSet);
085: DBUtils.closeStatement(statement);
086: DBUtils.closeConnection(connection);
087: }
088: }
089:
090: /*
091: * Included columns: PostID, ParentPostID, ForumID, ThreadID, MemberID,
092: * MemberName, LastEditMemberName, PostTopic, PostBody, PostCreationDate,
093: * PostLastEditDate, PostCreationIP, PostLastEditIP, PostEditCount, PostFormatOption,
094: * PostOption, PostStatus, PostIcon, PostAttachCount
095: * Excluded columns:
096: */
097: public void create(int postID, int parentPostID, int forumID,
098: int threadID, int memberID, String memberName,
099: String lastEditMemberName, String postTopic,
100: String postBody, Timestamp postCreationDate,
101: Timestamp postLastEditDate, String postCreationIP,
102: String postLastEditIP, int postEditCount,
103: int postFormatOption, int postOption, int postStatus,
104: String postIcon, int postAttachCount)
105: throws CreateException, DatabaseException,
106: DuplicateKeyException, ForeignKeyNotFoundException {
107:
108: // @todo: comment this try-catch block if the needed columns dont have attribute 'include'
109: // If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
110: // However, if primary key is a auto_increament column, then you can safely delete this block
111: try {
112: //Check if primary key already exists
113: findByPrimaryKey(postID);
114: //If so, then we have to throw an exception
115: throw new DuplicateKeyException(
116: "Primary key already exists. Cannot create new post with the same [PostID] ("
117: + postID + ").");
118: } catch (ObjectNotFoundException e) {
119: //Otherwise we can go ahead
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: + " (PostID, ParentPostID, ForumID, ThreadID, MemberID, MemberName, LastEditMemberName, PostTopic, PostBody, PostCreationDate, PostLastEditDate, PostCreationIP, PostLastEditIP, PostEditCount, PostFormatOption, PostOption, PostStatus, PostIcon, PostAttachCount)");
128: sql
129: .append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
130: try {
131: connection = DBUtils.getMvnConnection();
132: statement = connection.prepareStatement(sql.toString());
133:
134: statement.setInt(1, postID);
135: statement.setInt(2, parentPostID);
136: statement.setInt(3, forumID);
137: statement.setInt(4, threadID);
138: statement.setInt(5, memberID);
139: statement.setString(6, memberName);
140: statement.setString(7, lastEditMemberName);
141: statement.setString(8, postTopic);
142: statement.setString(9, postBody);
143: statement.setTimestamp(10, postCreationDate);
144: statement.setTimestamp(11, postLastEditDate);
145: statement.setString(12, postCreationIP);
146: statement.setString(13, postLastEditIP);
147: statement.setInt(14, postEditCount);
148: statement.setInt(15, postFormatOption);
149: statement.setInt(16, postOption);
150: statement.setInt(17, postStatus);
151: statement.setString(18, postIcon);
152: statement.setInt(19, postAttachCount);
153:
154: if (statement.executeUpdate() != 1) {
155: throw new CreateException(
156: "Error adding a row into table 'post'.");
157: }
158: } catch (SQLException sqle) {
159: throw new DatabaseException(
160: "Error executing SQL in postDAOImplJDBC.create.");
161: } finally {
162: DBUtils.closeStatement(statement);
163: DBUtils.closeConnection(connection);
164: }
165: }
166:
167: public void createMultiple(Collection beans) {
168: // TODO Auto-generated method stub
169:
170: }
171: }
|