[Yanel-commits] rev 60751 - public/yanel/trunk/src/resources/comment/src/java/org/wyona/yanel/impl/resources/comment
michi at wyona.com
michi at wyona.com
Fri Sep 16 13:41:13 CEST 2011
Author: michi
Date: 2011-09-16 13:41:13 +0200 (Fri, 16 Sep 2011)
New Revision: 60751
Modified:
public/yanel/trunk/src/resources/comment/src/java/org/wyona/yanel/impl/resources/comment/CommentResource.java
Log:
send notification added
Modified: public/yanel/trunk/src/resources/comment/src/java/org/wyona/yanel/impl/resources/comment/CommentResource.java
===================================================================
--- public/yanel/trunk/src/resources/comment/src/java/org/wyona/yanel/impl/resources/comment/CommentResource.java 2011-09-16 08:52:05 UTC (rev 60750)
+++ public/yanel/trunk/src/resources/comment/src/java/org/wyona/yanel/impl/resources/comment/CommentResource.java 2011-09-16 11:41:13 UTC (rev 60751)
@@ -51,20 +51,28 @@
log.warn("No title set!");
}
String email = getEnvironment().getRequest().getParameter("email");
- if (email != null) comment.setAuthorMail(email);
+ if (email != null && email.trim().length() > 0) {
+ comment.setAuthorMail(email);
+ } else {
+ log.warn("No author email specified!");
+ }
String name = getEnvironment().getRequest().getParameter("name");
- if (name != null) comment.setAuthorName(name);
+ if (name != null && name.trim().length() > 0) {
+ comment.setAuthorName(name);
+ } else {
+ log.info("No author name specified!");
+ }
// TODO: Validate fields (e.g. email should be mandatory)!
cMan.addComment(getRealm(), path, comment);
- log.warn("TODO: Send an email to administrator that a new comment has been added to : " + path);
+ notifyAdministrator(path, comment);
// INFO: Return content of comment as confirmation of what has been saved
sb.append("<comment path=\"" + path + "\">");
sb.append(body);
sb.append("</comment>");
} else {
- sb.append("<no-comment-yet path=\"" + path + "\"/>");
+ sb.append("<no-valid-comment-submitted-yet path=\"" + path + "\"/>");
}
} else {
String message = "Resource is not commentable: " + path;
@@ -84,4 +92,42 @@
return new ByteArrayInputStream(sb.toString().getBytes());
}
+
+ /**
+ * Notify an "administrator" by email re a new comment
+ * @param path Path of commentable resource
+ * @param comment Comment which has been added to commentable resource
+ */
+ private void notifyAdministrator(String path, CommentV1 comment) throws Exception {
+ String emailTo = getResourceConfigProperty("email-to");
+ String emailFrom = getResourceConfigProperty("email-from");
+ if (emailTo != null && emailFrom != null) {
+ String from = emailFrom;
+ String name = null; // TODO: Make this configurable
+ String replyTo = from; // TODO: Make this configurable
+ String to = emailTo;
+ String subject = "New comment added"; // TODO: Make this configurable
+
+ StringBuilder content = new StringBuilder("Commented page URL: " + path);
+ content.append("\n\nE-Mail address of author of comment: " + comment.getAuthorMail());
+ if (comment.getAuthorName() != null) {
+ content.append("\n\nName of author of comment: " + comment.getAuthorName());
+ } else {
+ content.append("\n\nNo name of author available.");
+ }
+ javax.servlet.http.Cookie cookie = org.wyona.yanel.servlet.AccessLog.getYanelAnalyticsCookie(getEnvironment().getRequest());
+ String cookieValue = null;
+ if (cookie != null) {
+ content.append("\n\nYanel analytics cookie: " + cookie.getValue());
+ } else {
+ log.warn("No Yanel analytics cookie set yet!");
+ }
+ content.append("\n\nComment title: " + comment.getTitle());
+ content.append("\n\nComment text:\n" + comment.getCommentText());
+
+ org.wyona.yanel.core.util.MailUtil.send(from, name, replyTo, to, subject, content.toString());
+ } else {
+ log.warn("No email addresses (either 'to' or 'from') are configured in order to notify 'administrator' re a new comment!");
+ }
+ }
}
More information about the Yanel-commits
mailing list