From 3cd229ca74e6b71b11e167f817d61635221fdb14 Mon Sep 17 00:00:00 2001 From: "costantino.perciante" Date: Thu, 18 Feb 2016 14:30:58 +0000 Subject: [PATCH] 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 --- .../social/mailing/EmailPlugin.java | 209 ++++++++++-------- 1 file changed, 122 insertions(+), 87 deletions(-) diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java index 10b8361..253cd8e 100644 --- a/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java +++ b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java @@ -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(""); - String htmlPost = ""; - if (notification2Save.getType() == NotificationType.POST_ALERT || notification2Save.getType() == NotificationType.COMMENT || notification2Save.getType() == NotificationType.MENTION || notification2Save.getType() == NotificationType.OWN_COMMENT) { body.append("
").append(WRITE_ABOVE_TO_REPLY).append("

"); - 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 = "
" - + feed.getFullName() - + ": " - + (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml) - + "

" + formatter.format(feed.getTime()) + "

" - +"
"; - else - htmlPost = "
\"" - + feed.getFullName() - + ": " - + (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml) - + "

" + formatter.format(feed.getTime()) + "

" - +"\"
"; - - 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 += "
\"" - + comments.get(i).getFullName() - + ": " - + commentTextNoHtml - + "

" + formatter.format(comments.get(i).getTime()) + "

" - +"\"
"; - - break; - } - else - htmlPost += "
\"" - + comments.get(i).getFullName() - + ": " - + commentTextNoHtml - + "

" + formatter.format(comments.get(i).getTime()) + "

" - +"\"
"; - } - - }catch(Exception e){ - _log.error("Unable to reconstruct the discussion to put into the email body.", e); - } } if (notification2Save.getType() == NotificationType.MESSAGE) { body.append("
").append(WRITE_ABOVE_MESSAGE_REPLY).append("

"); @@ -170,7 +108,7 @@ public class EmailPlugin { .append("Dear ").append(userFirstName).append(",") //dear .append("

").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("

") .append("
") .append("

") @@ -222,16 +160,126 @@ 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 + .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 comments, String commentKey){ + + try{ + + String htmlPost = "
" + "
----

Original post:

"; + // 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 += "
" + + feed.getFullName() + + ": " + + (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml) + + "

" + formatter.format(feed.getTime()) + "

" + +"
"; + else + htmlPost += "
" + + feed.getFullName() + + ": " + + (feedTextNoHtml.length() > 30 ? feedTextNoHtml.substring(0, 30) + " ..." : feedTextNoHtml) + + "

" + formatter.format(feed.getTime()) + "

" + +"
"; + + 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 += "
" + + comments.get(i).getFullName() + + ": " + + commentTextNoHtml + + "

" + formatter.format(comments.get(i).getTime()) + "

" + +"
"; + + break; + } + else + htmlPost += "
" + + comments.get(i).getFullName() + + ": " + + commentTextNoHtml + + "

" + formatter.format(comments.get(i).getTime()) + "

" + +"
"; + } + + 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 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 - .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 *