001: /*
002: * Created on Oct 15, 2006
003: */
004: package com.openedit.blog;
005:
006: import java.util.HashSet;
007: import java.util.Iterator;
008: import java.util.Set;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012:
013: import com.openedit.OpenEditException;
014: import com.openedit.WebPageRequest;
015: import com.openedit.modules.email.PostMail;
016: import com.openedit.modules.email.TemplateWebEmail;
017: import com.openedit.page.Page;
018: import com.openedit.page.manage.PageManager;
019: import com.openedit.users.User;
020: import com.openedit.users.UserManager;
021:
022: public class BlogCommentNotification {
023: protected PageManager fieldPageManager;
024: protected UserManager fieldUserManager;
025: private static final Log log = LogFactory
026: .getLog(BlogCommentNotification.class);
027: private PostMail postMail;
028:
029: public PostMail getPostMail() {
030: return postMail;
031: }
032:
033: public void setPostMail(PostMail postMail) {
034: this .postMail = postMail;
035: }
036:
037: public void commentAdded(WebPageRequest inReq, Blog inBlog,
038: BlogEntry inEntry, Comment inComment)
039: throws OpenEditException {
040: if (!inBlog.isUseNotification()) {
041: return;
042: }
043:
044: // if( inReq.getUser() == inComment.getUser())
045: // {
046: // return;
047: // }
048: //notify the previous poster. If no previous poster then notify the author of the entry
049: Set notify = new HashSet();
050:
051: if (inEntry.getUser() != null) {
052: String email = inEntry.getUser().getEmail();
053: if (email != null) {
054: notify.add(email);
055: }
056: }
057:
058: for (Iterator iterator = inEntry.getComments().iterator(); iterator
059: .hasNext();) {
060: Comment lastcomment = (Comment) iterator.next();
061: User user = lastcomment.getUser();
062: if (user != null) //legacy comments
063: {
064: String email = user.getEmail();
065: if (email != null) {
066: notify.add(email);
067: }
068: }
069: }
070: if (inReq.getUser() != null
071: && inReq.getUser().getEmail() != null) {
072: notify.remove(inReq.getUser().getEmail()); //Dont email yourself
073: }
074:
075: if (notify.size() > 0) {
076: //send email out
077: inReq.putPageValue("comment", inComment);
078: inReq.putPageValue("entry", inEntry);
079: inReq.putPageValue("blog", inBlog);
080:
081: try {
082: Page page = getPageManager().getPage(
083: inBlog.getBlogHome()
084: + "/layout/commentemail.html");
085: TemplateWebEmail template = postMail
086: .getTemplateWebEmail();
087: template.setMailTemplatePage(page);
088: template.setWebPageContext(inReq.copy(page));
089: template.loadSettings(template.getWebPageContext());
090: String sub = template.getSubject();
091: sub = "[" + inBlog.getHostName() + "] " + sub;
092: template.setSubject(sub);
093: for (Iterator iterator = notify.iterator(); iterator
094: .hasNext();) {
095: String email = (String) iterator.next();
096: template.setTo(email);
097: template.send();
098: }
099: //new PostMail().postMail(email, "Blog Comment", "A new comment has been posted \n" + inEntry.getLink(), "support@openedit.org");
100: } catch (Exception ex) {
101: log.error(ex);
102: }
103: }
104: }
105:
106: public PageManager getPageManager() {
107: return fieldPageManager;
108: }
109:
110: public void setPageManager(PageManager inPageManager) {
111: fieldPageManager = inPageManager;
112: }
113:
114: public UserManager getUserManager() {
115: return fieldUserManager;
116: }
117:
118: public void setUserManager(UserManager inUserManager) {
119: fieldUserManager = inUserManager;
120: }
121: }
|