package org.gcube.portal.invites; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; import javax.mail.internet.AddressException; import javax.servlet.http.HttpServletRequest; import org.gcube.application.framework.core.session.ASLSession; 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.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.vomanagement.usermanagement.exception.GroupRetrievalFault; import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; 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; 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; /** * * @author Massimiliano Assante * */ public class InvitesManager { private static final Logger _log = LoggerFactory.getLogger(InvitesManager.class); private static InvitesManager instance; private static DatabookStore store; private InvitesManager() { } public static InvitesManager getInstance(){ instance = new InvitesManager(); initStore(); return instance; } /** * * @return the unique instance of the store */ public static synchronized DatabookStore initStore() { if (store == null) { store = new DBCassandraAstyanaxImpl(); } return store; } public InviteOperationResult sendInvite( HttpServletRequest request, ASLSession aslSession, String portalSenderEmail, String portalURL, String name, String lastName, String email, String vreDescription) { String username = aslSession.getUsername(); String fromFullName = aslSession.getUserFullName(); String controlcode = UUID.randomUUID().toString(); Invite invite = new Invite(UUID.randomUUID().toString(), username, aslSession.getScopeName(), email, controlcode, InviteStatus.PENDING, new Date(), fromFullName); InviteOperationResult result = null; boolean emailResult = false; try { String vreName = aslSession.getGroupName(); result = store.saveInvite(invite); emailResult = sendInviteEmail(request, aslSession, portalSenderEmail, portalURL, name, lastName, email, vreDescription); notifyInviteSent(request, username, aslSession.getScopeName(), invite, vreName); } catch (AddressException e) { _log.error("Email not valid " + e.getMessage()); e.printStackTrace(); return InviteOperationResult.FAILED; } return (emailResult) ? result : InviteOperationResult.FAILED; } private Boolean sendInviteEmail( HttpServletRequest request, ASLSession aslSession, String portalSenderEmail, String portalURL, String name, String lastName, String email, String vreDescription) { String vreName = aslSession.getGroupName(); String fromFullName = aslSession.getUserFullName(); try { String subject = "Join me on " + vreName; long groupId = aslSession.getGroupId(); 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 , subject, getHTMLEmail(aslSession, name, lastName, email, fromFullName, vreName, vreDescription, linkToAcceptInvite, linkToCreateAccount), request); mailToAdmin.sendEmail(); _log.debug("Sent message successfully to " + email ); } catch (Exception mex) { mex.printStackTrace(); _log.error("Sent message ERROR to " + email ); return false; } 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( ASLSession aslSession, 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(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("Dear manager of "+ scope +",
this email message was automatically generated by " + PortalContext.getConfiguration().getGatewayURL(request) +" to inform you that ");
body.append("
");
body.append(""+invite.getSenderFullName() + " (" + invite.getSenderUserId() +") has invited " + invite.getInvitedEmail() + " to the following environment:");
body.append("
");
body.append("" + scope+"");
body.append("