| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 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 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@author |
| 51 |
|
|
|
|
|
| 0% |
Uncovered Elements: 81 (81) |
Complexity: 14 |
Complexity Density: 0,22 |
|
| 52 |
|
public abstract class AbstractEmailNotificationListener extends CommentListenerSupport { |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
private static final String EMAIL_ADDRESS_TOKEN = "EMAIL_ADDRESS"; |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
@param |
| 61 |
|
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0,6 |
|
| 62 |
0
|
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 |
|
|
| 73 |
|
|
| 74 |
|
@param |
| 75 |
|
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
| 76 |
0
|
public void commentApproved(CommentEvent event) {... |
| 77 |
0
|
Comment comment = event.getComment(); |
| 78 |
0
|
sendNotification(comment); |
| 79 |
|
} |
| 80 |
|
|
|
|
|
| 0% |
Uncovered Elements: 41 (41) |
Complexity: 7 |
Complexity Density: 0,21 |
|
| 81 |
0
|
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 |
|
|
| 119 |
|
|
| 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 |
|
|
| 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 |
|
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 3 |
Complexity Density: 0,13 |
|
| 135 |
0
|
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 |
|
|
| 171 |
|
|
| 172 |
|
@param |
| 173 |
|
@return |
| 174 |
|
|
| 175 |
|
protected abstract Collection getEmailAddresses(Comment comment); |
| 176 |
|
|
| 177 |
|
} |