aslsocial/src/main/java/org/gcube/applicationsupportlayer/social/mailing/EmailPlugin.java

139 lines
5.9 KiB
Java

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("&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";
}
}
}