implemented groups notification

This commit is contained in:
Massimiliano Assante 2022-05-05 11:04:31 +02:00
parent 410f540219
commit 100d2484ff
1 changed files with 67 additions and 1 deletions

View File

@ -1,5 +1,6 @@
package org.gcube.portal.social.networking.ws.methods.v2;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
@ -21,16 +22,20 @@ import javax.ws.rs.core.Response.Status;
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager;
import org.gcube.applicationsupportlayer.social.NotificationsManager;
import org.gcube.applicationsupportlayer.social.ScopeBeanExt;
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite;
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser;
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.portal.databook.shared.Notification;
import org.gcube.portal.databook.shared.RunningJob;
import org.gcube.portal.notifications.bean.GenericItemBean;
import org.gcube.portal.notifications.thread.JobStatusNotificationThread;
import org.gcube.portal.social.networking.caches.SocialNetworkingSiteFinder;
import org.gcube.portal.social.networking.caches.UsersCache;
import org.gcube.portal.social.networking.liferay.ws.GroupManagerWSBuilder;
import org.gcube.portal.social.networking.liferay.ws.LiferayJSONWsCredentials;
import org.gcube.portal.social.networking.liferay.ws.UserManagerWSBuilder;
import org.gcube.portal.social.networking.ws.mappers.JobMapper;
@ -44,6 +49,10 @@ import org.gcube.social_networking.socialnetworking.model.beans.workspace.AddedI
import org.gcube.social_networking.socialnetworking.model.beans.workspace.RenamedFolderEvent;
import org.gcube.social_networking.socialnetworking.model.beans.workspace.SharedFolderEvent;
import org.gcube.social_networking.socialnetworking.model.beans.workspace.UnsharedFolderEvent;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.LoggerFactory;
@ -206,6 +215,33 @@ public class Notifications {
String userIdToNotify = idsToNotify[i];
deliveryResult = notifyWorkspaceEvent(event, nm, userIdToNotify);
}
} else { //the ids are contexts
for (int i = 0; i < idsToNotify.length; i++) {
String contextId = idsToNotify[i];
try {
ScopeBean scope = new ScopeBean(contextId);
if (scope.type() != ScopeBean.Type.VRE) {
logger.error("Context not a VRE");
status = Status.BAD_REQUEST;
responseBean.setSuccess(false);
responseBean.setMessage("CNot a VRE Context, only VRE is supported");
return Response.status(status).entity(responseBean).build();
} else { // it is a context and it is a valid VRE
String[] userIdsToNotify = getUsernamesByContext(scope).toArray(new String[0]); //resolve the members
for (int j = 0; j < userIdsToNotify.length; j++) {
String userIdToNotify = userIdsToNotify[j];
deliveryResult = notifyWorkspaceEvent(event, nm, userIdToNotify);
}
}
}
catch(IllegalArgumentException e) {
status = Status.BAD_REQUEST;
logger.error("Context not valid", e);
responseBean.setSuccess(false);
responseBean.setMessage("Context not valid, must start with / " + e.getMessage());
return Response.status(status).entity(responseBean).build();
}
}
}
} catch(Exception e){
logger.error("Unable to send job notification", e);
@ -220,7 +256,7 @@ public class Notifications {
responseBean.setResult(new Boolean(true));
} else {
responseBean.setSuccess(false);
responseBean.setMessage("Workspace notification not delivered correctly");
responseBean.setMessage("An error occurred between this service and Cassandra DB, notification not delivered correctly");
responseBean.setResult(new Boolean(false));
}
return Response.status(status).entity(responseBean).build();
@ -260,5 +296,35 @@ public class Notifications {
}
return false;
}
/**
*
* @param context
* @return
* @throws Exception
*/
private List<String> getUsernamesByContext(ScopeBean context) throws Exception {
List<String> usernames = new ArrayList<String>();
GroupManager groupManager = GroupManagerWSBuilder.getInstance().getGroupManager();
UserManager userManager = UserManagerWSBuilder.getInstance().getUserManager();
long groupId = groupManager.getGroupIdFromInfrastructureScope(context.toString());
// first retrieve ids
List<Long> userIds = userManager.getUserIdsByGroup(groupId);
// check info in cache when available
UsersCache cache = UsersCache.getSingleton();
for (Long userId : userIds) {
if(cache.getUser(userId) == null){
GCubeUser theUser = userManager.getUserById(userId);
if(theUser != null){
usernames.add(theUser.getUsername());
cache.pushEntry(userId, theUser);
}
}else
usernames.add(cache.getUser(userId).getUsername());
}
return usernames;
}
}