001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/jdbc/MessageStatisticsDAOImplJDBC.java,v 1.14 2007/10/09 11:09:20 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.14 $
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:
045: import com.mvnforum.db.DAOFactory;
046: import com.mvnforum.db.MessageStatisticsDAO;
047: import net.myvietnam.mvncore.db.DBUtils;
048: import net.myvietnam.mvncore.exception.*;
049: import net.myvietnam.mvncore.util.AssertionUtil;
050:
051: import org.apache.commons.logging.Log;
052: import org.apache.commons.logging.LogFactory;
053:
054: public class MessageStatisticsDAOImplJDBC implements
055: MessageStatisticsDAO {
056:
057: private static Log log = LogFactory
058: .getLog(MessageStatisticsDAOImplJDBC.class);
059:
060: // this variable will support caching if cache for this class is needed
061: private static boolean m_dirty = true;
062:
063: public MessageStatisticsDAOImplJDBC() {
064: }
065:
066: protected static boolean isDirty() {
067: return m_dirty;
068: }
069:
070: protected static void setDirty(boolean dirty) {
071: m_dirty = dirty;
072: }
073:
074: /*
075: * Included columns: FromID, ToID, MessageCreationDate, MessageAttachCount, MessageType,
076: * MessageOption, MessageStatus
077: * Excluded columns:
078: */
079: public void create(int fromID, int toID,
080: Timestamp messageCreationDate, int messageAttachCount,
081: int messageType, int messageOption, int messageStatus)
082: throws CreateException, DatabaseException,
083: ForeignKeyNotFoundException {
084:
085: try {
086: // @todo: modify the parameter list as needed
087: // You may have to regenerate this method if the needed columns dont have attribute 'include'
088: DAOFactory.getMemberDAO().findByPrimaryKey(fromID);
089: } catch (ObjectNotFoundException e) {
090: throw new ForeignKeyNotFoundException(
091: "Foreign key [FromID] refers to table 'Member' does not exist. Cannot create new MessageStatistics.");
092: }
093:
094: try {
095: // @todo: modify the parameter list as needed
096: // You may have to regenerate this method if the needed columns dont have attribute 'include'
097: DAOFactory.getMemberDAO().findByPrimaryKey(toID);
098: } catch (ObjectNotFoundException e) {
099: throw new ForeignKeyNotFoundException(
100: "Foreign key [ToID] refers to table 'Member' does not exist. Cannot create new MessageStatistics.");
101: }
102:
103: Connection connection = null;
104: PreparedStatement statement = null;
105: StringBuffer sql = new StringBuffer(512);
106: sql
107: .append("INSERT INTO "
108: + TABLE_NAME
109: + " (FromID, ToID, MessageCreationDate, MessageAttachCount, MessageType, MessageOption, MessageStatus)");
110: sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?)");
111: try {
112: connection = DBUtils.getConnection();
113: statement = connection.prepareStatement(sql.toString());
114:
115: statement.setInt(1, fromID);
116: statement.setInt(2, toID);
117: statement.setTimestamp(3, messageCreationDate);
118: statement.setInt(4, messageAttachCount);
119: statement.setInt(5, messageType);
120: statement.setInt(6, messageOption);
121: statement.setInt(7, messageStatus);
122:
123: if (statement.executeUpdate() != 1) {
124: throw new CreateException(
125: "Error adding a row into table 'MessageStatistics'.");
126: }
127: m_dirty = true;
128: } catch (SQLException sqle) {
129: log.error("Sql Execution Error!", sqle);
130: throw new DatabaseException(
131: "Error executing SQL in MessageStatisticsDAOImplJDBC.create.");
132: } finally {
133: DBUtils.closeStatement(statement);
134: DBUtils.closeConnection(connection);
135: }
136: }
137:
138: public void delete_inMember(int memberID) throws DatabaseException {
139:
140: Connection connection = null;
141: PreparedStatement statement = null;
142: StringBuffer sql = new StringBuffer(512);
143: sql.append("DELETE FROM " + TABLE_NAME);
144: sql.append(" WHERE (FromID = ?) OR (ToID = ?) ");
145:
146: try {
147: connection = DBUtils.getConnection();
148: statement = connection.prepareStatement(sql.toString());
149: statement.setInt(1, memberID);
150: statement.setInt(2, memberID);
151: statement.executeUpdate();
152: m_dirty = true;
153: } catch (SQLException sqle) {
154: log.error("Sql Execution Error!", sqle);
155: throw new DatabaseException(
156: "Error executing SQL in MessageStatisticsDAOImplJDBC.delete_inMember.");
157: } finally {
158: DBUtils.closeStatement(statement);
159: DBUtils.closeConnection(connection);
160: }
161: }
162:
163: public int getNumberOfBeans_inFromID(int fromID)
164: throws DatabaseException {
165:
166: Connection connection = null;
167: PreparedStatement statement = null;
168: ResultSet resultSet = null;
169: StringBuffer sql = new StringBuffer(512);
170: sql.append("SELECT Count(*)");
171: sql.append(" FROM " + TABLE_NAME);
172: sql.append(" WHERE FromID = ?");
173: try {
174: connection = DBUtils.getConnection();
175: statement = connection.prepareStatement(sql.toString());
176: statement.setInt(1, fromID);
177: resultSet = statement.executeQuery();
178: AssertionUtil
179: .doAssert(resultSet.next(),
180: "Assertion in MessageStatisticsDAOImplJDBC.getNumberOfBeans_inFromID.");
181: return resultSet.getInt(1);
182: } catch (SQLException sqle) {
183: log.error("Sql Execution Error!", sqle);
184: throw new DatabaseException(
185: "Error executing SQL in MessageStatisticsDAOImplJDBC.getNumberOfBeans_inFromID.");
186: } finally {
187: DBUtils.closeResultSet(resultSet);
188: DBUtils.closeStatement(statement);
189: DBUtils.closeConnection(connection);
190: }
191: }
192:
193: public int getNumberOfBeans_inToID(int toID)
194: throws DatabaseException {
195:
196: Connection connection = null;
197: PreparedStatement statement = null;
198: ResultSet resultSet = null;
199: StringBuffer sql = new StringBuffer(512);
200: sql.append("SELECT Count(*)");
201: sql.append(" FROM " + TABLE_NAME);
202: sql.append(" WHERE ToID = ?");
203: try {
204: connection = DBUtils.getConnection();
205: statement = connection.prepareStatement(sql.toString());
206: statement.setInt(1, toID);
207: resultSet = statement.executeQuery();
208: AssertionUtil
209: .doAssert(resultSet.next(),
210: "Assertion in MessageStatisticsDAOImplJDBC.getNumberOfBeans_inToID.");
211: return resultSet.getInt(1);
212: } catch (SQLException sqle) {
213: log.error("Sql Execution Error!", sqle);
214: throw new DatabaseException(
215: "Error executing SQL in MessageStatisticsDAOImplJDBC.getNumberOfBeans_inToID.");
216: } finally {
217: DBUtils.closeResultSet(resultSet);
218: DBUtils.closeStatement(statement);
219: DBUtils.closeConnection(connection);
220: }
221: }
222:
223: public int getNumberOfBeans_inFromID_supportTimestamp(int fromID,
224: Timestamp from, Timestamp to) throws DatabaseException {
225:
226: Connection connection = null;
227: PreparedStatement statement = null;
228: ResultSet resultSet = null;
229: StringBuffer sql = new StringBuffer(512);
230: sql.append("SELECT Count(*)");
231: sql.append(" FROM " + TABLE_NAME);
232: sql.append(" WHERE FromID = ?");
233: sql.append(" AND MessageCreationDate >= ?");
234: sql.append(" AND MessageCreationDate <= ?");
235:
236: try {
237: connection = DBUtils.getConnection();
238: statement = connection.prepareStatement(sql.toString());
239: statement.setInt(1, fromID);
240: statement.setTimestamp(2, from);
241: statement.setTimestamp(3, to);
242: resultSet = statement.executeQuery();
243: AssertionUtil
244: .doAssert(
245: resultSet.next(),
246: "Assertion in MessageStatisticsDAOImplJDBC.getNumberOfBeans_inFromID_supportTimestamp.");
247: return resultSet.getInt(1);
248: } catch (SQLException sqle) {
249: log.error("Sql Execution Error!", sqle);
250: throw new DatabaseException(
251: "Error executing SQL in MessageStatisticsDAOImplJDBC.getNumberOfBeans_inFromID_supportTimestamp.");
252: } finally {
253: DBUtils.closeResultSet(resultSet);
254: DBUtils.closeStatement(statement);
255: DBUtils.closeConnection(connection);
256: }
257: }
258:
259: public int getNumberOfBeans_inToID_supportTimestamp(int toID,
260: Timestamp from, Timestamp to) throws DatabaseException {
261:
262: Connection connection = null;
263: PreparedStatement statement = null;
264: ResultSet resultSet = null;
265: StringBuffer sql = new StringBuffer(512);
266: sql.append("SELECT Count(*)");
267: sql.append(" FROM " + TABLE_NAME);
268: sql.append(" WHERE ToID = ?");
269: sql.append(" AND MessageCreationDate >= ?");
270: sql.append(" AND MessageCreationDate <= ?");
271:
272: try {
273: connection = DBUtils.getConnection();
274: statement = connection.prepareStatement(sql.toString());
275: statement.setInt(1, toID);
276: statement.setTimestamp(2, from);
277: statement.setTimestamp(3, to);
278: resultSet = statement.executeQuery();
279: AssertionUtil
280: .doAssert(
281: resultSet.next(),
282: "Assertion in MessageStatisticsDAOImplJDBC.getNumberOfBeans_inToID_supportTimestamp.");
283: return resultSet.getInt(1);
284: } catch (SQLException sqle) {
285: log.error("Sql Execution Error!", sqle);
286: throw new DatabaseException(
287: "Error executing SQL in MessageStatisticsDAOImplJDBC.getNumberOfBeans_inToID_supportTimestamp.");
288: } finally {
289: DBUtils.closeResultSet(resultSet);
290: DBUtils.closeStatement(statement);
291: DBUtils.closeConnection(connection);
292: }
293: }
294:
295: }// end of class MessageStaticDAOImplJDBC
|