|
|
|
@ -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 <portlet:resourceURL var="uploadFileURL" id="uploadFiles" />
|
|
|
|
|
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<GCubeUser> 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<GCubeUser> 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<String> attachmentsList = Arrays.asList(attachmentIds);
|
|
|
|
|
String body = sb.toString();
|
|
|
|
|
String messageId = workspace.getWorkspaceMessageManager().sendMessageToPortalLogins(subject, body, new ArrayList<String>(), 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<String> listAttachmentsId) throws ItemNotFoundException, InternalErrorException{
|
|
|
|
|
|
|
|
|
|
if (listAttachmentsId != null && (!listAttachmentsId.isEmpty()) ) {
|
|
|
|
|
List<WorkspaceItem> attachments = new ArrayList<WorkspaceItem>();
|
|
|
|
|
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 "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|