From fb8e7b6ac8bee7e601d8e6cb4f347a73bb853a2e Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Thu, 4 Nov 2021 10:41:09 +0100 Subject: [PATCH] added copy of info submitted email to vre managers added --- .../portlets/user/moving/CompileForm.java | 147 +++++++++++++++++- .../user/moving/FormConfigurationAction.java | 37 +++++ src/main/webapp/WEB-INF/liferay-portlet.xml | 2 + src/main/webapp/WEB-INF/portlet.xml | 4 + src/main/webapp/css/main.css | 12 +- src/main/webapp/html/form-compile/config.jsp | 29 ++++ src/main/webapp/html/init.jsp | 3 + 7 files changed, 223 insertions(+), 11 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/moving/FormConfigurationAction.java create mode 100644 src/main/webapp/html/form-compile/config.jsp diff --git a/src/main/java/org/gcube/portlets/user/moving/CompileForm.java b/src/main/java/org/gcube/portlets/user/moving/CompileForm.java index 674f3c1..7bb25a1 100644 --- a/src/main/java/org/gcube/portlets/user/moving/CompileForm.java +++ b/src/main/java/org/gcube/portlets/user/moving/CompileForm.java @@ -5,8 +5,11 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Properties; +import java.util.Set; import javax.mail.Message; import javax.mail.MessagingException; @@ -20,10 +23,26 @@ import javax.portlet.ActionResponse; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; +import javax.servlet.http.HttpServletRequest; +import org.gcube.common.portal.GCubePortalConstants; import org.gcube.common.portal.PortalContext; +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.gcube.vomanagement.usermanagement.model.GatewayRolesNames; +import com.liferay.portal.kernel.exception.PortalException; +import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.log.LogFactoryUtil; +import com.liferay.portal.kernel.util.GetterUtil; +import com.liferay.portal.kernel.util.StringPool; +import com.liferay.portal.service.GroupLocalServiceUtil; +import com.liferay.portal.util.PortalUtil; import com.liferay.util.bridges.mvc.MVCPortlet; /** @@ -31,15 +50,22 @@ import com.liferay.util.bridges.mvc.MVCPortlet; */ public class CompileForm extends MVCPortlet { private static com.liferay.portal.kernel.log.Log _log = LogFactoryUtil.getLog(CompileForm.class); + + private static final String MANAGE_USERS_REQUESTS_FRIENDLY_URL = "/manage-expression-of-interest"; private static final String MAIL_FROM = "MOVING Gateway "; private static final String MAIL_SUBJECT = "MOVING EU Multi-Actor Platform: email confirmation"; + private static final String MAIL_SUBJECT_VRE_MANAGER = "MOVING EU Multi-Actor Platform: new form submission notification"; private static final String MAIL_BODY = "Dear Sir/Madam,\n" + "Thank you for expressing your interest to join the MOVING EU-level Multi-Actor Platform (MAP). The MOVING team will carefully review the information provided and assess the suitability of your involvement to form a dynamic and relevant community.\n" + "Please receive below a copy of the information submitted. \n" + "\n" + "Best regards,\n" + "MOVING team\n"; - + + private static final String MAIL_BODY_VRE_MANAGER = "Dear MOVING EU-MAP Manager,\n" + + "A new request to join the MOVING EU-level Multi-Actor Platform (MAP) has been submitted.\n" + + "\nPlease manage this request:\n"; + private String mailServiceHost = "localhost"; private String mailServicePort = "25"; @Override @@ -83,6 +109,7 @@ public class CompileForm extends MVCPortlet { String textareaMotivation = actionRequest.getParameter("textareaMotivation"); String elaborated_expertise = actionRequest.getParameter("textareaExperience"); String degree_of_participation = actionRequest.getParameter("commitment"); + _log.debug("name:" + name); _log.debug("surname:" + surname); _log.debug("organisation:" + organisation); @@ -126,6 +153,34 @@ public class CompileForm extends MVCPortlet { preparedStatement .executeUpdate(); _log.info("Inserting in DB done, sending confirmation to user and notification to managers via email"); + String copySubmitted = new StringBuffer("\n\n\nCOPY OF INFORMATION SUBMITTED\n\n") + .append("\n").append("Name: ") + .append(name).append(" ").append(surname) + .append("\n").append("Organisation: ") + .append(organisation) + .append("\n").append("Organisation type: ") + .append(organisationType) + .append("\n").append("emailAddress: ") + .append("*** hidden on purpose ***") + .append("\n").append("Country: ") + .append(country) + .append("\n\n").append("Have you participated in the activities of any of our MOVING Regional Multi-Actors Platforms? ") + .append(participatedInActivities) + .append("\n\n").append("What is your main motivation for joining the EU MAP of MOVING? ") + .append(mainMotivation) + .append("\n\n").append("Please elaborate on your motivation expressed above so that we understand better your interest, and form a dynamic and relevant community. Vague explanations of the motivation might not be taken into consideration. ") + .append("\n\n").append(textareaMotivation).append("\n") + .append("\n\n").append("What is your main area of expertise in relation to mountain sustainability and resilience? (Multiple choice): ") + .append(areaOfExpertise) + .append("\n\n").append("Please elaborate on the relevant experience you can bring to mountain value chains and the resilience of these areas, so that we have more detailed information to be able to form a dynamic and relevant community: ") + .append("\n\n").append(elaborated_expertise) + .append("\n\n").append("As a starting point, the degree of participation you commit to is: ") + .append(degree_of_participation) + .append("\n").append("\n").append("Data Management: ") + .append("Agreed") + .append("\n\nEND COPY OF INFORMATION SUBMITTED\n\n").toString(); + + Properties props = System.getProperties(); Session session = null; props.put("mail.smtp.host", mailServiceHost); @@ -133,30 +188,106 @@ public class CompileForm extends MVCPortlet { //use localhost (probaly postfix instance) session = Session.getDefaultInstance(props); - sendEmailConfirmationToUser(props, session, emailAddress); - sendEmailNotificationToVREManagers(props, session, emailAddress); + sendEmailConfirmationToUser(props, session, emailAddress, copySubmitted); + + String groupIdVREforManagers = GetterUtil.getString(actionRequest.getPreferences().getValue("VREGroupId", StringPool.BLANK)); + if (groupIdVREforManagers != null && groupIdVREforManagers.compareTo("") != 0) { + HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(actionRequest); + sendEmailNotificationToVREManagers(httpRequest, props, session, groupIdVREforManagers, copySubmitted); + } + else { + _log.warn("WARNING, non config found for groupId, no email will be send to VRE Managers"); + } + } catch (Exception e) { _log.error("Some error while trying to insert form"); e.printStackTrace(); } } - - private void sendEmailConfirmationToUser(Properties props, Session session, String emailadress) throws AddressException, MessagingException { + /** + * + * @param props + * @param session + * @param emailadress + * @throws AddressException + * @throws MessagingException + */ + private void sendEmailConfirmationToUser(Properties props, Session session, String emailadress, String copySubmitted) throws AddressException, MessagingException { _log.info("Sending confirmation to user"); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(MAIL_FROM)); message.setRecipients( Message.RecipientType.TO, InternetAddress.parse(emailadress)); message.setSubject(MAIL_SUBJECT); - message.setText(MAIL_BODY); + message.setText(MAIL_BODY+copySubmitted); // Send message Transport.send(message); _log.info("Sending confirmation email done"); } - private void sendEmailNotificationToVREManagers(Properties props, Session session, String emailadress) { - + /** + * + * @param props + * @param session + * @param groupId the groupid of the VRE where to find the managers to alert + * @throws MessagingException + * @throws AddressException + * @throws SystemException + * @throws PortalException + */ + private void sendEmailNotificationToVREManagers(HttpServletRequest httprequest , Properties props, Session session, String groupIdString, String copySubmitted) throws AddressException, MessagingException, PortalException, SystemException { + _log.info("Sending notification email to VRE-Managers"); + long groupId = Long.parseLong(groupIdString); + List vreManagersEmails = getAdministratorsEmails(groupId); + for (String emailadress : vreManagersEmails) { + _log.info("Sending notificsation to user"); + Message message = new MimeMessage(session); + message.setFrom(new InternetAddress(MAIL_FROM)); + message.setRecipients( + Message.RecipientType.TO, InternetAddress.parse(emailadress)); + message.setSubject(MAIL_SUBJECT_VRE_MANAGER); + String vreName = GroupLocalServiceUtil.getGroup(groupId).getName(); + String gatewayURL = PortalContext.getConfiguration().getGatewayURL(httprequest); + String manageRequestURL = new StringBuffer(gatewayURL) + .append(GCubePortalConstants.PREFIX_GROUP_URL) + .append("/").append(vreName.toLowerCase()) + .append(MANAGE_USERS_REQUESTS_FRIENDLY_URL).toString(); + + message.setText(MAIL_BODY_VRE_MANAGER+manageRequestURL+copySubmitted); + // Send message + Transport.send(message); + _log.info("Sending notification email to VRE-Managers done"); + } } + /** + * + * @param groupId the VRE group id + * @return the VRE Managers emails of the VRE groupid passed in the parameter + */ + private static List getAdministratorsEmails(long groupId) { + _log.debug("getAdministratorsEmails"); + LiferayUserManager userManager = new LiferayUserManager(); + Map> usersAndRoles = null; + try { + usersAndRoles = userManager.listUsersAndRolesByGroup(groupId); + } catch (Exception e) { + e.printStackTrace(); + } + Set users = usersAndRoles.keySet(); + ArrayList adminEmailsList = new ArrayList(); + for (GCubeUser usr:users) { + List roles = usersAndRoles.get(usr); + for (int i = 0; i < roles.size(); i++) { + if (roles.get(i).getRoleName().equals(GatewayRolesNames.VRE_MANAGER.getRoleName())) { + adminEmailsList.add(usr.getEmail()); + _log.debug("Manager: " + usr.getFullname()); + break; + } + } + } + return adminEmailsList; + } + private static List getOrganisationTypes(Connection conn) throws Exception { _log.debug("getting organisation_types "); diff --git a/src/main/java/org/gcube/portlets/user/moving/FormConfigurationAction.java b/src/main/java/org/gcube/portlets/user/moving/FormConfigurationAction.java new file mode 100644 index 0000000..013da1a --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/moving/FormConfigurationAction.java @@ -0,0 +1,37 @@ +package org.gcube.portlets.user.moving; + +import javax.portlet.ActionRequest; +import javax.portlet.ActionResponse; +import javax.portlet.PortletConfig; +import javax.portlet.PortletPreferences; +import javax.portlet.RenderRequest; +import javax.portlet.RenderResponse; + +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; +import com.liferay.portal.kernel.portlet.DefaultConfigurationAction; + +public class FormConfigurationAction extends DefaultConfigurationAction { + private static Log _log = LogFactoryUtil.getLog(FormConfigurationAction.class); + @Override + public void processAction( + PortletConfig portletConfig, ActionRequest actionRequest, + ActionResponse actionResponse) throws Exception { + + super.processAction(portletConfig, actionRequest, actionResponse); + + PortletPreferences prefs = actionRequest.getPreferences(); + String VREGroupId = prefs.getValue("VREGroupId", "true"); + + _log.debug("VREGroupId = " + VREGroupId + " processAction()."); + } + + @Override + public String render(PortletConfig portletConfig, + RenderRequest renderRequest, RenderResponse renderResponse) + throws Exception { + + return "/html/form-compile/config.jsp"; + } + +} \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/liferay-portlet.xml b/src/main/webapp/WEB-INF/liferay-portlet.xml index ef14f19..b998216 100644 --- a/src/main/webapp/WEB-INF/liferay-portlet.xml +++ b/src/main/webapp/WEB-INF/liferay-portlet.xml @@ -5,6 +5,8 @@ moving-form-compile /icon.png + org.gcube.portlets.user.moving.FormConfigurationAction + false /css/main.css /js/main.js diff --git a/src/main/webapp/WEB-INF/portlet.xml b/src/main/webapp/WEB-INF/portlet.xml index bdcf03e..861767b 100644 --- a/src/main/webapp/WEB-INF/portlet.xml +++ b/src/main/webapp/WEB-INF/portlet.xml @@ -12,6 +12,10 @@ view-jsp /html/form-compile/form-map.jsp + + config-template + /html/form-compile/config.jsp + 0 text/html diff --git a/src/main/webapp/css/main.css b/src/main/webapp/css/main.css index fa98df3..d945fe2 100644 --- a/src/main/webapp/css/main.css +++ b/src/main/webapp/css/main.css @@ -1,12 +1,17 @@ -#wrapper input[type=text] , #wrapper textarea { +#wrapper input[type=text] { height: 25px; } +#wrapper .form-inline .checkbox input[type=checkbox] { + margin-right: 10px; + height: 18px; + } + + #wrapper label { min-width: 200px; } - #wrapper label { display: inline-block; max-width: 100%; @@ -17,4 +22,5 @@ font-size: 16px; line-height: 1.5; font-family: "Open Sans", Arial, sans-serif !important; -} \ No newline at end of file +} + diff --git a/src/main/webapp/html/form-compile/config.jsp b/src/main/webapp/html/form-compile/config.jsp new file mode 100644 index 0000000..7daee3a --- /dev/null +++ b/src/main/webapp/html/form-compile/config.jsp @@ -0,0 +1,29 @@ +<%@include file="../init.jsp"%> + + + +<% + String VREGroupId_cfg = GetterUtil.getString(portletPreferences.getValue("VREGroupId", StringPool.BLANK)); + +%> + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/html/init.jsp b/src/main/webapp/html/init.jsp index 49c0a1e..a872e53 100644 --- a/src/main/webapp/html/init.jsp +++ b/src/main/webapp/html/init.jsp @@ -6,6 +6,9 @@ <%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %> <%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %> <%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %> +<%@ page import="com.liferay.portal.kernel.util.Constants" %> +<%@ page import="com.liferay.portal.kernel.util.GetterUtil" %> +<%@ page import="com.liferay.portal.kernel.util.StringPool" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ page import="javax.portlet.PortletURL" %>