diff --git a/src/main/java/org/gcube/portal/social/networking/ws/mappers/CatalogueEventTypeMapper.java b/src/main/java/org/gcube/portal/social/networking/ws/mappers/CatalogueEventTypeMapper.java new file mode 100644 index 0000000..5b83fb3 --- /dev/null +++ b/src/main/java/org/gcube/portal/social/networking/ws/mappers/CatalogueEventTypeMapper.java @@ -0,0 +1,28 @@ +package org.gcube.portal.social.networking.ws.mappers; + +import org.gcube.portal.databook.shared.NotificationType; +import org.gcube.portal.databook.shared.ex.NotificationTypeNotFoundException; +import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEventType; + +public class CatalogueEventTypeMapper { + + public CatalogueEventTypeMapper() { + } + + public static NotificationType getType(CatalogueEventType type) throws NotificationTypeNotFoundException { + switch (type) { + case ITEM_PUBLISHED: + return NotificationType.CAT_ITEM_PUBLISHED; + case ITEM_REJECTED: + return NotificationType.CAT_ITEM_REJECTED; + case ITEM_REMOVED: + return NotificationType.CAT_ITEM_DELETE; + case ITEM_SUBMITTED: + return NotificationType.CAT_ITEM_SUBMITTED; + case ITEM_UPDATED: + return NotificationType.CAT_ITEM_UPDATED; + default: + throw new NotificationTypeNotFoundException("The Catalogue event could not be mapped"); + } + } +} diff --git a/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Notifications.java b/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Notifications.java index b8fb07b..e7e092d 100644 --- a/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Notifications.java +++ b/src/main/java/org/gcube/portal/social/networking/ws/methods/v2/Notifications.java @@ -40,6 +40,7 @@ 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.CatalogueEventTypeMapper; import org.gcube.portal.social.networking.ws.mappers.JobMapper; import org.gcube.portal.social.networking.ws.mappers.WorkspaceItemMapper; import org.gcube.portal.social.networking.ws.outputs.ResponseBean; @@ -223,7 +224,46 @@ public class Notifications { if (! event.idsAsGroup()) { for (int i = 0; i < idsToNotify.length; i++) { String userIdToNotify = idsToNotify[i]; - deliveryResult = nm.notifyCatalogueEvent(NotificationType.CAT_ITEM_PUBLISHED, userIdToNotify, event.getItemId(), event.getNotifyText(), event.getItemURL()); + deliveryResult = + nm.notifyCatalogueEvent( + CatalogueEventTypeMapper.getType(event.getType()), + userIdToNotify, + event.getItemId(), + event.getNotifyText(), + event.getItemURL()); + } + } 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("Not a VRE Context, only VREs are 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 = + nm.notifyCatalogueEvent( + CatalogueEventTypeMapper.getType(event.getType()), + userIdToNotify, + event.getItemId(), + event.getNotifyText(), + event.getItemURL()); + } + } + } + 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){