KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > calendar > MailThread


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package org.lucane.applications.calendar;
21
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.text.DateFormat JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.lucane.common.Logging;
29 import org.lucane.common.concepts.UserConcept;
30 import org.lucane.server.Server;
31 import org.lucane.server.Service;
32 import org.lucane.server.ServiceManager;
33 import org.lucane.server.store.UserStore;
34
35 public class MailThread extends Thread JavaDoc
36 {
37     private Event event;
38     private String JavaDoc template;
39     
40     public MailThread(Event event, String JavaDoc template)
41     {
42         this.event = event;
43         this.template = template;
44     }
45     
46     public void run()
47     {
48         sendMails();
49     }
50     
51     private String JavaDoc getMailContent(String JavaDoc language)
52     {
53         String JavaDoc content = null;
54         InputStream JavaDoc is = null;
55         try {
56             try {
57                 is = new URL JavaDoc(template + "_"+ language).openStream();
58             } catch(Exception JavaDoc e) {
59                 is = new URL JavaDoc(template + "_en").openStream();
60             }
61             int length = is.available();
62             byte[] buffer = new byte[length];
63             is.read(buffer);
64             is.close();
65             content = new String JavaDoc(buffer);
66         } catch(Exception JavaDoc e) {
67             Logging.getLogger().warning("Unable to read mail template : " + e);
68             content = "<html><body><p>%title%</p><p></p><p>%description%</p></body></html>";
69         }
70         
71         //strip html header & footer from description
72
String JavaDoc description = event.getDescription();
73         int index = description.toLowerCase().indexOf("<body>");
74         if(index > 0)
75             description = description.substring(index+6);
76         index = description.toLowerCase().indexOf("</body>");
77         if(index > 0)
78             description = description.substring(0, index);
79         
80         DateFormat JavaDoc format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
81         
82         content = content.replaceAll("%title%", event.getTitle());
83         content = content.replaceAll("%start%", format.format(event.getStartDate()));
84         content = content.replaceAll("%end%", format.format(event.getEndDate()));
85         content = content.replaceAll("%description%", description);
86         return content;
87     }
88     
89     private void sendMails()
90     {
91         UserStore store = Server.getInstance().getStore().getUserStore();
92         
93         String JavaDoc from = null;
94         try {
95             UserConcept organizer = store.getUser(event.getOrganizer());
96             from = organizer.getMailAddress();
97             if(from != null && from.length() > 0)
98                 from = organizer.getRealName() + '<' + from + '>';
99         } catch (Exception JavaDoc e) {
100             Logging.getLogger().warning("Unable to get organizer user : " + e);
101         }
102         if(from == null || from.length() == 0)
103             return;
104         
105         String JavaDoc title = "[Lucane] " + event.getTitle();
106         Iterator JavaDoc attendees = event.getAttendees();
107         while(attendees.hasNext())
108         {
109             Attendee attendee = (Attendee)attendees.next();
110             try {
111                 UserConcept user = store.getUser(attendee.getUser());
112                 String JavaDoc content = getMailContent(user.getLanguage());
113                 String JavaDoc to = user.getMailAddress();
114                 if(to != null && to.length() > 0)
115                     sendMail(from, to, title, content);
116             } catch(Exception JavaDoc e) {
117                 Logging.getLogger().warning("Unable to send mail to " + attendee.getUser() + " : " + e);
118             }
119         }
120     }
121     
122     private void sendMail(String JavaDoc from, String JavaDoc to, String JavaDoc subject, String JavaDoc content)
123     throws Exception JavaDoc
124     {
125         HashMap JavaDoc params = new HashMap JavaDoc();
126         params.put("from", from);
127         params.put("subject", subject);
128         params.put("to", to);
129         params.put("content", content);
130         params.put("content-type", "text/html");
131         
132         Service s = ServiceManager.getInstance().getService("org.lucane.applications.sendmail");
133         s.invoke("sendMail", new Class JavaDoc[]{HashMap JavaDoc.class}, new Object JavaDoc[]{params});
134         
135         System.out.println("Mail sent to : " + to);
136     }
137 }
Popular Tags