KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > blog > BlogCommentNotification


1 /*
2  * Created on Oct 15, 2006
3  */

4 package com.openedit.blog;
5
6 import java.util.HashSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Set JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import com.openedit.OpenEditException;
14 import com.openedit.WebPageRequest;
15 import com.openedit.modules.email.TemplateWebEmail;
16 import com.openedit.page.Page;
17 import com.openedit.page.manage.PageManager;
18 import com.openedit.users.User;
19 import com.openedit.users.UserManager;
20 import com.openedit.modules.email.PostMail;
21 public class BlogCommentNotification
22 {
23     protected PageManager fieldPageManager;
24     protected UserManager fieldUserManager;
25     private static final Log log = LogFactory.getLog(BlogCommentNotification.class);
26     private PostMail postMail;
27     public PostMail getPostMail() {
28         return postMail;
29     }
30
31     public void setPostMail(PostMail postMail) {
32         this.postMail = postMail;
33     }
34
35     public void commentAdded( WebPageRequest inReq, Blog inBlog, BlogEntry inEntry, Comment inComment) throws OpenEditException
36     {
37         if( !inBlog.isUseNotification() )
38         {
39             return;
40         }
41         
42 // if( inReq.getUser() == inComment.getUser())
43
// {
44
// return;
45
// }
46
//notify the previous poster. If no previous poster then notify the author of the entry
47
Set JavaDoc notify = new HashSet JavaDoc();
48
49         if( inEntry.getUser() != null)
50         {
51             String JavaDoc email = inEntry.getUser().getEmail();
52             if( email != null)
53             {
54                 notify.add(email);
55             }
56         }
57
58         for (Iterator JavaDoc iterator = inEntry.getComments().iterator(); iterator.hasNext();)
59         {
60             Comment lastcomment = (Comment)iterator.next();
61             User user = lastcomment.getUser();
62             if( user != null) //legacy comments
63
{
64                 String JavaDoc email = user.getEmail();
65                 if( email != null)
66                 {
67                     notify.add(email);
68                 }
69             }
70         }
71         if( inReq.getUser().getEmail() != null)
72         {
73             notify.remove(inReq.getUser().getEmail()); //Dont email yourself
74
}
75         
76         if( notify.size() > 0 )
77         {
78             //send email out
79
inReq.putPageValue("comment", inComment);
80             inReq.putPageValue("entry", inEntry);
81             inReq.putPageValue("blog", inBlog);
82             
83             try
84             {
85                 Page page = getPageManager().getPage( inBlog.getBlogHome() + "/layout/commentemail.html");
86                 TemplateWebEmail template = postMail.getTemplateWebEmail();
87                 template.setMailTemplatePage(page);
88                 template.setWebPageContext(inReq);
89                 template.loadSettings(page);
90                 String JavaDoc sub = template.getSubject();
91                 sub = "[" + inBlog.getHostName() + "] " + sub;
92                 template.setSubject(sub);
93                 for (Iterator JavaDoc iterator = notify.iterator(); iterator.hasNext();)
94                 {
95                     String JavaDoc email = (String JavaDoc) iterator.next();
96                     template.setTo(email);
97                     template.send();
98                 }
99                 //new PostMail().postMail(email, "Blog Comment", "A new comment has been posted \n" + inEntry.getLink(), "support@openedit.org");
100
}
101             catch ( Exception JavaDoc ex)
102             {
103                 log.error(ex);
104             }
105         }
106     }
107
108     public PageManager getPageManager()
109     {
110         return fieldPageManager;
111     }
112
113     public void setPageManager(PageManager inPageManager)
114     {
115         fieldPageManager = inPageManager;
116     }
117
118     public UserManager getUserManager()
119     {
120         return fieldUserManager;
121     }
122
123     public void setUserManager(UserManager inUserManager)
124     {
125         fieldUserManager = inUserManager;
126     }
127 }
128
Popular Tags