emailwatchupdatetask.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 160 行

JAVA
160
字号
/** * $RCSfile: EmailWatchUpdateTask.java,v $ * $Revision: 1.3 $ * $Date: 2002/04/02 05:06:04 $ * * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.database;import java.sql.*;import com.jivesoftware.util.*;import com.jivesoftware.forum.util.*;import com.jivesoftware.forum.*;/** * A task that sends out email notifications about a thread watch update. */public class EmailWatchUpdateTask implements Runnable {    /** DATABASE QUERIES **/    private static final String GET_WATCHES =        "SELECT userID from jiveWatch WHERE forumID=? AND threadID=? and " +        "watchType=" + WatchManager.EMAIL_NOTIFY_WATCH;    private DbWatchManager watchManager;    private ForumThread thread;    private ForumMessage message;    /**     * Creates a new email watch update task.     */    public EmailWatchUpdateTask(DbWatchManager watchManager,            ForumThread thread, ForumMessage message)    {        this.watchManager = watchManager;        this.thread = thread;        this.message = message;    }    /**     * Executes the task by loading a list of watches and sending emails.     */    public void run() {        // First, load all watches from the db for this thread. We only need        // to pay attention to email watches at this point.        LongList users = new LongList();        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(GET_WATCHES);            pstmt.setLong(1, thread.getForum().getID());            pstmt.setLong(2, thread.getID());            ResultSet rs = pstmt.executeQuery();            while (rs.next()) {                users.add(rs.getLong(1));            }        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        // If there were any results, send out emails.        if (users.size() > 0) {            // Now, create an email task            EmailTask emailTask = new EmailTask();            for (int i=0; i<users.size(); i++) {                try {                    DbForumFactory factory = DbForumFactory.getInstance();                    User user = factory.userManager.getUser(users.get(i));                    // Create an email message and send it. However, don't send email                    // to the user if they were the one that created the new message.                    if (!user.equals(message.getUser())) {                        String toName = user.getName();                        String toEmail = user.getEmail();                        String fromEmail = watchManager.getEmailFromEmail();                        String fromName = watchManager.getEmailFromName();                        String subject = replaceTokens(watchManager.getEmailSubject(),                                user);                        String body = replaceTokens(watchManager.getEmailBody(),                                user);                        emailTask.addMessage(toName, toEmail, fromName, fromEmail,                                subject, body);                    }                }                catch (UserNotFoundException e) { }            }            // Send all emails using the currently running worker.            emailTask.run();        }    }    /**     * Replaces tokens with the correct values.     *     * @param string the email template to do token replacements on.     * @param user the user that the message will be sent to.     */    private String replaceTokens(String string, User user) {        string = StringUtils.replace(string, "{username}", user.getUsername());        string = StringUtils.replace(string, "{email}", user.getEmail());        String name = user.getName();        if (name != null) {            string = StringUtils.replace(string, "{name}", name);        }        // If name is null, replace {name} with username        else {            string = StringUtils.replace(string, "{name}", user.getUsername());        }        string = StringUtils.replace(string, "{userID}",                Long.toString(user.getID()));        // Thread values        string = StringUtils.replace(string, "{threadID}",                Long.toString(thread.getID()));        string = StringUtils.replace(string, "{threadName}", thread.getName());        string = StringUtils.replace(string, "{threadModifiedDate}",                JiveGlobals.formatDateTime(thread.getModifiedDate()));        string = StringUtils.replace(string, "{threadCreationDate}",                JiveGlobals.formatDateTime(thread.getCreationDate()));        // Message values        String messageUser = "Guest";        if (!message.isAnonymous()) {            if (message.getUser().getName() != null) {                messageUser = message.getUser().getName();            }            else {                messageUser = message.getUser().getUsername();            }        }        string = StringUtils.replace(string, "{messageUser}", messageUser);        string = StringUtils.replace(string, "{messageID}",                Long.toString(message.getID()));        string = StringUtils.replace(string, "{messageSubject}",                message.getSubject());        string = StringUtils.replace(string, "{messageBody}", message.getBody());        // Forum Values        Forum forum = thread.getForum();        string = StringUtils.replace(string, "{forumID}",                Long.toString(forum.getID()));        string = StringUtils.replace(string, "{forumName}", forum.getName());        return string;    }}

⌨️ 快捷键说明

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