From 4380cc01dbf45c28fb77aea7261c6c22a5c09b19 Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Tue, 5 Jul 2016 12:09:31 +0000 Subject: [PATCH] using thread for sending email git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/liferay62-plugins/user-registration-hook@129917 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 7 +- .../MyCreateUserAccountListener.java | 67 ++----------------- 2 files changed, 10 insertions(+), 64 deletions(-) diff --git a/pom.xml b/pom.xml index 21bf86d..6df873b 100644 --- a/pom.xml +++ b/pom.xml @@ -60,6 +60,11 @@ + + org.gcube.portal + notifications-common-library + provided + org.gcube.dvos usermanagement-core @@ -86,13 +91,11 @@ com.liferay.portal portal-service - ${liferay.version} provided com.liferay.portal util-java - ${liferay.version} provided diff --git a/src/main/java/org/gcube/portal/usersaccount/MyCreateUserAccountListener.java b/src/main/java/org/gcube/portal/usersaccount/MyCreateUserAccountListener.java index 0323413..986514e 100644 --- a/src/main/java/org/gcube/portal/usersaccount/MyCreateUserAccountListener.java +++ b/src/main/java/org/gcube/portal/usersaccount/MyCreateUserAccountListener.java @@ -1,18 +1,6 @@ package org.gcube.portal.usersaccount; -import java.util.List; - -import org.gcube.common.portal.PortalContext; -import org.gcube.common.portal.mailing.EmailNotification; -import org.gcube.vomanagement.usermanagement.GroupManager; -import org.gcube.vomanagement.usermanagement.RoleManager; -import org.gcube.vomanagement.usermanagement.UserManager; -import org.gcube.vomanagement.usermanagement.exception.RoleRetrievalFault; -import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager; -import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager; -import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; -import org.gcube.vomanagement.usermanagement.model.GCubeUser; -import org.gcube.vomanagement.usermanagement.model.GatewayRolesNames; +import org.gcube.portal.notifications.thread.NewUserAccountNotificationThread; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,55 +23,10 @@ public class MyCreateUserAccountListener extends BaseModelListener { @Override public void onAfterCreate(User user) throws ModelListenerException { - System.out.println("onAfterCreate User INTERCEPTED " + user.getFullName()); - handleUserRegistration(user); + _log.info("onAfterCreate NewUserAccount listener for: " + user.getScreenName() + " / " + user.getFullName()); + Thread emailManagersThread = new Thread(new NewUserAccountNotificationThread(user.getScreenName(), user.getFullName(), user.getEmailAddress())); + emailManagersThread.start(); } - private void handleUserRegistration(User user) { - UserManager um = new LiferayUserManager(); - GroupManager gm = new LiferayGroupManager(); - RoleManager rm = new LiferayRoleManager(); - try { - System.out.println("addUser hook ON"); - String rootVoName = PortalContext.getConfiguration().getInfrastructureName(); - long groupId = gm.getGroupIdFromInfrastructureScope("/"+rootVoName); - long infraManagerRoleId = -1; - try { - infraManagerRoleId = rm.getRoleIdByName(GatewayRolesNames.INFRASTRUCTURE_MANAGER.getRoleName()); - } - catch (RoleRetrievalFault e) { - _log.warn("There is no (Site) Role " + infraManagerRoleId + " in this portal. Will not notify about newly user accounts."); - return; - } - _log.trace("Root is: " + rootVoName + " Scanning roles ...."); - - List managers = um.listUsersByGroupAndRole(groupId, infraManagerRoleId); - if (managers == null || managers.isEmpty()) { - _log.warn("There are no users with (Site) Role " + infraManagerRoleId + " on " + rootVoName + " in this portal. Will not notify about newly user accounts."); - } - else { - for (GCubeUser manager : managers) { - sendNotification(manager, user); - _log.info("sent email to manager: " + manager.getEmail()); - } - } - - } catch (Exception e) { - e.printStackTrace(); - } - } - - private void sendNotification(GCubeUser manager, User user) { - StringBuilder body = new StringBuilder("Dear ").append(manager.getFirstName()) - .append("

").append("") - .append(user.getFullName()).append(" (").append(user.getScreenName()).append(")").append("").append(" registered to the infrastructure gateway with the following email: ").append(user.getEmailAddress()) - .append("

") - .append("

") - .append("You received this email because you are an Infrastructure Manager in this portal.") - .append("

"); - - - EmailNotification toSend = new EmailNotification(manager.getEmail(), SUBJECT, body.toString(), null); - toSend.sendEmail(); - } + } \ No newline at end of file