⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbwatchmanager.java

📁 一个jive论坛管理的源码 学习Jive源程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * $RCSfile: DbWatchManager.java,v $
 * $Revision: 1.1.1.1 $
 * $Date: 2002/09/09 13:50:59 $
 *
 * New Jive  from Jdon.com.
 *
 * This software is the proprietary information of CoolServlets, Inc.
 * Use is subject to license terms.
 */

package com.jivesoftware.forum.database;

import java.util.*;
import java.sql.*;

import com.jivesoftware.forum.*;
import com.jivesoftware.forum.util.*;
import com.jivesoftware.util.*;

/**
 * Database implementation of the WatchManager interface. The following Jive
 * properties control the behavior of this class:<ul>
 *
 *  <li><tt>watches.emailNotifyEnabled</tt> -- set to <tt>true</tt> or </tt> false to
 *      enable or disable email watch notifications. Default is false.
 *  <li><tt>watches.cacheSize</tt> -- the size of the watch cache in bytes. Default
 *      value is 512K.
 *  <li><tt>watches.deleteDays</tt> -- the number of days a thread can be
 *      inactive before a watch on that thread is automatically deleted. Default
 *      is 30 days.
 * </ul>
 *
 * @see EmailWatchUpdateTask
 */
public class DbWatchManager implements WatchManager {

    /** DATABASE QUERIES **/
    private static final String CREATE_WATCH =
        "INSERT INTO jiveWatch(userID,forumID,threadID,watchType,expirable) " +
        "VALUES (?,?,?,?,1)";
    private static final String DELETE_WATCH =
        "DELETE FROM jiveWatch WHERE userID=? AND threadID=? AND watchType=?";
    private static final String DELETE_THREAD_WATCHES =
        "DELETE FROM jiveWatch WHERE threadID=?";
    private static final String ALL_WATCHES =
        "SELECT jiveWatch.threadID, jiveWatch.expirable, jiveThread.forumID, " +
        "modifiedDate FROM jiveWatch, jiveThread WHERE jiveWatch.userID=? AND " +
        "jiveWatch.watchType=? AND jiveWatch.threadID=jiveThread.threadID " +
        "ORDER BY modifiedDate DESC";
    private static final String ALL_WATCHES_BY_FORUM =
        "SELECT jiveWatch.threadID, jiveThread.forumID, modifiedDate FROM " +
        "jiveWatch, jiveThread WHERE jiveWatch.userID=? AND jiveWatch.watchType=? " +
        "AND jiveWatch.threadID=jiveThread.threadID ORDER BY jiveThread.forumID, " +
        "modifiedDate DESC";
    private static final String GET_EXPIRED_WATCHES =
        "SELECT jiveWatch.threadID FROM jiveWatch, jiveThread WHERE " +
        "jiveWatch.threadID=jiveThread.threadID AND jiveThread.modifiedDate < ? " +
        "AND jiveWatch.expirable=1";
    private static final String EXPIRE_OLD_WATCHES =
        "DELETE FROM jiveWatch WHERE threadID IN (SELECT jiveWatch.threadID FROM " +
        "jiveWatch, jiveThread WHERE jiveWatch.threadID=jiveThread.threadID AND " +
        "jiveThread.modifiedDate < ? AND jiveWatch.expirable=1)";
    private static final String UPDATE_WATCH_EXPIRATION =
        "UPDATE jiveWatch SET expirable=? WHERE userID=? AND threadID=? AND watchType=?";

    // Jive property values.
    private boolean emailNotifyEnabled = false;
    private int deleteDays;
    private String fromName;
    private String fromEmail;
    private String subject;
    private String body;

    private Cache watchCache;
    private DbForumFactory factory;

    /**
     * Creates a new DbWatchManager.
     */
    public DbWatchManager(DbForumFactory factory) {
        this.factory = factory;

        // Enable or disable email notifications
        String emailNotify = JiveGlobals.getJiveProperty("watches.emailNotifyEnabled");
        if ("true".equals(emailNotify)) {
            emailNotifyEnabled = true;
        }

        // Set number of days that watches on unmodified threads will live.
        // Default is 30 days.
        deleteDays = 30;
        String days = JiveGlobals.getJiveProperty("watches.deleteDays");
        if (days != null) {
            try {
                deleteDays = Integer.parseInt(days);
            } catch (Exception e) { }
        }

        // Load email watch properties.
        fromName = JiveGlobals.getJiveProperty("watches.email.fromName");
        fromEmail = JiveGlobals.getJiveProperty("watches.email.fromEmail");
        subject = JiveGlobals.getJiveProperty("watches.email.subject");
        body = JiveGlobals.getJiveProperty("watches.email.body");

        //If any properties fail to load, print error.
        if (fromName==null || fromEmail==null || subject==null || body==null) {
            System.err.println("Error! One or more email watch properties could " +
            "not be loaded. Email watch updates cannot be sent until the " +
            "problem is resolved.");
        }

        // Create watch cache with correct size.
        int cacheSize = 512*1024;   //default is 512K
        String size = JiveGlobals.getJiveProperty("watches.cacheSize");
        if (size != null) {
            try {
                cacheSize = Integer.parseInt(size);
            } catch (Exception e) { }
        }
        watchCache = new Cache(cacheSize, 1*JiveGlobals.HOUR);

        // Add a scheduled task to delete old watches.
        long period = 12*JiveGlobals.HOUR;
        TaskEngine.scheduleTask(new DeleteWatchesTask(),period,period);
    }

    public int getDeleteDays() {
        return deleteDays;
    }

    public void setDeleteDays(int deleteDays) {
        this.deleteDays = deleteDays;
        JiveGlobals.setJiveProperty("watches.deleteDays", Integer.toString(deleteDays));
    }

    public boolean isEmailNotifyEnabled() {
        return emailNotifyEnabled;
    }

    public void setEmailNotifyEnabled(boolean enabled) {
        this.emailNotifyEnabled = enabled;
        JiveGlobals.setJiveProperty("watches.emailNotifyEnabled", ""+emailNotifyEnabled);
    }

    public String getEmailBody()
    {
        return body;
    }

    public void setEmailBody(String body)
    {
        this.body = body;
        JiveGlobals.setJiveProperty("watches.email.body", body);
    }

    public String getEmailSubject()
    {
        return subject;
    }

    public void setEmailSubject(String subject)
    {
        this.subject = subject;
        JiveGlobals.setJiveProperty("watches.email.subject", subject);
    }

    public String getEmailFromName()
    {
        return fromName;
    }

    public void setEmailFromName(String fromName)
    {
        this.fromName = fromName;
        JiveGlobals.setJiveProperty("watches.email.fromName", fromName);
    }

    public String getEmailFromEmail()
    {
        return fromEmail;
    }

    public void setEmailFromEmail(String fromEmail)
    {
        this.fromEmail = fromEmail;
        JiveGlobals.setJiveProperty("watches.email.fromEmail", fromEmail);
    }

    public void createWatch(User user, ForumThread thread, int watchType) {
        //If the user is already watching the thread, do nothing.
        if (getWatchList(user, watchType).isWatchedThread(thread.getID())) {
            return;
        }
        //Otherwise, add a new watch.
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(CREATE_WATCH);
            pstmt.setLong(1, user.getID());
            pstmt.setLong(2, thread.getForum().getID());
            pstmt.setLong(3, thread.getID());
            pstmt.setInt(4, watchType);
            pstmt.execute();
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close();   }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        //Remove user's watch list from cache.
        StringBuffer key = new StringBuffer(Long.toString(user.getID()));
        key.append("-").append(watchType);
        watchCache.remove(key.toString());
    }

    public void deleteWatch(User user, ForumThread thread, int watchType) {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_WATCH);
            pstmt.setLong(1, user.getID());
            pstmt.setLong(2, thread.getID());
            pstmt.setInt(3, watchType);
            pstmt.execute();
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close();   }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        //Remove user's watch list from cache.
        StringBuffer key = new StringBuffer(Long.toString(user.getID()));
        key.append("-").append(watchType);
        watchCache.remove(key.toString());
    }

    public boolean isWatchExpirable(User user, ForumThread thread,
            int watchType)
    {
        return getWatchList(user, watchType).isWatchExpirable(thread.getID());
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -