001: package com.Yasna.forum.Tasks;
002:
003: import com.Yasna.forum.*;
004: import com.Yasna.forum.database.DbConnectionManager;
005: import com.Yasna.util.MailSender;
006:
007: import java.util.LinkedList;
008: import java.sql.Connection;
009: import java.sql.PreparedStatement;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012:
013: /**
014: * Copyright (C) 2001 Yasna.com. All rights reserved.
015: *
016: * ===================================================================
017: * The Apache Software License, Version 1.1
018: *
019: * Redistribution and use in source and binary forms, with or without
020: * modification, are permitted provided that the following conditions
021: * are met:
022: *
023: * 1. Redistributions of source code must retain the above copyright
024: * notice, this list of conditions and the following disclaimer.
025: *
026: * 2. Redistributions in binary form must reproduce the above copyright
027: * notice, this list of conditions and the following disclaimer in
028: * the documentation and/or other materials provided with the
029: * distribution.
030: *
031: * 3. The end-user documentation included with the redistribution,
032: * if any, must include the following acknowledgment:
033: * "This product includes software developed by
034: * Yasna.com (http://www.yasna.com)."
035: * Alternately, this acknowledgment may appear in the software itself,
036: * if and wherever such third-party acknowledgments normally appear.
037: *
038: * 4. The names "Yazd" and "Yasna.com" must not be used to
039: * endorse or promote products derived from this software without
040: * prior written permission. For written permission, please
041: * contact yazd@yasna.com.
042: *
043: * 5. Products derived from this software may not be called "Yazd",
044: * nor may "Yazd" appear in their name, without prior written
045: * permission of Yasna.com.
046: *
047: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
048: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
049: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
050: * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
051: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
052: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
053: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
054: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
055: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
056: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
057: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
058: * SUCH DAMAGE.
059: * ====================================================================
060: *
061: * This software consists of voluntary contributions made by many
062: * individuals on behalf of Yasna.com. For more information
063: * on Yasna.com, please see <http://www.yasna.com>.
064: */
065:
066: /**
067: * This class is meant to handle all people watching the forum.
068: * This will be added to add the handle for people also watching a thread.
069: * Currently the forum only supports notification of a reply!
070: */
071: public class EmailWatchManager {
072: private LinkedList newMessages;
073: // This linked list will retain all the new messages and will notify the people that needs to be notified.
074: private Thread worker;
075: private ForumFactory factory;
076:
077: public EmailWatchManager(ForumFactory factory) {
078: // this is called from the factory and it will retain a handle on it.
079: this .factory = factory;
080: newMessages = new LinkedList();
081: // We are going to start a new worker thread to handle all the emails.
082: worker = new Thread(new EmailWatchWorker(this , factory));
083: worker.setDaemon(true);
084: worker.start();
085: }
086:
087: public synchronized void addMessage(ForumMessage message) {
088: newMessages.addLast(message);
089: // There is now a message to be processed and the Thread needs to be notified
090: notify();
091: }
092:
093: public synchronized ForumMessage getNextMessage() {
094: if (newMessages.isEmpty()) {
095: try {
096: System.out.println("waiting for a new message");
097: wait();
098: //System.out.println("finished waiting");
099: } catch (InterruptedException ie) {
100: }
101: }
102: return (ForumMessage) newMessages.removeFirst();
103: }
104:
105: public class EmailWatchWorker implements Runnable {
106: private static final String GET_USERLIST = "select distinct userID from yazdUserProp where (name=? or name=?) and propValue=?";
107:
108: private EmailWatchManager manager;
109: private ForumFactory factory;
110:
111: public EmailWatchWorker(EmailWatchManager m,
112: ForumFactory factory) {
113: this .manager = m;
114: this .factory = factory;
115: }
116:
117: public void run() {
118: while (true) {
119: sendEmailsForMessage(manager.getNextMessage());
120: }
121: }
122:
123: private void sendEmailsForMessage(ForumMessage message) {
124: //send out all the emails necessary
125: if (PropertyManager.getProperty("yazdMailSMTPServer") == null
126: || "".equals(PropertyManager
127: .getProperty("yazdMailSMTPServer"))) {
128: return;
129: // don't do anything if there is no setting for smtp server
130: }
131: int ForumID = message.getForumThread().getForum().getID();
132: int ThreadID = message.getForumThread().getID();
133: int OriginalUserID = message.getUser().getID();
134: Connection con = null;
135: PreparedStatement pstmt = null;
136: try {
137: con = DbConnectionManager.getConnection();
138: pstmt = con.prepareStatement(GET_USERLIST);
139: pstmt.setString(1, "WatchForum"
140: + Integer.toString(ForumID));
141: pstmt.setString(2, "WatchThread"
142: + Integer.toString(ThreadID));
143: pstmt.setString(3, "true");
144: ResultSet rs = pstmt.executeQuery();
145:
146: while (rs.next()) {
147: User user = factory.getProfileManager().getUser(
148: rs.getInt("userID"));
149: if (user.getEmail() != null
150: && !"".equals(user.getEmail())
151: && user.getID() != OriginalUserID) {
152: // ok ready to send mail to user
153: String emailBody = PropertyManager
154: .getProperty("yazdThreadWatch.MailBody")
155: + " \n\r"
156: + PropertyManager
157: .getProperty("yazdUrl")
158: + "viewThread.jsp?forum="
159: + ForumID
160: + "&thread=" + ThreadID;
161: MailSender
162: .send(
163: PropertyManager
164: .getProperty("yazdMailSMTPServer"),
165: PropertyManager
166: .getProperty("yazdMailFrom"),
167: user.getEmail(),
168: PropertyManager
169: .getProperty("yazdThreadWatch.MailSubject"),
170: emailBody);
171: }
172:
173: }
174: } catch (SQLException sqle) {
175: System.err
176: .println("EmailWatchException (394) Exception:"
177: + sqle.getMessage());
178: sqle.printStackTrace();
179: } catch (Exception e) {
180: System.err
181: .println("EmailWatchManager (3847) Exception:"
182: + e.getMessage());
183: } finally {
184: try {
185: pstmt.close();
186: } catch (Exception e) {
187: e.printStackTrace();
188: }
189: try {
190: con.close();
191: } catch (Exception e) {
192: e.printStackTrace();
193: }
194: }
195:
196: }
197:
198: }
199: }
|