0001: /**
0002: * Copyright (C) 2001 Yasna.com. All rights reserved.
0003: *
0004: * ===================================================================
0005: * The Apache Software License, Version 1.1
0006: *
0007: * Redistribution and use in source and binary forms, with or without
0008: * modification, are permitted provided that the following conditions
0009: * are met:
0010: *
0011: * 1. Redistributions of source code must retain the above copyright
0012: * notice, this list of conditions and the following disclaimer.
0013: *
0014: * 2. Redistributions in binary form must reproduce the above copyright
0015: * notice, this list of conditions and the following disclaimer in
0016: * the documentation and/or other materials provided with the
0017: * distribution.
0018: *
0019: * 3. The end-user documentation included with the redistribution,
0020: * if any, must include the following acknowledgment:
0021: * "This product includes software developed by
0022: * Yasna.com (http://www.yasna.com)."
0023: * Alternately, this acknowledgment may appear in the software itself,
0024: * if and wherever such third-party acknowledgments normally appear.
0025: *
0026: * 4. The names "Yazd" and "Yasna.com" must not be used to
0027: * endorse or promote products derived from this software without
0028: * prior written permission. For written permission, please
0029: * contact yazd@yasna.com.
0030: *
0031: * 5. Products derived from this software may not be called "Yazd",
0032: * nor may "Yazd" appear in their name, without prior written
0033: * permission of Yasna.com.
0034: *
0035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
0036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0038: * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
0039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
0042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0046: * SUCH DAMAGE.
0047: * ====================================================================
0048: *
0049: * This software consists of voluntary contributions made by many
0050: * individuals on behalf of Yasna.com. For more information
0051: * on Yasna.com, please see <http://www.yasna.com>.
0052: */
0053:
0054: /**
0055: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
0056: *
0057: * ===================================================================
0058: * The Apache Software License, Version 1.1
0059: *
0060: * Redistribution and use in source and binary forms, with or without
0061: * modification, are permitted provided that the following conditions
0062: * are met:
0063: *
0064: * 1. Redistributions of source code must retain the above copyright
0065: * notice, this list of conditions and the following disclaimer.
0066: *
0067: * 2. Redistributions in binary form must reproduce the above copyright
0068: * notice, this list of conditions and the following disclaimer in
0069: * the documentation and/or other materials provided with the
0070: * distribution.
0071: *
0072: * 3. The end-user documentation included with the redistribution,
0073: * if any, must include the following acknowledgment:
0074: * "This product includes software developed by
0075: * CoolServlets.com (http://www.coolservlets.com)."
0076: * Alternately, this acknowledgment may appear in the software itself,
0077: * if and wherever such third-party acknowledgments normally appear.
0078: *
0079: * 4. The names "Jive" and "CoolServlets.com" must not be used to
0080: * endorse or promote products derived from this software without
0081: * prior written permission. For written permission, please
0082: * contact webmaster@coolservlets.com.
0083: *
0084: * 5. Products derived from this software may not be called "Jive",
0085: * nor may "Jive" appear in their name, without prior written
0086: * permission of CoolServlets.com.
0087: *
0088: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
0089: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0090: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0091: * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
0092: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0093: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0094: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
0095: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0096: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0097: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0098: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0099: * SUCH DAMAGE.
0100: * ====================================================================
0101: *
0102: * This software consists of voluntary contributions made by many
0103: * individuals on behalf of CoolServlets.com. For more information
0104: * on CoolServlets.com, please see <http://www.coolservlets.com>.
0105: */package com.Yasna.forum.database;
0106:
0107: import java.util.Iterator;
0108: import java.util.Enumeration;
0109: import java.util.Properties;
0110: import java.util.ArrayList;
0111: import java.util.Date;
0112: import java.sql.*;
0113: import java.io.*;
0114:
0115: import com.Yasna.forum.*;
0116: import com.Yasna.forum.Exceptions.RapidPostingException;
0117: import com.Yasna.forum.Exceptions.UserBlackListedException;
0118: import com.Yasna.forum.util.ClientIP;
0119: import com.Yasna.forum.filter.*;
0120: import com.Yasna.util.Cache;
0121: import com.Yasna.util.Cacheable;
0122: import com.Yasna.util.CacheSizes;
0123:
0124: /**
0125: * Database implementation of the Forum interface. It loads and stores forum
0126: * information from a a database.
0127: *
0128: * @see Forum
0129: */
0130: public class DbForum implements Forum, Cacheable {
0131:
0132: /** DATABASE QUERIES **/
0133: private static final String ADD_THREAD = "UPDATE yazdThread set forumID=? WHERE threadID=?";
0134: protected static final String DELETE_THREAD = "DELETE FROM yazdThread WHERE threadID=?";
0135: private static final String THREAD_COUNT = "SELECT count(*) FROM yazdThread WHERE forumID=?";
0136: private static final String MESSAGE_COUNT = "SELECT count(*) FROM yazdThread, yazdMessage WHERE "
0137: + "yazdThread.forumID=? AND yazdThread.threadID=yazdMessage.threadID";
0138: private static final String ADD_USER_PERM = "INSERT INTO yazdUserPerm(forumID,userID,permission) VALUES(?,?,?)";
0139: private static final String REMOVE_USER_PERM = "DELETE FROM yazdUserPerm WHERE forumID=? AND userID=? AND permission=?";
0140: private static final String USERS_WITH_PERM = "SELECT DISTINCT userID FROM yazdUserPerm WHERE forumID=? AND permission=?";
0141: private static final String ADD_GROUP_PERM = "INSERT INTO yazdGroupPerm(forumID,groupID,permission) VALUES(?,?,?)";
0142: private static final String REMOVE_GROUP_PERM = "DELETE FROM yazdGroupPerm WHERE forumID=? AND groupID=? AND permission=?";
0143: private static final String GROUPS_WITH_PERM = "SELECT DISTINCT groupID FROM yazdGroupPerm WHERE forumID=? AND permission=?";
0144: private static final String LOAD_FILTERS = "SELECT filterObject, filterIndex FROM yazdFilter WHERE forumID=? ORDER BY filterIndex ASC";
0145: private static final String DELETE_FILTERS = "DELETE FROM yazdFilter WHERE forumID=?";
0146: private static final String ADD_FILTER = "INSERT INTO yazdFilter(forumID,filterIndex,filterObject) VALUES(?,?,?)";
0147: private static final String LOAD_PROPERTIES = "SELECT name, propValue FROM yazdForumProp WHERE forumID=?";
0148: private static final String DELETE_PROPERTIES = "DELETE FROM yazdForumProp WHERE forumID=?";
0149: private static final String INSERT_PROPERTY = "INSERT INTO yazdForumProp(forumID,name,propValue) VALUES(?,?,?)";
0150: private static final String LOAD_FORUM_BY_ID = "SELECT forumID, name, description, creationDate, modifiedDate, moderated, article,forumorder FROM yazdForum WHERE forumID=?";
0151: private static final String LOAD_FORUM_BY_NAME = "SELECT forumID, name, description, creationDate, modifiedDate, moderated, article,forumorder FROM yazdForum WHERE name=?";
0152: private static final String ADD_FORUM = "INSERT INTO yazdForum(forumID, name, description, creationDate, "
0153: + "modifiedDate, moderated, forumGroupID,article,forumorder) VALUES (?,?,?,?,?,?,?,?,0)";
0154: private static final String SAVE_FORUM = "UPDATE yazdForum SET name=?, description=?, creationDate=?, "
0155: + "modifiedDate=?, moderated=?,forumorder=? WHERE forumID=?";
0156: private static final String UPDATE_FORUM_MODIFIED_DATE = "UPDATE yazdForum SET modifiedDate=? WHERE forumID=?";
0157: private static final String INSERT_ARTICLE_MAP = "insert into yazdArticleMap(threadID,pageKey,forumID) values(?,?,?)";
0158:
0159: private int id = -1;
0160: private String name;
0161: private String description;
0162: private int forumGroupID;
0163: private java.util.Date creationDate;
0164: private java.util.Date modifiedDate;
0165: private boolean moderated;
0166: private boolean isarticle = false;
0167: private ForumMessageFilter[] filters;
0168: private Properties properties;
0169: //Lock for saving state to database.
0170: private Object saveLock = new Object();
0171: private int order;
0172:
0173: private DbForumFactory factory;
0174:
0175: /**
0176: * Creates a new forum with the specified name and description.
0177: *
0178: * @param name the name of the forum.
0179: * @param description the description of the forum.
0180: * @param moderated when true - posted messages and threads must first be approved
0181: * @param forumGroupID every forum belongs to a Category and ForumGroup
0182: * @param factory the DbForumFactory the forum is a part of.
0183: */
0184: protected DbForum(String name, String description,
0185: boolean moderated, int forumGroupID,
0186: DbForumFactory factory, boolean article) {
0187: this .id = DbSequenceManager.nextID("Forum");
0188: this .name = name;
0189: this .description = description;
0190: this .moderated = moderated;
0191: this .forumGroupID = forumGroupID;
0192: long now = System.currentTimeMillis();
0193: creationDate = new java.util.Date(now);
0194: modifiedDate = new java.util.Date(now);
0195: this .factory = factory;
0196: this .isarticle = article;
0197: insertIntoDb();
0198: properties = new Properties();
0199: //Forums should start with an html filter by default for
0200: //security purposes.
0201: filters = new ForumMessageFilter[2];
0202: filters[0] = new FilterHtml();
0203: filters[1] = new FilterNewline();
0204: saveFiltersToDb();
0205: //**Commenting out below since it doesn't seem to work for some reason.
0206: //try {
0207: // addForumMessageFilter(new FilterHtml(), 0);
0208: // addForumMessageFilter(new FilterNewline(), 1);
0209: //}
0210: //catch (UnauthorizedException ue) {
0211: // ue.printStackTrace();
0212: //}
0213: }
0214:
0215: /**
0216: * Loads a forum with the specified id.
0217: */
0218: protected DbForum(int id, DbForumFactory factory)
0219: throws ForumNotFoundException {
0220: this .id = id;
0221: this .factory = factory;
0222: loadFromDb();
0223: loadFiltersFromDb();
0224: loadProperties();
0225: }
0226:
0227: /**
0228: * Loads a forum with the specified name.
0229: */
0230: protected DbForum(String name, DbForumFactory factory)
0231: throws ForumNotFoundException {
0232: this .name = name;
0233: this .factory = factory;
0234: loadFromDb();
0235: loadFiltersFromDb();
0236: loadProperties();
0237: }
0238:
0239: //FROM THE FORUM INTERFACE//
0240:
0241: public int getID() {
0242: return id;
0243: }
0244:
0245: public String getName() {
0246: return name;
0247: }
0248:
0249: public void setName(String name) throws UnauthorizedException {
0250: this .name = name;
0251: saveToDb();
0252: }
0253:
0254: public String getDescription() {
0255: return description;
0256: }
0257:
0258: public void setDescription(String description)
0259: throws UnauthorizedException {
0260: this .description = description;
0261: saveToDb();
0262: }
0263:
0264: public java.util.Date getCreationDate() {
0265: return creationDate;
0266: }
0267:
0268: public void setCreationDate(java.util.Date creationDate)
0269: throws UnauthorizedException {
0270: this .creationDate = creationDate;
0271: saveToDb();
0272: }
0273:
0274: public java.util.Date getModifiedDate() {
0275: return modifiedDate;
0276: }
0277:
0278: public void setModifiedDate(java.util.Date modifiedDate)
0279: throws UnauthorizedException {
0280: this .modifiedDate = modifiedDate;
0281: saveToDb();
0282: }
0283:
0284: public String getProperty(String name) {
0285: return (String) properties.get(name);
0286: }
0287:
0288: public void setProperty(String name, String value)
0289: throws UnauthorizedException {
0290: properties.put(name, value);
0291: saveProperties();
0292: }
0293:
0294: public Enumeration propertyNames() {
0295: return properties.keys();
0296: }
0297:
0298: public boolean isModerated() {
0299: return moderated;
0300: }
0301:
0302: public void setModerated(boolean moderated)
0303: throws UnauthorizedException {
0304: this .moderated = moderated;
0305: saveToDb();
0306: }
0307:
0308: public ForumThread createThread(ForumMessage rootMessage,
0309: ThreadType typeid) throws UnauthorizedException {
0310: //If the forum is moderated, the thread is not automatically
0311: //approved.
0312: boolean approved = !isModerated();
0313: return new DbForumThread(rootMessage, approved, this , factory,
0314: typeid);
0315: }
0316:
0317: public ForumMessage createMessage(User user, ClientIP clientIP)
0318: throws UnauthorizedException, RapidPostingException,
0319: UserBlackListedException {
0320: //If the forum is moderated, the message is not automatically
0321: //approved.
0322: boolean approved = !isModerated();
0323: ForumMessage message = new DbForumMessage(user, factory,
0324: approved, clientIP);
0325: return message;
0326: }
0327:
0328: public ForumMessage createDummyMessage(User user)
0329: throws UnauthorizedException {
0330: return new DbForumMessage(user, factory);
0331: }
0332:
0333: public void addThread(ForumThread thread)
0334: throws UnauthorizedException {
0335: boolean abortTransaction = false;
0336: boolean supportsTransactions = false;
0337: //Add message to db
0338: Connection con = null;
0339: PreparedStatement pstmt = null;
0340: try {
0341: con = DbConnectionManager.getConnection();
0342: supportsTransactions = con.getMetaData()
0343: .supportsTransactions();
0344: if (supportsTransactions) {
0345: con.setAutoCommit(false);
0346: }
0347:
0348: pstmt = con.prepareStatement(ADD_THREAD);
0349: pstmt.setInt(1, id);
0350: pstmt.setInt(2, thread.getID());
0351: pstmt.executeUpdate();
0352: pstmt.close();
0353:
0354: //Now, insert the thread into the database.
0355: ((ForumThreadProxy) thread).insertIntoDb(con);
0356: } catch (Exception e) {
0357: e.printStackTrace();
0358: abortTransaction = true;
0359: return;
0360: } finally {
0361: try {
0362: if (supportsTransactions) {
0363: if (abortTransaction == true) {
0364: con.rollback();
0365: } else {
0366: con.commit();
0367: }
0368: }
0369: } catch (Exception e) {
0370: e.printStackTrace();
0371: }
0372: try {
0373: if (supportsTransactions) {
0374: con.setAutoCommit(true);
0375: }
0376: con.close();
0377: } catch (Exception e) {
0378: e.printStackTrace();
0379: }
0380: }
0381:
0382: //Since we added a thread, update the modified date of this thread.
0383: updateModifiedDate(thread.getModifiedDate());
0384: //we add the subscription to the thread if necessary also
0385: User newUser = thread.getRootMessage().getUser();
0386: if (newUser.getThreadSubscribe() && newUser.getID() > 1) {
0387: newUser.setProperty("WatchThread" + thread.getID(), "true");
0388: }
0389:
0390: }
0391:
0392: public ForumThread getThread(int threadID)
0393: throws ForumThreadNotFoundException {
0394: return factory.getThread(threadID, this );
0395: }
0396:
0397: public void deleteThread(ForumThread thread)
0398: throws UnauthorizedException {
0399: //Delete all messages from the thread. Deleting the root
0400: //message will delete all submessages.
0401: ForumMessage message = thread.getRootMessage();
0402: thread.deleteMessage(message);
0403: }
0404:
0405: protected void deleteThreadRecord(int threadID) {
0406:
0407: //Delete the actual thread
0408: Connection con = null;
0409: PreparedStatement pstmt = null;
0410: try {
0411: con = DbConnectionManager.getConnection();
0412: pstmt = con.prepareStatement(DELETE_THREAD);
0413: pstmt.setInt(1, threadID);
0414: pstmt.execute();
0415: } catch (Exception sqle) {
0416: System.err.println("Error in DbForum:deleteThread()-"
0417: + sqle);
0418: } finally {
0419: try {
0420: pstmt.close();
0421: } catch (Exception e) {
0422: e.printStackTrace();
0423: }
0424: try {
0425: con.close();
0426: } catch (Exception e) {
0427: e.printStackTrace();
0428: }
0429: }
0430:
0431: //Now, delete from cache
0432: Integer threadIDInteger = new Integer(threadID);
0433: factory.getCacheManager().remove(DbCacheManager.THREAD_CACHE,
0434: threadIDInteger);
0435: }
0436:
0437: public void moveThread(ForumThread thread, Forum forum)
0438: throws UnauthorizedException {
0439: //Ensure that thread belongs to this forum
0440: if (thread.getForum().getID() != this .id) {
0441: throw new IllegalArgumentException(
0442: "The thread does not belong to this forum.");
0443: }
0444: //Ensure that thread is not in the same forum
0445: if (thread.getForum().getID() == forum.getID()) {
0446: throw new IllegalArgumentException(
0447: "The thread is already in this forum.");
0448: }
0449:
0450: //Modify the SQL record. Only the thread table has information about
0451: //forumID, so we only need to modify that record. The message records
0452: //underneath the thread can be left alone.
0453: Connection con = null;
0454: PreparedStatement pstmt = null;
0455: try {
0456: con = DbConnectionManager.getConnection();
0457: pstmt = con.prepareStatement(ADD_THREAD);
0458: pstmt.setInt(1, forum.getID());
0459: pstmt.setInt(2, thread.getID());
0460: pstmt.executeUpdate();
0461: pstmt.close();
0462: } catch (SQLException sqle) {
0463: System.err.println("Error in DbForum:addThread()-" + sqle);
0464: return;
0465: } finally {
0466: try {
0467: pstmt.close();
0468: } catch (Exception e) {
0469: e.printStackTrace();
0470: }
0471: try {
0472: con.close();
0473: } catch (Exception e) {
0474: e.printStackTrace();
0475: }
0476: }
0477:
0478: DbCacheManager cacheManager = factory.getCacheManager();
0479: SearchIndexer indexer = factory.getSearchIndexer();
0480:
0481: //Remove both forums from cache.
0482: Integer key = new Integer(this .id);
0483: cacheManager.remove(DbCacheManager.FORUM_CACHE, key);
0484: key = new Integer(forum.getID());
0485: cacheManager.remove(DbCacheManager.FORUM_CACHE, key);
0486:
0487: //Remove thread from cache.
0488: key = new Integer(thread.getID());
0489: cacheManager.remove(DbCacheManager.THREAD_CACHE, key);
0490:
0491: //Loop through all messages in thread
0492: Iterator messages = thread.messages();
0493: while (messages.hasNext()) {
0494: ForumMessage message = (ForumMessage) messages.next();
0495: //Remove each message from cache.
0496: key = new Integer(message.getID());
0497: cacheManager.remove(DbCacheManager.MESSAGE_CACHE, key);
0498: //Remove and re-add every message to the search index.
0499: indexer.removeFromIndex(message);
0500: indexer.addToIndex(message);
0501: }
0502:
0503: // Update the modified date of thread
0504: Date now = new Date();
0505: thread.setModifiedDate(now);
0506: // Update the modified date of forum thread is now in
0507: forum.setModifiedDate(now);
0508: }
0509:
0510: public Iterator threads() {
0511: return new DbForumIterator(this , factory);
0512: }
0513:
0514: public Iterator threads(int startIndex, int numResults, int sortBy) {
0515: return new DbForumIterator(this , factory, startIndex,
0516: numResults, sortBy);
0517: }
0518:
0519: public int getThreadCount() {
0520: int threadCount = 0;
0521: // Based on the id in the object, get the thread data from the database:
0522: Connection con = null;
0523: PreparedStatement pstmt = null;
0524: try {
0525: con = DbConnectionManager.getConnection();
0526: pstmt = con.prepareStatement(THREAD_COUNT);
0527: pstmt.setInt(1, id);
0528: ResultSet rs = pstmt.executeQuery();
0529: rs.next();
0530: threadCount = rs.getInt(1 /*"threadCount"*/);
0531: } catch (SQLException sqle) {
0532: System.err.println("DbForum:getThreadCount() failed: "
0533: + sqle);
0534: } finally {
0535: try {
0536: pstmt.close();
0537: } catch (Exception e) {
0538: e.printStackTrace();
0539: }
0540: try {
0541: con.close();
0542: } catch (Exception e) {
0543: e.printStackTrace();
0544: }
0545: }
0546: return threadCount;
0547: }
0548:
0549: public int getMessageCount() {
0550: int messageCount = 0;
0551: Connection con = null;
0552: PreparedStatement pstmt = null;
0553: try {
0554: con = DbConnectionManager.getConnection();
0555: pstmt = con.prepareStatement(MESSAGE_COUNT);
0556: pstmt.setInt(1, id);
0557: ResultSet rs = pstmt.executeQuery();
0558: rs.next();
0559: messageCount = rs.getInt(1 /*"messageCount"*/);
0560: } catch (SQLException sqle) {
0561: System.err.println("DbForum:getMessageCount() failed: "
0562: + sqle);
0563: } finally {
0564: try {
0565: pstmt.close();
0566: } catch (Exception e) {
0567: e.printStackTrace();
0568: }
0569: try {
0570: con.close();
0571: } catch (Exception e) {
0572: e.printStackTrace();
0573: }
0574: }
0575: return messageCount;
0576: }
0577:
0578: public Query createQuery() {
0579: return new DbQuery(this , factory);
0580: }
0581:
0582: public void addUserPermission(User user, int permissionType)
0583: throws UnauthorizedException {
0584: Connection con = null;
0585: PreparedStatement pstmt = null;
0586: try {
0587: con = DbConnectionManager.getConnection();
0588: pstmt = con.prepareStatement(ADD_USER_PERM);
0589: pstmt.setInt(1, id);
0590: pstmt.setInt(2, user.getID());
0591: pstmt.setInt(3, permissionType);
0592: pstmt.execute();
0593: //Remove user permissions from cache since they've changed.
0594: factory.getCacheManager().removeUserPerm(
0595: new Integer(user.getID()), new Integer(id));
0596: } catch (SQLException sqle) {
0597: System.err.println("Error in DbForum.java:" + sqle);
0598: sqle.printStackTrace();
0599: } finally {
0600: try {
0601: pstmt.close();
0602: } catch (Exception e) {
0603: e.printStackTrace();
0604: }
0605: try {
0606: con.close();
0607: } catch (Exception e) {
0608: e.printStackTrace();
0609: }
0610: }
0611: }
0612:
0613: public void removeUserPermission(User user, int permissionType)
0614: throws UnauthorizedException {
0615: Connection con = null;
0616: PreparedStatement pstmt = null;
0617: try {
0618: con = DbConnectionManager.getConnection();
0619: pstmt = con.prepareStatement(REMOVE_USER_PERM);
0620: pstmt.setInt(1, id);
0621: pstmt.setInt(2, user.getID());
0622: pstmt.setInt(3, permissionType);
0623: pstmt.execute();
0624: //Remove user permissions from cache since they've changed.
0625: factory.getCacheManager().removeUserPerm(
0626: new Integer(user.getID()), new Integer(id));
0627: } catch (SQLException sqle) {
0628: System.err.println("Error in DbForum.java:" + sqle);
0629: sqle.printStackTrace();
0630: } finally {
0631: try {
0632: pstmt.close();
0633: } catch (Exception e) {
0634: e.printStackTrace();
0635: }
0636: try {
0637: con.close();
0638: } catch (Exception e) {
0639: e.printStackTrace();
0640: }
0641: }
0642: }
0643:
0644: public int[] usersWithPermission(int permissionType)
0645: throws UnauthorizedException {
0646: int[] users = new int[0];
0647: Connection con = null;
0648: PreparedStatement pstmt = null;
0649: try {
0650: con = DbConnectionManager.getConnection();
0651: pstmt = con.prepareStatement(USERS_WITH_PERM);
0652: pstmt.setInt(1, id);
0653: pstmt.setInt(2, permissionType);
0654: ResultSet rs = pstmt.executeQuery();
0655: ArrayList userList = new ArrayList();
0656: while (rs.next()) {
0657: userList.add(new Integer(rs.getInt("userID")));
0658: }
0659: users = new int[userList.size()];
0660: for (int i = 0; i < users.length; i++) {
0661: users[i] = ((Integer) userList.get(i)).intValue();
0662: }
0663: } catch (SQLException sqle) {
0664: System.err.println("Error in DbForum.java:" + sqle);
0665: sqle.printStackTrace();
0666: } finally {
0667: try {
0668: pstmt.close();
0669: } catch (Exception e) {
0670: e.printStackTrace();
0671: }
0672: try {
0673: con.close();
0674: } catch (Exception e) {
0675: e.printStackTrace();
0676: }
0677: }
0678: return users;
0679: }
0680:
0681: public void addGroupPermission(Group group, int permissionType)
0682: throws UnauthorizedException {
0683: Connection con = null;
0684: PreparedStatement pstmt = null;
0685: try {
0686: con = DbConnectionManager.getConnection();
0687: pstmt = con.prepareStatement(ADD_GROUP_PERM);
0688: pstmt.setInt(1, id);
0689: pstmt.setInt(2, group.getID());
0690: pstmt.setInt(3, permissionType);
0691: pstmt.execute();
0692: //Remove user permissions from cache since they've changed. Because
0693: //of the way that user perm cache is handled, it is easiest to
0694: //simply remove all the user perm cache for the forum. This is ok
0695: //since happens infrequently.
0696: factory.getCacheManager().remove(
0697: DbCacheManager.USER_PERMS_CACHE, new Integer(id));
0698: } catch (SQLException sqle) {
0699: System.err.println("Error in DbForum.java:" + sqle);
0700: sqle.printStackTrace();
0701: } finally {
0702: try {
0703: pstmt.close();
0704: } catch (Exception e) {
0705: e.printStackTrace();
0706: }
0707: try {
0708: con.close();
0709: } catch (Exception e) {
0710: e.printStackTrace();
0711: }
0712: }
0713: }
0714:
0715: public void removeGroupPermission(Group group, int permissionType)
0716: throws UnauthorizedException {
0717: Connection con = null;
0718: PreparedStatement pstmt = null;
0719: try {
0720: con = DbConnectionManager.getConnection();
0721: pstmt = con.prepareStatement(REMOVE_GROUP_PERM);
0722: pstmt.setInt(1, id);
0723: pstmt.setInt(2, group.getID());
0724: pstmt.setInt(3, permissionType);
0725: pstmt.execute();
0726: //Remove user permissions from cache since they've changed. Because
0727: //of the way that user perm cache is handled, it is easiest to
0728: //simply remove all the user perm cache for the forum. This is ok
0729: //since happens infrequently.
0730: factory.getCacheManager().remove(
0731: DbCacheManager.USER_PERMS_CACHE, new Integer(id));
0732: } catch (SQLException sqle) {
0733: System.err.println("Error in DbForum.java:" + sqle);
0734: sqle.printStackTrace();
0735: } finally {
0736: try {
0737: pstmt.close();
0738: } catch (Exception e) {
0739: e.printStackTrace();
0740: }
0741: try {
0742: con.close();
0743: } catch (Exception e) {
0744: e.printStackTrace();
0745: }
0746: }
0747: }
0748:
0749: public int[] groupsWithPermission(int permissionType)
0750: throws UnauthorizedException {
0751: int[] groups = new int[0];
0752: Connection con = null;
0753: PreparedStatement pstmt = null;
0754: try {
0755: con = DbConnectionManager.getConnection();
0756: pstmt = con.prepareStatement(GROUPS_WITH_PERM);
0757: pstmt.setInt(1, id);
0758: pstmt.setInt(2, permissionType);
0759: ResultSet rs = pstmt.executeQuery();
0760: ArrayList groupList = new ArrayList();
0761: while (rs.next()) {
0762: groupList.add(new Integer(rs.getInt("groupID")));
0763: }
0764: groups = new int[groupList.size()];
0765: for (int i = 0; i < groups.length; i++) {
0766: groups[i] = ((Integer) groupList.get(i)).intValue();
0767: }
0768: } catch (SQLException sqle) {
0769: System.err.println("Error in DbForum.groupsWithPermission:"
0770: + sqle);
0771: sqle.printStackTrace();
0772: } finally {
0773: try {
0774: pstmt.close();
0775: } catch (Exception e) {
0776: e.printStackTrace();
0777: }
0778: try {
0779: con.close();
0780: } catch (Exception e) {
0781: e.printStackTrace();
0782: }
0783: }
0784: return groups;
0785: }
0786:
0787: public ForumMessage applyFilters(ForumMessage message) {
0788: //Loop through filters and apply them
0789: for (int i = 0; i < filters.length; i++) {
0790: message = filters[i].clone(message);
0791: }
0792: return message;
0793: }
0794:
0795: public ForumMessageFilter[] getForumMessageFilters()
0796: throws UnauthorizedException {
0797: ForumMessageFilter[] dbFilters = new ForumMessageFilter[filters.length];
0798: for (int i = 0; i < filters.length; i++) {
0799: dbFilters[i] = new DbForumMessageFilter(
0800: (ForumMessage) filters[i], this );
0801: }
0802: return dbFilters;
0803: }
0804:
0805: public void addForumMessageFilter(ForumMessageFilter filter)
0806: throws UnauthorizedException {
0807: ArrayList newFilters = new ArrayList(filters.length + 1);
0808: for (int i = 0; i < filters.length; i++) {
0809: newFilters.add(filters[i]);
0810: }
0811: newFilters.add(filter);
0812: ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters
0813: .size()];
0814: for (int i = 0; i < newArray.length; i++) {
0815: newArray[i] = (ForumMessageFilter) newFilters.get(i);
0816: }
0817: //Finally, overwrite filters with the new array
0818: filters = newArray;
0819: saveFiltersToDb();
0820: }
0821:
0822: public void addForumMessageFilter(ForumMessageFilter filter,
0823: int index) throws UnauthorizedException {
0824: ArrayList newFilters = new ArrayList(filters.length + 1);
0825: for (int i = 0; i < filters.length; i++) {
0826: newFilters.add(filters[i]);
0827: }
0828: newFilters.add(index, filter);
0829: ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters
0830: .size()];
0831: for (int i = 0; i < newArray.length; i++) {
0832: newArray[i] = (ForumMessageFilter) newFilters.get(i);
0833: }
0834: //Finally, overwrite filters with the new array
0835: filters = newArray;
0836: saveFiltersToDb();
0837: }
0838:
0839: public void removeForumMessageFilter(int index)
0840: throws UnauthorizedException {
0841: ArrayList newFilters = new ArrayList(filters.length);
0842: for (int i = 0; i < filters.length; i++) {
0843: newFilters.add(filters[i]);
0844: }
0845: newFilters.remove(index);
0846: ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters
0847: .size()];
0848: for (int i = 0; i < newArray.length; i++) {
0849: newArray[i] = (ForumMessageFilter) newFilters.get(i);
0850: }
0851: //Finally, overwrite filters with the new array
0852: filters = newArray;
0853: saveFiltersToDb();
0854: }
0855:
0856: public ForumPermissions getPermissions(Authorization authorization) {
0857: int userID = authorization.getUserID();
0858:
0859: //Get the user perm cache for this forum
0860: Cache userPermCache = (Cache) factory.getCacheManager().get(
0861: DbCacheManager.USER_PERMS_CACHE, new Integer(id));
0862:
0863: //Simple case: if cache is turned on and the user is already cached,
0864: //we can simply return the cached permissions.
0865: if (userPermCache != null) {
0866: ForumPermissions permissions = (ForumPermissions) userPermCache
0867: .get(new Integer(userID));
0868: if (permissions != null) {
0869: return permissions;
0870: }
0871: }
0872:
0873: //Not so simple case: cache is not turned on or the user permissions
0874: //have not been cached yet.
0875: boolean isAnonymous = (userID == -1);
0876: boolean isUser = !isAnonymous;
0877:
0878: ForumPermissions finalPermissions = ForumPermissions.none();
0879:
0880: //Step 1 - Get permissions for the User. This includes anonymous
0881: //perms, "special user" perms, and the specific perms for the user.
0882: if (isUser) {
0883: ForumPermissions userPermissions = factory
0884: .getUserPermissions(userID, id);
0885: //Combine permissions
0886: finalPermissions = new ForumPermissions(finalPermissions,
0887: userPermissions);
0888: }
0889: //Add in anonymous perms.
0890: ForumPermissions anonyPermissions = null;
0891: if (userPermCache != null) {
0892: anonyPermissions = (ForumPermissions) userPermCache
0893: .get(new Integer(-1));
0894: }
0895: //Otherwise, do our own lookup.
0896: if (anonyPermissions == null) {
0897: anonyPermissions = factory.getUserPermissions(-1, id);
0898: //Add to cache so it will be there next time.
0899: if (userPermCache != null) {
0900: userPermCache.add(new Integer(-1), anonyPermissions);
0901: }
0902: }
0903: //Combine permissions
0904: finalPermissions = new ForumPermissions(finalPermissions,
0905: anonyPermissions);
0906:
0907: //If they are a valid user, figure out "any user" permissions.
0908: if (isUser) {
0909: ForumPermissions specialUserPermissions = null;
0910: //Check for cache
0911: if (userPermCache != null) {
0912: specialUserPermissions = (ForumPermissions) userPermCache
0913: .get(new Integer(0));
0914: }
0915: //Otherwise, do our own lookup.
0916: if (specialUserPermissions == null) {
0917: specialUserPermissions = factory.getUserPermissions(0,
0918: id);
0919: //Add to cache so it will be there next time.
0920: if (userPermCache != null) {
0921: userPermCache.add(new Integer(0),
0922: specialUserPermissions);
0923: }
0924: }
0925: //Combine permissions
0926: finalPermissions = new ForumPermissions(finalPermissions,
0927: specialUserPermissions);
0928: }
0929:
0930: //Step 2 -- get Permissions for all groups the user is in.
0931: int[] groups = ((DbProfileManager) factory.getProfileManager())
0932: .getUserGroups(userID);
0933: for (int i = 0; i < groups.length; i++) {
0934: ForumPermissions groupPermissions = factory
0935: .getGroupPermissions(groups[i], id);
0936: finalPermissions = new ForumPermissions(finalPermissions,
0937: groupPermissions);
0938: }
0939:
0940: //Finally, add user to cache so it will be there next time.
0941: if (isUser && userPermCache != null) {
0942: userPermCache.add(new Integer(userID), finalPermissions);
0943: }
0944:
0945: return finalPermissions;
0946: }
0947:
0948: public boolean hasPermission(int type) {
0949: return true;
0950: }
0951:
0952: //FROM THE CACHEABLE INTERFACE//
0953:
0954: public int getSize() {
0955: //Approximate the size of the object in bytes by calculating the size
0956: //of each field.
0957: int size = 0;
0958: size += CacheSizes.sizeOfObject(); //overhead of object
0959: size += CacheSizes.sizeOfInt(); //id
0960: size += CacheSizes.sizeOfString(name); //name
0961: size += CacheSizes.sizeOfString(description); //description
0962: size += CacheSizes.sizeOfDate(); //creation date
0963: size += CacheSizes.sizeOfDate(); //modified date
0964: size += CacheSizes.sizeOfBoolean(); //moderated
0965: size += filters.length * 8; //each filter is 8 bytes
0966: size += CacheSizes.sizeOfProperties(properties);//properties object
0967: size += CacheSizes.sizeOfObject(); //save lock
0968: size += CacheSizes.sizeOfBoolean(); //isarticle
0969:
0970: return size;
0971: }
0972:
0973: //OTHER METHODS
0974:
0975: /**
0976: * Returns a String representation of the Forum object using the forum name.
0977: *
0978: * @return a String representation of the Forum object.
0979: */
0980: public String toString() {
0981: return name;
0982: }
0983:
0984: public int hashCode() {
0985: return id;
0986: }
0987:
0988: public boolean equals(Object object) {
0989: if (this == object) {
0990: return true;
0991: }
0992: if (object != null && object instanceof DbForum) {
0993: return id == ((DbForum) object).getID();
0994: } else {
0995: return false;
0996: }
0997: }
0998:
0999: /**
1000: * Updates the modified date but doesn't require a security check since
1001: * it is a protected method.
1002: */
1003: protected void updateModifiedDate(java.util.Date modifiedDate) {
1004: this .modifiedDate = modifiedDate;
1005: Connection con = null;
1006: PreparedStatement pstmt = null;
1007: try {
1008: con = DbConnectionManager.getConnection();
1009: pstmt = con.prepareStatement(UPDATE_FORUM_MODIFIED_DATE);
1010: pstmt.setString(1, "" + modifiedDate.getTime());
1011: pstmt.setInt(2, id);
1012: pstmt.executeUpdate();
1013: } catch (SQLException sqle) {
1014: System.err.println("Error in DbForum:updateModifiedDate()-"
1015: + sqle);
1016: sqle.printStackTrace();
1017: } finally {
1018: try {
1019: pstmt.close();
1020: } catch (Exception e) {
1021: e.printStackTrace();
1022: }
1023: try {
1024: con.close();
1025: } catch (Exception e) {
1026: e.printStackTrace();
1027: }
1028: }
1029: }
1030:
1031: /**
1032: * Loads forum properties from the database.
1033: */
1034: private void loadProperties() {
1035: synchronized (saveLock) {
1036: Properties newProps = new Properties();
1037: Connection con = null;
1038: PreparedStatement pstmt = null;
1039: try {
1040: con = DbConnectionManager.getConnection();
1041: pstmt = con.prepareStatement(LOAD_PROPERTIES);
1042: pstmt.setInt(1, id);
1043: ResultSet rs = pstmt.executeQuery();
1044: while (rs.next()) {
1045: String name = rs.getString("name");
1046: String value = rs.getString("propValue");
1047: newProps.put(name, value);
1048: }
1049: } catch (SQLException sqle) {
1050: System.err.println("Error in DbForum:loadProperties():"
1051: + sqle);
1052: sqle.printStackTrace();
1053: } finally {
1054: try {
1055: pstmt.close();
1056: } catch (Exception e) {
1057: e.printStackTrace();
1058: }
1059: try {
1060: con.close();
1061: } catch (Exception e) {
1062: e.printStackTrace();
1063: }
1064: }
1065: this .properties = newProps;
1066: }
1067: }
1068:
1069: /**
1070: * Saves forum properties to the database.
1071: */
1072: private void saveProperties() {
1073: synchronized (saveLock) {
1074: Connection con = null;
1075: PreparedStatement pstmt = null;
1076: try {
1077: con = DbConnectionManager.getConnection();
1078: //Delete all old values.
1079: pstmt = con.prepareStatement(DELETE_PROPERTIES);
1080: pstmt.setInt(1, id);
1081: pstmt.execute();
1082: pstmt.close();
1083: //Now insert new values.
1084: pstmt = con.prepareStatement(INSERT_PROPERTY);
1085: Enumeration enume = properties.keys();
1086: while (enume.hasMoreElements()) {
1087: String name = (String) enume.nextElement();
1088: String value = (String) properties.get(name);
1089: pstmt.setInt(1, id);
1090: pstmt.setString(2, name);
1091: pstmt.setString(3, value);
1092: pstmt.executeUpdate();
1093: }
1094: } catch (SQLException sqle) {
1095: System.err.println(sqle);
1096: } finally {
1097: try {
1098: pstmt.close();
1099: } catch (Exception e) {
1100: e.printStackTrace();
1101: }
1102: try {
1103: con.close();
1104: } catch (Exception e) {
1105: e.printStackTrace();
1106: }
1107: }
1108: }
1109: }
1110:
1111: /**
1112: * Loads filters from the database.
1113: */
1114: private void loadFiltersFromDb() {
1115: ArrayList newFilters = new ArrayList();
1116: Connection con = null;
1117: boolean abort = false;
1118: boolean supportsTransactions = false;
1119: PreparedStatement pstmt = null;
1120: try {
1121: con = DbConnectionManager.getConnection();
1122: supportsTransactions = con.getMetaData()
1123: .supportsTransactions();
1124: if (supportsTransactions) {
1125: con.setAutoCommit(false);
1126: }
1127:
1128: pstmt = con.prepareStatement(LOAD_FILTERS);
1129: pstmt.setInt(1, id);
1130: ResultSet rs = pstmt.executeQuery();
1131: while (rs.next()) {
1132: try {
1133: ObjectInputStream in = new ObjectInputStream(rs
1134: .getBinaryStream("filterObject"));
1135: newFilters.add(in.readObject());
1136: } catch (ClassCastException cce) {
1137: //ignore for now since the filter might be updated. we
1138: //need a solution for this. probably custom class loading
1139: //of filter classes to protect against failure like this.
1140: } catch (Exception e) {
1141: e.printStackTrace();
1142: }
1143: }
1144: } catch (SQLException sqle) {
1145: sqle.printStackTrace();
1146: } finally {
1147: try {
1148: if (supportsTransactions) {
1149: if (abort == true) {
1150: con.rollback();
1151: } else {
1152: con.commit();
1153: }
1154: }
1155: } catch (Exception e) {
1156: e.printStackTrace();
1157: }
1158: try {
1159: if (supportsTransactions) {
1160: con.setAutoCommit(true);
1161: }
1162: } catch (Exception e) {
1163: e.printStackTrace();
1164: }
1165: try {
1166: pstmt.close();
1167: } catch (Exception e) {
1168: e.printStackTrace();
1169: }
1170: try {
1171: con.close();
1172: } catch (Exception e) {
1173: e.printStackTrace();
1174: }
1175:
1176: }
1177: filters = new ForumMessageFilter[newFilters.size()];
1178: for (int i = 0; i < filters.length; i++) {
1179: filters[i] = (ForumMessageFilter) newFilters.get(i);
1180: }
1181: //Finally, save filters back to Db. Effectively, this deletes filters
1182: //from the database that failed to load. See note above.
1183: //saveFiltersToDb(); <<-- commenting out to try to fix filters bug.
1184: }
1185:
1186: /**
1187: * Saves filters to the database. Filter saving works by serializing
1188: * each filter to a byte stream and then inserting that stream into
1189: * the database.
1190: */
1191: protected void saveFiltersToDb() {
1192: boolean abort = false;
1193: boolean supportsTransactions = false;
1194: synchronized (saveLock) {
1195: Connection con = null;
1196: PreparedStatement pstmt = null;
1197: try {
1198: con = DbConnectionManager.getConnection();
1199:
1200: supportsTransactions = con.getMetaData()
1201: .supportsTransactions();
1202: if (supportsTransactions) {
1203: con.setAutoCommit(false);
1204: }
1205:
1206: pstmt = con.prepareStatement(DELETE_FILTERS);
1207: pstmt.setInt(1, id);
1208: pstmt.execute();
1209: //Now insert new list of filters.
1210: pstmt.close();
1211: pstmt = con.prepareStatement(ADD_FILTER);
1212: for (int i = 0; i < filters.length; i++) {
1213: try {
1214: ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
1215: ObjectOutputStream out = new ObjectOutputStream(
1216: byteOut);
1217: out.writeObject(filters[i]);
1218: pstmt.setInt(1, id);
1219: pstmt.setInt(2, i);
1220: pstmt.setBytes(3, byteOut.toByteArray());
1221: pstmt.execute();
1222: } catch (Exception e) {
1223: abort = true;
1224: e.printStackTrace();
1225: }
1226: }
1227: pstmt.close();
1228: } catch (SQLException sqle) {
1229: abort = true;
1230: sqle.printStackTrace();
1231: } finally {
1232: try {
1233: if (supportsTransactions) {
1234: if (abort == true) {
1235: con.rollback();
1236: } else {
1237: con.commit();
1238: }
1239: }
1240: } catch (Exception e) {
1241: e.printStackTrace();
1242: }
1243: try {
1244: if (supportsTransactions) {
1245: con.setAutoCommit(true);
1246: }
1247: } catch (Exception e) {
1248: e.printStackTrace();
1249: }
1250: try {
1251: con.close();
1252: } catch (Exception e) {
1253: e.printStackTrace();
1254: }
1255: }
1256: }
1257: }
1258:
1259: /**
1260: * Loads forum data from the database.
1261: */
1262: private void loadFromDb() throws ForumNotFoundException {
1263: Connection con = null;
1264: PreparedStatement pstmt = null;
1265: try {
1266: con = DbConnectionManager.getConnection();
1267: //See if we should load by forumID or by name
1268: if (id == -1) {
1269: pstmt = con.prepareStatement(LOAD_FORUM_BY_NAME);
1270: pstmt.setString(1, name);
1271: } else {
1272: pstmt = con.prepareStatement(LOAD_FORUM_BY_ID);
1273: pstmt.setInt(1, id);
1274: }
1275: ResultSet rs = pstmt.executeQuery();
1276: if (!rs.next()) {
1277: throw new ForumNotFoundException("Forum " + getID()
1278: + " could not be loaded from the database.");
1279: }
1280: id = rs.getInt("forumID");
1281: name = rs.getString("name");
1282: description = rs.getString("description");
1283: this .creationDate = new java.util.Date(Long.parseLong(rs
1284: .getString("creationDate").trim()));
1285: this .modifiedDate = new java.util.Date(Long.parseLong(rs
1286: .getString("modifiedDate").trim()));
1287: moderated = rs.getInt("moderated") == 1 ? true : false;
1288: isarticle = rs.getInt("article") == 1 ? true : false;
1289: order = rs.getInt("forumorder");
1290: } catch (SQLException sqle) {
1291: sqle.printStackTrace();
1292: throw new ForumNotFoundException("Forum " + getID()
1293: + " could not be loaded from the database.");
1294: } catch (NumberFormatException nfe) {
1295: System.err
1296: .println("WARNING: In DbForum.loadFromDb() -- there "
1297: + "was an error parsing the dates returned from the database. Ensure "
1298: + "that they're being stored correctly.");
1299: } finally {
1300: try {
1301: pstmt.close();
1302: } catch (Exception e) {
1303: e.printStackTrace();
1304: }
1305: try {
1306: con.close();
1307: } catch (Exception e) {
1308: e.printStackTrace();
1309: }
1310: }
1311: }
1312:
1313: /**
1314: * Inserts a new record into the database.
1315: */
1316: private void insertIntoDb() {
1317: Connection con = null;
1318: PreparedStatement pstmt = null;
1319: try {
1320: con = DbConnectionManager.getConnection();
1321: pstmt = con.prepareStatement(ADD_FORUM);
1322: pstmt.setInt(1, id);
1323: pstmt.setString(2, name);
1324: pstmt.setString(3, description);
1325: pstmt.setString(4, Long.toString(creationDate.getTime()));
1326: pstmt.setString(5, Long.toString(modifiedDate.getTime()));
1327: pstmt.setInt(6, moderated ? 1 : 0);
1328: pstmt.setInt(7, forumGroupID);
1329: pstmt.setInt(8, isarticle ? 1 : 0);
1330: pstmt.executeUpdate();
1331: } catch (SQLException sqle) {
1332: System.err.println("Error in DbForum:insertIntoDb()-"
1333: + sqle);
1334: sqle.printStackTrace();
1335: } finally {
1336: try {
1337: pstmt.close();
1338: } catch (Exception e) {
1339: e.printStackTrace();
1340: }
1341: try {
1342: con.close();
1343: } catch (Exception e) {
1344: e.printStackTrace();
1345: }
1346: }
1347: }
1348:
1349: /**
1350: * Saves forum data to the database.
1351: */
1352: private synchronized void saveToDb() {
1353: Connection con = null;
1354: PreparedStatement pstmt = null;
1355: try {
1356: con = DbConnectionManager.getConnection();
1357: pstmt = con.prepareStatement(SAVE_FORUM);
1358: pstmt.setString(1, name);
1359: pstmt.setString(2, description);
1360: pstmt.setString(3, Long.toString(creationDate.getTime()));
1361: pstmt.setString(4, Long.toString(modifiedDate.getTime()));
1362: pstmt.setInt(5, moderated ? 1 : 0);
1363: pstmt.setInt(6, order);
1364: pstmt.setInt(7, id);
1365: pstmt.executeUpdate();
1366: } catch (SQLException sqle) {
1367: System.err.println("Error in DbForum:saveToDb()-" + sqle);
1368: sqle.printStackTrace();
1369: } finally {
1370: try {
1371: pstmt.close();
1372: } catch (Exception e) {
1373: e.printStackTrace();
1374: }
1375: try {
1376: con.close();
1377: } catch (Exception e) {
1378: e.printStackTrace();
1379: }
1380: }
1381: }
1382:
1383: public boolean isArticleForum() {
1384: return this .isarticle;
1385: }
1386:
1387: public void addArticleMap(String pageKey, ForumThread thread)
1388: throws UnauthorizedException {
1389: Connection con = null;
1390: PreparedStatement pstmt = null;
1391: try {
1392: con = DbConnectionManager.getConnection();
1393: pstmt = con.prepareStatement(INSERT_ARTICLE_MAP);
1394: pstmt.setInt(1, thread.getID());
1395: pstmt.setString(2, pageKey);
1396: pstmt.setInt(3, id);
1397: pstmt.executeUpdate();
1398: } catch (SQLException sqle) {
1399: sqle.printStackTrace();
1400: } finally {
1401: try {
1402: pstmt.close();
1403: } catch (Exception e) {
1404: e.printStackTrace();
1405: }
1406: try {
1407: con.close();
1408: } catch (Exception e) {
1409: e.printStackTrace();
1410: }
1411: }
1412:
1413: }
1414:
1415: public int forumOrder() {
1416: return order;
1417: }
1418:
1419: public void setForumOrder(int param) throws UnauthorizedException {
1420: this.order = param;
1421: saveToDb();
1422: }
1423:
1424: }
|