KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > db > jdbc > PmAttachMessageDAOImplJDBC


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/jdbc/PmAttachMessageDAOImplJDBC.java,v 1.6 2006/04/14 17:05:26 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.6 $
5  * $Date: 2006/04/14 17:05:26 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.db.jdbc;
42
43 import java.sql.*;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Collection JavaDoc;
46
47 import com.mvnforum.db.*;
48 import net.myvietnam.mvncore.db.DBUtils;
49 import net.myvietnam.mvncore.exception.*;
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52
53 public class PmAttachMessageDAOImplJDBC implements PmAttachMessageDAO {
54
55     private static Log log = LogFactory.getLog(PmAttachMessageDAOImplJDBC.class);
56
57     // this variable will support caching if cache for this class is needed
58
private static boolean m_dirty = true;
59
60     public PmAttachMessageDAOImplJDBC() {
61     }
62
63     protected static boolean isDirty() {
64         return m_dirty;
65     }
66
67     protected static void setDirty(boolean dirty) {
68         m_dirty = dirty;
69     }
70
71     public void findByPrimaryKey(int messageID, int pmAttachID)
72         throws ObjectNotFoundException, DatabaseException {
73
74         Connection connection = null;
75         PreparedStatement statement = null;
76         ResultSet resultSet = null;
77         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
78         sql.append("SELECT MessageID, PmAttachID");
79         sql.append(" FROM " + TABLE_NAME);
80         sql.append(" WHERE MessageID = ? AND PmAttachID = ?");
81         try {
82             connection = DBUtils.getConnection();
83             statement = connection.prepareStatement(sql.toString());
84             statement.setInt(1, messageID);
85             statement.setInt(2, pmAttachID);
86             resultSet = statement.executeQuery();
87             if (!resultSet.next()) {
88                 throw new ObjectNotFoundException("Cannot find the primary key (" + messageID + ", " + pmAttachID + ") in table 'PmAttachMessage'.");
89             }
90         } catch(SQLException sqle) {
91             log.error("Sql Execution Error!", sqle);
92             throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.findByPrimaryKey.");
93         } finally {
94             DBUtils.closeResultSet(resultSet);
95             DBUtils.closeStatement(statement);
96             DBUtils.closeConnection(connection);
97         }
98     }
99
100     /*
101      * Included columns: MessageID, PmAttachID, RelationType, RelationOption, RelationStatus
102      * Excluded columns:
103      */

104     public void create(int messageID, int pmAttachID, int relationType,
105                        int relationOption, int relationStatus)
106         throws CreateException, DatabaseException, 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(messageID, pmAttachID);
114             //If so, then we have to throw an exception
115
throw new DuplicateKeyException("Primary key already exists. Cannot create new PmAttachMessage with the same [MessageID, PmAttachID] (" + messageID + ", " + pmAttachID + ").");
116         } catch(ObjectNotFoundException e) {
117             //Otherwise we can go ahead
118
}
119
120         try {
121             // @todo: modify the parameter list as needed
122
// You may have to regenerate this method if the needed columns dont have attribute 'include'
123
DAOFactory.getMessageDAO().findByPrimaryKey(messageID);
124         } catch(ObjectNotFoundException e) {
125             throw new ForeignKeyNotFoundException("Foreign key refers to table 'Message' does not exist. Cannot create new PmAttachMessage.");
126         }
127
128         try {
129             // @todo: modify the parameter list as needed
130
// You may have to regenerate this method if the needed columns dont have attribute 'include'
131
DAOFactory.getPmAttachmentDAO().findByPrimaryKey(pmAttachID);
132         } catch(ObjectNotFoundException e) {
133             throw new ForeignKeyNotFoundException("Foreign key refers to table 'PmAttachment' does not exist. Cannot create new PmAttachMessage.");
134         }
135
136         Connection connection = null;
137         PreparedStatement statement = null;
138         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
139         sql.append("INSERT INTO " + TABLE_NAME + " (MessageID, PmAttachID, RelationType, RelationOption, RelationStatus)");
140         sql.append(" VALUES (?, ?, ?, ?, ?)");
141         try {
142             connection = DBUtils.getConnection();
143             statement = connection.prepareStatement(sql.toString());
144
145             statement.setInt(1, messageID);
146             statement.setInt(2, pmAttachID);
147             statement.setInt(3, relationType);
148             statement.setInt(4, relationOption);
149             statement.setInt(5, relationStatus);
150
151             if (statement.executeUpdate() != 1) {
152                 throw new CreateException("Error adding a row into table 'PmAttachMessage'.");
153             }
154             m_dirty = true;
155         } catch(SQLException sqle) {
156             log.error("Sql Execution Error!", sqle);
157             throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.create.");
158         } finally {
159             DBUtils.closeStatement(statement);
160             DBUtils.closeConnection(connection);
161         }
162     }
163
164     public void delete(int messageID, int pmAttachID)
165         throws DatabaseException, ObjectNotFoundException {
166
167         Connection connection = null;
168         PreparedStatement statement = null;
169         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
170         sql.append("DELETE FROM " + TABLE_NAME);
171         sql.append(" WHERE MessageID = ? AND PmAttachID = ?");
172
173         try {
174             connection = DBUtils.getConnection();
175             statement = connection.prepareStatement(sql.toString());
176             statement.setInt(1, messageID);
177             statement.setInt(2, pmAttachID);
178             if (statement.executeUpdate() != 1) {
179                 throw new ObjectNotFoundException("Cannot delete a row in table PmAttachMessage where primary key = (" + messageID + ", " + pmAttachID + ").");
180             }
181             m_dirty = true;
182         } catch(SQLException sqle) {
183             log.error("Sql Execution Error!", sqle);
184             throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.delete.");
185         } finally {
186             DBUtils.closeStatement(statement);
187             DBUtils.closeConnection(connection);
188         }
189     }
190
191     public void delete_inMessage(int messageID)
192         throws DatabaseException {
193
194         Connection connection = null;
195         PreparedStatement statement = null;
196         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
197         sql.append("DELETE FROM " + TABLE_NAME);
198         sql.append(" WHERE MessageID = ? ");
199
200         try {
201             connection = DBUtils.getConnection();
202             statement = connection.prepareStatement(sql.toString());
203             statement.setInt(1, messageID);
204             statement.executeUpdate();
205             m_dirty = true;
206         } catch(SQLException sqle) {
207             log.error("Sql Execution Error!", sqle);
208             throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.delete_inMessage.");
209         } finally {
210             DBUtils.closeStatement(statement);
211             DBUtils.closeConnection(connection);
212         }
213     }
214
215     /*
216      * Included columns: MessageID, PmAttachID, RelationType, RelationOption, RelationStatus
217      * Excluded columns:
218      */

219     public Collection JavaDoc getBeans_inMessage(int messageID)
220         throws DatabaseException {
221
222         Connection connection = null;
223         PreparedStatement statement = null;
224         ResultSet resultSet = null;
225         Collection JavaDoc retValue = new ArrayList JavaDoc();
226         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
227         sql.append("SELECT MessageID, PmAttachID, RelationType, RelationOption, RelationStatus");
228         sql.append(" FROM " + TABLE_NAME);
229         sql.append(" WHERE MessageID = ?");
230         //sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
231
try {
232             connection = DBUtils.getConnection();
233             statement = connection.prepareStatement(sql.toString());
234             statement.setInt(1, messageID);
235             resultSet = statement.executeQuery();
236             while (resultSet.next()) {
237                 PmAttachMessageBean bean = new PmAttachMessageBean();
238                 bean.setMessageID(resultSet.getInt("MessageID"));
239                 bean.setPmAttachID(resultSet.getInt("PmAttachID"));
240                 bean.setRelationType(resultSet.getInt("RelationType"));
241                 bean.setRelationOption(resultSet.getInt("RelationOption"));
242                 bean.setRelationStatus(resultSet.getInt("RelationStatus"));
243                 retValue.add(bean);
244             }
245             return retValue;
246         } catch(SQLException sqle) {
247             log.error("Sql Execution Error!", sqle);
248             throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.getBeans_inMessage.");
249         } finally {
250             DBUtils.closeResultSet(resultSet);
251             DBUtils.closeStatement(statement);
252             DBUtils.closeConnection(connection);
253         }
254     }
255
256     /*
257      * Included columns: MessageID, PmAttachID, RelationType, RelationOption, RelationStatus
258      * Excluded columns:
259      */

260     public Collection JavaDoc getBeans_inPmAttach(int pmAttachID)
261         throws DatabaseException {
262
263         Connection connection = null;
264         PreparedStatement statement = null;
265         ResultSet resultSet = null;
266         Collection JavaDoc retValue = new ArrayList JavaDoc();
267         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
268         sql.append("SELECT MessageID, PmAttachID, RelationType, RelationOption, RelationStatus");
269         sql.append(" FROM " + TABLE_NAME);
270         sql.append(" WHERE PmAttachID = ?");
271         //sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
272
try {
273             connection = DBUtils.getConnection();
274             statement = connection.prepareStatement(sql.toString());
275             statement.setInt(1, pmAttachID);
276             resultSet = statement.executeQuery();
277             while (resultSet.next()) {
278                 PmAttachMessageBean bean = new PmAttachMessageBean();
279                 bean.setMessageID(resultSet.getInt("MessageID"));
280                 bean.setPmAttachID(resultSet.getInt("PmAttachID"));
281                 bean.setRelationType(resultSet.getInt("RelationType"));
282                 bean.setRelationOption(resultSet.getInt("RelationOption"));
283                 bean.setRelationStatus(resultSet.getInt("RelationStatus"));
284                 retValue.add(bean);
285             }
286             return retValue;
287         } catch(SQLException sqle) {
288             log.error("Sql Execution Error!", sqle);
289             throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.getBeans_inPmAttach.");
290         } finally {
291             DBUtils.closeResultSet(resultSet);
292             DBUtils.closeStatement(statement);
293             DBUtils.closeConnection(connection);
294         }
295     }
296
297     public int getNumberOfBeans_inMessage(int messageID)
298         throws AssertionException, DatabaseException {
299
300         Connection connection = null;
301         PreparedStatement statement = null;
302         ResultSet resultSet = null;
303         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
304         sql.append("SELECT Count(*)");
305         sql.append(" FROM " + TABLE_NAME);
306         sql.append(" WHERE MessageID = ?");
307         try {
308             connection = DBUtils.getConnection();
309             statement = connection.prepareStatement(sql.toString());
310             statement.setInt(1, messageID);
311             resultSet = statement.executeQuery();
312             if (!resultSet.next()) {
313                 throw new AssertionException("Assertion in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inMessage.");
314             }
315             return resultSet.getInt(1);
316         } catch(SQLException sqle) {
317             log.error("Sql Execution Error!", sqle);
318             throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inMessage.");
319         } finally {
320             DBUtils.closeResultSet(resultSet);
321             DBUtils.closeStatement(statement);
322             DBUtils.closeConnection(connection);
323         }
324     }
325
326     public int getNumberOfBeans_inPmAttach(int pmAttachID)
327         throws AssertionException, DatabaseException {
328
329         Connection connection = null;
330         PreparedStatement statement = null;
331         ResultSet resultSet = null;
332         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
333         sql.append("SELECT Count(*)");
334         sql.append(" FROM " + TABLE_NAME);
335         sql.append(" WHERE PmAttachID = ?");
336         try {
337             connection = DBUtils.getConnection();
338             statement = connection.prepareStatement(sql.toString());
339             statement.setInt(1, pmAttachID);
340             resultSet = statement.executeQuery();
341             if (!resultSet.next()) {
342                 throw new AssertionException("Assertion in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inPmAttach.");
343             }
344             return resultSet.getInt(1);
345         } catch(SQLException sqle) {
346             log.error("Sql Execution Error!", sqle);
347             throw new DatabaseException("Error executing SQL in PmAttachMessageDAOImplJDBC.getNumberOfBeans_inPmAttach.");
348         } finally {
349             DBUtils.closeResultSet(resultSet);
350             DBUtils.closeStatement(statement);
351             DBUtils.closeConnection(connection);
352         }
353     }
354
355 }// end of class PmAttachMessageDAOImplJDBC
356
Popular Tags