add mapping to CatalogueEvent

This commit is contained in:
Francesco Mangiacrapa 2024-03-25 11:16:40 +01:00
parent 27126d1c9f
commit 45164a02da
7 changed files with 128 additions and 80 deletions

View File

@ -11,7 +11,7 @@ public class NotificationGenericConstants {
};
public static enum NOTIFICATION_TYPE {
USER_MESSAGE, EMAIL, VRE_POST
USER_POST, EMAIL, VRE_POST
};
}

View File

@ -13,6 +13,7 @@ import org.gcube.application.cms.notifications.config.NotificationFor;
import org.gcube.application.cms.notifications.config.NotificationWhen;
import org.gcube.application.cms.notifications.config.SubscribeNotificationEvent;
import org.gcube.application.cms.notifications.gis.GisClient;
import org.gcube.application.cms.notifications.manage.mapping.MappingToCatalogue;
import org.gcube.application.cms.notifications.social.SocialClients;
import org.gcube.application.cms.notifications.substitutor.NMessagesPlaceholdersSubstitutorUtil;
import org.gcube.application.cms.notifications.substitutor.SubstitutorMessagesMap;
@ -20,9 +21,8 @@ import org.gcube.application.cms.plugins.events.ItemObserved;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.document.accounting.User;
import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.RESOLVE_AS;
import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEventType;
import lombok.extern.slf4j.Slf4j;
@ -87,7 +87,7 @@ public class ManageDoActionNotification {
String lastInvokedStep = lInfo.getLastInvokedStep();
log.debug("ItemObserved phase is: {}, lastInvokedStep is: {}", itemPhase, lastInvokedStep);
final SecretManager cm = SecretManagerProvider.instance.get();
//final SecretManager cm = SecretManagerProvider.instance.get();
for (final NotificationFor notificationFor : listNotificationFor) {
// Filtering notifications on 'phase' and 'lastInvokedStep' according to item
@ -100,39 +100,23 @@ public class ManageDoActionNotification {
if (notificationsCompliantToPhase != null && notificationsCompliantToPhase.size() > 0) {
// GET LIST USER COMPLIANT TO ROLES
List<User> notifyUsers = getListUserCompliantToRoles(notificationFor, itemObserved);
List<User> notifyUsers = getListUserCompliantToRoles(notificationFor, itemObserved, true);
final SocialClients socialClients = new SocialClients();
CatalogueEventType catalogueEventType = MappingToCatalogue.toCatalogueEventType(itemPhase, lastInvokedStep);
if (notifyUsers.size() > 0) {
log.info("Users compliant to role are: {}", notifyUsers.size());
if (log.isDebugEnabled()) {
notifyUsers.stream().forEach(u -> log.debug("user: " + u));
notifyUsers.stream().forEach(u -> log.debug("notifying user: " + u));
}
for (final User user : notifyUsers) {
try {
NotifyUser mnu = new NotifyUser(socialClients, notificationsCompliantToPhase,
nMPlaceholdersS, user);
nMPlaceholdersS, notifyUsers, catalogueEventType);
mnu.send();
} catch (Exception e) {
log.error("Error occurred when send notify: ", e);
}
}
/*
* for (final User user : notifyUsers) { Thread notificationT = new Thread() {
* public void run() { SecretManagerProvider.instance.set(cm); NotifyUser mnu =
* new NotifyUser(socialClients, notificationsCompliantToPhase, nMPlaceholdersS,
* user); try { mnu.send(); } catch (Exception e) {
* log.error("Error occurred when send notify: ", e); } };
*
* };
*
* notificationT.start(); }
*/
}
}
}
@ -150,7 +134,8 @@ public class ManageDoActionNotification {
for (NotificationWhen notificationWhen : notificationFor.getWhen()) {
log.info(++i + ") notificationWhen target phases: {} and last_invoked_step: {}",
notificationWhen.getTarget_phase(), notificationWhen.getLast_invoked_step());
if (notificationWhen.getTarget_phase().contains(itemPhase)) {
if (notificationWhen.getTarget_phase().contains(itemPhase) || notificationWhen.getTarget_phase()
.contains(NotificationGenericConstants.CUSTOM_TARGET_PHASE.Any.name())) {
if (notificationWhen.getLast_invoked_step() == null) {
// no filtering on lastInvokedStep
filteredList.add(notificationWhen);
@ -173,22 +158,31 @@ public class ManageDoActionNotification {
*
* @param notificationFor the notification for
* @param itemObserved the item observed
* @param ignoreUserCaller the ignore user caller
* @return the list user compliant to roles
*/
// GET LIST USER COMPLIANT TO ROLES
public List<User> getListUserCompliantToRoles(NotificationFor notificationFor, ItemObserved<Project> itemObserved) {
log.debug("getListUserCompliantToRoles called for scope: {}", UserUtils.getCurrent().getContext());
// GET LIST USER COMPLIANT TO ROLES, ignore USER caller
public List<User> getListUserCompliantToRoles(NotificationFor notificationFor, ItemObserved<Project> itemObserved,
boolean ignoreUserCaller) {
log.debug("getListUserCompliantToRoles called for scope: {}, ignore user caller: {}", itemObserved.getContext(),
ignoreUserCaller);
List<String> notifyUserWithRoles = notificationFor.getRoles();
List<User> notifyUsers = new ArrayList<User>();
String anyRole = NotificationGenericConstants.CUSTOM_USER_ROLES.Any.name();
SocialClients socialClients = new SocialClients();
User userCaller = itemObserved.getUserCaller();
if (notifyUserWithRoles.contains(anyRole)) {
try {
// String token = AccessTokenProvider.instance.set(s);
List<String> listUsername = socialClients.getUsernamesByScope();
notifyUsers.addAll(
listUsername.stream().map(username -> new User(username, null)).collect(Collectors.toList()));
if (ignoreUserCaller) {
notifyUsers.removeIf(u -> u.equals(userCaller));
}
} catch (Exception e) {
log.error("Error on reading users from scope {}: ", UserUtils.getCurrent().getContext(), e);
}
@ -198,7 +192,6 @@ public class ManageDoActionNotification {
// We need to create the list of users that must be notified
for (String role : notifyUserWithRoles) {
// Reading the list of users in the VRE with specific role
// Creator of the item
String itemCreatorRole = NotificationGenericConstants.CUSTOM_USER_ROLES.Item_Creator.name();
if (role.compareTo(itemCreatorRole) == 0) {
@ -213,12 +206,17 @@ public class ManageDoActionNotification {
UserUtils.getCurrent().getContext(), e);
}
} else {
// Reading the list of users in the VRE with specific role
try {
List<String> listUsernameForRole = socialClients.getUsernamesByRole(role);
notifyUsers.addAll(listUsernameForRole.stream()
.map(username -> new User(username,
Stream.of(role).collect(Collectors.toCollection(HashSet::new))))
.collect(Collectors.toList()));
if (ignoreUserCaller) {
notifyUsers.removeIf(u -> u.equals(userCaller));
}
log.info("added {} users for role {}", notifyUsers.size(), role);
} catch (Exception e) {
log.error("Error on reading users from scope {}: ", UserUtils.getCurrent().getContext(), e);

View File

@ -2,7 +2,6 @@ package org.gcube.application.cms.notifications.manage;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@ -11,11 +10,11 @@ import org.gcube.application.cms.notifications.config.Notify;
import org.gcube.application.cms.notifications.social.SocialClients;
import org.gcube.application.cms.notifications.substitutor.NMessagesPlaceholdersSubstitutorUtil;
import org.gcube.application.geoportal.common.model.document.accounting.User;
import org.gcube.common.authorization.utils.manager.SecretManager;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.portal.databook.shared.Post;
import org.gcube.social_networking.social_networking_client_library.NotificationClient;
import org.gcube.social_networking.socialnetworking.model.beans.PostInputBean;
import org.gcube.social_networking.socialnetworking.model.beans.Recipient;
import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEvent;
import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEventType;
@ -24,54 +23,44 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class NotifyUser {
private User recipientUser;
private List<User> recipientUsers;
private NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil;
private List<NotificationWhen> listNotificationWhen;
private SocialClients socialClients;
private CatalogueEventType catalogueEventType;
public NotifyUser(SocialClients socialClients, List<NotificationWhen> listWhen,
NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil, User recipientUser) {
NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil, List<User> recipientUsers, CatalogueEventType catalogueEventType) {
this.socialClients = socialClients;
this.listNotificationWhen = listWhen;
this.recipientUser = recipientUser;
this.recipientUsers = recipientUsers;
this.nMPlaceholdersSUtil = nMPlaceholdersSUtil;
this.catalogueEventType = catalogueEventType;
}
public void send() throws Exception {
log.info("send call...");
List<Notify> listNotifies = toNotify();
// TODO
// NotificationClient client = new NotificationClient();
// client.send ??????
public void send() throws Exception {
log.info("send notify...");
List<Notify> listNotifies = toNotify();
for (Notify notify : listNotifies) {
switch (notify.getType()) {
case USER_MESSAGE:
case USER_POST:
log.info("Notification type {}, send: {}", notify.getType(), notify.getSend());
if (notify.getSend()) {
log.debug("Building message...");
String subject = "Message";
String body = null;
// PostClient postClient = socialClients.getPostClient();
// PostInputBean postInputBean = new PostInputBean();
// postInputBean.setEnablenotification(true);
// postInputBean.setText(message);
//
// socialClients.writeUserPost(null)
subject = nMPlaceholdersSUtil.replacePlaceholder(notify.getPlaceholder_title());
body = nMPlaceholdersSUtil.replacePlaceholder(notify.getPlaceholder_msg());
log.debug("subject: {}", subject);
log.debug("body: {}", body);
List<Recipient> rec = Arrays.asList(new Recipient(recipientUser.getUsername()));
log.info("Sending message to users: {}", rec);
log.info("Sending message to users: {}", recipientUsers);
postMessage(subject, body, catalogueEventType);
// sendNotification(null);
// MessageInputBean message = new MessageInputBean(subject, body, new ArrayList<Recipient>(rec));
// String idMessage = socialClients.writeMessage(message);
// log.info("Message sent with id: {}", idMessage);
}
break;
case VRE_POST:
@ -92,12 +81,61 @@ public class NotifyUser {
}
private List<Notify> toNotify() {
List<Notify> listNotifies = new ArrayList<Notify>();
for (NotificationWhen notificationWhen : listNotificationWhen) {
listNotifies.addAll(notificationWhen.getNotify().stream().filter(n -> n.getSend() == true)
.collect(Collectors.toList()));
}
return listNotifies;
}
protected void postMessage(String subject, String bodyMessage, CatalogueEventType catalogueEventType) throws Exception {
CatalogueEvent catalogueEvent = getCatalogueEvent(subject, bodyMessage);
SecretManager secretManager = SecretManagerProvider.instance.get();
//GET geoportal SECRET
// Secret secret = Constants.getCatalogueSecret();
// if(notificationSentByGCat) {
// secretManager.startSession(secret);
// }
// try {
// sendNotification(catalogueEvent);
// }finally {
// if(notificationSentByGCat) {
// secretManager.endSession();
// }
// }
sendNotification(catalogueEvent);
}
protected CatalogueEvent getCatalogueEvent(String subject, String bodyMessage) throws Exception {
CatalogueEvent catalogueEvent = new CatalogueEvent();
catalogueEvent.setType(catalogueEventType);
catalogueEvent.setNotifyText(bodyMessage);
catalogueEvent.setItemId(subject);
String itemURL = nMPlaceholdersSUtil.getPlaceholderMapValues().getGisLink();
if(itemURL!=null) {
catalogueEvent.setItemURL(new URL(itemURL));
}
// Adding recipient users
String[] usersToNotify = recipientUsers.stream().map(u -> u.getUsername()).toArray(String[]::new);
catalogueEvent.setIdsToNotify(usersToNotify);
catalogueEvent.setIdsAsGroup(false);
return catalogueEvent;
}
protected void sendNotification(CatalogueEvent catalogueEvent) throws Exception {
Thread thread = new Thread() {
public void run() {
try {
log.trace("{} is going to send the following notification {}", SecretManagerProvider.instance.get().getUser().getUsername(), catalogueEvent);
NotificationClient nc = new NotificationClient();
NotificationClient nc = socialClients.getNotificationClient();
nc.sendCatalogueEvent(catalogueEvent);
} catch(Exception e) {
log.error("Error while sending notification.", e);
@ -122,13 +160,6 @@ public class NotifyUser {
return catalogueEvent;
}
private List<Notify> toNotify() {
List<Notify> listNotifies = new ArrayList<Notify>();
for (NotificationWhen notificationWhen : listNotificationWhen) {
listNotifies.addAll(notificationWhen.getNotify().stream().filter(n -> n.getSend() == true)
.collect(Collectors.toList()));
}
return listNotifies;
}
}

View File

@ -11,10 +11,10 @@ public class MappingToCatalogue {
// SimpleLifeCycleManager
if (itemPhase.equals(SimpleLifeCycleManager.Phases.PUBLISHED)
&& lastInvokedStep.equals(SimpleLifeCycleManager.Steps.PUBLISH.getId())) {
&& (lastInvokedStep == null || lastInvokedStep.equals(SimpleLifeCycleManager.Steps.PUBLISH.getId()))) {
return CatalogueEventType.ITEM_SUBMITTED;
} else if (itemPhase.equals(SimpleLifeCycleManager.Phases.UNPUBLISHED)
&& lastInvokedStep.equals(SimpleLifeCycleManager.Steps.UNPUBLISH.getId())) {
} else if (itemPhase.equals(SimpleLifeCycleManager.Phases.UNPUBLISHED) && (lastInvokedStep == null
|| lastInvokedStep.equals(SimpleLifeCycleManager.Steps.UNPUBLISH.getId()))) {
// TODO
// Missing ITEM UNPUBLISHED;
@ -22,16 +22,16 @@ public class MappingToCatalogue {
// Default3PhaseManager
if (itemPhase.equals(Default3PhaseManager.Phases.PENDING_APPROVAL)
&& lastInvokedStep.equals(Default3PhaseManager.STEPS.SUBMIT.getId())) {
&& (lastInvokedStep == null || lastInvokedStep.equals(Default3PhaseManager.STEPS.SUBMIT.getId()))) {
return CatalogueEventType.ITEM_SUBMITTED;
} else if (itemPhase.equals(Default3PhaseManager.Phases.PUBLISHED)
&& lastInvokedStep.equals(Default3PhaseManager.STEPS.APPROVE.getId())) {
&& (lastInvokedStep == null || lastInvokedStep.equals(Default3PhaseManager.STEPS.APPROVE.getId()))) {
return CatalogueEventType.ITEM_PUBLISHED;
} else if (itemPhase.equals(LifecycleInformation.CommonPhases.DRAFT_PHASE)
&& lastInvokedStep.equals(Default3PhaseManager.STEPS.REJECT.getId())) {
&& (lastInvokedStep == null || lastInvokedStep.equals(Default3PhaseManager.STEPS.REJECT.getId()))) {
return CatalogueEventType.ITEM_REJECTED;
} else if (itemPhase.equals(LifecycleInformation.CommonPhases.DRAFT_PHASE)
&& lastInvokedStep.equals(Default3PhaseManager.STEPS.UNPUBLISH.getId())) {
&& (lastInvokedStep == null || lastInvokedStep.equals(Default3PhaseManager.STEPS.UNPUBLISH.getId()))) {
// TODO
// Missing ITEM UNPUBLISHED;
}

View File

@ -5,6 +5,7 @@ import java.util.List;
import org.gcube.portal.databook.shared.Post;
import org.gcube.social_networking.social_networking_client_library.MessageClient;
import org.gcube.social_networking.social_networking_client_library.NotificationClient;
import org.gcube.social_networking.social_networking_client_library.PostClient;
import org.gcube.social_networking.social_networking_client_library.UserClient;
import org.gcube.social_networking.socialnetworking.model.beans.MessageInputBean;
@ -25,6 +26,7 @@ public class SocialClients {
private PostClient postClient;
private MessageClient messagesClient;
private UserClient userClient;
private NotificationClient notificationClient;
/**
* Instantiates a new social util.
@ -47,6 +49,12 @@ public class SocialClients {
} catch (Exception e) {
log.error("Error on instacing {}: ", UserClient.class.getSimpleName(), e);
}
try {
this.notificationClient = new NotificationClient();
} catch (Exception e) {
log.error("Error on instacing {}: ", NotificationClient.class.getSimpleName(), e);
}
}
/**
@ -107,4 +115,8 @@ public class SocialClients {
return userClient;
}
public NotificationClient getNotificationClient() {
return notificationClient;
}
}

View File

@ -65,4 +65,8 @@ public class NMessagesPlaceholdersSubstitutorUtil {
return notificationPlaceholder;
}
public SubstitutorMessagesMap getPlaceholderMapValues() {
return placeholderMapValues;
}
}

View File

@ -35,5 +35,8 @@ public class SubstitutorMessagesMap extends HashMap<String, String> {
public String putProjectId(String projectId) {
return super.put(PROJECT_ID, projectId);
}
}
public String getGisLink() {
return super.get(GIS_LINK);
}
}