001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/jdbc/PmAttachMessageDAOImplJDBC.java,v 1.10 2007/10/09 11:09:20 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.10 $
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: import java.util.ArrayList;
045: import java.util.Collection;
046:
047: import com.mvnforum.db.*;
048: import net.myvietnam.mvncore.db.DBUtils;
049: import net.myvietnam.mvncore.exception.*;
050: import net.myvietnam.mvncore.util.AssertionUtil;
051:
052: import org.apache.commons.logging.Log;
053: import org.apache.commons.logging.LogFactory;
054:
055: public class PmAttachMessageDAOImplJDBC implements PmAttachMessageDAO {
056:
057: private static Log log = LogFactory
058: .getLog(PmAttachMessageDAOImplJDBC.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 PmAttachMessageDAOImplJDBC() {
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: public void findByPrimaryKey(int messageID, int pmAttachID)
075: throws ObjectNotFoundException, DatabaseException {
076:
077: Connection connection = null;
078: PreparedStatement statement = null;
079: ResultSet resultSet = null;
080: StringBuffer sql = new StringBuffer(512);
081: sql.append("SELECT MessageID, PmAttachID");
082: sql.append(" FROM " + TABLE_NAME);
083: sql.append(" WHERE MessageID = ? AND PmAttachID = ?");
084: try {
085: connection = DBUtils.getConnection();
086: statement = connection.prepareStatement(sql.toString());
087: statement.setInt(1, messageID);
088: statement.setInt(2, pmAttachID);
089: resultSet = statement.executeQuery();
090: if (!resultSet.next()) {
091: throw new ObjectNotFoundException(
092: "Cannot find the primary key (" + messageID
093: + ", " + pmAttachID
094: + ") in table 'PmAttachMessage'.");
095: }
096: } catch (SQLException sqle) {
097: log.error("Sql Execution Error!", sqle);
098: throw new DatabaseException(
099: "Error executing SQL in PmAttachMessageDAOImplJDBC.findByPrimaryKey.");
100: } finally {
101: DBUtils.closeResultSet(resultSet);
102: DBUtils.closeStatement(statement);
103: DBUtils.closeConnection(connection);
104: }
105: }
106:
107: /*
108: * Included columns: MessageID, PmAttachID, RelationType, RelationOption, RelationStatus
109: * Excluded columns:
110: */
111: public void create(int messageID, int pmAttachID, int relationType,
112: int relationOption, int relationStatus)
113: throws CreateException, DatabaseException,
114: DuplicateKeyException, ForeignKeyNotFoundException {
115:
116: // @todo: comment this try-catch block if the needed columns dont have attribute 'include'
117: // If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
118: // However, if primary key is a auto_increament column, then you can safely delete this block
119: try {
120: //Check if primary key already exists
121: findByPrimaryKey(messageID, pmAttachID);
122: //If so, then we have to throw an exception
123: throw new DuplicateKeyException(
124: "Primary key already exists. Cannot create new PmAttachMessage with the same [MessageID, PmAttachID] ("
125: + messageID + ", " + pmAttachID + ").");
126: } catch (ObjectNotFoundException e) {
127: //Otherwise we can go ahead
128: }
129:
130: try {
131: // @todo: modify the parameter list as needed
132: // You may have to regenerate this method if the needed columns dont have attribute 'include'
133: DAOFactory.getMessageDAO().findByPrimaryKey(messageID);
134: } catch (ObjectNotFoundException e) {
135: throw new ForeignKeyNotFoundException(
136: "Foreign key refers to table 'Message' does not exist. Cannot create new PmAttachMessage.");
137: }
138:
139: try {
140: // @todo: modify the parameter list as needed
141: // You may have to regenerate this method if the needed columns dont have attribute 'include'
142: DAOFactory.getPmAttachmentDAO()
143: .findByPrimaryKey(pmAttachID);
144: } catch (ObjectNotFoundException e) {
145: throw new ForeignKeyNotFoundException(
146: "Foreign key refers to table 'PmAttachment' does not exist. Cannot create new PmAttachMessage.");
147: }
148:
149: Connection connection = null;
150: PreparedStatement statement = null;
151: StringBuffer sql = new StringBuffer(512);
152: sql
153: .append("INSERT INTO "
154: + TABLE_NAME
155: + " (MessageID, PmAttachID, RelationType, RelationOption, RelationStatus)");
156: sql.append(" VALUES (?, ?, ?, ?, ?)");
157: try {
158: connection = DBUtils.getConnection();
159: statement = connection.prepareStatement(sql.toString());
160:
161: statement.setInt(1, messageID);
162: statement.setInt(2, pmAttachID);
163: statement.setInt(3, relationType);
164: statement.setInt(4, relationOption);
165: statement.setInt(5, relationStatus);
166:
167: if (statement.executeUpdate() != 1) {
168: throw new CreateException(
169: "Error adding a row into table 'PmAttachMessage'.");
170: }
171: m_dirty = true;
172: } catch (SQLException sqle) {
173: log.error("Sql Execution Error!", sqle);
174: throw new DatabaseException(
175: "Error executing SQL in PmAttachMessageDAOImplJDBC.create.");
176: } finally {
177: DBUtils.closeStatement(statement);
178: DBUtils.closeConnection(connection);
179: }
180: }
181:
182: public void delete(int messageID, int pmAttachID)
183: throws DatabaseException, ObjectNotFoundException {
184:
185: Connection connection = null;
186: PreparedStatement statement = null;
187: StringBuffer sql = new StringBuffer(512);
188: sql.append("DELETE FROM " + TABLE_NAME);
189: sql.append(" WHERE MessageID = ? AND PmAttachID = ?");
190:
191: try {
192: connection = DBUtils.getConnection();
193: statement = connection.prepareStatement(sql.toString());
194: statement.setInt(1, messageID);
195: statement.setInt(2, pmAttachID);
196: if (statement.executeUpdate() != 1) {
197: throw new ObjectNotFoundException(
198: "Cannot delete a row in table PmAttachMessage where primary key = ("
199: + messageID + ", " + pmAttachID + ").");
200: }
201: m_dirty = true;
202: } catch (SQLException sqle) {
203: log.error("Sql Execution Error!", sqle);
204: throw new DatabaseException(
205: "Error executing SQL in PmAttachMessageDAOImplJDBC.delete.");
206: } finally {
207: DBUtils.closeStatement(statement);
208: DBUtils.closeConnection(connection);
209: }
210: }
211:
212: public void delete_inMessage(int messageID)
213: throws DatabaseException {
214:
215: Connection connection = null;
216: PreparedStatement statement = null;
217: StringBuffer sql = new StringBuffer(512);
218: sql.append("DELETE FROM " + TABLE_NAME);
219: sql.append(" WHERE MessageID = ? ");
220:
221: try {
222: connection = DBUtils.getConnection();
223: statement = connection.prepareStatement(sql.toString());
224: statement.setInt(1, messageID);
225: statement.executeUpdate();
226: m_dirty = true;
227: } catch (SQLException sqle) {
228: log.error("Sql Execution Error!", sqle);
229: throw new DatabaseException(
230: "Error executing SQL in PmAttachMessageDAOImplJDBC.delete_inMessage.");
231: } finally {
232: DBUtils.closeStatement(statement);
233: DBUtils.closeConnection(connection);
234: }
235: }
236:
237: /*
238: * Included columns: MessageID, PmAttachID, RelationType, RelationOption, RelationStatus
239: * Excluded columns:
240: */
241: public Collection getBeans_inMessage(int messageID)
242: throws DatabaseException {
243:
244: Connection connection = null;
245: PreparedStatement statement = null;
246: ResultSet resultSet = null;
247: Collection retValue = new ArrayList();
248: StringBuffer sql = new StringBuffer(512);
249: sql
250: .append("SELECT MessageID, PmAttachID, RelationType, RelationOption, RelationStatus");
251: sql.append(" FROM " + TABLE_NAME);
252: sql.append(" WHERE MessageID = ?");
253: //sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
254: try {
255: connection = DBUtils.getConnection();
256: statement = connection.prepareStatement(sql.toString());
257: statement.setInt(1, messageID);
258: resultSet = statement.executeQuery();
259: while (resultSet.next()) {
260: PmAttachMessageBean bean = new PmAttachMessageBean();
261: bean.setMessageID(resultSet.getInt("MessageID"));
262: bean.setPmAttachID(resultSet.getInt("PmAttachID"));
263: bean.setRelationType(resultSet.getInt("RelationType"));
264: bean.setRelationOption(resultSet
265: .getInt("RelationOption"));
266: bean.setRelationStatus(resultSet
267: .getInt("RelationStatus"));
268: retValue.add(bean);
269: }
270: return retValue;
271: } catch (SQLException sqle) {
272: log.error("Sql Execution Error!", sqle);
273: throw new DatabaseException(
274: "Error executing SQL in PmAttachMessageDAOImplJDBC.getBeans_inMessage.");
275: } finally {
276: DBUtils.closeResultSet(resultSet);
277: DBUtils.closeStatement(statement);
278: DBUtils.closeConnection(connection);
279: }
280: }
281:
282: /*
283: * Included columns: MessageID, PmAttachID, RelationType, RelationOption, RelationStatus
284: * Excluded columns:
285: */
286: public Collection getBeans_inPmAttach(int pmAttachID)
287: throws DatabaseException {
288:
289: Connection connection = null;
290: PreparedStatement statement = null;
291: ResultSet resultSet = null;
292: Collection retValue = new ArrayList();
293: StringBuffer sql = new StringBuffer(512);
294: sql
295: .append("SELECT MessageID, PmAttachID, RelationType, RelationOption, RelationStatus");
296: sql.append(" FROM " + TABLE_NAME);
297: sql.append(" WHERE PmAttachID = ?");
298: //sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
299: try {
300: connection = DBUtils.getConnection();
301: statement = connection.prepareStatement(sql.toString());
302: statement.setInt(1, pmAttachID);
303: resultSet = statement.executeQuery();
304: while (resultSet.next()) {
305: PmAttachMessageBean bean = new PmAttachMessageBean();
306: bean.setMessageID(resultSet.getInt("MessageID"));
307: bean.setPmAttachID(resultSet.getInt("PmAttachID"));
308: bean.setRelationType(resultSet.getInt("RelationType"));
309: bean.setRelationOption(resultSet
310: .getInt("RelationOption"));
311: bean.setRelationStatus(resultSet
312: .getInt("RelationStatus"));
313: retValue.add(bean);
314: }
315: return retValue;
316: } catch (SQLException sqle) {
317: log.error("Sql Execution Error!", sqle);
318: throw new DatabaseException(
319: "Error executing SQL in PmAttachMessageDAOImplJDBC.getBeans_inPmAttach.");
320: } finally {
321: DBUtils.closeResultSet(resultSet);
322: DBUtils.closeStatement(statement);
323: DBUtils.closeConnection(connection);
324: }
325: }
326:
327: public int getNumberOfBeans_inMessage(int messageID)
328: throws DatabaseException {
329:
330: Connection connection = null;
331: PreparedStatement statement = null;
332: ResultSet resultSet = null;
333: StringBuffer sql = new StringBuffer(512);
334: sql.append("SELECT Count(*)");
335: sql.append(" FROM " + TABLE_NAME);
336: sql.append(" WHERE MessageID = ?");
337: try {
338: connection = DBUtils.getConnection();
339: statement = connection.prepareStatement(sql.toString());
340: statement.setInt(1, messageID);
341: resultSet = statement.executeQuery();
342: AssertionUtil
343: .doAssert(resultSet.next(),
344: "Assertion in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inMessage.");
345: return resultSet.getInt(1);
346: } catch (SQLException sqle) {
347: log.error("Sql Execution Error!", sqle);
348: throw new DatabaseException(
349: "Error executing SQL in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inMessage.");
350: } finally {
351: DBUtils.closeResultSet(resultSet);
352: DBUtils.closeStatement(statement);
353: DBUtils.closeConnection(connection);
354: }
355: }
356:
357: public int getNumberOfBeans_inPmAttach(int pmAttachID)
358: throws DatabaseException {
359:
360: Connection connection = null;
361: PreparedStatement statement = null;
362: ResultSet resultSet = null;
363: StringBuffer sql = new StringBuffer(512);
364: sql.append("SELECT Count(*)");
365: sql.append(" FROM " + TABLE_NAME);
366: sql.append(" WHERE PmAttachID = ?");
367: try {
368: connection = DBUtils.getConnection();
369: statement = connection.prepareStatement(sql.toString());
370: statement.setInt(1, pmAttachID);
371: resultSet = statement.executeQuery();
372: AssertionUtil
373: .doAssert(resultSet.next(),
374: "Assertion in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inPmAttach.");
375: return resultSet.getInt(1);
376: } catch (SQLException sqle) {
377: log.error("Sql Execution Error!", sqle);
378: throw new DatabaseException(
379: "Error executing SQL in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inPmAttach.");
380: } finally {
381: DBUtils.closeResultSet(resultSet);
382: DBUtils.closeStatement(statement);
383: DBUtils.closeConnection(connection);
384: }
385: }
386:
387: }// end of class PmAttachMessageDAOImplJDBC
|