1 19 20 package org.lucane.applications.calendar; 21 22 import java.io.InputStream ; 23 import java.net.URL ; 24 import java.text.DateFormat ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 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 36 { 37 private Event event; 38 private String template; 39 40 public MailThread(Event event, String template) 41 { 42 this.event = event; 43 this.template = template; 44 } 45 46 public void run() 47 { 48 sendMails(); 49 } 50 51 private String getMailContent(String language) 52 { 53 String content = null; 54 InputStream is = null; 55 try { 56 try { 57 is = new URL (template + "_"+ language).openStream(); 58 } catch(Exception e) { 59 is = new URL (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 (buffer); 66 } catch(Exception 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 String 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 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 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 e) { 100 Logging.getLogger().warning("Unable to get organizer user : " + e); 101 } 102 if(from == null || from.length() == 0) 103 return; 104 105 String title = "[Lucane] " + event.getTitle(); 106 Iterator 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 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("Unable to send mail to " + attendee.getUser() + " : " + e); 118 } 119 } 120 } 121 122 private void sendMail(String from, String to, String subject, String content) 123 throws Exception 124 { 125 HashMap params = new HashMap (); 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 []{HashMap .class}, new Object []{params}); 134 135 System.out.println("Mail sent to : " + to); 136 } 137 } | Popular Tags |