001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package org.lucane.applications.calendar;
021:
022: import java.io.InputStream;
023: import java.net.URL;
024: import java.text.DateFormat;
025: import java.util.HashMap;
026: import java.util.Iterator;
027:
028: import org.lucane.common.Logging;
029: import org.lucane.common.concepts.UserConcept;
030: import org.lucane.server.Server;
031: import org.lucane.server.Service;
032: import org.lucane.server.ServiceManager;
033: import org.lucane.server.store.UserStore;
034:
035: public class MailThread extends Thread {
036: private Event event;
037: private String template;
038:
039: public MailThread(Event event, String template) {
040: this .event = event;
041: this .template = template;
042: }
043:
044: public void run() {
045: sendMails();
046: }
047:
048: private String getMailContent(String language) {
049: String content = null;
050: InputStream is = null;
051: try {
052: try {
053: is = new URL(template + "_" + language).openStream();
054: } catch (Exception e) {
055: is = new URL(template + "_en").openStream();
056: }
057: int length = is.available();
058: byte[] buffer = new byte[length];
059: is.read(buffer);
060: is.close();
061: content = new String(buffer);
062: } catch (Exception e) {
063: Logging.getLogger().warning(
064: "Unable to read mail template : " + e);
065: content = "<html><body><p>%title%</p><p></p><p>%description%</p></body></html>";
066: }
067:
068: //strip html header & footer from description
069: String description = event.getDescription();
070: int index = description.toLowerCase().indexOf("<body>");
071: if (index > 0)
072: description = description.substring(index + 6);
073: index = description.toLowerCase().indexOf("</body>");
074: if (index > 0)
075: description = description.substring(0, index);
076:
077: DateFormat format = DateFormat.getDateTimeInstance(
078: DateFormat.SHORT, DateFormat.SHORT);
079:
080: content = content.replaceAll("%title%", event.getTitle());
081: content = content.replaceAll("%start%", format.format(event
082: .getStartDate()));
083: content = content.replaceAll("%end%", format.format(event
084: .getEndDate()));
085: content = content.replaceAll("%description%", description);
086: return content;
087: }
088:
089: private void sendMails() {
090: UserStore store = Server.getInstance().getStore()
091: .getUserStore();
092:
093: String from = null;
094: try {
095: UserConcept organizer = store.getUser(event.getOrganizer());
096: from = organizer.getMailAddress();
097: if (from != null && from.length() > 0)
098: from = organizer.getRealName() + '<' + from + '>';
099: } catch (Exception e) {
100: Logging.getLogger().warning(
101: "Unable to get organizer user : " + e);
102: }
103: if (from == null || from.length() == 0)
104: return;
105:
106: String title = "[Lucane] " + event.getTitle();
107: Iterator attendees = event.getAttendees();
108: while (attendees.hasNext()) {
109: Attendee attendee = (Attendee) attendees.next();
110: try {
111: UserConcept user = store.getUser(attendee.getUser());
112: String content = getMailContent(user.getLanguage());
113: String to = user.getMailAddress();
114: if (to != null && to.length() > 0)
115: sendMail(from, to, title, content);
116: } catch (Exception e) {
117: Logging.getLogger().warning(
118: "Unable to send mail to " + attendee.getUser()
119: + " : " + e);
120: }
121: }
122: }
123:
124: private void sendMail(String from, String to, String subject,
125: String content) throws Exception {
126: HashMap params = new HashMap();
127: params.put("from", from);
128: params.put("subject", subject);
129: params.put("to", to);
130: params.put("content", content);
131: params.put("content-type", "text/html");
132:
133: Service s = ServiceManager.getInstance().getService(
134: "org.lucane.applications.sendmail");
135: s.invoke("sendMail", new Class[] { HashMap.class },
136: new Object[] { params });
137:
138: System.out.println("Mail sent to : " + to);
139: }
140: }
|