From 312ee9a83a75c5c33394b9a928d462c6f0c43d9c Mon Sep 17 00:00:00 2001 From: "massimiliano.assante" Date: Thu, 31 Jan 2013 00:31:07 +0000 Subject: [PATCH] works like a charm git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerSocial@68592 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 11 ++ .../ApplicationNotificationsManager.java | 20 ++- .../social/EmailNotification.java | 80 ---------- .../social/mailing/EmailPlugin.java | 138 ++++++++++++++++++ .../social/mailing/email_html.vm | 34 +++++ 5 files changed, 197 insertions(+), 86 deletions(-) delete mode 100644 src/main/java/org/gcube/applicationsupportlayer/social/EmailNotification.java create mode 100644 src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java create mode 100644 src/main/java/org/gcube/applicationsupportlayer/social/mailing/email_html.vm diff --git a/pom.xml b/pom.xml index 5fd870d..ae425f9 100644 --- a/pom.xml +++ b/pom.xml @@ -78,6 +78,17 @@ mail 1.4.3 + + javax.portlet + portlet-api + 2.0 + provided + + + org.gcube.portal + custom-portal-handler + 1.2.0-SNAPSHOT + junit junit diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java b/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java index 593a9c2..fd4883d 100644 --- a/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java +++ b/src/main/java/org/gcube/applicationsupportlayer/social/ApplicationNotificationsManager.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.UUID; import org.gcube.application.framework.core.session.ASLSession; +import org.gcube.applicationsupportlayer.social.mailing.EmailPlugin; import org.gcube.common.core.utils.logging.GCUBEClientLog; import org.gcube.portal.databook.shared.ApplicationProfile; import org.gcube.portal.databook.shared.Notification; @@ -61,16 +62,23 @@ public class ApplicationNotificationsManager extends SocialPortalBridge implemen try { channels = getStoreInstance().getUserNotificationChannels(notification2Save.getUserid()); } catch (NotificationChannelTypeNotFoundException e) { - // TODO Auto-generated catch block e.printStackTrace(); } boolean result = false; - if (channels.contains(NotificationChannelType.PORTAL)) + if (channels.contains(NotificationChannelType.PORTAL)) { result = getStoreInstance().saveNotification(notification2Save); - if (result) - _log.trace("Notification Saved Successfully! "); - else - _log.error("While trying to save Notification"); + if (result) + _log.trace("Notification Saved Successfully! "); + else + _log.error("Error While trying to save Notification"); + } + if (channels.contains(NotificationChannelType.EMAIL)) + EmailPlugin.sendNotification(notification2Save); + + if (channels.isEmpty()) { + _log.info("Notification was not needed as the user decided not to be notified"); + result = true; + } return result; } /** diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/EmailNotification.java b/src/main/java/org/gcube/applicationsupportlayer/social/EmailNotification.java deleted file mode 100644 index 3b77e2d..0000000 --- a/src/main/java/org/gcube/applicationsupportlayer/social/EmailNotification.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.gcube.applicationsupportlayer.social; - -import java.util.Date; -import java.util.Properties; - -import javax.mail.Address; -import javax.mail.Message; -import javax.mail.Session; -import javax.mail.Transport; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeMessage; - -import org.gcube.common.core.utils.logging.GCUBEClientLog; - -/** - * A class for sending email - * - * @author Massimiliano Assante - * - */ -public class EmailNotification { - - private static GCUBEClientLog _log = new GCUBEClientLog(EmailNotification.class); - - private String sender; - private String recipients[]; - private String subject; - private String body; - - /** - * - * @param sender - * @param subject - * @param body - * @param recipients - */ - public EmailNotification(String sender, String subject, String body, String... recipients) { - if (recipients == null || recipients.length == 0) - throw new IllegalArgumentException("There must be at least one recipient"); - this.sender = sender; - this.recipients = recipients; - this.subject = subject; - this.body = body; - } - - public void send() { - Properties props = System.getProperties(); - String mailServiceHost = "localhost"; - props.put("mail.smtp.host", mailServiceHost); - String mailServicePort = "25"; - props.put("mail.smtp.port", mailServicePort); - Session session = Session.getDefaultInstance(props, null); - session.setDebug(true); - Message mimeMessage = new MimeMessage(session); - - try { - // EMAIL SENDER - Address from = new InternetAddress(sender); - mimeMessage.setFrom(from); - - // EMAIL RECIPIENTS - for (int i=0; i < recipients.length; i++) { - Address address = new InternetAddress(recipients[i]); - mimeMessage.addRecipient(Message.RecipientType.TO, address); - - } - - mimeMessage.setSubject(subject); - // mimeMessage.setText(body); - mimeMessage.setContent(body, "text/html"); - mimeMessage.setSentDate(new Date()); - Transport.send(mimeMessage); - } catch (Exception e) { - e.printStackTrace(); - _log.error("Failed to send the notification email:", e); - } -} -} - - diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java new file mode 100644 index 0000000..651cdbe --- /dev/null +++ b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java @@ -0,0 +1,138 @@ +package org.gcube.applicationsupportlayer.social.mailing; + +import java.util.Date; +import java.util.Properties; + +import javax.mail.Address; +import javax.mail.Message; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +import org.gcube.common.core.utils.logging.GCUBEClientLog; +import org.gcube.portal.custom.communitymanager.OrganizationsUtil; +import org.gcube.portal.databook.shared.Notification; + +import com.liferay.portal.model.UserModel; +import com.liferay.portal.service.UserLocalServiceUtil; +import com.liferay.portal.util.PortalUtil; + +/** + * + * @author Massimiliano Assante + * + */ +public class EmailPlugin { + private static GCUBEClientLog _log = new GCUBEClientLog(EmailPlugin.class); + + private static String getHTMLEmail(Notification notification2Save, String userFirstName, String portalUrl, String email) { + + String removeMarkup = notification2Save.getDescription().replaceAll("&", "&"); + removeMarkup = removeMarkup.replaceAll(">", ">"); + removeMarkup = removeMarkup.replaceAll("<", "<"); + + return "" + + "
Hi " + userFirstName + "," + + "

" + notification2Save.getSenderFullName() + " " + removeMarkup + "

" + + "

See this at " + portalUrl + "" + + "

" + + "This message was sent to "+email+". " + + "If you don't want to receive these emails in the future, please unsubscribe." + + "

" + + ""; + + } + + public static void sendNotification(Notification notification2Save) { + + UserModel user = null; + String portalUrl = null; + try { + user = UserLocalServiceUtil.getUserByScreenName(OrganizationsUtil.getCompany().getCompanyId(), notification2Save.getUserid()); + portalUrl = PortalUtil.getPortalURL(OrganizationsUtil.getCompany().getVirtualHost(), PortalUtil.getPortalPort(), true); + } catch (Exception e1) { + e1.printStackTrace(); + } + String email = user.getEmailAddress(); + + Properties props = System.getProperties(); + String mailServiceHost = "localhost"; + props.put("mail.smtp.host", mailServiceHost); + String mailServicePort = "25"; + props.put("mail.smtp.port", mailServicePort); + Session session = Session.getDefaultInstance(props, null); + session.setDebug(true); + Message mimeMessage = new MimeMessage(session); + + try { + // EMAIL SENDER + Address from = new InternetAddress("no-reply@d4science.org"); + mimeMessage.setFrom(from); + + Address address = new InternetAddress(email); + mimeMessage.addRecipient(Message.RecipientType.TO, address); + + mimeMessage.setSubject(getSubjectByNotificationType(notification2Save)); + // mimeMessage.setText(body); + mimeMessage.setContent(getHTMLEmail(notification2Save, user.getFirstName(), portalUrl, email), "text/html"); + mimeMessage.setSentDate(new Date()); + Transport.send(mimeMessage); + _log.trace("notification email sent successfully"); + } catch (Exception e) { + e.printStackTrace(); + _log.error("While sending the notification email:", e); + } + } + + private static String getSubjectByNotificationType(Notification notification2Save) { + switch (notification2Save.getType()) { + case LIKE: + return notification2Save.getSenderFullName() + " liked your post"; + case COMMENT: + return notification2Save.getSenderFullName() + " also replied on a post you replied"; + case MESSAGE: + return notification2Save.getSenderFullName() + " sent you a message"; + case WP_FOLDER_ADDEDUSER: + return notification2Save.getSenderFullName() + " added a new user to your shared folder"; + case WP_FOLDER_REMOVEDUSER: + return notification2Save.getSenderFullName() + " removed a user to your shared folder"; + case WP_FOLDER_SHARE: + return notification2Save.getSenderFullName() + " shared a folder with you"; + case WP_ITEM_NEW: + return notification2Save.getSenderFullName() + " added a new item to your shared folder"; + case WP_ITEM_DELETE: + return notification2Save.getSenderFullName() + " deleted an item from your shared folder"; + case WP_ITEM_UPDATED: + return notification2Save.getSenderFullName() + " updated an item in your shared folder"; + case OWN_COMMENT: + return notification2Save.getSenderFullName() + " replied to your post"; + case REQUEST_CONNECTION: + return notification2Save.getSenderFullName() + " wants to connect with you"; + case JOB_COMPLETED_NOK: + return notification2Save.getSenderFullName() + " job KO"; + case JOB_COMPLETED_OK: + return notification2Save.getSenderFullName() + " job OK"; + case DOCUMENT_WORKFLOW_EDIT: + return notification2Save.getSenderFullName() + " edited your Document Workflow"; + case DOCUMENT_WORKFLOW_VIEW: + return notification2Save.getSenderFullName() + " viewed your Document Workflow"; + case DOCUMENT_WORKFLOW_STEP_REQUEST_TASK: + return "You are requested to perform a new task in the Document Workflow"; + case DOCUMENT_WORKFLOW_FIRST_STEP_REQUEST_INVOLVMENT: + return notification2Save.getSenderFullName() + " has involved you in a Document Workflow "; + case DOCUMENT_WORKFLOW_USER_FORWARD_TO_OWNER: + return notification2Save.getSenderFullName() + " has forwarded a document workflow you created"; + case DOCUMENT_WORKFLOW_STEP_FORWARD_PEER: + return notification2Save.getSenderFullName() + " has forwarded a document workflow you are involved into"; + case DOCUMENT_WORKFLOW_FORWARD_STEP_COMPLETED_OWNER: + return notification2Save.getSenderFullName() + " has performed the last needed forward on a document workflow you created "; + default: + return "You have a new Notification"; + } + } + +} + + + diff --git a/src/main/java/org/gcube/applicationsupportlayer/social/mailing/email_html.vm b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/email_html.vm new file mode 100644 index 0000000..2bb0030 --- /dev/null +++ b/src/main/java/org/gcube/applicationsupportlayer/social/mailing/email_html.vm @@ -0,0 +1,34 @@ + + + Pet Store Sale! + + + + +
+ $petList.size() Pets on Sale! + +
+ This is an email generated by velocity +
+ This month only, choose from : + + #set( $count = 1 ) + + #foreach( $pet in $petList ) + + + + + + #set( $count = $count + 1 ) + #end +
$count)$pet.name$pet.price
+ + Call Today! + Bests
+ www.java2s.com +
+ + +