From 59ca1a0a3a88e905ee1b40c8f05bc6e74667b05d Mon Sep 17 00:00:00 2001 From: Massimiliano Assante Date: Wed, 20 Jun 2018 14:23:53 +0000 Subject: [PATCH] added form for requesting VREs git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/sbd-uploadshare-portlet@169350 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../user/vremanagement/RequestVRE.java | 298 ++++++++++++++++++ src/main/webapp/WEB-INF/liferay-display.xml | 3 + .../WEB-INF/liferay-plugin-package.properties | 2 +- src/main/webapp/WEB-INF/liferay-portlet.xml | 9 + src/main/webapp/WEB-INF/portlet.xml | 33 ++ .../html/requestvre/view-experiment-error.jsp | 29 ++ .../requestvre/view-experiment-inserted.jsp | 30 ++ src/main/webapp/html/requestvre/view.jsp | 100 ++++++ 8 files changed, 503 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/gcube/portlets/user/vremanagement/RequestVRE.java create mode 100644 src/main/webapp/html/requestvre/view-experiment-error.jsp create mode 100644 src/main/webapp/html/requestvre/view-experiment-inserted.jsp create mode 100644 src/main/webapp/html/requestvre/view.jsp diff --git a/src/main/java/org/gcube/portlets/user/vremanagement/RequestVRE.java b/src/main/java/org/gcube/portlets/user/vremanagement/RequestVRE.java new file mode 100644 index 0000000..c34429c --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/vremanagement/RequestVRE.java @@ -0,0 +1,298 @@ +package org.gcube.portlets.user.vremanagement; + + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.portlet.ActionRequest; +import javax.portlet.ActionResponse; +import javax.portlet.PortletException; +import javax.portlet.ResourceRequest; +import javax.portlet.ResourceResponse; +import javax.servlet.http.HttpServletRequest; + +import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager; +import org.gcube.applicationsupportlayer.social.NotificationsManager; +import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite; +import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser; +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.homelibary.model.items.type.WorkspaceItemType; +import org.gcube.common.homelibrary.home.HomeLibrary; +import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException; +import org.gcube.common.homelibrary.home.exceptions.InternalErrorException; +import org.gcube.common.homelibrary.home.workspace.Workspace; +import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder; +import org.gcube.common.homelibrary.home.workspace.WorkspaceItem; +import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException; +import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException; +import org.gcube.common.homelibrary.home.workspace.folder.FolderItem; +import org.gcube.common.homelibrary.util.WorkspaceUtil; +import org.gcube.common.portal.PortalContext; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.portal.notifications.bean.GenericItemBean; +import org.gcube.portal.notifications.thread.MessageNotificationsThread; +import org.gcube.vomanagement.usermanagement.exception.RoleRetrievalFault; +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 com.liferay.portal.kernel.json.JSONFactoryUtil; +import com.liferay.portal.kernel.json.JSONObject; +import com.liferay.portal.kernel.log.Log; +import com.liferay.portal.kernel.log.LogFactoryUtil; +import com.liferay.portal.kernel.servlet.SessionErrors; +import com.liferay.portal.kernel.servlet.SessionMessages; +import com.liferay.portal.kernel.upload.FileItem; +import com.liferay.portal.kernel.upload.UploadPortletRequest; +import com.liferay.portal.kernel.util.FileUtil; +import com.liferay.portal.kernel.util.ParamUtil; +import com.liferay.portal.kernel.util.WebKeys; +import com.liferay.portal.model.User; +import com.liferay.portal.theme.ThemeDisplay; +import com.liferay.portal.util.PortalUtil; +import com.liferay.util.bridges.mvc.MVCPortlet; + +/** + * +- Request a VRE form + * Portlet implementation class RequestVRE + */ +public class RequestVRE extends MVCPortlet { + private static Log _log = LogFactoryUtil.getLog(RequestVRE.class); + private static String REQUEST_VRE_ROLE_MANAGER_NAME = "Infrastructure-Manager"; + private final static String UPLOADED_FILE_ATTR_NAME = "myfile"; + //make sure in the jsp there's + private final static String RESOURCE_URL_ID = "uploadFiles"; + public static final String ATTACHMENT_FOLDER ="_shared attachments"; + + private LiferayRoleManager roleManager = new LiferayRoleManager(); + private LiferayUserManager userManager = new LiferayUserManager(); + + public void addIdea(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException { + try{ + User currentUser = (User) actionRequest.getAttribute(WebKeys.USER); + ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); + long groupId = themeDisplay.getLayout().getGroupId(); + String vre_name = ParamUtil.getString(actionRequest, "vre_name"); + String theAbstract = ParamUtil.getString(actionRequest, "abstract"); + String vreManagerName = ParamUtil.getString(actionRequest, "vre-manager-name"); + String contact_email = ParamUtil.getString(actionRequest, "email"); + String[] attachmentIds = ParamUtil.getParameterValues(actionRequest, "selected-attachment"); + for (int i = 0; i < attachmentIds.length; i++) { + _log.debug(i+"id="+attachmentIds[i]); + } + + SessionMessages.add(actionRequest.getPortletSession(), "method-form-success"); + + _log.debug("new VRE Request: " + vre_name); + SessionMessages.add(actionRequest.getPortletSession(), "experiment-form-success"); + + + + long roleId = roleManager.getRole(REQUEST_VRE_ROLE_MANAGER_NAME, groupId).getRoleId(); + List theManagers = userManager.listUsersByGroupAndRole(groupId, roleId); + long infraManagerRoleId = -1; + boolean result = false; + if (theManagers == null || theManagers.isEmpty()) { + + try { + infraManagerRoleId = roleManager.getRoleIdByName(GatewayRolesNames.INFRASTRUCTURE_MANAGER.getRoleName()); + } + catch (RoleRetrievalFault e) { + _log.warn("There is no (Site) Role " + infraManagerRoleId + " in this portal. Will not notify about removed user accounts."); + return; + } + theManagers = userManager.listUsersByGlobalRole(infraManagerRoleId); + + } + + if (theManagers == null || theManagers.isEmpty()) { + _log.warn("There are no users with (Site) Role " + infraManagerRoleId + " on this portal. Will not notify about this vre request."); + } + else { + HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest); + result = sendNotificationToUser(request, groupId, currentUser, theManagers, + vre_name, theAbstract, vreManagerName, contact_email, attachmentIds); + } + if (result) + actionResponse.setRenderParameter("mvcPath", "/html/requestvre/view-experiment-inserted.jsp"); + else + actionResponse.setRenderParameter("mvcPath", "/html/requestvre/view-experiment-error.jsp"); + } catch(Exception e){ + SessionErrors.add(actionRequest.getPortletSession(),"experiment-form-error"); + e.printStackTrace(); + } + } + + + @Override + public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws PortletException, IOException { + String resourceID = resourceRequest.getResourceID(); + if (resourceID.equals(RESOURCE_URL_ID)) { + UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(resourceRequest); + File multipartFile = uploadRequest.getFile(UPLOADED_FILE_ATTR_NAME); + + FileItem[] items = uploadRequest.getMultipartParameterMap().get(UPLOADED_FILE_ATTR_NAME); + String fileName = ""; + String contentType = ""; + for (int i = 0; i < items.length; i++) { + fileName = items[i].getFileName(); + contentType = items[i].getContentType(); + System.out.println(fileName); + System.out.println(contentType); + } + JSONObject fileObject = JSONFactoryUtil.createJSONObject(); + try { + Workspace workspace = getWorkspace(uploadRequest); + String wsItemId = uploadToUserWorkspace(workspace, fileName, contentType, new ByteArrayInputStream(FileUtil.getBytes(multipartFile))); + fileObject.put("itemId", wsItemId); + fileObject.put("fileName", fileName); + } catch (Exception e) { + _log.error("There was an error uploading the file " + fileName, e); + } + + resourceResponse.getWriter().println(fileObject); + } else { + _log.debug("nothing to do"); + } + } + /** + * + * @param uploadRequest is the XHR http servlet request + * @return + * @throws Exception + */ + private Workspace getWorkspace(UploadPortletRequest uploadRequest ) throws Exception { + Workspace workspace; + try{ + PortalContext pContext = PortalContext.getConfiguration(); + ScopeProvider.instance.set(pContext.getCurrentScope(uploadRequest)); + workspace = HomeLibrary.getUserWorkspace(pContext.getCurrentUser(uploadRequest).getUsername()); + }catch(InternalErrorException | HomeNotFoundException | WorkspaceFolderNotFoundException e){ + String msg = "Sorry, an error occurred when retrieving workspace item, Refresh an try again"; + _log.error("HL error: ",e); + throw new Exception(msg); + } + return workspace; + } + + private String uploadToUserWorkspace(Workspace ownerWS, String fileName, String mimeType,InputStream fileData) throws Exception { + WorkspaceFolder attachment = null; + try{ + attachment = (WorkspaceFolder) ownerWS.getItemByPath(ownerWS.getRoot().getPath() + "/" + ATTACHMENT_FOLDER); + } catch (ItemNotFoundException e) { + _log.info(ATTACHMENT_FOLDER + " Workspace Folder does not exists, creating it "); + attachment = ownerWS.getRoot().createFolder(ATTACHMENT_FOLDER, "Folder created automatically by the System"); + } + String itemName = WorkspaceUtil.getUniqueName(fileName, attachment); + FolderItem item = WorkspaceUtil.createExternalFile(attachment, itemName, "File created automatically by the System", mimeType, fileData); + String toReturn = item.getId(); + _log.debug("Uploaded " + item.getName() + " - Returned Workspace id=" +toReturn); + return toReturn; + } + + + private boolean sendNotificationToUser(HttpServletRequest request, long groupId, User currentUser, List theManagers + , String vre_name, String theAbstract, String vreManagerName, String email, String[] attachmentIds) throws Exception { + String scope = PortalContext.getConfiguration().getCurrentScope(""+groupId); + _log.info("sendNotificationToUser Scope: " + scope); + + String authorizationToken = PortalContext.getConfiguration().getCurrentUserToken(scope, currentUser.getScreenName()); + ScopeProvider.instance.set(scope); + SecurityTokenProvider.instance.set(authorizationToken); + Workspace workspace = HomeLibrary.getUserWorkspace(currentUser.getScreenName()); + + List recipientIds = new ArrayList(); + List recipients = new ArrayList(); + for (GCubeUser addressee : theManagers) { + recipients.add(new GenericItemBean(addressee.getUsername(), addressee.getUsername(), addressee.getFullname(), "")); + recipientIds.add(addressee.getUsername()); + } + + String subject = "A new VRE has been requested"; + StringBuilder sb = new StringBuilder("Hi, the following VRE was requested by ").append(currentUser.getFullName()) + .append(" (").append(currentUser.getEmailAddress()).append(").") + .append("\n\n").append("VRE name: ").append(vre_name).append("\n") + .append("\n").append("Manager(s)").append(vreManagerName).append("\n") + .append("\n").append("Abstract:\n").append(theAbstract).append("\n") + .append("\n").append("Contact email: ").append(email).append("\n"); + + List attachmentsList = Arrays.asList(attachmentIds); + String body = sb.toString(); + try { + body += getPublicLinksForAttachs(workspace, attachmentsList); + } + catch (Exception e) { + _log.error("Ops, could not generate publick link for some of the attachments"); + } + String messageId = + workspace.getWorkspaceMessageManager(). + sendMessageToPortalLogins(subject, body, attachmentsList, recipientIds); + + _log.info("Sending upload method message notification to: " + recipientIds.toString()); + NotificationsManager nm = new ApplicationNotificationsManager(new SocialNetworkingSite(request), scope, new SocialNetworkingUser( + currentUser.getScreenName(), currentUser.getEmailAddress(), currentUser.getFullName(), "")); + Thread thread = new Thread(new MessageNotificationsThread(recipients, messageId, subject, body, nm)); + thread.start(); + + return (messageId != null); + } + + /** + * + * @param workspace + * @param listAttachmentsId + * @return + * @throws ItemNotFoundException + * @throws InternalErrorException + */ + private String getPublicLinksForAttachs(Workspace workspace, List listAttachmentsId) throws ItemNotFoundException, InternalErrorException{ + + if (listAttachmentsId != null && (!listAttachmentsId.isEmpty()) ) { + List attachments = new ArrayList(); + for (String itemId : listAttachmentsId) { + attachments.add(workspace.getItem(itemId)); + } + + StringBuilder builder = new StringBuilder(); + + if(attachments!=null && attachments.size() > 0){ + builder.append("\n\n\nThe following "); + String msg = attachments.size()>1?"files were attached to this message:":"file was attached to this message:"; + builder.append(msg+"\n"); + for (WorkspaceItem workspaceItem : attachments) { + + if(workspaceItem.getType().equals(WorkspaceItemType.FOLDER_ITEM)) { + FolderItem folderItem = (FolderItem) workspaceItem; + String publicLink = ""; + String itemName = ""; + try { + itemName = workspaceItem.getName(); + publicLink = folderItem.getPublicLink(true); + } + catch (InternalErrorException e) { + _log.warn("An error occurred when creating public link for attachment, skipping file: " + itemName); + return ""; + } + builder.append(itemName + " ("+publicLink+")"); + builder.append("\n"); + } + } + _log.debug("returning public links: "+builder.toString()); + return builder.toString(); + } + else return ""; + } + else return ""; + } + +} + + + diff --git a/src/main/webapp/WEB-INF/liferay-display.xml b/src/main/webapp/WEB-INF/liferay-display.xml index 12cea3a..06e06c5 100644 --- a/src/main/webapp/WEB-INF/liferay-display.xml +++ b/src/main/webapp/WEB-INF/liferay-display.xml @@ -8,4 +8,7 @@ + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/liferay-plugin-package.properties b/src/main/webapp/WEB-INF/liferay-plugin-package.properties index c216b7c..d42d293 100644 --- a/src/main/webapp/WEB-INF/liferay-plugin-package.properties +++ b/src/main/webapp/WEB-INF/liferay-plugin-package.properties @@ -1,6 +1,6 @@ name=SBD-UploadAndSharePortlets module-group-id=liferay -module-incremental-version=2 +module-incremental-version=3 tags= short-description= change-log= diff --git a/src/main/webapp/WEB-INF/liferay-portlet.xml b/src/main/webapp/WEB-INF/liferay-portlet.xml index 7e4c4b0..6bbf0f5 100644 --- a/src/main/webapp/WEB-INF/liferay-portlet.xml +++ b/src/main/webapp/WEB-INF/liferay-portlet.xml @@ -54,6 +54,15 @@ access-application-portlet + + request-vre + /icon.png + /css/main.css + + /js/main.js + + request-vre-portlet + administrator Administrator diff --git a/src/main/webapp/WEB-INF/portlet.xml b/src/main/webapp/WEB-INF/portlet.xml index a61ec91..5864b29 100644 --- a/src/main/webapp/WEB-INF/portlet.xml +++ b/src/main/webapp/WEB-INF/portlet.xml @@ -143,4 +143,37 @@ user + + request-vre + Request a VRE + + org.gcube.portlets.user.vremanagement.RequestVRE + + + view-template + /html/requestvre/view.jsp + + 0 + + text/html + view + + + Request a VRE + Request Vre + + + + administrator + + + guest + + + power-user + + + user + + \ No newline at end of file diff --git a/src/main/webapp/html/requestvre/view-experiment-error.jsp b/src/main/webapp/html/requestvre/view-experiment-error.jsp new file mode 100644 index 0000000..fb11007 --- /dev/null +++ b/src/main/webapp/html/requestvre/view-experiment-error.jsp @@ -0,0 +1,29 @@ +<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> +<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%> +<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%> +<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%> +<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%> +<%@page import="com.liferay.portal.kernel.servlet.SessionErrors"%> +<%@page import="com.liferay.portal.kernel.servlet.SessionMessages"%> +<%@page import="com.liferay.portal.model.User"%> +<%@page import="com.liferay.portal.kernel.util.WebKeys"%> + + + +<% +User currentUser = (User) request.getAttribute(WebKeys.USER); +%> + + + +

+ Thank you + <%=currentUser.getFirstName()%>. There was an error submitting your request. Please report this issue. +

+ + + Close and return to home +
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> +<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%> +<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%> +<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%> +<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%> +<%@page import="com.liferay.portal.kernel.servlet.SessionErrors"%> +<%@page import="com.liferay.portal.kernel.servlet.SessionMessages"%> +<%@page import="com.liferay.portal.model.User"%> +<%@page import="com.liferay.portal.kernel.util.WebKeys"%> + + + +<% +User currentUser = (User) request.getAttribute(WebKeys.USER); +%> + + + +

+ Thank you + <%=currentUser.getFirstName()%>. We have been notified about your + request, will get back to you shortly. +

+ + + Close and return to home +
+<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> +<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%> +<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%> +<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme"%> +<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%> +<%@page import="com.liferay.portal.kernel.servlet.SessionErrors"%> + + + + + +<% + if (SessionErrors.contains(renderRequest.getPortletSession(), "experiment-form-error")) { +%> + +<% + } +%> + + + + + + + + + + + + +
+ +
+
+ + + +  Back (Cancel) +

Please give us a + description about the Virtual Research Environment (VRE) you wish to + create. The project research, technical and ethical boards will + evaluate your application and contact you about the result as soon as possible and always within 5 working days.

+
+ + + + + + + + + Logo and Additional Material (max 3 files, allowed maxsize per file: 10 MB): +
Select file to attach
+
+ + + + Cancel + + +
+
+
+ +
+ + +