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
This commit is contained in:
Massimiliano Assante 2013-01-31 00:31:07 +00:00
parent c8efef754d
commit 312ee9a83a
5 changed files with 197 additions and 86 deletions

11
pom.xml
View File

@ -78,6 +78,17 @@
<artifactId>mail</artifactId>
<version>1.4.3</version>
</dependency>
<dependency>
<groupId>javax.portlet</groupId>
<artifactId>portlet-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.portal</groupId>
<artifactId>custom-portal-handler</artifactId>
<version>1.2.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -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;
}
/**

View File

@ -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);
}
}
}

View File

@ -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("&amp;", "&");
removeMarkup = removeMarkup.replaceAll("&gt;", ">");
removeMarkup = removeMarkup.replaceAll("&lt;", "<");
return "<body>" +
"<br />Hi " + userFirstName + "," +
"<p>" + notification2Save.getSenderFullName() + " " + removeMarkup + "</p>" +
"<br /><p>See this at <a href=\"portalUrl\">" + portalUrl + "</a>" +
"<br /><p><div style=\"color:#999999; font-size:11px; font-family:'lucida grande',tahoma,verdana,arial,sans-serif; padding-top:30px;\">" +
"This message was sent to <a href=\"mailto:"+email+"\" style=\"color:#3b5998;text-decoration:none\" target=\"_blank\">"+email+"</a>. " +
"If you don't want to receive these emails in the future, please <a href=\""+portalUrl+"/group/data-e-infrastructure-gateway/notifications\" style=\"color:#3b5998;text-decoration:none\" target=\"_blank\">unsubscribe</a>." +
"</div></p>" +
"</body>";
}
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";
}
}
}

View File

@ -0,0 +1,34 @@
<HTML>
<HEAD>
<TITLE>Pet Store Sale!</TITLE>
</HEAD>
<BODY>
<CENTER>
<B>$petList.size() Pets on Sale!</B>
<BR/>
This is an email generated by velocity
<BR/>
This month only, choose from :
#set( $count = 1 )
<TABLE>
#foreach( $pet in $petList )
<TR>
<TD>$count)</TD>
<TD>$pet.name</TD>
<TD>$pet.price</TD>
</TR>
#set( $count = $count + 1 )
#end
</TABLE>
<I>Call Today!</I>
Bests <br>
www.java2s.com
</CENTER>
</BODY>
</HTML>