183 lines
8.3 KiB
Java
183 lines
8.3 KiB
Java
package org.gcube.portlets.user.sbdportlets;
|
|
|
|
import java.io.IOException;
|
|
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.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.InternalErrorException;
|
|
import org.gcube.common.homelibrary.home.workspace.Workspace;
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException;
|
|
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
|
|
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.impl.LiferayRoleManager;
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
|
|
|
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.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;
|
|
|
|
/**
|
|
* upload method/dataset: UserUpload Admin
|
|
- Share idea: UserIdea Admin
|
|
* Portlet implementation class UploadDataset
|
|
*/
|
|
public class UploadDataset extends MVCPortlet {
|
|
private static Log _log = LogFactoryUtil.getLog(UploadDataset.class);
|
|
private static String UPLOAD_DATASET_ROLE_MANAGER_NAME = "UserUpload-Admin";
|
|
|
|
private LiferayRoleManager roleManager = new LiferayRoleManager();
|
|
private LiferayUserManager userManager = new LiferayUserManager();
|
|
|
|
public void addExperiment(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 method_name = ParamUtil.getString(actionRequest, "method_name");
|
|
String people_institutions = ParamUtil.getString(actionRequest, "people_institutions");
|
|
String demo_tutorial = ParamUtil.getString(actionRequest, "demo_tutorial");
|
|
String experiment_tags = ParamUtil.getString(actionRequest, "experiment_tags");
|
|
String[] attachmentIds = ParamUtil.getParameterValues(actionRequest, "selected-attachment");
|
|
for (int i = 0; i < attachmentIds.length; i++) {
|
|
System.out.println(i+"id="+attachmentIds[i]);
|
|
}
|
|
|
|
_log.debug("new dataset_name : " + method_name);
|
|
SessionMessages.add(actionRequest.getPortletSession(), "method-form-success");
|
|
|
|
|
|
long teamId = roleManager.getTeam(groupId, UPLOAD_DATASET_ROLE_MANAGER_NAME).getTeamId();
|
|
List<GCubeUser> theManagers = userManager.listUsersByTeam(teamId);
|
|
HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
|
|
boolean result = sendNotificationToUser(
|
|
request, groupId, currentUser, theManagers, method_name, people_institutions, demo_tutorial, attachmentIds, experiment_tags);
|
|
if (result)
|
|
actionResponse.setRenderParameter("mvcPath", "/html/uploaddataset/view-method-inserted.jsp");
|
|
else
|
|
actionResponse.setRenderParameter("mvcPath", "/html/uploaddataset/view-method-error.jsp");
|
|
}catch(Exception e){
|
|
SessionErrors.add(actionRequest.getPortletSession(),"view-method-error");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
private boolean sendNotificationToUser(HttpServletRequest request, long groupId, User currentUser, List<GCubeUser> theManagers
|
|
, String method_name, String people_institutions, String demo_tutorial, String[] attachmentIds, String experiment_tags) 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<String> recipientIds = new ArrayList<String>();
|
|
List<GenericItemBean> recipients = new ArrayList<GenericItemBean>();
|
|
for (GCubeUser addressee : theManagers) {
|
|
recipients.add(new GenericItemBean(addressee.getUsername(), addressee.getUsername(), addressee.getFullname(), ""));
|
|
recipientIds.add(addressee.getUsername());
|
|
}
|
|
|
|
String subject = "A new user uploaded a method or dataset";
|
|
StringBuilder sb = new StringBuilder("Hi, the following dataset/method was shared by ").append(currentUser.getFullName())
|
|
.append(" (").append(currentUser.getEmailAddress()).append(").")
|
|
.append("\n\n").append("Dataset/Method name: ").append(method_name).append("\n")
|
|
.append("\n").append("Involved people and or institutions: ").append(people_institutions).append("\n")
|
|
.append("\n").append("Demo/Tutorial:\n").append(demo_tutorial).append("\n")
|
|
.append("\n").append("Keywords (Comma separated): ").append(experiment_tags).append("\n");
|
|
|
|
List<String> 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<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 "";
|
|
}
|
|
|
|
}
|