KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/db/jdbc/WatchDAOImplJDBC.java,v 1.13 2006/04/14 17:05:27 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.13 $
5  * $Date: 2006/04/14 17:05:27 $
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 org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49 import com.mvnforum.MVNForumConstant;
50 import com.mvnforum.db.*;
51 import net.myvietnam.mvncore.db.DBUtils;
52 import net.myvietnam.mvncore.exception.*;
53
54 public class WatchDAOImplJDBC implements WatchDAO {
55
56     private static Log log = LogFactory.getLog(WatchDAOImplJDBC.class);
57
58     // this variable will support caching if cache for this class is needed
59
private static boolean m_dirty = true;
60
61     public WatchDAOImplJDBC() {
62     }
63
64     protected static boolean isDirty() {
65         return m_dirty;
66     }
67
68     protected static void setDirty(boolean dirty) {
69         m_dirty = dirty;
70     }
71
72     public void findByPrimaryKey(int watchID)
73         throws ObjectNotFoundException, DatabaseException {
74
75         Connection connection = null;
76         PreparedStatement statement = null;
77         ResultSet resultSet = null;
78         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
79         sql.append("SELECT WatchID");
80         sql.append(" FROM " + TABLE_NAME);
81         sql.append(" WHERE WatchID = ?");
82         try {
83             connection = DBUtils.getConnection();
84             statement = connection.prepareStatement(sql.toString());
85             statement.setInt(1, watchID);
86             resultSet = statement.executeQuery();
87             if (!resultSet.next()) {
88                 throw new ObjectNotFoundException("Cannot find the primary key (" + watchID + ") in table 'Watch'.");
89             }
90         } catch(SQLException sqle) {
91             log.error("Sql Execution Error!", sqle);
92             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.findByPrimaryKey.");
93         } finally {
94             DBUtils.closeResultSet(resultSet);
95             DBUtils.closeStatement(statement);
96             DBUtils.closeConnection(connection);
97         }
98     }
99
100     public void findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID(int memberID, int categoryID, int forumID, int threadID)
101         throws ObjectNotFoundException, DatabaseException {
102
103         Connection connection = null;
104         PreparedStatement statement = null;
105         ResultSet resultSet = null;
106         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
107         sql.append("SELECT MemberID, CategoryID, ForumID, ThreadID");
108         sql.append(" FROM " + TABLE_NAME);
109         sql.append(" WHERE MemberID = ? AND CategoryID = ? AND ForumID = ? AND ThreadID = ?");
110         try {
111             connection = DBUtils.getConnection();
112             statement = connection.prepareStatement(sql.toString());
113             statement.setInt(1, memberID);
114             statement.setInt(2, categoryID);
115             statement.setInt(3, forumID);
116             statement.setInt(4, threadID);
117             resultSet = statement.executeQuery();
118             if (!resultSet.next()) {
119                 throw new ObjectNotFoundException("Cannot find the alternate key [MemberID, CategoryID, ForumID, ThreadID] (" + memberID + ", " + categoryID + ", " + forumID + ", " + threadID + ") in table 'Watch'.");
120             }
121         } catch(SQLException sqle) {
122             log.error("Sql Execution Error!", sqle);
123             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID.");
124         } finally {
125             DBUtils.closeResultSet(resultSet);
126             DBUtils.closeStatement(statement);
127             DBUtils.closeConnection(connection);
128         }
129     }
130
131     /*
132      * Included columns: MemberID, CategoryID, ForumID, ThreadID, WatchType,
133      * WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate
134      * Excluded columns: WatchID
135      */

136     public void create(int memberID, int categoryID, int forumID,
137                        int threadID, int watchType, int watchOption,
138                        int watchStatus, Timestamp watchCreationDate, Timestamp watchLastSentDate,
139                        Timestamp watchEndDate)
140         throws IllegalArgumentException JavaDoc, CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException {
141
142         if ((memberID == 0) || (memberID == MVNForumConstant.MEMBER_ID_OF_GUEST)) {
143             throw new IllegalArgumentException JavaDoc("Cannot add a new watch for Guest (id = " + memberID + ")");
144         }
145         int notZeroCount = 0;
146         if (categoryID != 0) {
147             notZeroCount++;
148         }
149         if (forumID != 0) {
150             notZeroCount++;
151         }
152         if (threadID != 0) {
153             notZeroCount++;
154         }
155         if (notZeroCount > 1) {
156             throw new IllegalArgumentException JavaDoc("Cannot add watch with more than 1 element.");
157         }
158
159         // @todo: Comment this try-catch block if the needed columns dont have attribute 'include'
160
// If this is the case, then it is highly recommended that you regenerate this method with the attribute 'include' turned on
161
try {
162             //Check if alternate key already exists
163
findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID(memberID, categoryID, forumID, threadID);
164             //If so, then we have to throw an exception
165
throw new DuplicateKeyException("Alternate key already exists. Cannot create new Watch with the same [MemberID, CategoryID, ForumID, ThreadID] (" + memberID + ", " + categoryID + ", " + forumID + ", " + threadID + ").");
166         } catch(ObjectNotFoundException e) {
167             //Otherwise we can go ahead
168
}
169
170         try {
171             // @todo: modify the parameter list as needed
172
// You may have to regenerate this method if the needed columns dont have attribute 'include'
173
DAOFactory.getMemberDAO().findByPrimaryKey(memberID);
174         } catch(ObjectNotFoundException e) {
175             throw new ForeignKeyNotFoundException("Foreign key refers to table 'Member' does not exist. Cannot create new Watch.");
176         }
177
178         try {
179             // @todo: modify the parameter list as needed
180
// You may have to regenerate this method if the needed columns dont have attribute 'include'
181
if (categoryID != 0) {
182                 DAOFactory.getCategoryDAO().findByPrimaryKey(categoryID);
183             }
184         } catch(ObjectNotFoundException e) {
185             throw new ForeignKeyNotFoundException("Foreign key refers to table 'Category' does not exist. Cannot create new Watch.");
186         }
187
188         try {
189             // @todo: modify the parameter list as needed
190
// You may have to regenerate this method if the needed columns dont have attribute 'include'
191
if (forumID != 0) {
192                 DAOFactory.getForumDAO().findByPrimaryKey(forumID);
193             }
194         } catch(ObjectNotFoundException e) {
195             throw new ForeignKeyNotFoundException("Foreign key refers to table 'Forum' does not exist. Cannot create new Watch.");
196         }
197
198         try {
199             // @todo: modify the parameter list as needed
200
// You may have to regenerate this method if the needed columns dont have attribute 'include'
201
if (threadID != 0) {
202                 DAOFactory.getThreadDAO().findByPrimaryKey(threadID);
203             }
204         } catch(ObjectNotFoundException e) {
205             throw new ForeignKeyNotFoundException("Foreign key refers to table 'Thread' does not exist. Cannot create new Watch.");
206         }
207
208         Connection connection = null;
209         PreparedStatement statement = null;
210         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
211         sql.append("INSERT INTO " + TABLE_NAME + " (MemberID, CategoryID, ForumID, ThreadID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate)");
212         sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
213         try {
214             connection = DBUtils.getConnection();
215             statement = connection.prepareStatement(sql.toString());
216
217             statement.setInt(1, memberID);
218             statement.setInt(2, categoryID);
219             statement.setInt(3, forumID);
220             statement.setInt(4, threadID);
221             statement.setInt(5, watchType);
222             statement.setInt(6, watchOption);
223             statement.setInt(7, watchStatus);
224             statement.setTimestamp(8, watchCreationDate);
225             statement.setTimestamp(9, watchLastSentDate);
226             statement.setTimestamp(10, watchEndDate);
227
228             if (statement.executeUpdate() != 1) {
229                 throw new CreateException("Error adding a row into table 'Watch'.");
230             }
231             m_dirty = true;
232         } catch(SQLException sqle) {
233             log.error("Sql Execution Error!", sqle);
234             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.create.");
235         } finally {
236             DBUtils.closeStatement(statement);
237             DBUtils.closeConnection(connection);
238         }
239     }
240
241     public void delete(int watchID)
242         throws DatabaseException, ObjectNotFoundException {
243
244         Connection connection = null;
245         PreparedStatement statement = null;
246         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
247         sql.append("DELETE FROM " + TABLE_NAME);
248         sql.append(" WHERE WatchID = ?");
249
250         try {
251             connection = DBUtils.getConnection();
252             statement = connection.prepareStatement(sql.toString());
253             statement.setInt(1, watchID);
254             if (statement.executeUpdate() != 1) {
255                 throw new ObjectNotFoundException("Cannot delete a row in table Watch where primary key = (" + watchID + ").");
256             }
257             m_dirty = true;
258         } catch(SQLException sqle) {
259             log.error("Sql Execution Error!", sqle);
260             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.delete.");
261         } finally {
262             DBUtils.closeStatement(statement);
263             DBUtils.closeConnection(connection);
264         }
265     }
266
267     public void delete_inMember(int memberID)
268         throws DatabaseException {
269
270         Connection connection = null;
271         PreparedStatement statement = null;
272         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
273         sql.append("DELETE FROM " + TABLE_NAME);
274         sql.append(" WHERE MemberID = ?");
275
276         try {
277             connection = DBUtils.getConnection();
278             statement = connection.prepareStatement(sql.toString());
279             statement.setInt(1, memberID);
280             statement.executeUpdate();
281             m_dirty = true;
282         } catch(SQLException sqle) {
283             log.error("Sql Execution Error!", sqle);
284             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.delete_inMember.");
285         } finally {
286             DBUtils.closeStatement(statement);
287             DBUtils.closeConnection(connection);
288         }
289     }
290
291     public void delete_inCategory(int categoryID)
292         throws DatabaseException {
293
294         Connection connection = null;
295         PreparedStatement statement = null;
296         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
297         sql.append("DELETE FROM " + TABLE_NAME);
298         sql.append(" WHERE CategoryID = ?");
299
300         try {
301             connection = DBUtils.getConnection();
302             statement = connection.prepareStatement(sql.toString());
303             statement.setInt(1, categoryID);
304             statement.executeUpdate();
305             m_dirty = true;
306         } catch(SQLException sqle) {
307             log.error("Sql Execution Error!", sqle);
308             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.delete_inCategory.");
309         } finally {
310             DBUtils.closeStatement(statement);
311             DBUtils.closeConnection(connection);
312         }
313     }
314
315     public void delete_inForum(int forumID)
316         throws DatabaseException {
317
318         Connection connection = null;
319         PreparedStatement statement = null;
320         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
321         sql.append("DELETE FROM " + TABLE_NAME);
322         sql.append(" WHERE ForumID = ?");
323
324         try {
325             connection = DBUtils.getConnection();
326             statement = connection.prepareStatement(sql.toString());
327             statement.setInt(1, forumID);
328             statement.executeUpdate();
329             m_dirty = true;
330         } catch(SQLException sqle) {
331             log.error("Sql Execution Error!", sqle);
332             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.delete_inForum.");
333         } finally {
334             DBUtils.closeStatement(statement);
335             DBUtils.closeConnection(connection);
336         }
337     }
338
339     public void delete_inThread(int threadID)
340         throws DatabaseException {
341
342         Connection connection = null;
343         PreparedStatement statement = null;
344         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
345         sql.append("DELETE FROM " + TABLE_NAME);
346         sql.append(" WHERE ThreadID = ?");
347
348         try {
349             connection = DBUtils.getConnection();
350             statement = connection.prepareStatement(sql.toString());
351             statement.setInt(1, threadID);
352             statement.executeUpdate();
353             m_dirty = true;
354         } catch(SQLException sqle) {
355             log.error("Sql Execution Error!", sqle);
356             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.delete_inThread.");
357         } finally {
358             DBUtils.closeStatement(statement);
359             DBUtils.closeConnection(connection);
360         }
361     }
362
363     /*
364      * Included columns: WatchLastSentDate
365      * Excluded columns: WatchID, MemberID, CategoryID, ForumID, ThreadID,
366      * WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchEndDate
367      */

368     public void updateLastSentDate(int watchID, // primary key
369
Timestamp watchLastSentDate)
370         throws ObjectNotFoundException, DatabaseException {
371
372         Connection connection = null;
373         PreparedStatement statement = null;
374         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
375         sql.append("UPDATE " + TABLE_NAME + " SET WatchLastSentDate = ?");
376         sql.append(" WHERE WatchID = ?");
377         try {
378             connection = DBUtils.getConnection();
379             statement = connection.prepareStatement(sql.toString());
380
381             // // column(s) to update
382
statement.setTimestamp(1, watchLastSentDate);
383
384             // primary key column(s)
385
statement.setInt(2, watchID);
386
387             if (statement.executeUpdate() != 1) {
388                 throw new ObjectNotFoundException("Cannot update table Watch where primary key = (" + watchID + ").");
389             }
390             m_dirty = true;
391         } catch(SQLException sqle) {
392             log.error("Sql Execution Error!", sqle);
393             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.updateLastSentDate.");
394         } finally {
395             DBUtils.closeStatement(statement);
396             DBUtils.closeConnection(connection);
397         }
398     }
399
400     /*
401      * Included columns: MemberID, CategoryID, ForumID, ThreadID, WatchType,
402      * WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate
403      * Excluded columns: WatchID
404      */

405     public WatchBean getWatch(int watchID)
406         throws ObjectNotFoundException, DatabaseException {
407
408         Connection connection = null;
409         PreparedStatement statement = null;
410         ResultSet resultSet = null;
411         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
412         sql.append("SELECT MemberID, CategoryID, ForumID, ThreadID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate");
413         sql.append(" FROM " + TABLE_NAME);
414         sql.append(" WHERE WatchID = ?");
415         try {
416             connection = DBUtils.getConnection();
417             statement = connection.prepareStatement(sql.toString());
418             statement.setInt(1, watchID);
419             resultSet = statement.executeQuery();
420             if(!resultSet.next()) {
421                 throw new ObjectNotFoundException("Cannot find the row in table Watch where primary key = (" + watchID + ").");
422             }
423
424             WatchBean bean = new WatchBean();
425             // @todo: uncomment the following line(s) as needed
426
bean.setWatchID(watchID);
427             bean.setMemberID(resultSet.getInt("MemberID"));
428             bean.setCategoryID(resultSet.getInt("CategoryID"));
429             bean.setForumID(resultSet.getInt("ForumID"));
430             bean.setThreadID(resultSet.getInt("ThreadID"));
431             bean.setWatchType(resultSet.getInt("WatchType"));
432             bean.setWatchOption(resultSet.getInt("WatchOption"));
433             bean.setWatchStatus(resultSet.getInt("WatchStatus"));
434             bean.setWatchCreationDate(resultSet.getTimestamp("WatchCreationDate"));
435             bean.setWatchLastSentDate(resultSet.getTimestamp("WatchLastSentDate"));
436             bean.setWatchEndDate(resultSet.getTimestamp("WatchEndDate"));
437             return bean;
438         } catch(SQLException sqle) {
439             log.error("Sql Execution Error!", sqle);
440             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.getWatch(pk).");
441         } finally {
442             DBUtils.closeResultSet(resultSet);
443             DBUtils.closeStatement(statement);
444             DBUtils.closeConnection(connection);
445         }
446     }
447
448     /*
449      * Included columns: WatchID, WatchType, WatchOption, WatchStatus, WatchCreationDate,
450      * WatchLastSentDate, WatchEndDate
451      * Excluded columns: MemberID, CategoryID, ForumID, ThreadID
452      */

453     public WatchBean getWatch_byAlternateKey_MemberID_CategoryID_ForumID_ThreadID(int memberID, int categoryID, int forumID, int threadID)
454         throws ObjectNotFoundException, DatabaseException {
455
456         Connection connection = null;
457         PreparedStatement statement = null;
458         ResultSet resultSet = null;
459         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
460         sql.append("SELECT WatchID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate");
461         sql.append(" FROM " + TABLE_NAME);
462         sql.append(" WHERE MemberID = ? AND CategoryID = ? AND ForumID = ? AND ThreadID = ?");
463         try {
464             connection = DBUtils.getConnection();
465             statement = connection.prepareStatement(sql.toString());
466             statement.setInt(1, memberID);
467             statement.setInt(2, categoryID);
468             statement.setInt(3, forumID);
469             statement.setInt(4, threadID);
470             resultSet = statement.executeQuery();
471             if(!resultSet.next()) {
472                 throw new ObjectNotFoundException("Cannot find the row in table Watch where alternate key [MemberID, CategoryID, ForumID, ThreadID] = (" + memberID + ", " + categoryID + ", " + forumID + ", " + threadID + ").");
473             }
474
475             WatchBean bean = new WatchBean();
476             // @todo: uncomment the following line(s) as needed
477
bean.setMemberID(memberID);
478             bean.setCategoryID(categoryID);
479             bean.setForumID(forumID);
480             bean.setThreadID(threadID);
481             bean.setWatchID(resultSet.getInt("WatchID"));
482             bean.setWatchType(resultSet.getInt("WatchType"));
483             bean.setWatchOption(resultSet.getInt("WatchOption"));
484             bean.setWatchStatus(resultSet.getInt("WatchStatus"));
485             bean.setWatchCreationDate(resultSet.getTimestamp("WatchCreationDate"));
486             bean.setWatchLastSentDate(resultSet.getTimestamp("WatchLastSentDate"));
487             bean.setWatchEndDate(resultSet.getTimestamp("WatchEndDate"));
488             return bean;
489         } catch(SQLException sqle) {
490             log.error("Sql Execution Error!", sqle);
491             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.getWatch_byAlternateKey_MemberID_CategoryID_ForumID_ThreadID(ak).");
492         } finally {
493             DBUtils.closeResultSet(resultSet);
494             DBUtils.closeStatement(statement);
495             DBUtils.closeConnection(connection);
496         }
497     }
498
499     /*
500      * Included columns: WatchID, MemberID, CategoryID, ForumID, ThreadID,
501      * WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate,
502      * WatchEndDate
503      * Excluded columns:
504      */

505     public Collection JavaDoc getWatches()
506         throws DatabaseException {
507
508         Connection connection = null;
509         PreparedStatement statement = null;
510         ResultSet resultSet = null;
511         Collection JavaDoc retValue = new ArrayList JavaDoc();
512         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
513         sql.append("SELECT WatchID, MemberID, CategoryID, ForumID, ThreadID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate");
514         sql.append(" FROM " + TABLE_NAME);
515         //sql.append(" WHERE "); // @todo: uncomment as needed
516
//sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
517
try {
518             connection = DBUtils.getConnection();
519             statement = connection.prepareStatement(sql.toString());
520             resultSet = statement.executeQuery();
521             while (resultSet.next()) {
522                 WatchBean bean = new WatchBean();
523                 bean.setWatchID(resultSet.getInt("WatchID"));
524                 bean.setMemberID(resultSet.getInt("MemberID"));
525                 bean.setCategoryID(resultSet.getInt("CategoryID"));
526                 bean.setForumID(resultSet.getInt("ForumID"));
527                 bean.setThreadID(resultSet.getInt("ThreadID"));
528                 bean.setWatchType(resultSet.getInt("WatchType"));
529                 bean.setWatchOption(resultSet.getInt("WatchOption"));
530                 bean.setWatchStatus(resultSet.getInt("WatchStatus"));
531                 bean.setWatchCreationDate(resultSet.getTimestamp("WatchCreationDate"));
532                 bean.setWatchLastSentDate(resultSet.getTimestamp("WatchLastSentDate"));
533                 bean.setWatchEndDate(resultSet.getTimestamp("WatchEndDate"));
534                 retValue.add(bean);
535             }
536             return retValue;
537         } catch(SQLException sqle) {
538             log.error("Sql Execution Error!", sqle);
539             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.getWatchs.");
540         } finally {
541             DBUtils.closeResultSet(resultSet);
542             DBUtils.closeStatement(statement);
543             DBUtils.closeConnection(connection);
544         }
545     }
546
547     public int getNumberOfWatches()
548         throws AssertionException, DatabaseException {
549
550         Connection connection = null;
551         PreparedStatement statement = null;
552         ResultSet resultSet = null;
553         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
554         sql.append("SELECT Count(*)");
555         sql.append(" FROM " + TABLE_NAME);
556         //sql.append(" WHERE "); // @todo: uncomment as needed
557
try {
558             connection = DBUtils.getConnection();
559             statement = connection.prepareStatement(sql.toString());
560             resultSet = statement.executeQuery();
561             if (!resultSet.next()) {
562                 throw new AssertionException("Assertion in WatchDAOImplJDBC.getNumberOfWatches.");
563             }
564             return resultSet.getInt(1);
565         } catch(SQLException sqle) {
566             log.error("Sql Execution Error!", sqle);
567             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.getNumberOfWatches.");
568         } finally {
569             DBUtils.closeResultSet(resultSet);
570             DBUtils.closeStatement(statement);
571             DBUtils.closeConnection(connection);
572         }
573     }
574
575     public int getNumberOfWatches_forMember(int memberID)
576         throws AssertionException, DatabaseException {
577
578         Connection connection = null;
579         PreparedStatement statement = null;
580         ResultSet resultSet = null;
581         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
582         sql.append("SELECT Count(*)");
583         sql.append(" FROM " + TABLE_NAME);
584         sql.append(" WHERE MemberID = ?");
585         try {
586             connection = DBUtils.getConnection();
587             statement = connection.prepareStatement(sql.toString());
588             statement.setInt(1, memberID);
589             resultSet = statement.executeQuery();
590             if (!resultSet.next()) {
591                 throw new AssertionException("Assertion in WatchDAOImplJDBC.getNumberOfWatches_forMember.");
592             }
593             return resultSet.getInt(1);
594         } catch(SQLException sqle) {
595             log.error("Sql Execution Error!", sqle);
596             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.getNumberOfWatches_forMember.");
597         } finally {
598             DBUtils.closeResultSet(resultSet);
599             DBUtils.closeStatement(statement);
600             DBUtils.closeConnection(connection);
601         }
602     }
603
604 /***************************************************************************
605  * Customized methods come below
606  ***************************************************************************/

607
608     /*
609      * Included columns: MemberID, WatchLastSentDate
610      * Excluded columns: WatchID, CategoryID, ForumID, ThreadID, WatchType,
611      * WatchOption, WatchStatus, WatchCreationDate, WatchEndDate
612      */

613     public Collection JavaDoc getMemberBeans()
614         throws DatabaseException {
615
616         Connection connection = null;
617         PreparedStatement statement = null;
618         ResultSet resultSet = null;
619         Collection JavaDoc retValue = new ArrayList JavaDoc();
620         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
621         sql.append("SELECT DISTINCT MemberID, MIN(WatchLastSentDate) AS lastsent");// postgreSQL need AS
622
sql.append(" FROM " + TABLE_NAME);
623         //sql.append(" WHERE "); // @todo: uncomment as needed
624
//sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
625
sql.append(" GROUP BY MemberID ");
626         try {
627             connection = DBUtils.getConnection();
628             statement = connection.prepareStatement(sql.toString());
629             resultSet = statement.executeQuery();
630             while (resultSet.next()) {
631                 WatchBean bean = new WatchBean();
632                 bean.setMemberID(resultSet.getInt("MemberID"));
633                 bean.setWatchLastSentDate(resultSet.getTimestamp("lastsent"));
634                 retValue.add(bean);
635             }
636             return retValue;
637         } catch(SQLException sqle) {
638             log.error("Sql Execution Error!", sqle);
639             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.getMemberBeans.");
640         } finally {
641             DBUtils.closeResultSet(resultSet);
642             DBUtils.closeStatement(statement);
643             DBUtils.closeConnection(connection);
644         }
645     }
646
647     /*
648      * Included columns: WatchID, MemberID, CategoryID, ForumID, ThreadID,
649      * WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate,
650      * WatchEndDate
651      * Excluded columns:
652      */

653     public Collection JavaDoc getWatches_forMember(int memberID)
654         throws DatabaseException {
655
656         Connection connection = null;
657         PreparedStatement statement = null;
658         ResultSet resultSet = null;
659         Collection JavaDoc retValue = new ArrayList JavaDoc();
660         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
661         sql.append("SELECT WatchID, MemberID, CategoryID, ForumID, ThreadID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate");
662         sql.append(" FROM " + TABLE_NAME);
663         sql.append(" WHERE MemberID = ? ");
664         //sql.append(" ORDER BY ColumnName ASC|DESC "); // @todo: uncomment as needed
665
try {
666             connection = DBUtils.getConnection();
667             statement = connection.prepareStatement(sql.toString());
668             statement.setInt(1, memberID);
669             resultSet = statement.executeQuery();
670             while (resultSet.next()) {
671                 WatchBean bean = new WatchBean();
672                 bean.setWatchID(resultSet.getInt("WatchID"));
673                 bean.setMemberID(resultSet.getInt("MemberID"));
674                 bean.setCategoryID(resultSet.getInt("CategoryID"));
675                 bean.setForumID(resultSet.getInt("ForumID"));
676                 bean.setThreadID(resultSet.getInt("ThreadID"));
677                 bean.setWatchType(resultSet.getInt("WatchType"));
678                 bean.setWatchOption(resultSet.getInt("WatchOption"));
679                 bean.setWatchStatus(resultSet.getInt("WatchStatus"));
680                 bean.setWatchCreationDate(resultSet.getTimestamp("WatchCreationDate"));
681                 bean.setWatchLastSentDate(resultSet.getTimestamp("WatchLastSentDate"));
682                 bean.setWatchEndDate(resultSet.getTimestamp("WatchEndDate"));
683                 retValue.add(bean);
684             }
685             return retValue;
686         } catch(SQLException sqle) {
687             log.error("Sql Execution Error!", sqle);
688             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.getWatches_forMember.");
689         } finally {
690             DBUtils.closeResultSet(resultSet);
691             DBUtils.closeStatement(statement);
692             DBUtils.closeConnection(connection);
693         }
694     }
695
696     /*
697      * Included columns: WatchLastSentDate
698      * Excluded columns: WatchID, MemberID, CategoryID, ForumID, ThreadID,
699      * WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchEndDate
700      */

701     public void updateLastSentDate_forMember(int memberID,
702                         Timestamp watchLastSentDate)
703         throws ObjectNotFoundException, DatabaseException {
704
705         Connection connection = null;
706         PreparedStatement statement = null;
707         StringBuffer JavaDoc sql = new StringBuffer JavaDoc(512);
708         sql.append("UPDATE " + TABLE_NAME + " SET WatchLastSentDate = ?");
709         sql.append(" WHERE MemberID = ?");
710         try {
711             connection = DBUtils.getConnection();
712             statement = connection.prepareStatement(sql.toString());
713
714             // // column(s) to update
715
statement.setTimestamp(1, watchLastSentDate);
716
717             // primary key column(s)
718
statement.setInt(2, memberID);
719
720             if (statement.executeUpdate() < 1) {
721                 throw new ObjectNotFoundException("Cannot update table Watch where primary key = (" + memberID + ").");
722             }
723             m_dirty = true;
724         } catch(SQLException sqle) {
725             log.error("Sql Execution Error!", sqle);
726             throw new DatabaseException("Error executing SQL in WatchDAOImplJDBC.updateLastSentDate_forMember.");
727         } finally {
728             DBUtils.closeStatement(statement);
729             DBUtils.closeConnection(connection);
730         }
731     }
732
733 }// end of class WatchDAOImplJDBC
734
Popular Tags