1 5 package org.exoplatform.services.communication.forum.hibernate; 6 7 import java.text.MessageFormat ; 8 import java.util.List ; 9 import net.sf.hibernate.Session; 10 import org.exoplatform.container.configuration.ConfigurationManager; 11 import org.exoplatform.container.configuration.ServiceConfiguration; 12 import org.exoplatform.services.communication.forum.*; 13 import org.exoplatform.services.communication.message.MailService; 14 import org.exoplatform.services.communication.message.Message; 15 import org.exoplatform.services.communication.message.impl.MessageImpl; 16 import org.exoplatform.services.database.ObjectQuery; 17 import org.exoplatform.services.database.XResources; 18 import org.exoplatform.services.task.BaseTask; 19 import org.exoplatform.services.task.TaskService ; 20 25 public class WatcherNotifyListener extends ForumEventListener { 26 private String forumChangeMessage_ ; 27 private String forumChangeSubject_ ; 28 private String topicChangeMessage_ ; 29 private String topicChangeSubject_ ; 30 private String messageAuthor_ ; 31 32 private MailService mservice_ ; 33 private TaskService tservice_ ; 34 35 public WatcherNotifyListener(ForumServiceContainer container, 36 MailService mservice, 37 TaskService tservice, 38 ConfigurationManager confService) throws Exception { 39 super(container) ; 40 mservice_ = mservice ; 41 tservice_ = tservice ; 42 ServiceConfiguration sconf = confService.getServiceConfiguration(getClass()) ; 43 forumChangeSubject_ = sconf.getValueParam("forum.change.subject").getValue() ; 44 forumChangeMessage_ = sconf.getValueParam("forum.change.message").getValue() ; 45 topicChangeSubject_ = sconf.getValueParam("topic.change.subject").getValue() ; 46 topicChangeMessage_ = sconf.getValueParam("topic.change.message").getValue() ; 47 messageAuthor_ = sconf.getValueParam("message.author").getValue() ; 48 } 49 50 public void onSave(XResources resources, Post post) throws Exception { 51 PostImpl impl = (PostImpl) post ; 52 ObjectQuery q = new ObjectQuery(WatcherImpl.class). 53 addEQ("target", WatcherImpl.FORUM_TARGET). 54 addEQ("forumId", impl.getForumId()) ; 55 Session session = (Session) resources.getResource(Session.class) ; 56 List watchers = session.find(q.getHibernateQuery()) ; 57 if(watchers.size() == 0) return ; 58 Forum forum = (Forum) resources.getResource(Forum.class) ; 59 Object [] params = {forum.getForumName(), post.getSubject(), post.getMessage()} ; 60 String subject = MessageFormat.format(forumChangeSubject_, params) ; 61 String body = MessageFormat.format(forumChangeMessage_, params) ; 62 send(watchers, subject, body) ; 63 64 q = new ObjectQuery(WatcherImpl.class). 65 addEQ("target", WatcherImpl.TOPIC_TARGET). 66 addEQ("topicId", impl.getTopicId()) ; 67 watchers = session.find(q.getHibernateQuery()) ; 68 if(watchers.size() == 0) return ; 69 Topic topic = (Topic) resources.getResource(Topic.class) ; 70 Object [] topicparams = {topic.getTopic(), post.getSubject(), post.getMessage()} ; 71 subject = MessageFormat.format(topicChangeSubject_, topicparams) ; 72 body = MessageFormat.format(topicChangeMessage_, topicparams) ; 73 send(watchers, subject, body) ; 74 } 75 76 private void send(List watchers, String subject, String body) throws Exception { 77 StringBuffer bcc = new StringBuffer () ; 78 for(int i = 0; i < watchers.size(); i++) { 79 if(i > 0) bcc.append(", ") ; 80 Watcher watcher = (Watcher) watchers.get(i) ; 81 bcc.append(watcher.getAddress()) ; 82 } 83 Message message = new MessageImpl() ; 84 message.setFrom(messageAuthor_) ; 85 message.setBCC(bcc.toString()) ; 86 message.setSubject(subject) ; 87 message.setBody(body) ; 88 tservice_.queueTask(new NotifyForumWatcherTask(mservice_, message)) ; 89 } 90 91 static public class NotifyForumWatcherTask extends BaseTask { 92 private MailService mservice_ ; 93 private Message message_ ; 94 95 public NotifyForumWatcherTask(MailService service, Message message) { 96 mservice_ = service ; 97 message_ = message ; 98 } 99 100 public void execute() throws Exception { 101 mservice_.sendMessage(message_) ; 102 } 103 104 public String getName() { return "NotifyForumWatcherTask" ;} 105 public String getDescription() { return "Send message : " + message_.getSubject(); } 106 } 107 } | Popular Tags |