001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/phpbb2mvnforum/db/jdbc/PhpbbPostDAOImpl.java,v 1.6 2007/12/18 04:58:44 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.6 $
005: * $Date: 2007/12/18 04:58:44 $
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.util.ArrayList;
047: import java.util.Collection;
048:
049: import net.myvietnam.mvncore.exception.DatabaseException;
050: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
051:
052: import org.mvnforum.phpbb2mvnforum.db.PhpbbPost;
053: import org.mvnforum.phpbb2mvnforum.db.PhpbbPostDAO;
054: import org.mvnforum.util.DBUtils;
055:
056: public class PhpbbPostDAOImpl implements PhpbbPostDAO {
057:
058: /*
059: * Included columns: post_id, topic_id, forum_id, poster_id, post_time,
060: * poster_ip, post_username, enable_bbcode, enable_html, enable_smilies,
061: * enable_sig, post_edit_time, post_edit_count
062: * Excluded columns:
063: */
064: public int getPosterIDFromPostID(int post_id)
065: throws ObjectNotFoundException, DatabaseException {
066:
067: Connection connection = null;
068: PreparedStatement statement = null;
069: ResultSet resultSet = null;
070: StringBuffer sql = new StringBuffer(512);
071: sql.append("SELECT poster_id");
072: sql.append(" FROM " + TABLE_NAME);
073: sql.append(" WHERE post_id = ?");
074: int posterID = 0;
075: try {
076: connection = DBUtils.getPhpbbConnection();
077: statement = connection.prepareStatement(sql.toString());
078: statement.setInt(1, post_id);
079: resultSet = statement.executeQuery();
080: if (!resultSet.next()) {
081: throw new ObjectNotFoundException(
082: "Cannot find the row in table phpbb_posts where primary key = ("
083: + post_id + ").");
084: }
085:
086: posterID = resultSet.getInt("poster_id");
087: } catch (SQLException sqle) {
088: throw new DatabaseException(
089: "Error executing SQL in phpbb_postsDAOImplJDBC.getBean(pk).");
090: } finally {
091: DBUtils.closeResultSet(resultSet);
092: DBUtils.closeStatement(statement);
093: DBUtils.closeConnection(connection);
094: }
095: return posterID;
096: }
097:
098: /*
099: * Included columns: post_id, topic_id, forum_id, poster_id, post_time,
100: * poster_ip, post_username, enable_bbcode, enable_html, enable_smilies,
101: * enable_sig, post_edit_time, post_edit_count
102: * Excluded columns:
103: */
104: public Collection getBeans() throws DatabaseException {
105:
106: Connection connection = null;
107: PreparedStatement statement = null;
108: ResultSet resultSet = null;
109: Collection retValue = new ArrayList();
110: StringBuffer sql = new StringBuffer(512);
111: sql
112: .append("SELECT post_id, topic_id, forum_id, poster_id, post_time, poster_ip, post_username, enable_bbcode, enable_html, enable_smilies, enable_sig, post_edit_time, post_edit_count");
113: sql.append(" FROM " + TABLE_NAME);
114: //sql.append(" WHERE "); // @todo: uncomment as needed
115: //sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
116: try {
117: connection = DBUtils.getPhpbbConnection();
118: statement = connection.prepareStatement(sql.toString());
119: resultSet = statement.executeQuery();
120: while (resultSet.next()) {
121: PhpbbPost bean = new PhpbbPost();
122: bean.setpost_id(resultSet.getInt("post_id"));
123: bean.settopic_id(resultSet.getInt("topic_id"));
124: bean.setforum_id(resultSet.getInt("forum_id"));
125: bean.setposter_id(resultSet.getInt("poster_id"));
126: bean.setpost_time(resultSet.getInt("post_time"));
127: bean.setposter_ip(resultSet.getString("poster_ip"));
128: bean.setpost_username(resultSet
129: .getString("post_username"));
130: bean
131: .setenable_bbcode(resultSet
132: .getInt("enable_bbcode"));
133: bean.setenable_html(resultSet.getInt("enable_html"));
134: bean.setenable_smilies(resultSet
135: .getInt("enable_smilies"));
136: bean.setenable_sig(resultSet.getInt("enable_sig"));
137: bean.setpost_edit_time(resultSet
138: .getInt("post_edit_time"));
139: bean.setpost_edit_count(resultSet
140: .getInt("post_edit_count"));
141: retValue.add(bean);
142: }
143: return retValue;
144: } catch (SQLException sqle) {
145: throw new DatabaseException(
146: "Error executing SQL in phpbb_postsDAOImplJDBC.getBeans.");
147: } finally {
148: DBUtils.closeResultSet(resultSet);
149: DBUtils.closeStatement(statement);
150: DBUtils.closeConnection(connection);
151: }
152: }
153:
154: /*
155: * Included columns: post_id, topic_id, forum_id, poster_id, post_time,
156: * poster_ip, post_username, enable_bbcode, enable_html, enable_smilies,
157: * enable_sig, post_edit_time, post_edit_count
158: * Excluded columns:
159: */
160: public Collection getPostIDsFromTopicID(int topicID)
161: throws DatabaseException {
162:
163: Connection connection = null;
164: PreparedStatement statement = null;
165: ResultSet resultSet = null;
166: Collection retValue = new ArrayList();
167: StringBuffer sql = new StringBuffer(512);
168: sql.append("SELECT post_id");
169: sql.append(" FROM " + TABLE_NAME);
170: sql.append(" WHERE topic_id = ?");
171: try {
172: connection = DBUtils.getPhpbbConnection();
173: statement = connection.prepareStatement(sql.toString());
174: statement.setInt(1, topicID);
175: resultSet = statement.executeQuery();
176:
177: while (resultSet.next()) {
178:
179: PhpbbPost bean = new PhpbbPost();
180: bean.setpost_id(resultSet.getInt("post_id"));
181: retValue.add(bean);
182: }
183: return retValue;
184: } catch (SQLException sqle) {
185: throw new DatabaseException(
186: "Error executing SQL in phpbb_postsDAOImplJDBC.getBean(pk).");
187: } finally {
188: DBUtils.closeResultSet(resultSet);
189: DBUtils.closeStatement(statement);
190: DBUtils.closeConnection(connection);
191: }
192: }
193:
194: public long getPostTimeFromPostID(int post_id)
195: throws ObjectNotFoundException, DatabaseException {
196:
197: Connection connection = null;
198: PreparedStatement statement = null;
199: ResultSet resultSet = null;
200: StringBuffer sql = new StringBuffer(512);
201: sql.append("SELECT post_time");
202: sql.append(" FROM " + TABLE_NAME);
203: sql.append(" WHERE post_id = ?");
204: try {
205: connection = DBUtils.getPhpbbConnection();
206: statement = connection.prepareStatement(sql.toString());
207: statement.setInt(1, post_id);
208: resultSet = statement.executeQuery();
209: if (!resultSet.next()) {
210: throw new ObjectNotFoundException(
211: "Cannot find the row in table phpbb_posts where primary key = ("
212: + post_id + ").");
213: }
214: return resultSet.getLong("post_time");
215: } catch (SQLException sqle) {
216: throw new DatabaseException(
217: "Error executing SQL in phpbb_postsDAOImplJDBC.getBean(pk).");
218: } finally {
219: DBUtils.closeResultSet(resultSet);
220: DBUtils.closeStatement(statement);
221: DBUtils.closeConnection(connection);
222: }
223:
224: }
225:
226: public Collection getBeansByThreadID(int threadID)
227: throws DatabaseException {
228:
229: Connection connection = null;
230: PreparedStatement statement = null;
231: ResultSet resultSet = null;
232: Collection retValue = new ArrayList();
233: StringBuffer sql = new StringBuffer(512);
234: sql
235: .append("SELECT post_id, topic_id, forum_id, poster_id, post_time, poster_ip, post_username, enable_bbcode, enable_html, enable_smilies, enable_sig, post_edit_time, post_edit_count");
236: sql.append(" FROM " + TABLE_NAME);
237: sql.append(" WHERE topic_id = ?");
238: //sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
239: try {
240: connection = DBUtils.getPhpbbConnection();
241: statement = connection.prepareStatement(sql.toString());
242: statement.setInt(1, threadID);
243: resultSet = statement.executeQuery();
244: while (resultSet.next()) {
245: PhpbbPost bean = new PhpbbPost();
246: bean.setpost_id(resultSet.getInt("post_id"));
247: bean.settopic_id(resultSet.getInt("topic_id"));
248: bean.setforum_id(resultSet.getInt("forum_id"));
249: bean.setposter_id(resultSet.getInt("poster_id"));
250: bean.setpost_time(resultSet.getInt("post_time"));
251: bean.setposter_ip(resultSet.getString("poster_ip"));
252: bean.setpost_username(resultSet
253: .getString("post_username"));
254: bean
255: .setenable_bbcode(resultSet
256: .getInt("enable_bbcode"));
257: bean.setenable_html(resultSet.getInt("enable_html"));
258: bean.setenable_smilies(resultSet
259: .getInt("enable_smilies"));
260: bean.setenable_sig(resultSet.getInt("enable_sig"));
261: bean.setpost_edit_time(resultSet
262: .getInt("post_edit_time"));
263: bean.setpost_edit_count(resultSet
264: .getInt("post_edit_count"));
265: retValue.add(bean);
266: }
267: return retValue;
268: } catch (SQLException sqle) {
269: throw new DatabaseException(
270: "Error executing SQL in phpbb_postsDAOImplJDBC.getBeans.");
271: } finally {
272: DBUtils.closeResultSet(resultSet);
273: DBUtils.closeStatement(statement);
274: DBUtils.closeConnection(connection);
275: }
276: }
277:
278: }
|