refactored

This commit is contained in:
Massimiliano Assante 2022-05-04 16:32:48 +02:00
parent b45fd2c323
commit 306ecf935b
1 changed files with 49 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package org.gcube.portal.social.networking.ws.methods.v2;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import javax.validation.Valid;
@ -40,6 +41,7 @@ import org.gcube.portal.social.networking.ws.utils.ErrorMessages;
import org.gcube.social_networking.socialnetworking.model.beans.JobNotificationBean;
import org.gcube.social_networking.socialnetworking.model.beans.workspace.WorkspaceEvent;
import org.gcube.social_networking.socialnetworking.model.beans.workspace.AddedItemEvent;
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.model.GCubeUser;
@ -148,7 +150,7 @@ public class Notifications {
NotificationsManager nm = new ApplicationNotificationsManager(UserManagerWSBuilder.getInstance().getUserManager(), site, context, user);
RunningJob theJob = JobMapper.getJob(job);
new Thread(new JobStatusNotificationThread(theJob, Arrays.asList(recipientBean), nm)).start();
responseBean.setSuccess(true);
responseBean.setResult("Notification thread started");
@ -186,40 +188,65 @@ public class Notifications {
Caller caller = AuthorizationProvider.instance.get();
String context = ScopeProvider.instance.get();
String username = caller.getClient().getId();
logger.debug("workspace notifications from user = " + username);
ResponseBean responseBean = new ResponseBean();
Status status = Status.OK;
boolean deliveryResult = false;
try {
logger.debug("workspace notifications type is " + event.getType());
SocialNetworkingSite site = SocialNetworkingSiteFinder.getSocialNetworkingSiteFromScope(context);
GCubeUser senderUser = UserManagerWSBuilder.getInstance().getUserManager().getUserByUsername(username);
SocialNetworkingUser user = new SocialNetworkingUser(senderUser.getUsername(), senderUser.getEmail(), senderUser.getFullname(), senderUser.getUserAvatarURL());
NotificationsManager nm = new ApplicationNotificationsManager(UserManagerWSBuilder.getInstance().getUserManager(), site, context, user);
switch (event.getType()) {
case ITEM_NEW: {
AddedItemEvent itemBean = (AddedItemEvent) event;
String userIdToNotify = itemBean.getUserIdToNotify();
nm.notifyAddedItem(userIdToNotify, WorkspaceItemMapper.getFileItem(itemBean.getItem()), WorkspaceItemMapper.getSharedFolder(itemBean.getItem().getParent()));
String[] userIdsToNotify = itemBean.getIdsToNotify();
if (! itemBean.idsAsGroup()) {
for (int i = 0; i < userIdsToNotify.length; i++) {
String userIdToNotify = userIdsToNotify[i];
deliveryResult = nm.notifyAddedItem(userIdToNotify, WorkspaceItemMapper.getFileItem(itemBean.getItem()), WorkspaceItemMapper.getSharedFolder(itemBean.getItem().getParent()));
}
}
break;
}
case FOLDER_SHARE: {
SharedFolderEvent itemBean = (SharedFolderEvent) event;
String userIdToNotify = itemBean.getUserIdToNotify();
nm.notifyFolderSharing(userIdToNotify, WorkspaceItemMapper.getSharedFolder(itemBean.getFolder()));
String[] userIdsToNotify = itemBean.getIdsToNotify();
if (! itemBean.idsAsGroup()) {
for (int i = 0; i < userIdsToNotify.length; i++) {
String userIdToNotify = userIdsToNotify[i];
deliveryResult = nm.notifyFolderSharing(userIdToNotify, WorkspaceItemMapper.getSharedFolder(itemBean.getFolder()));
}
}
break;
}
case FOLDER_UNSHARE: {
UnsharedFolderEvent itemBean = (UnsharedFolderEvent) event;
String userIdToNotify = itemBean.getUserIdToNotify();
nm.notifyFolderUnsharing(userIdToNotify, itemBean.getUnsharedFolderId(), itemBean.getUnsharedFolderName());
String[] userIdsToNotify = itemBean.getIdsToNotify();
if (! itemBean.idsAsGroup()) {
for (int i = 0; i < userIdsToNotify.length; i++) {
String userIdToNotify = userIdsToNotify[i];
deliveryResult = nm.notifyFolderUnsharing(userIdToNotify, itemBean.getUnsharedFolderId(), itemBean.getUnsharedFolderName());
}
}
break;
}
case FOLDER_RENAME: {
RenamedFolderEvent itemBean = (RenamedFolderEvent) event;
String[] userIdsToNotify = itemBean.getIdsToNotify();
if (! itemBean.idsAsGroup()) {
for (int i = 0; i < userIdsToNotify.length; i++) {
String userIdToNotify = userIdsToNotify[i];
deliveryResult = nm.notifyFolderRenaming(userIdToNotify, itemBean.getPreviousName(), itemBean.getNewName(), itemBean.getRenamedFolderId());
}
}
break;
}
default:
break;
}
@ -230,8 +257,15 @@ public class Notifications {
status = Status.INTERNAL_SERVER_ERROR;
}
logger.debug("workspace notifications should have been sent");
responseBean.setSuccess(true);
responseBean.setMessage("Workspace notification delivered correctly");
if (deliveryResult) {
responseBean.setSuccess(true);
responseBean.setMessage("Workspace notification delivered correctly");
responseBean.setResult(new Boolean(true));
} else {
responseBean.setSuccess(false);
responseBean.setMessage("Workspace notification not delivered correctly");
responseBean.setResult(new Boolean(false));
}
return Response.status(status).entity(responseBean).build();
}