Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../../img/srcFileCovDistChart0.png 48% of files have more coverage
63   177   14   15,75
14   97   0,22   4
4     3,5  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  AbstractEmailNotificationListener       Line # 52 63 0% 14 81 0% 0.0
 
No Tests
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.event.comment;
33   
34    import net.sourceforge.pebble.domain.Blog;
35    import net.sourceforge.pebble.domain.Comment;
36    import net.sourceforge.pebble.api.decorator.ContentDecoratorContext;
37    import net.sourceforge.pebble.api.event.comment.CommentEvent;
38    import net.sourceforge.pebble.util.MailUtils;
39    import net.sourceforge.pebble.util.StringUtils;
40   
41    import javax.mail.Session;
42    import java.text.SimpleDateFormat;
43    import java.util.Collection;
44    import java.util.Iterator;
45   
46    /**
47    * Base class for listeners that send an e-mail notification when new
48    * comments are added.
49    *
50    * @author Simon Brown
51    */
 
52    public abstract class AbstractEmailNotificationListener extends CommentListenerSupport {
53   
54    /** a token to be replaced when sending e-mails */
55    private static final String EMAIL_ADDRESS_TOKEN = "EMAIL_ADDRESS";
56   
57    /**
58    * Called when a comment has been added.
59    *
60    * @param event a CommentEvent instance
61    */
 
62  0 toggle public void commentAdded(CommentEvent event) {
63  0 Comment comment = event.getComment();
64  0 if (comment.isApproved()) {
65  0 sendNotification(comment);
66  0 } else if (comment.isPending()) {
67  0 sendApprovalRequest(comment);
68    }
69    }
70   
71    /**
72    * Called when a comment has been approved.
73    *
74    * @param event a CommentEvent instance
75    */
 
76  0 toggle public void commentApproved(CommentEvent event) {
77  0 Comment comment = event.getComment();
78  0 sendNotification(comment);
79    }
80   
 
81  0 toggle private void sendNotification(Comment comment) {
82  0 Blog blog = comment.getBlogEntry().getBlog();
83   
84  0 ContentDecoratorContext context = new ContentDecoratorContext();
85  0 context.setView(ContentDecoratorContext.DETAIL_VIEW);
86  0 context.setMedia(ContentDecoratorContext.EMAIL);
87   
88  0 blog.getContentDecoratorChain().decorate(context, comment);
89   
90  0 SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
91  0 sdf.setTimeZone(blog.getTimeZone());
92   
93  0 String subject = "[Comment] " + comment.getTitle();
94  0 String author = StringUtils.transformHTML(comment.getAuthor());
95  0 if (comment.getWebsite() != null) {
96  0 author = "<a href=\"" + comment.getWebsite() + "\">" + author + "</a>";
97    }
98   
99  0 String message = "Comment from " + author + " on " + sdf.format(comment.getDate());
100  0 message += " in response to " + comment.getBlogEntry().getTitle();
101  0 message += "\n\n<br><br>";
102  0 message += comment.getBody();
103  0 message += "\n\n<br><br>";
104  0 message += "<a href=\"" + comment.getPermalink() + "\">Permalink</a>";
105  0 message += " | ";
106  0 message += "<a href=\"" + blog.getUrl() + "removeEmailAddress.action?entry=" + comment.getBlogEntry().getId() + "&email=" + EMAIL_ADDRESS_TOKEN + "\">Opt-out</a>";
107   
108  0 Collection to = getEmailAddresses(comment);
109  0 Iterator it = comment.getBlogEntry().getComments().iterator();
110  0 Comment blogComment;
111  0 while (it.hasNext()) {
112  0 blogComment = (Comment)it.next();
113  0 if (blogComment.getEmail() != null && blogComment.getEmail().length() > 0) {
114  0 to.add(blogComment.getEmail());
115    }
116    }
117   
118    // now send personalized e-mails to the blog owner and everybody
119    // that left a comment specifying their e-mail address
120  0 try {
121  0 Session session = MailUtils.createSession();
122  0 Iterator emailAddresses = to.iterator();
123  0 while (emailAddresses.hasNext()) {
124  0 String emailAddress = (String)emailAddresses.next();
125   
126    // customize the opt-out link and send the message
127  0 MailUtils.sendMail(session, blog, emailAddress, subject,
128    message.replaceAll(EMAIL_ADDRESS_TOKEN, emailAddress));
129    }
130    } catch (Exception e) {
131  0 e.printStackTrace();
132    }
133    }
134   
 
135  0 toggle private void sendApprovalRequest(Comment comment) {
136  0 Blog blog = comment.getBlogEntry().getBlog();
137   
138  0 SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
139  0 sdf.setTimeZone(blog.getTimeZone());
140   
141  0 String subject = "[Comment-" + comment.getState().getName() + "] " + comment.getTitle();
142  0 String author = StringUtils.transformHTML(comment.getAuthor());
143  0 if (comment.getWebsite() != null) {
144  0 author = "<a href=\"" + comment.getWebsite() + "\">" + author + "</a>";
145    }
146   
147  0 String message = "Comment from " + author + " on " + sdf.format(comment.getDate());
148  0 message += " in response to " + comment.getBlogEntry().getTitle();
149  0 message += "\n\n<br><br>";
150  0 message += comment.getBody();
151  0 message += "\n\n<br><br>";
152  0 message += "<a href=\"" + comment.getPermalink() + "\">Permalink</a>";
153  0 message += " | ";
154  0 message += "<a href=\"" + blog.getUrl() + "manageResponses.secureaction?response=" + comment.getGuid() + "&submit=Approve" + "\">Approve</a>";
155  0 message += " | ";
156  0 message += "<a href=\"" + blog.getUrl() + "manageResponses.secureaction?response=" + comment.getGuid() + "&submit=Reject" + "\">Reject</a>";
157  0 message += " | ";
158  0 message += "<a href=\"" + blog.getUrl() + "manageResponses.secureaction?response=" + comment.getGuid() + "&submit=Remove" + "\">Remove</a>";
159   
160  0 Collection to = getEmailAddresses(comment);
161   
162  0 try {
163  0 MailUtils.sendMail(MailUtils.createSession(), blog, to, subject, message);
164    } catch (Exception e) {
165  0 e.printStackTrace();
166    }
167    }
168   
169    /**
170    * Returns the collection of recipients.
171    *
172    * @param comment the Comment from the event
173    * @return a Collection of e-mail addresses (Strings)
174    */
175    protected abstract Collection getEmailAddresses(Comment comment);
176   
177    }