diff --git a/.classpath b/.classpath index 3b073e7..26981b6 100644 --- a/.classpath +++ b/.classpath @@ -25,7 +25,7 @@ - + diff --git a/pom.xml b/pom.xml index c53989a..9ba965a 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ sbd-uploadshare-portlet war SBD-UploadAndSharePortlets Portlet - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT Upload Dataset And Share Idea Portlets diff --git a/src/main/java/org/gcube/portlets/user/sbdportlets/CollaborateWithUs.java b/src/main/java/org/gcube/portlets/user/sbdportlets/CollaborateWithUs.java index 2fb7186..1b8876c 100644 --- a/src/main/java/org/gcube/portlets/user/sbdportlets/CollaborateWithUs.java +++ b/src/main/java/org/gcube/portlets/user/sbdportlets/CollaborateWithUs.java @@ -1,12 +1,18 @@ package org.gcube.portlets.user.sbdportlets; +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; @@ -14,8 +20,17 @@ 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; @@ -24,10 +39,15 @@ import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; import org.gcube.vomanagement.usermanagement.model.GCubeUser; +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; @@ -43,6 +63,10 @@ import com.liferay.util.bridges.mvc.MVCPortlet; public class CollaborateWithUs extends MVCPortlet { private static Log _log = LogFactoryUtil.getLog(CollaborateWithUs.class); private static String SHARE_IDEA_ROLE_MANAGER_NAME = "UserIdea-Admin"; + 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(); @@ -56,6 +80,12 @@ public class CollaborateWithUs extends MVCPortlet { String people_institutions = ParamUtil.getString(actionRequest, "people_institutions"); String theAbstract = ParamUtil.getString(actionRequest, "abstract"); String experiment_tags = ParamUtil.getString(actionRequest, "experiment_tags"); + 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 experiment shared: " + experiment_name); SessionMessages.add(actionRequest.getPortletSession(), "experiment-form-success"); @@ -64,7 +94,8 @@ public class CollaborateWithUs extends MVCPortlet { long teamId = roleManager.getTeam(groupId, SHARE_IDEA_ROLE_MANAGER_NAME).getTeamId(); List theManagers = userManager.listUsersByTeam(teamId); HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest); - boolean result = sendNotificationToUser(request, groupId, currentUser, theManagers, experiment_name, people_institutions, theAbstract, experiment_tags); + boolean result = sendNotificationToUser(request, groupId, currentUser, theManagers, + experiment_name, people_institutions, theAbstract, attachmentIds, experiment_tags); if (result) actionResponse.setRenderParameter("mvcPath", "/html/collaboratewithus/view-experiment-inserted.jsp"); else @@ -75,8 +106,76 @@ public class CollaborateWithUs extends MVCPortlet { } } + + @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 experiment_name, String people_institutions, String theAbstract, String experiment_tags) throws Exception { + , String experiment_name, String people_institutions, String theAbstract, String[] attachmentIds, String experiment_tags) throws Exception { String scope = PortalContext.getConfiguration().getCurrentScope(""+groupId); _log.info("sendNotificationToUser Scope: " + scope); @@ -100,10 +199,19 @@ public class CollaborateWithUs extends MVCPortlet { .append("\n").append("Abstract:\n").append(theAbstract).append("\n") .append("\n").append("Keywords (Comma separated): ").append(experiment_tags).append("\n"); + List attachmentsList = Arrays.asList(attachmentIds); String body = sb.toString(); - String messageId = workspace.getWorkspaceMessageManager().sendMessageToPortalLogins(subject, body, new ArrayList(), recipientIds); + 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.debug("Sending message notification to: " + recipientIds.toString()); + _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)); @@ -111,4 +219,53 @@ public class CollaborateWithUs extends MVCPortlet { 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/java/org/gcube/portlets/user/sbdportlets/UploadDataset.java b/src/main/java/org/gcube/portlets/user/sbdportlets/UploadDataset.java index 33a3564..57f8281 100644 --- a/src/main/java/org/gcube/portlets/user/sbdportlets/UploadDataset.java +++ b/src/main/java/org/gcube/portlets/user/sbdportlets/UploadDataset.java @@ -120,8 +120,6 @@ public class UploadDataset extends MVCPortlet { System.out.println(fileName); System.out.println(contentType); } -// Path written = Files.write(Paths.get(System.getProperty("catalina.home")+"/temp/"+fileName), FileUtil.getBytes(multipartFile)); -// System.out.println("Written File = " + written.toRealPath()); JSONObject fileObject = JSONFactoryUtil.createJSONObject(); try { Workspace workspace = getWorkspace(uploadRequest); diff --git a/src/main/webapp/WEB-INF/liferay-display.xml b/src/main/webapp/WEB-INF/liferay-display.xml index 3b455c0..12cea3a 100644 --- a/src/main/webapp/WEB-INF/liferay-display.xml +++ b/src/main/webapp/WEB-INF/liferay-display.xml @@ -2,16 +2,10 @@ - - - - - - \ 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 c115333..7e4c4b0 100644 --- a/src/main/webapp/WEB-INF/liferay-portlet.xml +++ b/src/main/webapp/WEB-INF/liferay-portlet.xml @@ -20,6 +20,9 @@ /icon.png false /css/main.css + + /js/jquery.min.js + /js/main.js @@ -38,7 +41,6 @@ access-exploratory-portlet - access-application diff --git a/src/main/webapp/html/collaboratewithus/view.jsp b/src/main/webapp/html/collaboratewithus/view.jsp index 92f36ec..fd44e78 100644 --- a/src/main/webapp/html/collaboratewithus/view.jsp +++ b/src/main/webapp/html/collaboratewithus/view.jsp @@ -8,6 +8,7 @@ + <% if (SessionErrors.contains(renderRequest.getPortletSession(), "experiment-form-error")) { @@ -39,16 +40,38 @@ + +  Back (Cancel) -

Please give us a description about the - dataset/method you want to insert in the catalogue. The SoBigData - research, technical and ethical boards will evaluate your application - before the actual upload. In particular, in case of datasets, please - visit also our - First aid for Responsible data Scientists: we - provide resources and information to understand and check possible - ethical issues for your dataset.

+

+ Please give us a description about the dataset/method you want to + insert in the catalogue. The SoBigData research, technical and + ethical boards will evaluate your application before the actual + upload. In particular, in case of datasets, please visit also our First aid for + Responsible data Scientists: we provide resources and information to + understand and check possible ethical issues for your dataset. +

@@ -61,6 +84,9 @@ + Additional Material (max 3 files, allowed max size per file: 10 MB): +
Select file to attach
+