added hashtags in subject email of post notifications, with redirection to search in the VRE

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerSocial@101406 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2014-11-03 18:31:09 +00:00
parent 028fb77fbc
commit 746d0df7ad
5 changed files with 76 additions and 55 deletions

View File

@ -72,7 +72,7 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen
* @param notification2Save the notification instance to save
* @return true if the notification was sent ok
*/
private boolean saveNotification(Notification notification2Save) {
private boolean saveNotification(Notification notification2Save, String ... hashtags) {
_log.trace("Trying to send notification to: " + notification2Save.getUserid() + " Type: " + notification2Save.getType());
if (notification2Save.getSenderid().compareTo(notification2Save.getUserid()) == 0) {
_log.trace("Sender and Receiver are the same " + notification2Save.getUserid() + " Notification Stopped");
@ -93,15 +93,15 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen
_log.error("Error While trying to save Notification");
}
if (channels.contains(NotificationChannelType.EMAIL))
EmailPlugin.getInstance().sendNotification(notification2Save, aslSession.getGroupName(), portalName, senderEmail);
EmailPlugin.getInstance().sendNotification(notification2Save, aslSession.getGroupName(), portalName, senderEmail, hashtags);
if (channels.isEmpty()) {
_log.info("Notification was not needed as "+ notification2Save.getUserid() +" decided not to be notified for " + notification2Save.getType());
result = true;
}
return result;
}
/**
* return the url of the application if exists in the profile
* @return .
@ -454,10 +454,10 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen
* {@inheritDoc}
*/
@Override
public boolean notifyPost(String userIdToNotify, String feedid, String feedText) {
public boolean notifyPost(String userIdToNotify, String feedid, String feedText, String ... hashtags) {
StringBuilder notificationText = new StringBuilder();
notificationText.append("posted a news on <b>").append(aslSession.getGroupName()).append(":</b>") // has done something
notificationText.append("posted a news on <b> ").append(aslSession.getGroupName()).append(":</b>") // has done something
.append("<br /><br /> ").append(escapeHtml(feedText)).append(". ")
.append("<br /><br />Follow the discussion and comment it. ");
@ -473,7 +473,7 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen
aslSession.getUsername(),
aslSession.getUserFullName(),
aslSession.getUserAvatarId());
return saveNotification(not);
return saveNotification(not, hashtags);
}
/**
* {@inheritDoc}

View File

@ -171,7 +171,7 @@ public interface NotificationsManager {
* @param feedText the liked feed text or a portion of it
* @return true if the notification is correctly delivered, false otherwise
*/
boolean notifyPost(String userIdToNotify, String feedid, String feedText);
boolean notifyPost(String userIdToNotify, String feedid, String feedText, String ... hashtags);
/**
* use to notify a user that someone commented on his post
*

View File

@ -133,7 +133,7 @@ public class EmailNotificationsConsumer extends Thread {
//sync method to ensure the producer do not put new emails in the meantime
synchronized(EmailPlugin.BUFFER_EMAILS){
for (NotificationMail mail : EmailPlugin.BUFFER_EMAILS) {
Message m = EmailPlugin.getMessageNotification(session, mail.getNotification2Send(), mail.getVreName(), mail.getPortalName(), mail.getSenderEmail());
Message m = EmailPlugin.getMessageNotification(session, mail.getNotification2Send(), mail.getVreName(), mail.getPortalName(), mail.getSenderEmail(), mail.getHashtags());
if (m != null) {
m.saveChanges();
Address[] addresses = m.getAllRecipients();

View File

@ -37,24 +37,34 @@ public class EmailPlugin {
protected static final int SECONDS2WAIT = 60;
private static EmailPlugin singleton;
public static EmailPlugin getInstance() {
if (singleton == null)
singleton = new EmailPlugin();
return singleton;
}
private EmailPlugin() {
new EmailNotificationsConsumer().start();
}
protected static ArrayList<NotificationMail> BUFFER_EMAILS = new ArrayList<NotificationMail>();
private static String getHTMLEmail(Notification notification2Save, String userFirstName, String portalURL, String email) {
private static String getHTMLEmail(Notification notification2Save, String userFirstName, String portalURL, String email, String ... hashtags) {
String removedMarkup = notification2Save.getDescription().replaceAll("&amp;", "&");
removedMarkup = removedMarkup.replaceAll("&gt;", ">");
removedMarkup = removedMarkup.replaceAll("&lt;", "<");
if (hashtags != null && hashtags.length > 0) {
_log.debug("editing hyperlinks for mail client");
//notification2Save uri contains the absoulte path to the feed in the correct channel, e.g. /group/ustore_vre?oid=f1637958-34d0-48fc-b5ad-13b1116e389d
String pathToVRE = "/group/data-e-infrastructure-gateway?";
if (notification2Save.getUri().split("\\?").length > 0) {
pathToVRE = notification2Save.getUri().split("\\?")[0];
}
removedMarkup = removedMarkup.replace("href=\"?", "href=\""+portalURL + pathToVRE + "?"); //because there is no indication of the portal
}
String sender = notification2Save.getSenderFullName();
if (notification2Save.getType() == NotificationType.DOCUMENT_WORKFLOW_STEP_REQUEST_TASK)
@ -91,11 +101,18 @@ public class EmailPlugin {
}
private static String getTextEmail(Notification notification2Save, String userFirstName, String portalURL, String email) {
private static String getTextEmail(Notification notification2Save, String userFirstName, String portalURL, String email, String[] hashtags) {
String removedMarkup = convertHTML2Text(notification2Save.getDescription());
if (hashtags != null && hashtags.length > 0) {
for (int i = 0; i < hashtags.length; i++) {
_log.debug("replacing " + hashtags[i]);
removedMarkup = removedMarkup.replace(hashtags[i], " " + hashtags[i] + " "); //because removing html cause trimming we put spaces back
}
}
_log.debug("*****removedMarkup=\n" + removedMarkup);
String sender = notification2Save.getSenderFullName();
if (notification2Save.getType() == NotificationType.DOCUMENT_WORKFLOW_STEP_REQUEST_TASK)
sender = "";
@ -119,9 +136,9 @@ public class EmailPlugin {
return body.toString();
}
/**
* Convert html into simple text
*
@ -135,8 +152,8 @@ public class EmailPlugin {
removedMarkup = removedMarkup.replaceAll("&lt;", "<");
String text = removedMarkup;
try {
Document document = Jsoup.parse(removedMarkup);
Element body = document.body();
Document document = Jsoup.parse(removedMarkup);
Element body = document.body();
text = buildStringFromNode(body).toString();
}
catch (Exception e) {
@ -145,36 +162,36 @@ public class EmailPlugin {
}
return text;
}
private static StringBuffer buildStringFromNode(Node node) {
StringBuffer buffer = new StringBuffer();
StringBuffer buffer = new StringBuffer();
if (node instanceof TextNode) {
TextNode textNode = (TextNode) node;
buffer.append(textNode.text().trim());
}
if (node instanceof TextNode) {
TextNode textNode = (TextNode) node;
buffer.append(textNode.text().trim());
}
for (Node childNode : node.childNodes()) {
buffer.append(buildStringFromNode(childNode));
}
for (Node childNode : node.childNodes()) {
buffer.append(buildStringFromNode(childNode));
}
if (node instanceof Element) {
Element element = (Element) node;
String tagName = element.tagName();
if ("p".equals(tagName) || "br".equals(tagName)) {
buffer.append("\n");
}
}
if (node instanceof Element) {
Element element = (Element) node;
String tagName = element.tagName();
if ("p".equals(tagName) || "br".equals(tagName)) {
buffer.append("\n");
}
}
return buffer;
return buffer;
}
private static String getActionLink(Notification notification2Save, String portalURL) {
StringBuilder actionLink = new StringBuilder("<a style=\"color:#3B5998; text-decoration:none\" target=\"_blank\" href=\"");
return completeActonLinkByNotificationType(notification2Save, actionLink, portalURL);
}
/**
* enqueue the message to send
* @param notification2Save
@ -182,17 +199,12 @@ public class EmailPlugin {
* @param portalName
* @param senderEmail
*/
public void sendNotification(Notification notification2Save, String vreName, String portalName, String senderEmail) {
EmailNotificationProducer thread = new EmailNotificationProducer(new NotificationMail(notification2Save, vreName, portalName, senderEmail));
public void sendNotification(Notification notification2Save, String vreName, String portalName, String senderEmail, String ... hashtags) {
EmailNotificationProducer thread = new EmailNotificationProducer(new NotificationMail(notification2Save, vreName, portalName, senderEmail, hashtags));
thread.start();
}
protected static Message getMessageNotification(
Session session,
Notification notification2Save,
String vreName,
String portalName,
String senderEmail) throws Exception {
protected static Message getMessageNotification(Session session, Notification notification2Save, String vreName, String portalName, String senderEmail, String ... hashtags) throws Exception {
UserModel user = null;
String portalUrl = null;
@ -214,13 +226,13 @@ public class EmailPlugin {
msg2Return.setFrom(new InternetAddress(senderEmail, portalName));
msg2Return.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
msg2Return.setSubject(getSubjectByNotificationType(notification2Save, portalUrl, vreName, user.getFirstName()));
msg2Return.setSubject(getSubjectByNotificationType(notification2Save, portalUrl, vreName, user.getFirstName(), hashtags));
final MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(getTextEmail(notification2Save, user.getFirstName(), portalUrl, email), "text/plain");
textPart.setContent(getTextEmail(notification2Save, user.getFirstName(), portalUrl, email, hashtags), "text/plain; charset=UTF-8");
final MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(getHTMLEmail(notification2Save, user.getFirstName(), portalUrl, email), "text/html; charset=UTF-8");
htmlPart.setContent(getHTMLEmail(notification2Save, user.getFirstName(), portalUrl, email, hashtags), "text/html; charset=UTF-8");
final Multipart mp = new MimeMultipart("alternative");
mp.addBodyPart(textPart);
@ -240,7 +252,7 @@ public class EmailPlugin {
* @param vreName
* @return
*/
private static String getSubjectByNotificationType(Notification notification2Save, String portalURL, String vreName, String userFirstName) {
private static String getSubjectByNotificationType(Notification notification2Save, String portalURL, String vreName, String userFirstName, String ...hashtags) {
switch (notification2Save.getType()) {
case LIKE:
return notification2Save.getSenderFullName()+" favorited your post";
@ -272,8 +284,12 @@ public class EmailPlugin {
return notification2Save.getSenderFullName() + " commented on your post";
case MENTION:
return notification2Save.getSenderFullName() + " mentioned you";
case POST_ALERT:
return notification2Save.getSenderFullName() + " shared a news on " + vreName;
case POST_ALERT:
String toReturn = notification2Save.getSenderFullName() + " shared a news on " + vreName;
if (hashtags != null)
for (int i = 0; i < hashtags.length; i++)
toReturn += " " + hashtags[i];
return toReturn;
case REQUEST_CONNECTION:
return "Connection request";
case JOB_COMPLETED_NOK:

View File

@ -12,14 +12,15 @@ public class NotificationMail {
private String vreName;
private String portalName;
private String senderEmail;
private String[] hashtags;
public NotificationMail(Notification notification2Send, String vreName,
String portalName, String senderEmail) {
public NotificationMail(Notification notification2Send, String vreName, String portalName, String senderEmail, String ... hashtags) {
super();
this.notification2Send = notification2Send;
this.vreName = vreName;
this.portalName = portalName;
this.senderEmail = senderEmail;
this.hashtags = hashtags;
}
protected Notification getNotification2Send() {
@ -37,4 +38,8 @@ public class NotificationMail {
protected String getSenderEmail() {
return senderEmail;
}
protected String[] getHashtags() {
return hashtags;
}
}