From eb95248699250beef2e21ac5807f7a314534f7b3 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Mon, 25 Mar 2024 12:02:35 +0100 Subject: [PATCH] updated --- .../cms/notifications/manage/NotifyUser.java | 165 ------------------ 1 file changed, 165 deletions(-) delete mode 100644 notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/NotifyUser.java diff --git a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/NotifyUser.java b/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/NotifyUser.java deleted file mode 100644 index 0acabb3..0000000 --- a/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/NotifyUser.java +++ /dev/null @@ -1,165 +0,0 @@ -package org.gcube.application.cms.notifications.manage; - -import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -import org.gcube.application.cms.notifications.config.NotificationWhen; -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.catalogue.CatalogueEvent; -import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEventType; - -import lombok.extern.slf4j.Slf4j; - -@Slf4j -public class NotifyUser { - - private List recipientUsers; - private NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil; - private List listNotificationWhen; - private SocialClients socialClients; - private CatalogueEventType catalogueEventType; - - public NotifyUser(SocialClients socialClients, List listWhen, - NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil, List recipientUsers, CatalogueEventType catalogueEventType) { - this.socialClients = socialClients; - this.listNotificationWhen = listWhen; - this.recipientUsers = recipientUsers; - this.nMPlaceholdersSUtil = nMPlaceholdersSUtil; - this.catalogueEventType = catalogueEventType; - } - - - public void send() throws Exception { - log.info("send notify..."); - List listNotifies = toNotify(); - - for (Notify notify : listNotifies) { - switch (notify.getType()) { - 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; - - subject = nMPlaceholdersSUtil.replacePlaceholder(notify.getPlaceholder_title()); - body = nMPlaceholdersSUtil.replacePlaceholder(notify.getPlaceholder_msg()); - - log.debug("subject: {}", subject); - log.debug("body: {}", body); - log.info("Sending message to users: {}", recipientUsers); - - postMessage(subject, body, catalogueEventType); - - } - break; - case VRE_POST: - if (notify.getSend()) { - log.info("Notification type {}, send: {}", notify.getType(), notify.getSend()); - PostInputBean toWrite = null; - Post thePost = socialClients.writeUserPost(toWrite); - log.info("{} post created: {} ", notify.getType(), thePost); - } - break; - case EMAIL: - break; - - default: - break; - } - } - - } - - private List toNotify() { - List listNotifies = new ArrayList(); - 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 = socialClients.getNotificationClient(); - nc.sendCatalogueEvent(catalogueEvent); - } catch(Exception e) { - log.error("Error while sending notification.", e); - } - } - }; - // thread.run(); - thread.start(); - } - - protected CatalogueEvent toCatalogueEvent(CatalogueEventType catalogueEventType, String messageString, String subject, String itemURL, List users) throws Exception { - CatalogueEvent catalogueEvent = new CatalogueEvent(); - catalogueEvent.setType(catalogueEventType); - catalogueEvent.setNotifyText(messageString); - catalogueEvent.setItemId(subject); - if(itemURL!=null) { - catalogueEvent.setItemURL(new URL(itemURL)); - } - catalogueEvent.setIdsToNotify(users.toArray(new String[users.size()])); - catalogueEvent.setIdsAsGroup(false); - - return catalogueEvent; - } - - - -}