2015-06-22 15:56:57 +02:00
package org.gcube.portal.invites ;
2015-06-30 18:49:37 +02:00
import java.util.ArrayList ;
2015-06-30 13:57:13 +02:00
import java.util.Date ;
2015-06-30 18:49:37 +02:00
import java.util.List ;
2016-03-14 14:29:15 +01:00
import java.util.Map ;
2015-06-30 18:49:37 +02:00
import java.util.Set ;
2015-06-30 13:57:13 +02:00
import java.util.UUID ;
2015-06-22 15:56:57 +02:00
2015-06-30 13:57:13 +02:00
import javax.mail.internet.AddressException ;
2016-03-24 12:19:08 +01:00
import javax.servlet.http.HttpServletRequest ;
2015-06-22 15:56:57 +02:00
2016-03-14 14:29:15 +01:00
import org.gcube.common.portal.PortalContext ;
2016-03-24 15:47:09 +01:00
import org.gcube.common.portal.mailing.EmailNotification ;
2015-06-30 13:57:13 +02:00
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl ;
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 ;
2017-05-11 19:50:54 +02:00
import org.gcube.portal.mailing.message.Recipient ;
import org.gcube.portal.mailing.service.EmailTemplateService ;
import org.gcube.portal.mailing.templates.TemplatedJoinMeInvite ;
2015-06-30 18:49:37 +02:00
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault ;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException ;
2016-03-14 14:29:15 +01:00
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager ;
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 ;
2015-06-22 15:56:57 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
*
* @author Massimiliano Assante
*
* /
public class InvitesManager {
private static final Logger _log = LoggerFactory . getLogger ( InvitesManager . class ) ;
private static InvitesManager instance ;
2015-06-30 13:57:13 +02:00
private static DatabookStore store ;
2015-06-22 15:56:57 +02:00
private InvitesManager ( ) { }
public static InvitesManager getInstance ( ) {
instance = new InvitesManager ( ) ;
2015-06-30 13:57:13 +02:00
initStore ( ) ;
2015-06-22 15:56:57 +02:00
return instance ;
}
2015-06-30 13:57:13 +02:00
/ * *
*
* @return the unique instance of the store
* /
public static synchronized DatabookStore initStore ( ) {
if ( store = = null ) {
store = new DBCassandraAstyanaxImpl ( ) ;
}
return store ;
}
2016-04-26 17:14:04 +02:00
2015-06-30 13:57:13 +02:00
public InviteOperationResult sendInvite (
2016-03-24 12:19:08 +01:00
HttpServletRequest request ,
2015-06-30 13:57:13 +02:00
String portalSenderEmail ,
String portalURL ,
String name ,
String lastName ,
String email ,
String vreDescription ) {
2016-04-26 17:14:04 +02:00
2016-11-15 12:09:54 +01:00
GCubeUser currUser = PortalContext . getConfiguration ( ) . getCurrentUser ( request ) ;
String username = currUser . getUsername ( ) ;
String fromFullName = currUser . getFullname ( ) ;
2015-06-30 13:57:13 +02:00
String controlcode = UUID . randomUUID ( ) . toString ( ) ;
2016-11-15 12:09:54 +01:00
String currScope = PortalContext . getConfiguration ( ) . getCurrentScope ( request ) ;
2016-04-26 17:14:04 +02:00
2016-11-15 12:09:54 +01:00
Invite invite = new Invite ( UUID . randomUUID ( ) . toString ( ) , username , currScope , email , controlcode , InviteStatus . PENDING , new Date ( ) , fromFullName ) ;
2015-06-30 13:57:13 +02:00
InviteOperationResult result = null ;
boolean emailResult = false ;
try {
2016-11-15 12:09:54 +01:00
String vreName = PortalContext . getConfiguration ( ) . getCurrentGroupName ( request ) ;
2015-06-30 13:57:13 +02:00
result = store . saveInvite ( invite ) ;
2016-11-15 12:09:54 +01:00
emailResult = sendInviteEmail ( request , vreName , portalSenderEmail , portalURL , name , lastName , email , vreDescription ) ;
notifyInviteSent ( request , username , currScope , invite , vreName ) ;
2016-04-26 17:14:04 +02:00
2015-06-30 13:57:13 +02:00
} catch ( AddressException e ) {
_log . error ( " Email not valid " + e . getMessage ( ) ) ;
e . printStackTrace ( ) ;
return InviteOperationResult . FAILED ;
}
2016-04-26 17:14:04 +02:00
2015-06-30 13:57:13 +02:00
return ( emailResult ) ? result : InviteOperationResult . FAILED ;
}
2016-04-26 17:14:04 +02:00
2015-06-30 13:57:13 +02:00
private Boolean sendInviteEmail (
2016-03-24 12:19:08 +01:00
HttpServletRequest request ,
2016-11-15 12:09:54 +01:00
String vreName ,
2015-06-22 15:56:57 +02:00
String portalSenderEmail ,
String portalURL ,
String name ,
String lastName ,
String email ,
String vreDescription ) {
2017-05-11 19:50:54 +02:00
PortalContext pContext = PortalContext . getConfiguration ( ) ;
GCubeUser currUser = pContext . getCurrentUser ( request ) ;
String gatewayURL = pContext . getGatewayURL ( request ) ;
String gatewayName = pContext . getGatewayName ( request ) ;
2015-06-22 15:56:57 +02:00
2016-04-26 17:14:04 +02:00
try {
2017-05-11 19:50:54 +02:00
String subject = " Join me on " + vreName + " VRE " ;
2015-06-22 15:56:57 +02:00
2016-11-15 12:09:54 +01:00
long groupId = PortalContext . getConfiguration ( ) . getCurrentGroupId ( request ) ;
2016-03-24 12:19:08 +01:00
final String linkToAcceptInvite = portalURL + PortalContext . getConfiguration ( ) . getSiteLandingPagePath ( request ) + " /explore?siteId= " + groupId ;
2016-07-19 15:49:55 +02:00
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 " ;
2017-05-11 19:50:54 +02:00
EmailTemplateService . send (
2016-04-26 17:14:04 +02:00
subject ,
2017-05-11 19:50:54 +02:00
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 ) ;
2015-06-22 15:56:57 +02:00
} catch ( Exception mex ) {
mex . printStackTrace ( ) ;
_log . error ( " Sent message ERROR to " + email ) ;
return false ;
}
return true ;
}
2017-05-11 19:50:54 +02:00
2015-06-30 18:49:37 +02:00
private static ArrayList < String > getAdministratorsEmails ( String scope ) {
LiferayUserManager userManager = new LiferayUserManager ( ) ;
LiferayGroupManager groupManager = new LiferayGroupManager ( ) ;
2016-03-14 14:29:15 +01:00
long groupId = - 1 ;
2015-06-30 18:49:37 +02:00
try {
2016-03-14 14:29:15 +01:00
List < GCubeGroup > allGroups = groupManager . listGroups ( ) ;
2015-06-30 18:49:37 +02:00
_log . debug ( " Number of groups retrieved: " + allGroups . size ( ) ) ;
for ( int i = 0 ; i < allGroups . size ( ) ; i + + ) {
2016-03-14 14:29:15 +01:00
long grId = allGroups . get ( i ) . getGroupId ( ) ;
String groupScope = groupManager . getInfrastructureScope ( grId ) ;
_log . debug ( " Comparing: " + groupScope + " " + scope ) ;
2015-06-30 18:49:37 +02:00
if ( groupScope . equals ( scope ) ) {
groupId = allGroups . get ( i ) . getGroupId ( ) ;
break ;
}
}
} catch ( UserManagementSystemException e ) {
e . printStackTrace ( ) ;
} catch ( GroupRetrievalFault e ) {
e . printStackTrace ( ) ;
}
2016-03-14 14:29:15 +01:00
Map < GCubeUser , List < GCubeRole > > usersAndRoles = null ;
2015-06-30 18:49:37 +02:00
try {
usersAndRoles = userManager . listUsersAndRolesByGroup ( groupId ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2016-03-14 14:29:15 +01:00
Set < GCubeUser > users = usersAndRoles . keySet ( ) ;
2015-06-30 18:49:37 +02:00
ArrayList < String > adminEmailsList = new ArrayList < String > ( ) ;
2016-03-14 14:29:15 +01:00
for ( GCubeUser usr : users ) {
List < GCubeRole > roles = usersAndRoles . get ( usr ) ;
2015-06-30 18:49:37 +02:00
for ( int i = 0 ; i < roles . size ( ) ; i + + ) {
if ( roles . get ( i ) . getRoleName ( ) . equals ( " VO-Admin " ) | | roles . get ( i ) . getRoleName ( ) . equals ( " VRE-Manager " ) ) {
adminEmailsList . add ( usr . getEmail ( ) ) ;
_log . debug ( " Admin: " + usr . getFullname ( ) ) ;
break ;
}
}
}
return adminEmailsList ;
}
2016-04-26 17:14:04 +02:00
2015-06-30 18:49:37 +02:00
/ * *
*
* @param scope .
* @param optionalMessage .
* /
2016-04-26 17:14:04 +02:00
public static void notifyInviteSent ( HttpServletRequest request , String username , String scope , Invite invite , String vreName ) {
2015-06-30 18:49:37 +02:00
ArrayList < String > adminEmails = getAdministratorsEmails ( scope ) ;
StringBuffer body = new StringBuffer ( ) ;
2016-04-26 17:14:04 +02:00
body . append ( " <p>Dear manager of " + scope + " ,<br />this email message was automatically generated by " + PortalContext . getConfiguration ( ) . getGatewayURL ( request ) + " to inform you that " ) ;
2015-06-30 18:49:37 +02:00
body . append ( " </p> " ) ;
body . append ( " <p> " ) ;
2015-06-30 19:29:37 +02:00
body . append ( " <b> " + invite . getSenderFullName ( ) + " ( " + invite . getSenderUserId ( ) + " )</b> has invited " + invite . getInvitedEmail ( ) + " to the following environment: " ) ;
2015-06-30 18:49:37 +02:00
body . append ( " <br /><br /> " ) ;
body . append ( " <b> " + scope + " </b> " ) ;
body . append ( " </p> " ) ;
2016-04-26 17:14:04 +02:00
2015-06-30 18:49:37 +02:00
String [ ] allMails = new String [ adminEmails . size ( ) ] ;
adminEmails . toArray ( allMails ) ;
2016-04-26 17:14:04 +02:00
EmailNotification mailToAdmin = new EmailNotification ( allMails , " An invite was sent on " + vreName + " by " + invite . getSenderFullName ( ) , body . toString ( ) , request ) ;
2015-06-30 18:49:37 +02:00
mailToAdmin . sendEmail ( ) ;
}
2016-04-26 17:14:04 +02:00
2015-06-22 15:56:57 +02:00
}