Finished adding support to show discussions in email's body.

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerSocial@124290 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
costantino.perciante 2016-02-18 14:30:58 +00:00
parent ed484080f0
commit 3cd229ca74
1 changed files with 122 additions and 87 deletions

View File

@ -1,5 +1,6 @@
package org.gcube.applicationsupportlayer.social.mailing;
import java.nio.charset.Charset;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@ -91,76 +92,13 @@ public class EmailPlugin {
body.append("<body>");
String htmlPost = "";
if (notification2Save.getType() == NotificationType.POST_ALERT ||
notification2Save.getType() == NotificationType.COMMENT ||
notification2Save.getType() == NotificationType.MENTION ||
notification2Save.getType() == NotificationType.OWN_COMMENT) {
body.append("<div>").append(WRITE_ABOVE_TO_REPLY).append("</div><br />");
try{
// data formatter
Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
// escape html
String feedTextNoHtml = convertHTML2Text(feed.getDescription());
// build up html post + comments
if(notification2Save.getType() == NotificationType.POST_ALERT || (comments.size() == 0 && notification2Save.getType() == NotificationType.MENTION))
htmlPost = "<div style=\"margin-top: 10px; margin-bottom: 10px; margin-left: 50px; padding-left: 15px; "
+ "border-left: 3px solid #ccc; font-style: italic; font-weight:bold\">"
+ feed.getFullName()
+ ": "
+ (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml)
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(feed.getTime()) + "</p>"
+"</div>";
else
htmlPost = "<div style=\"margin-top: 10px; margin-bottom: 10px; margin-left: 50px; padding-left: 15px; "
+ "border-left: 3px solid #ccc; font-style: italic\">\""
+ feed.getFullName()
+ ": "
+ (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml)
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(feed.getTime()) + "</p>"
+"\"</div>";
for (int i = 0; i < comments.size(); i++) {
String commentTextNoHtml = convertHTML2Text(comments.get(i).getText());
if((commentKey != null && comments.get(i).getKey().equals(commentKey)) && !(notification2Save.getType() == NotificationType.POST_ALERT)){
htmlPost += "<div style=\"margin-top: 10px; margin-bottom: 10px; margin-left: 100px; padding-left: 15px; "
+ "border-left: 3px solid #ccc; font-style: italic; font-weight:bold\">\""
+ comments.get(i).getFullName()
+ ": "
+ commentTextNoHtml
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(comments.get(i).getTime()) + "</p>"
+"\"</div>";
break;
}
else
htmlPost += "<div style=\"margin-top: 10px; margin-bottom: 10px; margin-left: 100px; padding-left: 15px; "
+ "border-left: 3px solid #ccc; font-style: italic;\">\""
+ comments.get(i).getFullName()
+ ": "
+ commentTextNoHtml
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(comments.get(i).getTime()) + "</p>"
+"\"</div>";
}
}catch(Exception e){
_log.error("Unable to reconstruct the discussion to put into the email body.", e);
}
}
if (notification2Save.getType() == NotificationType.MESSAGE) {
body.append("<div>").append(WRITE_ABOVE_MESSAGE_REPLY).append("</div><br />");
@ -170,7 +108,7 @@ public class EmailPlugin {
.append("Dear ").append(userFirstName).append(",") //dear <user>
.append("<p>").append(sender).append(" ").append(removedMarkup) // has done something
.append(getActionLink(notification2Save, portalURL)) //Goto
.append(htmlPost)
.append(buildHtmlDiscussion(notification2Save, feed, comments, commentKey)) // the original discussion
.append("</p>")
.append("</div><br/>")
.append("<p><div style=\"color:#999999; font-size:11px; font-family:'lucida grande',tahoma,verdana,arial,sans-serif;\">")
@ -222,15 +160,125 @@ public class EmailPlugin {
StringBuilder body = new StringBuilder();
String discussion = "";
if (notification2Save.getType() == NotificationType.POST_ALERT ||
notification2Save.getType() == NotificationType.COMMENT ||
notification2Save.getType() == NotificationType.MENTION ||
notification2Save.getType() == NotificationType.OWN_COMMENT) {
body.append(WRITE_ABOVE_TO_REPLY).append("\n\n");
}
if (notification2Save.getType() == NotificationType.MESSAGE) {
body.append(WRITE_ABOVE_MESSAGE_REPLY).append("\n\n");
}
body.append("Dear ").append(userFirstName).append(",") //dear <user>
.append("\n").append(sender).append(" ").append(removedMarkup) // has done something
.append("\nsee: ").append(portalURL).append(notification2Save.getUri())
.append(buildPlainTextDiscussion(feed, comments, commentKey))
.append("\n----\n")
.append("This message was sent to ")
.append(email)
.append(" by ")
.append(portalHost)
.append(" If you don't want to receive these emails in the future, please unsubscribe here: ")
.append(portalURL).append("/group/data-e-infrastructure-gateway/notifications .");
return body.toString();
}
/**
* Build up a discussion given the feed and its comments.
* @param notification2Save
* @param feed
* @param comments
* @param commentKey if not null, when building the discussion stop at this comment.
* @return an html string representing the discussion
*/
private static String buildHtmlDiscussion(Notification notification2Save, Feed feed, List<Comment> comments, String commentKey){
try{
String htmlPost = "<br />" + "<br />----<p>Original post:</p>";
// data formatter
Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
// escape html
String feedTextNoHtml = convertHTML2Text(feed.getDescription());
// build up html post + comments
if(notification2Save.getType() == NotificationType.POST_ALERT || (comments.size() == 0 && notification2Save.getType() == NotificationType.MENTION))
htmlPost += "<div style=\"margin-top: 10px; margin-bottom: 10px;padding-left: 15px; "
+ "font-style: italic; font-weight:bold\">"
+ feed.getFullName()
+ ": "
+ (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml)
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(feed.getTime()) + "</p>"
+"</div>";
else
htmlPost += "<div style=\"margin-top: 10px; margin-bottom: 10px;padding-left: 15px; "
+ "font-style: italic\">"
+ feed.getFullName()
+ ": "
+ (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml)
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(feed.getTime()) + "</p>"
+"</div>";
for (int i = 0; i < comments.size(); i++) {
String commentTextNoHtml = convertHTML2Text(comments.get(i).getText());
if((commentKey != null && comments.get(i).getKey().equals(commentKey)) && !(notification2Save.getType() == NotificationType.POST_ALERT)){
htmlPost += "<div style=\"margin-top: 10px; margin-bottom: 10px; margin-left: 50px; padding-left: 15px; "
+ "border-left: 3px solid #ccc; font-style: italic; font-weight:bold\">"
+ comments.get(i).getFullName()
+ ": "
+ commentTextNoHtml
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(comments.get(i).getTime()) + "</p>"
+"</div>";
break;
}
else
htmlPost += "<div style=\"margin-top: 10px; margin-bottom: 10px; margin-left: 50px; padding-left: 15px; "
+ "border-left: 3px solid #ccc; font-style: italic;\">"
+ comments.get(i).getFullName()
+ ": "
+ commentTextNoHtml
+ "<p style=\"font-family:Lucida Grande,"
+ "Verdana,Bitstream Vera Sans,Arial,sans-serif; "
+ "white-space: nowrap; font-size: smaller; color: #999;\">" + formatter.format(comments.get(i).getTime()) + "</p>"
+"</div>";
}
return htmlPost;
}catch(Exception e){
_log.error("Unable to reconstruct html discussion to put into the email body.", e);
}
return "";
}
/**
* Build a plain text discussion given a feed and its comments.
* @param feed
* @param comments
* @param commentKey if not null, when building the discussion stop at this comment.
* @return a string representing the discussion
*/
private static String buildPlainTextDiscussion(Feed feed, List<Comment> comments, String commentKey){
try{
// build discussion
discussion += "\n\nDiscussion:\n";
String discussion = "\n\n----\n\nOriginal post:";
// data formatter
Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
@ -239,8 +287,8 @@ public class EmailPlugin {
String feedTextNoHtml = convertHTML2Text(feed.getDescription());
// build up post + comments
discussion =
"\t"
discussion +=
"\n"
+ "[" + formatter.format(feed.getTime()) + "] "
+ feed.getFullName()
+ ": "
@ -253,7 +301,7 @@ public class EmailPlugin {
String commentTextNoHtml = convertHTML2Text(comments.get(i).getText());
discussion +=
"\t\t\t"
"\t"
+ "[" + formatter.format(comments.get(i).getTime()) + "] "
+ comments.get(i).getFullName()
+ ": "
@ -264,29 +312,16 @@ public class EmailPlugin {
break;
}
return discussion;
}
if (notification2Save.getType() == NotificationType.MESSAGE) {
body.append(WRITE_ABOVE_MESSAGE_REPLY).append("\n\n");
catch(Exception e){
_log.error("Unable to reconstruct plain text discussion to put into the email body.", e);
}
body.append("Dear ").append(userFirstName).append(",") //dear <user>
.append("\n").append(sender).append(" ").append(removedMarkup) // has done something
.append("\nsee: ").append(portalURL).append(notification2Save.getUri()).append( " if you want to reply.")
.append(discussion)
.append("\n\n\n----\n")
.append("This message was sent to ")
.append(email)
.append(" by ")
.append(portalHost)
.append(" If you don't want to receive these emails in the future, please unsubscribe here: ")
.append(portalURL).append("/group/data-e-infrastructure-gateway/notifications .");
return body.toString();
return "";
}
/**
* Convert html into simple text
*