From 0ec56ee80cff1085651fa59793d9fc9556a3a4db Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Thu, 11 May 2017 17:50:54 +0000 Subject: [PATCH] partially support for template email added git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/invites-common-library@148502 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 2 +- .settings/org.eclipse.jdt.core.prefs | 2 +- pom.xml | 13 +- .../gcube/portal/invites/InvitesManager.java | 126 +++--------------- 4 files changed, 30 insertions(+), 113 deletions(-) diff --git a/.classpath b/.classpath index faa9002..d6c4436 100644 --- a/.classpath +++ b/.classpath @@ -18,7 +18,7 @@ - + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 443e085..b257af7 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,6 +1,6 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 org.eclipse.jdt.core.compiler.compliance=1.7 org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enumIdentifier=error diff --git a/pom.xml b/pom.xml index c8611c4..bfc369a 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ org.gcube.portal invites-common-library - 1.3.0-SNAPSHOT + 1.4.0-SNAPSHOT jar gCube Invites Common Library is a common library containing shared code for Inviting external users to portal VREs. @@ -26,7 +26,7 @@ distro 1.7 - 1.7 + 1.8 6.2.5 UTF-8 UTF-8 @@ -53,6 +53,11 @@ portal-manager provided + + org.gcube.portal.mailing + email-templates-library + [1.0.0-SNAPSHOT,) + org.gcube.dvos usermanagement-core @@ -104,8 +109,8 @@ maven-compiler-plugin - 1.7 - 1.7 + ${maven.compiler.source} + ${maven.compiler.target} diff --git a/src/main/java/org/gcube/portal/invites/InvitesManager.java b/src/main/java/org/gcube/portal/invites/InvitesManager.java index 76fadd5..0a56f1a 100644 --- a/src/main/java/org/gcube/portal/invites/InvitesManager.java +++ b/src/main/java/org/gcube/portal/invites/InvitesManager.java @@ -17,6 +17,9 @@ import org.gcube.portal.databook.server.DatabookStore; import org.gcube.portal.databook.shared.Invite; import org.gcube.portal.databook.shared.InviteOperationResult; import org.gcube.portal.databook.shared.InviteStatus; +import org.gcube.portal.mailing.message.Recipient; +import org.gcube.portal.mailing.service.EmailTemplateService; +import org.gcube.portal.mailing.templates.TemplatedJoinMeInvite; import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; @@ -24,11 +27,6 @@ import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; import org.gcube.vomanagement.usermanagement.model.GCubeGroup; import org.gcube.vomanagement.usermanagement.model.GCubeRole; import org.gcube.vomanagement.usermanagement.model.GCubeUser; -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.nodes.Node; -import org.jsoup.nodes.TextNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -103,24 +101,28 @@ public class InvitesManager { String lastName, String email, String vreDescription) { - - GCubeUser currUser = PortalContext.getConfiguration().getCurrentUser(request); - String fromFullName = currUser.getFullname(); + + PortalContext pContext = PortalContext.getConfiguration(); + GCubeUser currUser = pContext.getCurrentUser(request); + String gatewayURL = pContext.getGatewayURL(request); + String gatewayName = pContext.getGatewayName(request); + try { - String subject = "Join me on " + vreName; + String subject = "Join me on " + vreName + " VRE"; long groupId = PortalContext.getConfiguration().getCurrentGroupId(request); final String linkToAcceptInvite = portalURL + PortalContext.getConfiguration().getSiteLandingPagePath(request)+"/explore?siteId="+groupId; final String linkToCreateAccount = portalURL + "/?p_p_id=58&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&saveLastPath=false&_58_struts_action=%2Flogin%2Fcreate_account"; - - EmailNotification mailToAdmin = new EmailNotification( - email , + + EmailTemplateService.send( subject, - getHTMLEmail(name, lastName, email, fromFullName, vreName, vreDescription, linkToAcceptInvite, linkToCreateAccount), - request); - mailToAdmin.sendEmail(); - _log.debug("Sent message successfully to " + email ); + new TemplatedJoinMeInvite(gatewayName, gatewayURL, currUser, name, vreName, vreDescription, linkToAcceptInvite, linkToCreateAccount), + request, + new Recipient(email)); + + + _log.debug("Join Me Invite email message sent successfully to " + email ); } catch (Exception mex) { mex.printStackTrace(); _log.error("Sent message ERROR to " + email ); @@ -128,97 +130,7 @@ public class InvitesManager { } return true; } - - /** - * - * @param aslSession - * @param portalUrl the url of the portal - * @param name the name of the invited person - * @param lastName the last name of the invited person - * @param email the email address of the invite person - * @param fromFullName who is inviting - * @param vreName the name of the environment where you are inviting the person - * @param vreDescription the description of the environment where you are inviting the person - * @return the email text - */ - private String getHTMLEmail( - String name, - String lastName, - String email, - String fromFullName, - String vreName, - String vreDescription, - String linkToAcceptInvite, - String linkToCreateAccount) { - - linkToAcceptInvite = " " + linkToAcceptInvite + " "; - linkToCreateAccount = " " + linkToCreateAccount + " "; - - StringBuilder body = new StringBuilder(); - body.append("
") - .append("Dear " + name) - .append(",

") - .append(fromFullName).append(" has invited you to " + vreName + ", you can find a brief description below:") - .append("

") - .append("

").append(vreDescription) - .append("

") - .append("

To accept the invite just follow this link: " + linkToAcceptInvite) - .append("

") - .append("

") - .append("Please note: if you do not have an account yet, sign up first: " + linkToCreateAccount) - .append("

") - .append("
"); - return body.toString(); - - } - - /** - * Convert html into simple text - * - */ - protected static String convertHTML2Text(String html) { - if (html == null) { - return null; - } - String removedMarkup = html.replaceAll("&", "&"); - removedMarkup = removedMarkup.replaceAll(">", ">"); - removedMarkup = removedMarkup.replaceAll("<", "<"); - String text = removedMarkup; - try { - Document document = Jsoup.parse(removedMarkup); - Element body = document.body(); - text = buildStringFromNode(body).toString(); - } - catch (Exception e) { - _log.error("While converting HTML into text: " +e.getMessage()); - return removedMarkup; - } - return text; - } - - private static StringBuffer buildStringFromNode(Node node) { - StringBuffer buffer = new StringBuffer(); - - if (node instanceof TextNode) { - TextNode textNode = (TextNode) node; - buffer.append(textNode.text().trim()); - } - - 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) || "div".equals(tagName) || "h1".equals(tagName) || "h2".equals(tagName) || "h3".equals(tagName) || "h4".equals(tagName)) { - buffer.append("\n"); - } - } - - return buffer; - } - + private static ArrayList getAdministratorsEmails(String scope) { LiferayUserManager userManager = new LiferayUserManager(); LiferayGroupManager groupManager = new LiferayGroupManager();