KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > Tasks > EmailWatchManager


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

65
66 /**
67  * This class is meant to handle all people watching the forum.
68  * This will be added to add the handle for people also watching a thread.
69  * Currently the forum only supports notification of a reply!
70  */

71 public class EmailWatchManager {
72     private LinkedList JavaDoc newMessages;
73     // This linked list will retain all the new messages and will notify the people that needs to be notified.
74
private Thread JavaDoc worker;
75     private ForumFactory factory;
76     public EmailWatchManager(ForumFactory factory){
77         // this is called from the factory and it will retain a handle on it.
78
this.factory = factory;
79         newMessages = new LinkedList JavaDoc();
80         // We are going to start a new worker thread to handle all the emails.
81
worker = new Thread JavaDoc(new EmailWatchWorker(this,factory));
82         worker.setDaemon(true);
83         worker.start();
84     }
85     public synchronized void addMessage(ForumMessage message){
86         newMessages.addLast(message);
87         // There is now a message to be processed and the Thread needs to be notified
88
notify();
89     }
90     public synchronized ForumMessage getNextMessage(){
91         if(newMessages.isEmpty()){
92             try{
93                 System.out.println("waiting for a new message");
94                 wait();
95                 //System.out.println("finished waiting");
96
}catch (InterruptedException JavaDoc ie) {}
97         }
98         return (ForumMessage)newMessages.removeFirst();
99     }
100
101     public class EmailWatchWorker implements Runnable JavaDoc{
102         private static final String JavaDoc GET_USERLIST=
103                 "select distinct userID from yazdUserProp where (name=? or name=?) and propValue=?";
104
105         private EmailWatchManager manager;
106         private ForumFactory factory;
107         public EmailWatchWorker(EmailWatchManager m,ForumFactory factory){
108             this.manager=m;
109             this.factory = factory;
110         }
111         public void run(){
112             while(true){
113                 sendEmailsForMessage(manager.getNextMessage());
114             }
115         }
116         private void sendEmailsForMessage(ForumMessage message){
117             //send out all the emails necessary
118
if(PropertyManager.getProperty("yazdMailSMTPServer")==null || "".equals(PropertyManager.getProperty("yazdMailSMTPServer"))) {
119                 return;
120                 // don't do anything if there is no setting for smtp server
121
}
122             int ForumID = message.getForumThread().getForum().getID();
123             int ThreadID = message.getForumThread().getID();
124             int OriginalUserID = message.getUser().getID();
125             Connection JavaDoc con = null;
126             PreparedStatement JavaDoc pstmt = null;
127             try {
128                 con = DbConnectionManager.getConnection();
129                 pstmt = con.prepareStatement(GET_USERLIST);
130                 pstmt.setString(1,"WatchForum"+Integer.toString(ForumID));
131                 pstmt.setString(2,"WatchThread"+Integer.toString(ThreadID));
132                 pstmt.setString(3, "true");
133                 ResultSet JavaDoc rs = pstmt.executeQuery();
134
135                 while( rs.next() ) {
136                     User user = factory.getProfileManager().getUser(rs.getInt("userID"));
137                     if (user.getEmail()!=null && !"".equals(user.getEmail()) && user.getID()!=OriginalUserID){
138                         // ok ready to send mail to user
139
String JavaDoc emailBody = PropertyManager.getProperty("yazdThreadWatch.MailBody") +
140                                 " \n\r"+PropertyManager.getProperty("yazdUrl")+
141                                 "viewThread.jsp?forum="+ForumID+"&thread="+ThreadID;
142                         MailSender.send(PropertyManager.getProperty("yazdMailSMTPServer"),
143                                 PropertyManager.getProperty("yazdMailFrom"),
144                                 user.getEmail(),
145                                 PropertyManager.getProperty("yazdThreadWatch.MailSubject"),
146                                 emailBody);
147                     }
148
149                 }
150             }
151             catch( SQLException JavaDoc sqle ) {
152                 System.err.println("EmailWatchException (394) Exception:"+sqle.getMessage());
153                 sqle.printStackTrace();
154             }
155             catch (Exception JavaDoc e) {
156                 System.err.println("EmailWatchManager (3847) Exception:"+e.getMessage());
157             }
158             finally {
159                 try { pstmt.close(); }
160                 catch (Exception JavaDoc e) { e.printStackTrace(); }
161                 try { con.close(); }
162                 catch (Exception JavaDoc e) { e.printStackTrace(); }
163             }
164
165
166         }
167
168     }
169 }
170
Popular Tags