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
pull/1/head
Massimiliano Assante 8 years ago
parent 8b084378de
commit 4380cc01db

@ -60,6 +60,11 @@
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.portal</groupId>
<artifactId>notifications-common-library</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.dvos</groupId>
<artifactId>usermanagement-core</artifactId>
@ -86,13 +91,11 @@
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>portal-service</artifactId>
<version>${liferay.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.liferay.portal</groupId>
<artifactId>util-java</artifactId>
<version>${liferay.version}</version>
<scope>provided</scope>
</dependency>
<dependency>

@ -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<User> {
@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<GCubeUser> 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("<p>").append("<b>")
.append(user.getFullName()).append(" (").append(user.getScreenName()).append(")").append("</b>").append(" registered to the infrastructure gateway with the following email: ").append(user.getEmailAddress())
.append("</p>")
.append("<p>")
.append("You received this email because you are an Infrastructure Manager in this portal.")
.append("</p>");
EmailNotification toSend = new EmailNotification(manager.getEmail(), SUBJECT, body.toString(), null);
toSend.sendEmail();
}
}
Loading…
Cancel
Save