KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > database > DbForum


1 /**
2  * Copyright (C) 2001 Yasna.com. All rights reserved.
3  *
4  * ===================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by
22  * Yasna.com (http://www.yasna.com)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Yazd" and "Yasna.com" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please
29  * contact yazd@yasna.com.
30  *
31  * 5. Products derived from this software may not be called "Yazd",
32  * nor may "Yazd" appear in their name, without prior written
33  * permission of Yasna.com.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of Yasna.com. For more information
51  * on Yasna.com, please see <http://www.yasna.com>.
52  */

53
54 /**
55  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
56  *
57  * ===================================================================
58  * The Apache Software License, Version 1.1
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. The end-user documentation included with the redistribution,
73  * if any, must include the following acknowledgment:
74  * "This product includes software developed by
75  * CoolServlets.com (http://www.coolservlets.com)."
76  * Alternately, this acknowledgment may appear in the software itself,
77  * if and wherever such third-party acknowledgments normally appear.
78  *
79  * 4. The names "Jive" and "CoolServlets.com" must not be used to
80  * endorse or promote products derived from this software without
81  * prior written permission. For written permission, please
82  * contact webmaster@coolservlets.com.
83  *
84  * 5. Products derived from this software may not be called "Jive",
85  * nor may "Jive" appear in their name, without prior written
86  * permission of CoolServlets.com.
87  *
88  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
92  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
95  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
96  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
98  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * SUCH DAMAGE.
100  * ====================================================================
101  *
102  * This software consists of voluntary contributions made by many
103  * individuals on behalf of CoolServlets.com. For more information
104  * on CoolServlets.com, please see <http://www.coolservlets.com>.
105  */

106
107 package com.Yasna.forum.database;
108
109 import java.util.Iterator JavaDoc;
110 import java.util.Enumeration JavaDoc;
111 import java.util.Properties JavaDoc;
112 import java.util.ArrayList JavaDoc;
113 import java.util.Date JavaDoc;
114 import java.sql.*;
115 import java.io.*;
116
117 import com.Yasna.forum.*;
118 import com.Yasna.forum.Exceptions.RapidPostingException;
119 import com.Yasna.forum.Exceptions.UserBlackListedException;
120 import com.Yasna.forum.util.ClientIP;
121 import com.Yasna.forum.filter.*;
122 import com.Yasna.util.Cache;
123 import com.Yasna.util.Cacheable;
124 import com.Yasna.util.CacheSizes;
125
126 /**
127  * Database implementation of the Forum interface. It loads and stores forum
128  * information from a a database.
129  *
130  * @see Forum
131  */

132 public class DbForum implements Forum, Cacheable {
133
134     /** DATABASE QUERIES **/
135     private static final String JavaDoc ADD_THREAD =
136         "UPDATE yazdThread set forumID=? WHERE threadID=?";
137     protected static final String JavaDoc DELETE_THREAD = "DELETE FROM yazdThread WHERE threadID=?";
138     private static final String JavaDoc THREAD_COUNT =
139         "SELECT count(*) FROM yazdThread WHERE forumID=?";
140     private static final String JavaDoc MESSAGE_COUNT =
141         "SELECT count(*) FROM yazdThread, yazdMessage WHERE " +
142         "yazdThread.forumID=? AND yazdThread.threadID=yazdMessage.threadID";
143     private static final String JavaDoc ADD_USER_PERM =
144         "INSERT INTO yazdUserPerm(forumID,userID,permission) VALUES(?,?,?)";
145     private static final String JavaDoc REMOVE_USER_PERM =
146         "DELETE FROM yazdUserPerm WHERE forumID=? AND userID=? AND permission=?";
147     private static final String JavaDoc USERS_WITH_PERM =
148         "SELECT DISTINCT userID FROM yazdUserPerm WHERE forumID=? AND permission=?";
149     private static final String JavaDoc ADD_GROUP_PERM =
150         "INSERT INTO yazdGroupPerm(forumID,groupID,permission) VALUES(?,?,?)";
151     private static final String JavaDoc REMOVE_GROUP_PERM =
152         "DELETE FROM yazdGroupPerm WHERE forumID=? AND groupID=? AND permission=?";
153     private static final String JavaDoc GROUPS_WITH_PERM =
154         "SELECT DISTINCT groupID FROM yazdGroupPerm WHERE forumID=? AND permission=?";
155     private static final String JavaDoc LOAD_FILTERS =
156         "SELECT filterObject, filterIndex FROM yazdFilter WHERE forumID=? ORDER BY filterIndex ASC";
157     private static final String JavaDoc DELETE_FILTERS = "DELETE FROM yazdFilter WHERE forumID=?";
158     private static final String JavaDoc ADD_FILTER =
159         "INSERT INTO yazdFilter(forumID,filterIndex,filterObject) VALUES(?,?,?)";
160     private static final String JavaDoc LOAD_PROPERTIES =
161         "SELECT name, propValue FROM yazdForumProp WHERE forumID=?";
162     private static final String JavaDoc DELETE_PROPERTIES =
163         "DELETE FROM yazdForumProp WHERE forumID=?";
164     private static final String JavaDoc INSERT_PROPERTY =
165         "INSERT INTO yazdForumProp(forumID,name,propValue) VALUES(?,?,?)";
166     private static final String JavaDoc LOAD_FORUM_BY_ID =
167         "SELECT forumID, name, description, creationDate, modifiedDate, moderated, article,forumorder FROM yazdForum WHERE forumID=?";
168     private static final String JavaDoc LOAD_FORUM_BY_NAME =
169         "SELECT forumID, name, description, creationDate, modifiedDate, moderated, article,forumorder FROM yazdForum WHERE name=?";
170     private static final String JavaDoc ADD_FORUM =
171         "INSERT INTO yazdForum(forumID, name, description, creationDate, " +
172         "modifiedDate, moderated, forumGroupID,article,forumorder) VALUES (?,?,?,?,?,?,?,?,0)";
173     private static final String JavaDoc SAVE_FORUM =
174         "UPDATE yazdForum SET name=?, description=?, creationDate=?, " +
175         "modifiedDate=?, moderated=?,forumorder=? WHERE forumID=?";
176     private static final String JavaDoc UPDATE_FORUM_MODIFIED_DATE =
177         "UPDATE yazdForum SET modifiedDate=? WHERE forumID=?";
178     private static final String JavaDoc INSERT_ARTICLE_MAP="insert into yazdArticleMap(threadID,pageKey,forumID) values(?,?,?)";
179
180     private int id = -1;
181     private String JavaDoc name;
182     private String JavaDoc description;
183     private int forumGroupID;
184     private java.util.Date JavaDoc creationDate;
185     private java.util.Date JavaDoc modifiedDate;
186     private boolean moderated;
187     private boolean isarticle=false;
188     private ForumMessageFilter[] filters;
189     private Properties JavaDoc properties;
190     //Lock for saving state to database.
191
private Object JavaDoc saveLock = new Object JavaDoc();
192     private int order;
193
194     private DbForumFactory factory;
195
196     /**
197      * Creates a new forum with the specified name and description.
198      *
199      * @param name the name of the forum.
200      * @param description the description of the forum.
201      * @param moderated when true - posted messages and threads must first be approved
202      * @param forumGroupID every forum belongs to a Category and ForumGroup
203      * @param factory the DbForumFactory the forum is a part of.
204      */

205     protected DbForum(String JavaDoc name, String JavaDoc description, boolean moderated,
206                       int forumGroupID, DbForumFactory factory,boolean article) {
207         this.id = DbSequenceManager.nextID("Forum");
208         this.name = name;
209         this.description = description;
210         this.moderated = moderated;
211         this.forumGroupID = forumGroupID;
212         long now = System.currentTimeMillis();
213         creationDate = new java.util.Date JavaDoc(now);
214         modifiedDate = new java.util.Date JavaDoc(now);
215         this.factory = factory;
216         this.isarticle=article;
217         insertIntoDb();
218         properties = new Properties JavaDoc();
219         //Forums should start with an html filter by default for
220
//security purposes.
221
filters = new ForumMessageFilter[2];
222         filters[0] = new FilterHtml();
223         filters[1] = new FilterNewline();
224         saveFiltersToDb();
225         //**Commenting out below since it doesn't seem to work for some reason.
226
//try {
227
// addForumMessageFilter(new FilterHtml(), 0);
228
// addForumMessageFilter(new FilterNewline(), 1);
229
//}
230
//catch (UnauthorizedException ue) {
231
// ue.printStackTrace();
232
//}
233
}
234
235     /**
236      * Loads a forum with the specified id.
237      */

238     protected DbForum(int id, DbForumFactory factory)
239             throws ForumNotFoundException
240     {
241         this.id = id;
242         this.factory = factory;
243         loadFromDb();
244         loadFiltersFromDb();
245         loadProperties();
246     }
247
248     /**
249      * Loads a forum with the specified name.
250      */

251     protected DbForum(String JavaDoc name, DbForumFactory factory)
252             throws ForumNotFoundException
253     {
254         this.name = name;
255         this.factory = factory;
256         loadFromDb();
257         loadFiltersFromDb();
258         loadProperties();
259     }
260
261     //FROM THE FORUM INTERFACE//
262

263     public int getID() {
264         return id;
265     }
266
267     public String JavaDoc getName() {
268         return name;
269     }
270
271     public void setName(String JavaDoc name) throws UnauthorizedException {
272         this.name = name;
273         saveToDb();
274     }
275
276     public String JavaDoc getDescription() {
277         return description;
278     }
279
280     public void setDescription(String JavaDoc description) throws UnauthorizedException
281     {
282         this.description = description;
283         saveToDb();
284     }
285
286     public java.util.Date JavaDoc getCreationDate() {
287         return creationDate;
288     }
289
290     public void setCreationDate(java.util.Date JavaDoc creationDate)
291             throws UnauthorizedException
292     {
293        this.creationDate = creationDate;
294        saveToDb();
295     }
296
297     public java.util.Date JavaDoc getModifiedDate() {
298         return modifiedDate;
299     }
300
301     public void setModifiedDate(java.util.Date JavaDoc modifiedDate)
302             throws UnauthorizedException
303     {
304         this.modifiedDate = modifiedDate;
305         saveToDb();
306     }
307
308     public String JavaDoc getProperty(String JavaDoc name) {
309         return (String JavaDoc)properties.get(name);
310     }
311
312     public void setProperty(String JavaDoc name, String JavaDoc value)
313             throws UnauthorizedException
314     {
315         properties.put(name, value);
316         saveProperties();
317     }
318
319     public Enumeration JavaDoc propertyNames() {
320         return properties.keys();
321     }
322
323     public boolean isModerated() {
324         return moderated;
325     }
326
327     public void setModerated(boolean moderated)
328             throws UnauthorizedException
329     {
330         this.moderated = moderated;
331         saveToDb();
332     }
333
334     public ForumThread createThread(ForumMessage rootMessage,ThreadType typeid)
335         throws UnauthorizedException
336     {
337         //If the forum is moderated, the thread is not automatically
338
//approved.
339
boolean approved = !isModerated();
340         return new DbForumThread(rootMessage, approved, this, factory,typeid);
341     }
342
343     public ForumMessage createMessage(User user,ClientIP clientIP)
344         throws UnauthorizedException,RapidPostingException, UserBlackListedException
345     {
346         //If the forum is moderated, the message is not automatically
347
//approved.
348
boolean approved = !isModerated();
349         ForumMessage message = new DbForumMessage(user, factory, approved,clientIP);
350         return message;
351     }
352     public ForumMessage createDummyMessage(User user)
353         throws UnauthorizedException
354     {
355         return new DbForumMessage(user, factory);
356     }
357
358     public void addThread(ForumThread thread) throws UnauthorizedException {
359         boolean abortTransaction = false;
360         boolean supportsTransactions = false;
361         //Add message to db
362
Connection con = null;
363         PreparedStatement pstmt = null;
364         try {
365             con = DbConnectionManager.getConnection();
366             supportsTransactions = con.getMetaData().supportsTransactions();
367             if (supportsTransactions) {
368                 con.setAutoCommit(false);
369             }
370
371             pstmt = con.prepareStatement(ADD_THREAD);
372             pstmt.setInt(1,id);
373             pstmt.setInt(2,thread.getID());
374             pstmt.executeUpdate();
375             pstmt.close();
376
377             //Now, insert the thread into the database.
378
((ForumThreadProxy)thread).insertIntoDb(con);
379         }
380         catch(Exception JavaDoc e) {
381             e.printStackTrace();
382             abortTransaction = true;
383             return;
384         }
385         finally {
386             try {
387                 if (supportsTransactions) {
388                     if (abortTransaction == true) {
389                         con.rollback();
390                     }
391                     else {
392                         con.commit();
393                     }
394                 }
395             }
396             catch (Exception JavaDoc e) { e.printStackTrace(); }
397             try {
398                 if (supportsTransactions) {
399                     con.setAutoCommit(true);
400                 }
401                 con.close();
402             }
403             catch (Exception JavaDoc e) { e.printStackTrace(); }
404         }
405
406         //Since we added a thread, update the modified date of this thread.
407
updateModifiedDate(thread.getModifiedDate());
408         //we add the subscription to the thread if necessary also
409
User newUser = thread.getRootMessage().getUser();
410         if(newUser.getThreadSubscribe() && newUser.getID() > 1){
411             newUser.setProperty("WatchThread"+thread.getID(),"true");
412         }
413
414     }
415
416     public ForumThread getThread(int threadID) throws
417             ForumThreadNotFoundException
418     {
419         return factory.getThread(threadID, this);
420     }
421
422     public void deleteThread(ForumThread thread) throws UnauthorizedException
423     {
424         //Delete all messages from the thread. Deleting the root
425
//message will delete all submessages.
426
ForumMessage message = thread.getRootMessage();
427         thread.deleteMessage(message);
428     }
429
430     protected void deleteThreadRecord(int threadID) {
431
432         //Delete the actual thread
433
Connection con = null;
434         PreparedStatement pstmt = null;
435         try {
436             con = DbConnectionManager.getConnection();
437             pstmt = con.prepareStatement(DELETE_THREAD);
438             pstmt.setInt(1, threadID);
439             pstmt.execute();
440         }
441         catch( Exception JavaDoc sqle ) {
442             System.err.println("Error in DbForum:deleteThread()-" + sqle);
443         }
444         finally {
445             try { pstmt.close(); }
446             catch (Exception JavaDoc e) { e.printStackTrace(); }
447             try { con.close(); }
448             catch (Exception JavaDoc e) { e.printStackTrace(); }
449         }
450
451         //Now, delete from cache
452
Integer JavaDoc threadIDInteger = new Integer JavaDoc(threadID);
453         factory.getCacheManager().remove(DbCacheManager.THREAD_CACHE, threadIDInteger);
454     }
455
456     public void moveThread(ForumThread thread, Forum forum)
457         throws UnauthorizedException
458     {
459         //Ensure that thread belongs to this forum
460
if (thread.getForum().getID() != this.id) {
461             throw new IllegalArgumentException JavaDoc("The thread does not belong to this forum.");
462         }
463         //Ensure that thread is not in the same forum
464
if (thread.getForum().getID() == forum.getID()) {
465             throw new IllegalArgumentException JavaDoc("The thread is already in this forum.");
466         }
467
468         //Modify the SQL record. Only the thread table has information about
469
//forumID, so we only need to modify that record. The message records
470
//underneath the thread can be left alone.
471
Connection con = null;
472         PreparedStatement pstmt = null;
473         try {
474             con = DbConnectionManager.getConnection();
475             pstmt = con.prepareStatement(ADD_THREAD);
476             pstmt.setInt(1,forum.getID());
477             pstmt.setInt(2,thread.getID());
478             pstmt.executeUpdate();
479             pstmt.close();
480         }
481         catch( SQLException sqle ) {
482             System.err.println("Error in DbForum:addThread()-" + sqle);
483             return;
484         }
485         finally {
486             try { pstmt.close(); }
487             catch (Exception JavaDoc e) { e.printStackTrace(); }
488             try { con.close(); }
489             catch (Exception JavaDoc e) { e.printStackTrace(); }
490         }
491
492         DbCacheManager cacheManager = factory.getCacheManager();
493         SearchIndexer indexer = factory.getSearchIndexer();
494
495         //Remove both forums from cache.
496
Integer JavaDoc key = new Integer JavaDoc(this.id);
497         cacheManager.remove(DbCacheManager.FORUM_CACHE, key);
498         key = new Integer JavaDoc(forum.getID());
499         cacheManager.remove(DbCacheManager.FORUM_CACHE, key);
500
501         //Remove thread from cache.
502
key = new Integer JavaDoc(thread.getID());
503         cacheManager.remove(DbCacheManager.THREAD_CACHE, key);
504
505         //Loop through all messages in thread
506
Iterator JavaDoc messages = thread.messages();
507         while (messages.hasNext()) {
508             ForumMessage message = (ForumMessage)messages.next();
509             //Remove each message from cache.
510
key = new Integer JavaDoc(message.getID());
511             cacheManager.remove(DbCacheManager.MESSAGE_CACHE, key);
512             //Remove and re-add every message to the search index.
513
indexer.removeFromIndex(message);
514             indexer.addToIndex(message);
515         }
516
517         // Update the modified date of thread
518
Date JavaDoc now = new Date JavaDoc();
519         thread.setModifiedDate(now);
520         // Update the modified date of forum thread is now in
521
forum.setModifiedDate(now);
522     }
523
524     public Iterator JavaDoc threads() {
525         return new DbForumIterator(this, factory);
526     }
527
528     public Iterator JavaDoc threads(int startIndex, int numResults, int sortBy) {
529         return new DbForumIterator(this, factory, startIndex, numResults, sortBy);
530     }
531
532     public int getThreadCount() {
533         int threadCount = 0;
534         // Based on the id in the object, get the thread data from the database:
535
Connection con = null;
536         PreparedStatement pstmt = null;
537         try {
538             con = DbConnectionManager.getConnection();
539             pstmt = con.prepareStatement(THREAD_COUNT);
540             pstmt.setInt(1, id);
541             ResultSet rs = pstmt.executeQuery();
542             rs.next();
543             threadCount = rs.getInt(1 /*"threadCount"*/);
544         }
545         catch( SQLException sqle ) {
546             System.err.println("DbForum:getThreadCount() failed: " + sqle);
547         }
548         finally {
549             try { pstmt.close(); }
550             catch (Exception JavaDoc e) { e.printStackTrace(); }
551             try { con.close(); }
552             catch (Exception JavaDoc e) { e.printStackTrace(); }
553         }
554         return threadCount;
555     }
556
557     public int getMessageCount() {
558         int messageCount = 0;
559         Connection con = null;
560         PreparedStatement pstmt = null;
561         try {
562             con = DbConnectionManager.getConnection();
563             pstmt = con.prepareStatement(MESSAGE_COUNT);
564             pstmt.setInt(1, id);
565             ResultSet rs = pstmt.executeQuery();
566             rs.next();
567             messageCount = rs.getInt(1 /*"messageCount"*/);
568         }
569         catch( SQLException sqle ) {
570             System.err.println("DbForum:getMessageCount() failed: " + sqle);
571         }
572         finally {
573             try { pstmt.close(); }
574             catch (Exception JavaDoc e) { e.printStackTrace(); }
575             try { con.close(); }
576             catch (Exception JavaDoc e) { e.printStackTrace(); }
577         }
578         return messageCount;
579     }
580
581     public Query createQuery() {
582         return new DbQuery(this, factory);
583     }
584
585     public void addUserPermission(User user, int permissionType)
586             throws UnauthorizedException
587     {
588         Connection con = null;
589         PreparedStatement pstmt = null;
590         try {
591             con = DbConnectionManager.getConnection();
592             pstmt = con.prepareStatement(ADD_USER_PERM);
593             pstmt.setInt(1,id);
594             pstmt.setInt(2,user.getID());
595             pstmt.setInt(3,permissionType);
596             pstmt.execute();
597             //Remove user permissions from cache since they've changed.
598
factory.getCacheManager().removeUserPerm(
599                     new Integer JavaDoc(user.getID()),
600                     new Integer JavaDoc(id)
601             );
602         }
603         catch( SQLException sqle ) {
604             System.err.println("Error in DbForum.java:" + sqle);
605             sqle.printStackTrace();
606         }
607         finally {
608             try { pstmt.close(); }
609             catch (Exception JavaDoc e) { e.printStackTrace(); }
610             try { con.close(); }
611             catch (Exception JavaDoc e) { e.printStackTrace(); }
612         }
613     }
614
615     public void removeUserPermission(User user, int permissionType)
616             throws UnauthorizedException
617     {
618         Connection con = null;
619         PreparedStatement pstmt = null;
620         try {
621             con = DbConnectionManager.getConnection();
622             pstmt = con.prepareStatement(REMOVE_USER_PERM);
623             pstmt.setInt(1,id);
624             pstmt.setInt(2,user.getID());
625             pstmt.setInt(3,permissionType);
626             pstmt.execute();
627             //Remove user permissions from cache since they've changed.
628
factory.getCacheManager().removeUserPerm(
629                     new Integer JavaDoc(user.getID()),
630                     new Integer JavaDoc(id)
631             );
632         }
633         catch( SQLException sqle ) {
634             System.err.println("Error in DbForum.java:" + sqle);
635             sqle.printStackTrace();
636         }
637         finally {
638             try { pstmt.close(); }
639             catch (Exception JavaDoc e) { e.printStackTrace(); }
640             try { con.close(); }
641             catch (Exception JavaDoc e) { e.printStackTrace(); }
642         }
643     }
644
645     public int[] usersWithPermission(int permissionType)
646             throws UnauthorizedException
647     {
648         int [] users = new int[0];
649         Connection con = null;
650         PreparedStatement pstmt = null;
651         try {
652             con = DbConnectionManager.getConnection();
653             pstmt = con.prepareStatement(USERS_WITH_PERM);
654             pstmt.setInt(1,id);
655             pstmt.setInt(2,permissionType);
656             ResultSet rs = pstmt.executeQuery();
657             ArrayList JavaDoc userList = new ArrayList JavaDoc();
658             while (rs.next()) {
659                 userList.add(new Integer JavaDoc(rs.getInt("userID")));
660             }
661             users = new int[userList.size()];
662             for (int i=0; i<users.length; i++) {
663                 users[i] = ((Integer JavaDoc)userList.get(i)).intValue();
664             }
665         }
666         catch( SQLException sqle ) {
667             System.err.println("Error in DbForum.java:" + sqle);
668             sqle.printStackTrace();
669         }
670         finally {
671             try { pstmt.close(); }
672             catch (Exception JavaDoc e) { e.printStackTrace(); }
673             try { con.close(); }
674             catch (Exception JavaDoc e) { e.printStackTrace(); }
675         }
676         return users;
677     }
678
679     public void addGroupPermission(Group group, int permissionType)
680             throws UnauthorizedException
681     {
682         Connection con = null;
683         PreparedStatement pstmt = null;
684         try {
685             con = DbConnectionManager.getConnection();
686             pstmt = con.prepareStatement(ADD_GROUP_PERM);
687             pstmt.setInt(1,id);
688             pstmt.setInt(2,group.getID());
689             pstmt.setInt(3,permissionType);
690             pstmt.execute();
691             //Remove user permissions from cache since they've changed. Because
692
//of the way that user perm cache is handled, it is easiest to
693
//simply remove all the user perm cache for the forum. This is ok
694
//since happens infrequently.
695
factory.getCacheManager().remove(
696                 DbCacheManager.USER_PERMS_CACHE,
697                 new Integer JavaDoc(id)
698             );
699         }
700         catch( SQLException sqle ) {
701             System.err.println("Error in DbForum.java:" + sqle);
702             sqle.printStackTrace();
703         }
704         finally {
705             try { pstmt.close(); }
706             catch (Exception JavaDoc e) { e.printStackTrace(); }
707             try { con.close(); }
708             catch (Exception JavaDoc e) { e.printStackTrace(); }
709         }
710     }
711
712     public void removeGroupPermission(Group group, int permissionType)
713             throws UnauthorizedException
714     {
715         Connection con = null;
716         PreparedStatement pstmt = null;
717         try {
718             con = DbConnectionManager.getConnection();
719             pstmt = con.prepareStatement(REMOVE_GROUP_PERM);
720             pstmt.setInt(1,id);
721             pstmt.setInt(2,group.getID());
722             pstmt.setInt(3,permissionType);
723             pstmt.execute();
724             //Remove user permissions from cache since they've changed. Because
725
//of the way that user perm cache is handled, it is easiest to
726
//simply remove all the user perm cache for the forum. This is ok
727
//since happens infrequently.
728
factory.getCacheManager().remove(
729                 DbCacheManager.USER_PERMS_CACHE,
730                 new Integer JavaDoc(id)
731             );
732         }
733         catch( SQLException sqle ) {
734             System.err.println("Error in DbForum.java:" + sqle);
735             sqle.printStackTrace();
736         }
737         finally {
738             try { pstmt.close(); }
739             catch (Exception JavaDoc e) { e.printStackTrace(); }
740             try { con.close(); }
741             catch (Exception JavaDoc e) { e.printStackTrace(); }
742         }
743     }
744
745     public int[] groupsWithPermission(int permissionType)
746             throws UnauthorizedException
747     {
748         int [] groups = new int[0];
749         Connection con = null;
750         PreparedStatement pstmt = null;
751         try {
752             con = DbConnectionManager.getConnection();
753             pstmt = con.prepareStatement(GROUPS_WITH_PERM);
754             pstmt.setInt(1,id);
755             pstmt.setInt(2,permissionType);
756             ResultSet rs = pstmt.executeQuery();
757             ArrayList JavaDoc groupList = new ArrayList JavaDoc();
758             while (rs.next()) {
759                 groupList.add(new Integer JavaDoc(rs.getInt("groupID")));
760             }
761             groups = new int[groupList.size()];
762             for (int i=0; i<groups.length; i++) {
763                 groups[i] = ((Integer JavaDoc)groupList.get(i)).intValue();
764             }
765         }
766         catch( SQLException sqle ) {
767             System.err.println("Error in DbForum.groupsWithPermission:" + sqle);
768             sqle.printStackTrace();
769         }
770         finally {
771             try { pstmt.close(); }
772             catch (Exception JavaDoc e) { e.printStackTrace(); }
773             try { con.close(); }
774             catch (Exception JavaDoc e) { e.printStackTrace(); }
775         }
776         return groups;
777     }
778
779     public ForumMessage applyFilters(ForumMessage message) {
780         //Loop through filters and apply them
781
for (int i=0; i < filters.length; i++) {
782             message = filters[i].clone(message);
783         }
784         return message;
785     }
786
787     public ForumMessageFilter[] getForumMessageFilters()
788             throws UnauthorizedException
789     {
790         ForumMessageFilter [] dbFilters = new ForumMessageFilter[filters.length];
791         for (int i=0; i<filters.length; i++) {
792             dbFilters[i] = new DbForumMessageFilter((ForumMessage)filters[i], this);
793         }
794         return dbFilters;
795     }
796
797     public void addForumMessageFilter(ForumMessageFilter filter)
798             throws UnauthorizedException
799     {
800         ArrayList JavaDoc newFilters = new ArrayList JavaDoc(filters.length+1);
801         for (int i=0; i<filters.length; i++) {
802             newFilters.add(filters[i]);
803         }
804         newFilters.add(filter);
805         ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters.size()];
806         for (int i=0; i<newArray.length; i++) {
807             newArray[i] = (ForumMessageFilter)newFilters.get(i);
808         }
809         //Finally, overwrite filters with the new array
810
filters = newArray;
811         saveFiltersToDb();
812     }
813
814     public void addForumMessageFilter(ForumMessageFilter filter, int index)
815             throws UnauthorizedException
816     {
817         ArrayList JavaDoc newFilters = new ArrayList JavaDoc(filters.length+1);
818         for (int i=0; i<filters.length; i++) {
819             newFilters.add(filters[i]);
820         }
821         newFilters.add(index, filter);
822         ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters.size()];
823         for (int i=0; i<newArray.length; i++) {
824             newArray[i] = (ForumMessageFilter)newFilters.get(i);
825         }
826         //Finally, overwrite filters with the new array
827
filters = newArray;
828         saveFiltersToDb();
829     }
830
831     public void removeForumMessageFilter(int index)
832             throws UnauthorizedException
833     {
834         ArrayList JavaDoc newFilters = new ArrayList JavaDoc(filters.length);
835         for (int i=0; i<filters.length; i++) {
836             newFilters.add(filters[i]);
837         }
838         newFilters.remove(index);
839         ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters.size()];
840         for (int i=0; i<newArray.length; i++) {
841             newArray[i] = (ForumMessageFilter)newFilters.get(i);
842         }
843         //Finally, overwrite filters with the new array
844
filters = newArray;
845         saveFiltersToDb();
846     }
847
848     public ForumPermissions getPermissions(Authorization authorization) {
849         int userID = authorization.getUserID();
850
851         //Get the user perm cache for this forum
852
Cache userPermCache = (Cache)factory.getCacheManager().get(
853             DbCacheManager.USER_PERMS_CACHE,
854             new Integer JavaDoc(id)
855         );
856
857         //Simple case: if cache is turned on and the user is already cached,
858
//we can simply return the cached permissions.
859
if (userPermCache != null) {
860             ForumPermissions permissions =
861                     (ForumPermissions)userPermCache.get(new Integer JavaDoc(userID));
862             if (permissions != null) {
863                 return permissions;
864             }
865         }
866
867         //Not so simple case: cache is not turned on or the user permissions
868
//have not been cached yet.
869
boolean isAnonymous = (userID == -1);
870         boolean isUser = !isAnonymous;
871
872         ForumPermissions finalPermissions = ForumPermissions.none();
873
874         //Step 1 - Get permissions for the User. This includes anonymous
875
//perms, "special user" perms, and the specific perms for the user.
876
if (isUser) {
877             ForumPermissions userPermissions = factory.getUserPermissions(userID, id);
878             //Combine permissions
879
finalPermissions = new ForumPermissions(finalPermissions, userPermissions);
880         }
881         //Add in anonymous perms.
882
ForumPermissions anonyPermissions = null;
883         if (userPermCache != null) {
884             anonyPermissions = (ForumPermissions)userPermCache.get(new Integer JavaDoc(-1));
885         }
886         //Otherwise, do our own lookup.
887
if (anonyPermissions == null) {
888             anonyPermissions = factory.getUserPermissions(-1, id);
889             //Add to cache so it will be there next time.
890
if (userPermCache != null) {
891                 userPermCache.add(new Integer JavaDoc(-1), anonyPermissions);
892             }
893         }
894         //Combine permissions
895
finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions);
896
897         //If they are a valid user, figure out "any user" permissions.
898
if (isUser) {
899             ForumPermissions specialUserPermissions = null;
900             //Check for cache
901
if (userPermCache != null) {
902                 specialUserPermissions = (ForumPermissions)userPermCache.get(new Integer JavaDoc(0));
903             }
904             //Otherwise, do our own lookup.
905
if (specialUserPermissions == null) {
906                 specialUserPermissions = factory.getUserPermissions(0, id);
907                 //Add to cache so it will be there next time.
908
if (userPermCache != null) {
909                     userPermCache.add(new Integer JavaDoc(0), specialUserPermissions);
910                 }
911             }
912             //Combine permissions
913
finalPermissions = new ForumPermissions(finalPermissions, specialUserPermissions);
914         }
915
916         //Step 2 -- get Permissions for all groups the user is in.
917
int [] groups = ((DbProfileManager)factory.getProfileManager()).getUserGroups(userID);
918         for (int i=0; i<groups.length; i++) {
919             ForumPermissions groupPermissions = factory.getGroupPermissions(groups[i], id);
920             finalPermissions = new ForumPermissions(finalPermissions, groupPermissions);
921         }
922
923         //Finally, add user to cache so it will be there next time.
924
if (isUser && userPermCache != null) {
925             userPermCache.add(new Integer JavaDoc(userID), finalPermissions);
926         }
927
928         return finalPermissions;
929     }
930
931     public boolean hasPermission(int type) {
932         return true;
933     }
934
935     //FROM THE CACHEABLE INTERFACE//
936

937     public int getSize() {
938         //Approximate the size of the object in bytes by calculating the size
939
//of each field.
940
int size = 0;
941         size += CacheSizes.sizeOfObject(); //overhead of object
942
size += CacheSizes.sizeOfInt(); //id
943
size += CacheSizes.sizeOfString(name); //name
944
size += CacheSizes.sizeOfString(description); //description
945
size += CacheSizes.sizeOfDate(); //creation date
946
size += CacheSizes.sizeOfDate(); //modified date
947
size += CacheSizes.sizeOfBoolean(); //moderated
948
size += filters.length * 8; //each filter is 8 bytes
949
size += CacheSizes.sizeOfProperties(properties);//properties object
950
size += CacheSizes.sizeOfObject(); //save lock
951
size += CacheSizes.sizeOfBoolean(); //isarticle
952

953         return size;
954     }
955
956     //OTHER METHODS
957

958     /**
959      * Returns a String representation of the Forum object using the forum name.
960      *
961      * @return a String representation of the Forum object.
962      */

963     public String JavaDoc toString() {
964         return name;
965     }
966
967     public int hashCode() {
968         return id;
969     }
970
971     public boolean equals(Object JavaDoc object) {
972         if (this == object) {
973             return true;
974         }
975         if (object != null && object instanceof DbForum) {
976             return id == ((DbForum)object).getID();
977         }
978         else {
979             return false;
980         }
981     }
982
983     /**
984      * Updates the modified date but doesn't require a security check since
985      * it is a protected method.
986      */

987     protected void updateModifiedDate(java.util.Date JavaDoc modifiedDate) {
988         this.modifiedDate = modifiedDate;
989         Connection con = null;
990         PreparedStatement pstmt = null;
991         try {
992             con = DbConnectionManager.getConnection();
993             pstmt = con.prepareStatement(UPDATE_FORUM_MODIFIED_DATE);
994             pstmt.setString(1, ""+modifiedDate.getTime());
995             pstmt.setInt(2, id);
996             pstmt.executeUpdate();
997         }
998         catch( SQLException sqle ) {
999             System.err.println("Error in DbForum:updateModifiedDate()-" + sqle);
1000            sqle.printStackTrace();
1001        }
1002        finally {
1003            try { pstmt.close(); }
1004            catch (Exception JavaDoc e) { e.printStackTrace(); }
1005            try { con.close(); }
1006            catch (Exception JavaDoc e) { e.printStackTrace(); }
1007        }
1008    }
1009
1010    /**
1011     * Loads forum properties from the database.
1012     */

1013    private void loadProperties() {
1014        synchronized(saveLock) {
1015            Properties JavaDoc newProps = new Properties JavaDoc();
1016            Connection con = null;
1017            PreparedStatement pstmt = null;
1018            try {
1019                con = DbConnectionManager.getConnection();
1020                pstmt = con.prepareStatement(LOAD_PROPERTIES);
1021                pstmt.setInt(1, id);
1022                ResultSet rs = pstmt.executeQuery();
1023                while(rs.next()) {
1024                    String JavaDoc name = rs.getString("name");
1025                    String JavaDoc value = rs.getString("propValue");
1026                    newProps.put(name, value);
1027                }
1028            }
1029            catch( SQLException sqle ) {
1030                System.err.println("Error in DbForum:loadProperties():" + sqle);
1031                sqle.printStackTrace();
1032            }
1033            finally {
1034                try { pstmt.close(); }
1035                catch (Exception JavaDoc e) { e.printStackTrace(); }
1036                try { con.close(); }
1037                catch (Exception JavaDoc e) { e.printStackTrace(); }
1038            }
1039            this.properties = newProps;
1040        }
1041    }
1042
1043    /**
1044     * Saves forum properties to the database.
1045     */

1046    private void saveProperties() {
1047        synchronized(saveLock) {
1048            Connection con = null;
1049            PreparedStatement pstmt = null;
1050            try {
1051                con = DbConnectionManager.getConnection();
1052                //Delete all old values.
1053
pstmt = con.prepareStatement(DELETE_PROPERTIES);
1054                pstmt.setInt(1, id);
1055                pstmt.execute();
1056                pstmt.close();
1057                //Now insert new values.
1058
pstmt = con.prepareStatement(INSERT_PROPERTY);
1059                Enumeration JavaDoc enume = properties.keys();
1060                while (enume.hasMoreElements()) {
1061                    String JavaDoc name = (String JavaDoc)enume.nextElement();
1062                    String JavaDoc value = (String JavaDoc)properties.get(name);
1063                    pstmt.setInt(1, id);
1064                    pstmt.setString(2, name);
1065                    pstmt.setString(3, value);
1066                    pstmt.executeUpdate();
1067                }
1068            }
1069            catch( SQLException sqle ) {
1070                System.err.println(sqle);
1071            }
1072            finally {
1073                try { pstmt.close(); }
1074                catch (Exception JavaDoc e) { e.printStackTrace(); }
1075                try { con.close(); }
1076                catch (Exception JavaDoc e) { e.printStackTrace(); }
1077            }
1078        }
1079    }
1080
1081    /**
1082     * Loads filters from the database.
1083     */

1084    private void loadFiltersFromDb() {
1085        ArrayList JavaDoc newFilters = new ArrayList JavaDoc();
1086        Connection con = null;
1087        boolean abort = false;
1088        boolean supportsTransactions = false;
1089        PreparedStatement pstmt = null;
1090        try {
1091            con = DbConnectionManager.getConnection();
1092            supportsTransactions = con.getMetaData().supportsTransactions();
1093            if (supportsTransactions) {
1094                 con.setAutoCommit(false);
1095            }
1096
1097            pstmt = con.prepareStatement(LOAD_FILTERS);
1098            pstmt.setInt(1,id);
1099            ResultSet rs = pstmt.executeQuery();
1100            while(rs.next()) {
1101                try {
1102                    ObjectInputStream in = new ObjectInputStream(rs.getBinaryStream("filterObject"));
1103                    newFilters.add(in.readObject());
1104                }
1105                catch (ClassCastException JavaDoc cce) {
1106                    //ignore for now since the filter might be updated. we
1107
//need a solution for this. probably custom class loading
1108
//of filter classes to protect against failure like this.
1109
}
1110                catch (Exception JavaDoc e) {
1111                    e.printStackTrace();
1112                }
1113            }
1114        }
1115        catch( SQLException sqle ) {
1116            sqle.printStackTrace();
1117        }
1118        finally {
1119                try {
1120                    if (supportsTransactions) {
1121                        if (abort == true) {
1122                            con.rollback();
1123                        }
1124                        else {
1125                            con.commit();
1126                        }
1127                    }
1128                }
1129                catch (Exception JavaDoc e) { e.printStackTrace(); }
1130                try {
1131                    if (supportsTransactions) {
1132                        con.setAutoCommit(true);
1133                    }
1134                }
1135                catch (Exception JavaDoc e) { e.printStackTrace(); }
1136                try { pstmt.close(); }
1137                catch (Exception JavaDoc e) { e.printStackTrace(); }
1138                try { con.close(); }
1139                catch (Exception JavaDoc e) { e.printStackTrace(); }
1140
1141        }
1142        filters = new ForumMessageFilter[newFilters.size()];
1143        for (int i=0; i<filters.length; i++) {
1144            filters[i] = (ForumMessageFilter)newFilters.get(i);
1145        }
1146        //Finally, save filters back to Db. Effectively, this deletes filters
1147
//from the database that failed to load. See note above.
1148
//saveFiltersToDb(); <<-- commenting out to try to fix filters bug.
1149
}
1150
1151    /**
1152     * Saves filters to the database. Filter saving works by serializing
1153     * each filter to a byte stream and then inserting that stream into
1154     * the database.
1155     */

1156    protected void saveFiltersToDb() {
1157        boolean abort = false;
1158        boolean supportsTransactions = false;
1159        synchronized (saveLock) {
1160            Connection con = null;
1161            PreparedStatement pstmt = null;
1162            try {
1163                con = DbConnectionManager.getConnection();
1164
1165                supportsTransactions = con.getMetaData().supportsTransactions();
1166                if (supportsTransactions) {
1167                    con.setAutoCommit(false);
1168                }
1169
1170                pstmt = con.prepareStatement(DELETE_FILTERS);
1171                pstmt.setInt(1,id);
1172                pstmt.execute();
1173                //Now insert new list of filters.
1174
pstmt.close();
1175                pstmt = con.prepareStatement(ADD_FILTER);
1176                for (int i=0; i<filters.length; i++) {
1177                    try {
1178                        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
1179                        ObjectOutputStream out = new ObjectOutputStream(byteOut);
1180                        out.writeObject(filters[i]);
1181                        pstmt.setInt(1,id);
1182                        pstmt.setInt(2,i);
1183                        pstmt.setBytes(3,byteOut.toByteArray());
1184                        pstmt.execute();
1185                    }
1186                    catch (Exception JavaDoc e) {
1187                        abort = true;
1188                        e.printStackTrace();
1189                    }
1190                }
1191                pstmt.close();
1192            }
1193            catch( SQLException sqle ) {
1194                abort = true;
1195                sqle.printStackTrace();
1196            }
1197            finally {
1198                try {
1199                    if (supportsTransactions) {
1200                        if (abort == true) {
1201                            con.rollback();
1202                        }
1203                        else {
1204                            con.commit();
1205                        }
1206                    }
1207                }
1208                catch (Exception JavaDoc e) { e.printStackTrace(); }
1209                try {
1210                    if (supportsTransactions) {
1211                        con.setAutoCommit(true);
1212                    }
1213                }
1214                catch (Exception JavaDoc e) { e.printStackTrace(); }
1215                try { con.close(); }
1216                catch (Exception JavaDoc e) { e.printStackTrace(); }
1217            }
1218        }
1219    }
1220
1221    /**
1222     * Loads forum data from the database.
1223     */

1224    private void loadFromDb() throws ForumNotFoundException {
1225        Connection con = null;
1226        PreparedStatement pstmt = null;
1227        try {
1228            con = DbConnectionManager.getConnection();
1229            //See if we should load by forumID or by name
1230
if (id == -1) {
1231                pstmt = con.prepareStatement(LOAD_FORUM_BY_NAME);
1232                pstmt.setString(1,name);
1233            }
1234            else {
1235                pstmt = con.prepareStatement(LOAD_FORUM_BY_ID);
1236                pstmt.setInt(1, id);
1237            }
1238            ResultSet rs = pstmt.executeQuery();
1239            if( !rs.next() ) {
1240                throw new ForumNotFoundException("Forum " + getID() +
1241                    " could not be loaded from the database.");
1242            }
1243            id = rs.getInt("forumID");
1244            name = rs.getString("name");
1245            description = rs.getString("description");
1246            this.creationDate =
1247                new java.util.Date JavaDoc(Long.parseLong(rs.getString("creationDate").trim()));
1248            this.modifiedDate =
1249                new java.util.Date JavaDoc(Long.parseLong(rs.getString("modifiedDate").trim()));
1250            moderated = rs.getInt("moderated")==1 ? true : false;
1251            isarticle = rs.getInt("article")==1 ? true : false;
1252            order = rs.getInt("forumorder");
1253        }
1254        catch( SQLException sqle ) {
1255            sqle.printStackTrace();
1256            throw new ForumNotFoundException("Forum " + getID() +
1257                " could not be loaded from the database.");
1258        }
1259        catch (NumberFormatException JavaDoc nfe) {
1260            System.err.println("WARNING: In DbForum.loadFromDb() -- there " +
1261                "was an error parsing the dates returned from the database. Ensure " +
1262                "that they're being stored correctly.");
1263        }
1264        finally {
1265            try { pstmt.close(); }
1266            catch (Exception JavaDoc e) { e.printStackTrace(); }
1267            try { con.close(); }
1268            catch (Exception JavaDoc e) { e.printStackTrace(); }
1269        }
1270    }
1271
1272    /**
1273     * Inserts a new record into the database.
1274     */

1275    private void insertIntoDb() {
1276        Connection con = null;
1277        PreparedStatement pstmt = null;
1278        try {
1279            con = DbConnectionManager.getConnection();
1280            pstmt = con.prepareStatement(ADD_FORUM);
1281            pstmt.setInt(1,id);
1282            pstmt.setString(2,name);
1283            pstmt.setString(3,description);
1284            pstmt.setString(4, Long.toString(creationDate.getTime()));
1285            pstmt.setString(5, Long.toString(modifiedDate.getTime()));
1286            pstmt.setInt(6, moderated?1:0);
1287            pstmt.setInt(7, forumGroupID);
1288            pstmt.setInt(8,isarticle?1:0);
1289            pstmt.executeUpdate();
1290        }
1291        catch( SQLException sqle ) {
1292            System.err.println("Error in DbForum:insertIntoDb()-" + sqle);
1293            sqle.printStackTrace();
1294        }
1295        finally {
1296            try { pstmt.close(); }
1297            catch (Exception JavaDoc e) { e.printStackTrace(); }
1298            try { con.close(); }
1299            catch (Exception JavaDoc e) { e.printStackTrace(); }
1300        }
1301    }
1302
1303    /**
1304     * Saves forum data to the database.
1305     */

1306    private synchronized void saveToDb() {
1307        Connection con = null;
1308        PreparedStatement pstmt = null;
1309        try {
1310            con = DbConnectionManager.getConnection();
1311            pstmt = con.prepareStatement(SAVE_FORUM);
1312            pstmt.setString(1, name);
1313            pstmt.setString(2, description);
1314            pstmt.setString(3, Long.toString(creationDate.getTime()));
1315            pstmt.setString(4, Long.toString(modifiedDate.getTime()));
1316            pstmt.setInt(5, moderated?1:0);
1317            pstmt.setInt(6,order);
1318            pstmt.setInt(7, id);
1319            pstmt.executeUpdate();
1320        }
1321        catch( SQLException sqle ) {
1322            System.err.println("Error in DbForum:saveToDb()-" + sqle);
1323            sqle.printStackTrace();
1324        }
1325        finally {
1326            try { pstmt.close(); }
1327            catch (Exception JavaDoc e) { e.printStackTrace(); }
1328            try { con.close(); }
1329            catch (Exception JavaDoc e) { e.printStackTrace(); }
1330        }
1331    }
1332    public boolean isArticleForum(){
1333        return this.isarticle;
1334    }
1335    public void addArticleMap(String JavaDoc pageKey,ForumThread thread) throws UnauthorizedException{
1336        Connection con = null;
1337        PreparedStatement pstmt = null;
1338        try {
1339            con = DbConnectionManager.getConnection();
1340            pstmt = con.prepareStatement(INSERT_ARTICLE_MAP);
1341            pstmt.setInt(1, thread.getID());
1342            pstmt.setString(2, pageKey);
1343            pstmt.setInt(3, id);
1344            pstmt.executeUpdate();
1345        }
1346        catch( SQLException sqle ) {
1347            sqle.printStackTrace();
1348        }
1349        finally {
1350            try { pstmt.close(); }
1351            catch (Exception JavaDoc e) { e.printStackTrace(); }
1352            try { con.close(); }
1353            catch (Exception JavaDoc e) { e.printStackTrace(); }
1354        }
1355
1356    }
1357    public int forumOrder(){
1358        return order;
1359    }
1360    public void setForumOrder(int param) throws UnauthorizedException{
1361        this.order=param;
1362        saveToDb();
1363    }
1364
1365}
1366
Popular Tags