KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > impl > DbForumThread


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.impl;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.nemesis.forum.Forum;
38 import org.nemesis.forum.ForumThread;
39 import org.nemesis.forum.Message;
40 import org.nemesis.forum.TreeWalker;
41 import org.nemesis.forum.proxy.MessageProxy;
42 import org.nemesis.forum.event.ForumEvent;
43 import org.nemesis.forum.event.ForumListener;
44 import org.nemesis.forum.exception.ForumMessageNotFoundException;
45 import org.nemesis.forum.exception.ForumThreadNotFoundException;
46 import org.nemesis.forum.exception.UnauthorizedException;
47 import org.nemesis.forum.util.cache.CacheSizes;
48 import org.nemesis.forum.util.cache.Cacheable;
49 import org.nemesis.forum.util.jdbc.DbConnectionManager;
50 /**
51  * Database implementation of the ForumThread interface.
52  *
53  * @see ForumThread
54  */

55 public class DbForumThread implements ForumThread, Cacheable {
56     static protected Log log = LogFactory.getLog(DbForumThread.class);
57     /** DATABASE QUERIES **/
58     private static final String JavaDoc MESSAGE_COUNT = "SELECT count(*) FROM yazdMessage WHERE threadID=?";
59     private static final String JavaDoc ADD_MESSAGE = "INSERT INTO yazdMessageTree(parentID,childID) VALUES(?,?)";
60     private static final String JavaDoc MOVE_MESSAGE = "UPDATE yazdMessageTree SET parentID=? WHERE childID=?";
61     private static final String JavaDoc CHANGE_MESSAGE_THREAD = "UPDATE yazdMessage SET threadID=? WHERE messageID=?";
62     private static final String JavaDoc UPDATE_THREAD_MODIFIED_DATE = "UPDATE yazdThread SET modifiedDate=? WHERE threadID=?";
63     private static final String JavaDoc DELETE_MESSAGE1 = "DELETE FROM yazdMessageTree WHERE childID=?";
64     private static final String JavaDoc DELETE_MESSAGE2 = "DELETE FROM yazdMessage WHERE messageID=?";
65     private static final String JavaDoc DELETE_MESSAGE_PROPERTIES = "DELETE FROM yazdMessageProp WHERE messageID=?";
66     
67     private static final String JavaDoc LOAD_THREAD = "SELECT rootMessageID, creationDate, modifiedDate,approved FROM yazdThread WHERE threadID=?";
68     private static final String JavaDoc INSERT_THREAD = "INSERT INTO yazdThread(threadID,forumID, rootMessageID,creationDate, modifiedDate,approved) VALUES(?,?,?,?,?,?)";
69     private static final String JavaDoc SAVE_THREAD = "UPDATE yazdThread SET rootMessageID=?, creationDate=?, modifiedDate=?, approved=? WHERE threadID=?";
70     
71     private static final String JavaDoc APPROVED_MESSAGE_COUNT = "SELECT count(*) FROM yazdMessage WHERE threadID=? AND approved=?";
72     
73     
74     private int id = -1;
75     //Temporary object reference for when inserting new record.
76
private Message rootMessage;
77     private int rootMessageID;
78     private java.util.Date JavaDoc creationDate;
79     private java.util.Date JavaDoc modifiedDate;
80     private boolean approved;
81
82     /**
83      * Indicates if the object is ready to be saved or not. An object is not
84      * ready to be saved if it has just been created and has not yet been added
85      * to its container. For example, a message added to a thread, etc.
86      */

87     private boolean isReadyToSave = false;
88
89     /**
90      * The forum allows us access to the message filters.
91      */

92     private DbForum forum;
93
94     /**
95      * The factory provides services such as db connections and logging.
96      */

97     private DbForumFactory factory;
98
99     /**
100      * Creates a new DbForumThread. The supplied message object is used to
101      * derive the name of the thread (subject of message), as well as the
102      * creation date and modified date of thread.
103      *
104      * @param rootMessage the root message of the thread.
105      */

106     protected DbForumThread(Message rootMessage, boolean approved, DbForum forum, DbForumFactory factory)
107         throws UnauthorizedException {
108         this.id = DbSequenceManager.nextID("ForumThread");
109         this.forum = forum;
110         this.factory = factory;
111         this.rootMessage = rootMessage;
112         this.rootMessageID = rootMessage.getID();
113         //Set the creation and modified dates to be the same as those of
114
//root message.
115
long rootMessageTime = rootMessage.getCreationDate().getTime();
116         this.creationDate = new java.util.Date JavaDoc(rootMessageTime);
117         this.modifiedDate = new java.util.Date JavaDoc(rootMessageTime);
118         this.approved = approved;
119     }
120
121     /**
122      * Loads a DbForumThread from the database based on its id.
123      *
124      * @param id in unique id of the ForumThread to load.
125      * @param forum the Forum that the thread belongs to.
126      * @param factory a ForumFactory to use for loading.
127      */

128     protected DbForumThread(int id, DbForum forum, DbForumFactory factory) throws ForumThreadNotFoundException {
129         this.id = id;
130         this.forum = forum;
131         this.factory = factory;
132         loadFromDb();
133         isReadyToSave = true;
134     }
135
136     //FROM THE FORUMMESSAGE INTERFACE//
137
public boolean isApproved(){
138             return approved;
139     }
140     public void setApproved(boolean approved) throws UnauthorizedException{
141         this.approved = approved;
142         //Only save to the db if the object is read
143
if (!isReadyToSave) {
144             return;
145         }
146         saveToDb();
147     }
148         
149     public int getID() {
150         return id;
151     }
152
153     public String JavaDoc getName() {
154         return getRootMessage().getSubject();
155     }
156
157     public java.util.Date JavaDoc getCreationDate() {
158         return creationDate;
159     }
160
161     public void setCreationDate(java.util.Date JavaDoc creationDate) throws UnauthorizedException {
162         this.creationDate = creationDate;
163         //Only save to the db if the object is ready
164
if (!isReadyToSave) {
165             return;
166         }
167         saveToDb();
168     }
169
170     public java.util.Date JavaDoc getModifiedDate() {
171         return modifiedDate;
172     }
173
174     public void setModifiedDate(java.util.Date JavaDoc modifiedDate) throws UnauthorizedException {
175         this.modifiedDate = modifiedDate;
176         //Only save to the db if the object is ready
177
if (!isReadyToSave) {
178             return;
179         }
180         saveToDb();
181     }
182
183     public Forum getForum() {
184         return forum;
185     }
186
187     public Message getMessage(int messageID) throws ForumMessageNotFoundException {
188         Message message = factory.getMessage(messageID);
189
190         //Apply filters to message.
191
message = forum.applyFilters(message);
192         return message;
193     }
194
195     public Message getRootMessage() {
196         try {
197             return getMessage(rootMessageID);
198         } catch (ForumMessageNotFoundException e) {
199             log.error("Could not load root message with id " + rootMessageID);
200             return null;
201         }
202         /*DbForumMessage message = (DbForumMessage)factory.cacheManager.get(
203                 DbCacheManager.MESSAGE_CACHE,
204                 new Integer(rootMessageID)
205         );
206         if (message == null) {
207             //Load and add to cache
208             try {
209                 message = new DbForumMessage(rootMessageID, factory);
210                 factory.cacheManager.add(DbCacheManager.MESSAGE_CACHE, new Integer(rootMessageID), message);
211             }
212             catch (ForumMessageNotFoundException e) {
213                 log("Could not load root message with id " + rootMessageID);
214                 
215             }
216         }
217         return message;
218         */

219     }
220
221     public int getMessageCount() {
222         int messageCount = 0;
223         Connection JavaDoc con = null;
224         PreparedStatement JavaDoc pstmt = null;
225         try {
226             con = DbConnectionManager.getConnection();
227             pstmt = con.prepareStatement(MESSAGE_COUNT);
228             pstmt.setInt(1, id);
229             ResultSet JavaDoc rs = pstmt.executeQuery();
230             rs.next();
231             messageCount = rs.getInt(1);
232         } catch (SQLException JavaDoc sqle) {
233             log.error("DbForumThread:getMessageCount() failed: " , sqle);
234         } finally {
235             try {
236                 pstmt.close();
237             } catch (Exception JavaDoc e) {
238                 log.error("",e);
239             }
240             try {
241                 con.close();
242             } catch (Exception JavaDoc e) {
243                 log.error("",e);
244             }
245         }
246         return messageCount;
247     }
248     
249     public int getMessageCount(boolean approved) {
250             int messageCount = 0;
251             Connection JavaDoc con = null;
252             PreparedStatement JavaDoc pstmt = null;
253             try {
254                 con = DbConnectionManager.getConnection();
255                 pstmt = con.prepareStatement(APPROVED_MESSAGE_COUNT);
256                 pstmt.setInt(1, id);
257                 pstmt.setInt(2, approved?1:0);
258                 ResultSet JavaDoc rs = pstmt.executeQuery();
259                 rs.next();
260                 messageCount = rs.getInt(1);
261             } catch (SQLException JavaDoc sqle) {
262                 log.error("DbForumThread:getMessageCount() failed: " , sqle);
263             } finally {
264                 try {
265                     pstmt.close();
266                 } catch (Exception JavaDoc e) {
267                     log.error("",e);
268                 }
269                 try {
270                     con.close();
271                 } catch (Exception JavaDoc e) {
272                     log.error("",e);
273                 }
274             }
275             return messageCount;
276         }
277
278     public void addMessage(Message parentMessage, Message newMessage) {
279         boolean abortTransaction = false;
280         boolean supportsTransactions = false;
281         //Add message to db
282
Connection JavaDoc con = null;
283         PreparedStatement JavaDoc pstmt = null;
284         try {
285             con = DbConnectionManager.getConnection();
286             supportsTransactions = con.getMetaData().supportsTransactions();
287             if (supportsTransactions) {
288                 con.setAutoCommit(false);
289             }
290
291             //Now, insert the message into the database.
292
//Now, insert the message into the database.
293
((MessageProxy) newMessage).insertIntoDb(con, this);
294
295             pstmt = con.prepareStatement(ADD_MESSAGE);
296             pstmt.setInt(1, parentMessage.getID());
297             pstmt.setInt(2, newMessage.getID());
298             pstmt.executeUpdate();
299             pstmt.close();
300         } catch (Exception JavaDoc e) {
301             log.error("",e);
302             abortTransaction = true;
303             return;
304         } finally {
305             try {
306                 if (supportsTransactions) {
307                     if (abortTransaction == true) {
308                         con.rollback();
309                     } else {
310                         con.commit();
311                     }
312                 }
313             } catch (Exception JavaDoc e) {
314                 log.error("",e);
315             }
316             try {
317                 if (supportsTransactions) {
318                     con.setAutoCommit(true);
319                 }
320                 con.close();
321             } catch (Exception JavaDoc e) {
322                 log.error("",e);
323             }
324         }
325
326         //Added new message, so update the modified date of this thread
327
updateModifiedDate(newMessage.getModifiedDate());
328         //Also, update the modified date of the forum
329
DbForum dbForum = (DbForum) factory.cacheManager.get(DbCacheManager.FORUM_CACHE, new Integer JavaDoc(forum.getID()));
330         if (dbForum != null) {
331             dbForum.updateModifiedDate(modifiedDate);
332         } else {
333             forum.updateModifiedDate(modifiedDate);
334         }
335     }
336
337     public void deleteMessage(Message message) throws UnauthorizedException {
338         //Skip null messages or the case that we're already deleting the thread.
339
if (message == null) {
340             return;
341         }
342         //If the message does not belong to this thread, don't perform delete.
343
if (message.getForumThread().getID() != this.id) {
344             throw new IllegalArgumentException JavaDoc(
345                 "Message "
346                     + message.getID()
347                     + " could not be deleted. It belongs to thread "
348                     + message.getForumThread().getID()
349                     + ", and not thread "
350                     + this.id
351                     + ".");
352         }
353
354         Connection JavaDoc con = null;
355         PreparedStatement JavaDoc pstmt = null;
356         try {
357             con = DbConnectionManager.getConnection();
358             //Delete the message from the parent/child table
359
pstmt = con.prepareStatement(DELETE_MESSAGE1);
360             pstmt.setInt(1, message.getID());
361             pstmt.execute();
362         } catch (SQLException JavaDoc sqle) {
363             log.error("Error in DbForumThread:deleteMessage()-" , sqle);
364         } finally {
365             try {
366                 pstmt.close();
367             } catch (Exception JavaDoc e) {
368                 log.error("",e);
369             }
370             try {
371                 con.close();
372             } catch (Exception JavaDoc e) {
373                 log.error("",e);
374             }
375         }
376
377         //Recursively delete all children
378
TreeWalker walker = treeWalker();
379         int childCount = walker.getChildCount(message);
380         for (int i = childCount - 1; i >= 0; i--) {
381             Message childMessage = walker.getChild(message, i);
382             if (childMessage == null) {
383                 log.error("child message was null -- index " + i);
384             }
385             deleteMessage(childMessage);
386         }
387
388         try {
389             //Delete the actual message.
390
con = DbConnectionManager.getConnection();
391             pstmt = con.prepareStatement(DELETE_MESSAGE2);
392             pstmt.setInt(1, message.getID());
393             pstmt.execute();
394             pstmt.close();
395
396             //Delete any message properties.
397
pstmt = con.prepareStatement(DELETE_MESSAGE_PROPERTIES);
398             pstmt.setInt(1, message.getID());
399             pstmt.execute();
400             
401             ForumEvent event = new ForumEvent(message);
402             for (int i = 0; i < listeners.size(); i++) {
403                 ((ForumListener) listeners.get(i)).objectDeleted(event);
404             }
405             
406             
407             
408         } catch (SQLException JavaDoc sqle) {
409             log.error("Error in DbForumThread:deleteMessage()-" , sqle);
410         } finally {
411             try {
412                 pstmt.close();
413             } catch (Exception JavaDoc e) {
414                 log.error("",e);
415             }
416             try {
417                 con.close();
418             } catch (Exception JavaDoc e) {
419                 log.error("",e);
420             }
421         }
422
423         //Now, delete from the cache.
424
factory.getCacheManager().remove(DbCacheManager.MESSAGE_CACHE, new Integer JavaDoc(message.getID()));
425
426         //Finally, delete it from the search index
427
//factory.getSearchIndexer().removeFromIndex(message);
428

429         //Now, make sure that the message being deleted isn't the root message
430
//of this thread. If it is, the whole thread should just be deleted.
431
if (message.getID() == this.rootMessageID) {
432             forum.deleteThreadRecord(this.id);
433         }
434     }
435
436     public void moveMessage(Message message, ForumThread newThread, Message parentMessage)
437         throws UnauthorizedException, IllegalArgumentException JavaDoc {
438         if (message.getForumThread().getID() != this.id
439             || parentMessage.getForumThread().getID() != newThread.getID()) {
440             throw new IllegalArgumentException JavaDoc("The messages and threads did not match.");
441         }
442
443         // Save the mesageID of message to move
444
int messageID = message.getID();
445         // Original message thread rootMessageID
446
int oldRootMessageID = getRootMessage().getID();
447
448         // Move the children of this message to the new thread
449
TreeWalker walker = treeWalker();
450         int childCount = walker.getChildCount(message);
451         for (int i = 0; i < childCount; i++) {
452             Message childMessage = walker.getChild(message, i);
453             changeMessageThread(childMessage, newThread);
454         }
455
456         //Move the message to the new thread.
457
changeMessageThread(message, newThread);
458
459         //Make message a child of parentMessage
460
Connection JavaDoc con = null;
461         PreparedStatement JavaDoc pstmt = null;
462         try {
463             con = DbConnectionManager.getConnection();
464
465             if (oldRootMessageID != messageID) {
466                 pstmt = con.prepareStatement(MOVE_MESSAGE);
467                 pstmt.setInt(1, parentMessage.getID());
468                 pstmt.setInt(2, messageID);
469             } else {
470                 pstmt = con.prepareStatement(ADD_MESSAGE);
471                 pstmt.setInt(1, parentMessage.getID());
472                 pstmt.setInt(2, messageID);
473             }
474
475             pstmt.executeUpdate();
476             pstmt.close();
477         } catch (SQLException JavaDoc sqle) {
478             log.error("Error in DbForumThread:moveMessage()-" , sqle);
479         } finally {
480             try {
481                 pstmt.close();
482             } catch (Exception JavaDoc e) {
483                 log.error("",e);
484             }
485             try {
486                 con.close();
487             } catch (Exception JavaDoc e) {
488                 log.error("",e);
489             }
490         }
491
492         //Update the modified date of newThread
493
Date JavaDoc now = new Date JavaDoc();
494         newThread.setModifiedDate(now);
495         //Update the modified date of newThread forum
496
newThread.getForum().setModifiedDate(now);
497
498         //Thread has been modified, invalidate the cache
499
DbCacheManager cacheManager = factory.getCacheManager();
500         Integer JavaDoc key = new Integer JavaDoc(this.id);
501         cacheManager.remove(DbCacheManager.THREAD_CACHE, key);
502
503         //If we moved the root message of this thread, the thread should be
504
//deleted. Normally, deleting a thread will delete all of it's messages.
505
//However, we've already adjusted the thread/message relationship at the
506
//SQL level and removed the thread from cache. That should mean we're safe.
507
if (getRootMessage().getID() == messageID) {
508             //rootMessage = null;
509
this.getForum().deleteThread(this);
510         }
511     }
512
513     public TreeWalker treeWalker() {
514         return new DbTreeWalker(this, factory);
515     }
516     
517     public TreeWalker treeWalker(boolean approved) {
518             return new DbTreeWalker(approved,this, factory);
519         }
520
521     public Iterator JavaDoc messages() {
522         return new DbThreadIterator(this);
523     }
524
525     public Iterator JavaDoc messages(int startIndex, int numResults) {
526         return new DbThreadIterator(this, startIndex, numResults);
527     }
528     
529     public Iterator JavaDoc messages(boolean approved) {
530         return new DbThreadIterator(approved,this);
531     }
532
533     public Iterator JavaDoc messages(boolean approved,int startIndex, int numResults) {
534         return new DbThreadIterator(approved,this, startIndex, numResults);
535     }
536
537     public boolean hasPermission(int type) {
538         return true;
539     }
540
541     //FROM THE CACHEABLE INTERFACE//
542

543     public int getSize() {
544         //Approximate the size of the object in bytes by calculating the size
545
//of each field.
546
int size = 0;
547         size += CacheSizes.sizeOfObject(); //overhead of object
548
size += CacheSizes.sizeOfInt(); //id
549
size += CacheSizes.sizeOfDate(); //creation date
550
size += CacheSizes.sizeOfDate(); //modified date
551
size += CacheSizes.sizeOfBoolean(); //approved
552
size += CacheSizes.sizeOfObject(); //ref to rootMessage
553
size += CacheSizes.sizeOfObject(); //ref to forum
554
size += CacheSizes.sizeOfObject(); //ref to factory
555
size += CacheSizes.sizeOfBoolean(); //ready save var
556
size += CacheSizes.sizeOfBoolean(); //deleting var
557

558         return size;
559     }
560
561     //OTHER METHODS//
562

563     /**
564      * Converts the object to a String by returning the name of the thread.
565      * This functionality is primarily for Java applications that might be
566      * accessing objects through a GUI.
567      */

568     public String JavaDoc toString() {
569         return getName();
570     }
571
572     public int hashCode() {
573         return id;
574     }
575
576     public boolean equals(Object JavaDoc object) {
577         if (this == object) {
578             return true;
579         }
580         if (object != null && object instanceof DbForumThread) {
581             return id == ((DbForumThread) object).getID();
582         } else {
583             return false;
584         }
585     }
586
587     /**
588      * Updates the modified date but doesn't require a security check since
589      * it is a protected method.
590      */

591     protected void updateModifiedDate(java.util.Date JavaDoc modifiedDate) {
592         this.modifiedDate = modifiedDate;
593         Connection JavaDoc con = null;
594         PreparedStatement JavaDoc pstmt = null;
595         try {
596             con = DbConnectionManager.getConnection();
597             pstmt = con.prepareStatement(UPDATE_THREAD_MODIFIED_DATE);
598             pstmt.setString(1, "" + modifiedDate.getTime());
599             pstmt.setInt(2, id);
600             pstmt.executeUpdate();
601         } catch (SQLException JavaDoc sqle) {
602             log.error("Error in DbForumThread:updateModifiedDate()-" , sqle);
603             
604         } finally {
605             try {
606                 pstmt.close();
607             } catch (Exception JavaDoc e) {
608                 log.error("",e);
609             }
610             try {
611                 con.close();
612             } catch (Exception JavaDoc e) {
613                 log.error("",e);
614             }
615         }
616     }
617
618     /**
619      * Moves a message to a new thread by modifying the message table threadID
620      * column.
621      *
622      * @param message the message to move.
623      * @param newThread the thread to move the message to.
624      */

625     private void changeMessageThread(Message message, ForumThread newThread) throws UnauthorizedException {
626         //Remove message from the search index
627
//factory.getSearchIndexer().removeFromIndex(message);
628

629         //Remove message from cache.
630
DbCacheManager cacheManager = factory.getCacheManager();
631         Integer JavaDoc key = new Integer JavaDoc(message.getID());
632         cacheManager.remove(DbCacheManager.MESSAGE_CACHE, key);
633
634         Connection JavaDoc con = null;
635         PreparedStatement JavaDoc pstmt = null;
636         try {
637             con = DbConnectionManager.getConnection();
638             pstmt = con.prepareStatement(CHANGE_MESSAGE_THREAD);
639             pstmt.setInt(1, newThread.getID());
640             pstmt.setInt(2, key.intValue());
641             pstmt.executeUpdate();
642         } catch (SQLException JavaDoc sqle) {
643             log.error("",sqle);
644         }
645
646         // Add message back to search index and update modified date
647
try {
648             Message movedMessage = newThread.getMessage(key.intValue());
649             //factory.getSearchIndexer().addToIndex(movedMessage);
650
movedMessage.setModifiedDate(new Date JavaDoc());
651         } catch (ForumMessageNotFoundException e) {
652             log.error(
653                 "Error in DbForumThread:changeMessageThread()-"
654                     + "messageID="
655                     + key.intValue()
656                     + "newThreadID="
657                     + newThread.getID());
658         } finally {
659             try {
660                 pstmt.close();
661             } catch (Exception JavaDoc e) {
662                 log.error("",e);
663             }
664             try {
665                 con.close();
666             } catch (Exception JavaDoc e) {
667                 log.error("",e);
668             }
669         }
670     }
671
672     /**
673      * Loads a ForumThread from the database.
674      */

675     private void loadFromDb() throws ForumThreadNotFoundException {
676         Connection JavaDoc con = null;
677         PreparedStatement JavaDoc pstmt = null;
678         try {
679             con = DbConnectionManager.getConnection();
680             pstmt = con.prepareStatement(LOAD_THREAD);
681             pstmt.setInt(1, id);
682             ResultSet JavaDoc rs = pstmt.executeQuery();
683             if (!rs.next()) {
684                 throw new ForumThreadNotFoundException("Thread " + id + " could not be loaded from the database.");
685             }
686             //try {
687
rootMessageID = rs.getInt("rootMessageID");
688             //}
689
//catch (ForumMessageNotFoundException fmnfe) {
690
// log("Error: could not load root message of thread "
691
// + id + ". The database record could be corrupt.");
692
//
693
//}
694
creationDate = new java.util.Date JavaDoc(Long.parseLong(rs.getString("creationDate").trim()));
695             modifiedDate = new java.util.Date JavaDoc(Long.parseLong(rs.getString("modifiedDate").trim()));
696             approved = rs.getInt("approved")==1;
697             pstmt.close();
698         } catch (SQLException JavaDoc sqle) {
699             log.error("load",sqle);
700             throw new ForumThreadNotFoundException("Thread " + id + " could not be loaded from the database.");
701         } catch (NumberFormatException JavaDoc nfe) {
702             log.error(
703                 "WARNING: In DbForumThread.loadFromDb() -- there "
704                     + "was an error parsing the dates returned from the database. Ensure "
705                     + "that they're being stored correctly.");
706         } finally {
707             try {
708                 con.close();
709             } catch (Exception JavaDoc e) {
710                 log.error("",e);
711             }
712         }
713     }
714
715     /**
716      * Inserts a new forum thread into the database. A connection object must
717      * be passed in. The connection must be open when passed in, and will
718      * remain open when passed back. This method allows us to make insertions
719      * be transactional.
720      *
721      * @param con an open Connection used to insert the thread to the db.
722      */

723     public void insertIntoDb(Connection JavaDoc con) throws SQLException JavaDoc {
724         PreparedStatement JavaDoc pstmt = con.prepareStatement(INSERT_THREAD);
725         pstmt.setInt(1, id);
726         pstmt.setInt(2, forum.getID());
727         pstmt.setInt(3, rootMessageID);
728         pstmt.setString(4, Long.toString(creationDate.getTime()));
729         pstmt.setString(5, Long.toString(modifiedDate.getTime()));
730         pstmt.setInt(6, approved ? 1 : 0);
731         pstmt.executeUpdate();
732         pstmt.close();
733
734         //Now, insert the message into the database.
735

736         
737          ((MessageProxy) rootMessage).insertIntoDb(con, this);
738
739         //since we're done inserting the object to the database, it is ready
740
//for future insertions.
741
isReadyToSave = true;
742     }
743
744     /**
745      * Saves the ForumThread to the database.
746      */

747     private synchronized void saveToDb() {
748         Connection JavaDoc con = null;
749         PreparedStatement JavaDoc pstmt = null;
750         try {
751             con = DbConnectionManager.getConnection();
752             pstmt = con.prepareStatement(SAVE_THREAD);
753             pstmt.setInt(1, rootMessageID);
754             pstmt.setString(2, Long.toString(creationDate.getTime()));
755             pstmt.setString(3, Long.toString(modifiedDate.getTime()));
756             pstmt.setInt(4, approved ? 1 : 0);
757             pstmt.setInt(5, id);
758             pstmt.executeUpdate();
759         } catch (SQLException JavaDoc sqle) {
760             log.error("Error in DbForumThread:saveToDb()-" , sqle);
761         } finally {
762             try {
763                 pstmt.close();
764             } catch (Exception JavaDoc e) {
765                 log.error("",e);
766             }
767             try {
768                 con.close();
769             } catch (Exception JavaDoc e) {
770                 log.error("",e);
771             }
772         }
773     }
774     
775     // gestion des Úvenements
776
private static Vector JavaDoc listeners = new Vector JavaDoc();
777
778     public static void addListener(ForumListener listener) {
779         listeners.add(listener);
780     }
781
782     public static void removeListener(ForumListener listener) {
783         listeners.remove(listener);
784     }
785 }
786
Popular Tags