Invite emails are now sent via Email Templates Library

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/invites-common-library@148572 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2017-05-12 12:43:15 +00:00
parent 0ec56ee80c
commit 7073028bb6
2 changed files with 39 additions and 33 deletions

View File

@ -1,5 +1,9 @@
<ReleaseNotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <ReleaseNotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="xsd/changelog.xsd"> xsi:noNamespaceSchemaLocation="xsd/changelog.xsd">
<Changeset component="org.gcube.common.portal.invites-common-library.1-4-0"
date="2017-05-12">
<Change>Invite emails are now sent via Email Templates Library</Change>
</Changeset>
<Changeset component="org.gcube.common.portal.invites-common-library.1-3-0" <Changeset component="org.gcube.common.portal.invites-common-library.1-3-0"
date="2016-11-27"> date="2016-11-27">
<Change>removed ASL Session, ported to gCube Client Context</Change> <Change>removed ASL Session, ported to gCube Client Context</Change>

View File

@ -11,14 +11,16 @@ import javax.mail.internet.AddressException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.gcube.common.portal.PortalContext; import org.gcube.common.portal.PortalContext;
import org.gcube.common.portal.mailing.EmailNotification;
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl; import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl;
import org.gcube.portal.databook.server.DatabookStore; import org.gcube.portal.databook.server.DatabookStore;
import org.gcube.portal.databook.shared.Invite; import org.gcube.portal.databook.shared.Invite;
import org.gcube.portal.databook.shared.InviteOperationResult; import org.gcube.portal.databook.shared.InviteOperationResult;
import org.gcube.portal.databook.shared.InviteStatus; import org.gcube.portal.databook.shared.InviteStatus;
import org.gcube.portal.mailing.message.EmailAddress;
import org.gcube.portal.mailing.message.Recipient; import org.gcube.portal.mailing.message.Recipient;
import org.gcube.portal.mailing.message.RecipientType;
import org.gcube.portal.mailing.service.EmailTemplateService; import org.gcube.portal.mailing.service.EmailTemplateService;
import org.gcube.portal.mailing.templates.TemplateUserHasInvited;
import org.gcube.portal.mailing.templates.TemplatedJoinMeInvite; import org.gcube.portal.mailing.templates.TemplatedJoinMeInvite;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault; import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
@ -80,8 +82,8 @@ public class InvitesManager {
try { try {
String vreName = PortalContext.getConfiguration().getCurrentGroupName(request); String vreName = PortalContext.getConfiguration().getCurrentGroupName(request);
result = store.saveInvite(invite); result = store.saveInvite(invite);
emailResult = sendInviteEmail(request, vreName, portalSenderEmail, portalURL, name, lastName, email, vreDescription); emailResult = sendInviteEmail(request, currUser, vreName, name, email, vreDescription);
notifyInviteSent(request, username, currScope, invite, vreName); notifyInviteSent(request, currUser, currScope, invite, vreName);
} catch (AddressException e) { } catch (AddressException e) {
_log.error("Email not valid " + e.getMessage()); _log.error("Email not valid " + e.getMessage());
@ -94,16 +96,13 @@ public class InvitesManager {
private Boolean sendInviteEmail( private Boolean sendInviteEmail(
HttpServletRequest request, HttpServletRequest request,
GCubeUser currUser,
String vreName, String vreName,
String portalSenderEmail,
String portalURL,
String name, String name,
String lastName,
String email, String email,
String vreDescription) { String vreDescription) {
PortalContext pContext = PortalContext.getConfiguration(); PortalContext pContext = PortalContext.getConfiguration();
GCubeUser currUser = pContext.getCurrentUser(request);
String gatewayURL = pContext.getGatewayURL(request); String gatewayURL = pContext.getGatewayURL(request);
String gatewayName = pContext.getGatewayName(request); String gatewayName = pContext.getGatewayName(request);
@ -112,14 +111,14 @@ public class InvitesManager {
String subject = "Join me on " + vreName + " VRE"; String subject = "Join me on " + vreName + " VRE";
long groupId = PortalContext.getConfiguration().getCurrentGroupId(request); long groupId = PortalContext.getConfiguration().getCurrentGroupId(request);
final String linkToAcceptInvite = portalURL + PortalContext.getConfiguration().getSiteLandingPagePath(request)+"/explore?siteId="+groupId; final String linkToAcceptInvite = gatewayURL + 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"; final String linkToCreateAccount = gatewayURL + "/?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";
EmailTemplateService.send( EmailTemplateService.send(
subject, subject,
new TemplatedJoinMeInvite(gatewayName, gatewayURL, currUser, name, vreName, vreDescription, linkToAcceptInvite, linkToCreateAccount), new TemplatedJoinMeInvite(gatewayName, gatewayURL, currUser, name, vreName, vreDescription, linkToAcceptInvite, linkToCreateAccount),
request, request,
new Recipient(email)); new Recipient(email), new Recipient(new EmailAddress(currUser.getEmail()), RecipientType.CC));
_log.debug("Join Me Invite email message sent successfully to " + email ); _log.debug("Join Me Invite email message sent successfully to " + email );
@ -130,6 +129,31 @@ public class InvitesManager {
} }
return true; return true;
} }
/**
*
* @param request
* @param username
* @param scope
* @param invite
* @param vreName
*/
public static void notifyInviteSent(HttpServletRequest request, GCubeUser currUser, String scope, Invite invite, String vreName) {
List<Recipient> adminRecipients = new ArrayList<>();
for (String email : getAdministratorsEmails(scope)) {
adminRecipients.add(new Recipient(email));
}
String gatewayURL = PortalContext.getConfiguration().getGatewayURL(request);
String gatewayName = PortalContext.getConfiguration().getGatewayName(request);
String subject = new StringBuffer("invite was sent on ").append(vreName).append(" by ").append(invite.getSenderFullName()).toString();
EmailTemplateService.send(
subject,
new TemplateUserHasInvited(currUser, invite.getInvitedEmail(), vreName, gatewayName, gatewayURL),
request,
adminRecipients.toArray(new Recipient[adminRecipients.size()]));
}
private static ArrayList<String> getAdministratorsEmails(String scope) { private static ArrayList<String> getAdministratorsEmails(String scope) {
LiferayUserManager userManager = new LiferayUserManager(); LiferayUserManager userManager = new LiferayUserManager();
@ -173,27 +197,5 @@ public class InvitesManager {
return adminEmailsList; return adminEmailsList;
} }
/**
*
* @param scope .
* @param optionalMessage .
*/
public static void notifyInviteSent(HttpServletRequest request, String username, String scope, Invite invite, String vreName) {
ArrayList<String> adminEmails = getAdministratorsEmails(scope);
StringBuffer body = new StringBuffer();
body.append("<p>Dear manager of "+ scope +",<br />this email message was automatically generated by " + PortalContext.getConfiguration().getGatewayURL(request) +" to inform you that ");
body.append("</p>");
body.append("<p>");
body.append("<b>"+invite.getSenderFullName() + " (" + invite.getSenderUserId() +")</b> has invited " + invite.getInvitedEmail() + " to the following environment:");
body.append("<br /><br />");
body.append("<b>" + scope+"</b>");
body.append("</p>");
String[] allMails = new String[adminEmails.size()];
adminEmails.toArray(allMails);
EmailNotification mailToAdmin = new EmailNotification(allMails , "An invite was sent on " + vreName + " by " + invite.getSenderFullName(), body.toString(), request);
mailToAdmin.sendEmail();
}
} }