135 lines
4.9 KiB
Java
135 lines
4.9 KiB
Java
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;
|
|
|
|
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.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;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
|
public class NotifyUser {
|
|
|
|
private User recipientUser;
|
|
private NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil;
|
|
private List<NotificationWhen> listNotificationWhen;
|
|
private SocialClients socialClients;
|
|
|
|
public NotifyUser(SocialClients socialClients, List<NotificationWhen> listWhen,
|
|
NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil, User recipientUser) {
|
|
this.socialClients = socialClients;
|
|
this.listNotificationWhen = listWhen;
|
|
this.recipientUser = recipientUser;
|
|
this.nMPlaceholdersSUtil = nMPlaceholdersSUtil;
|
|
}
|
|
|
|
public void send() throws Exception {
|
|
log.info("send call...");
|
|
List<Notify> listNotifies = toNotify();
|
|
|
|
// TODO
|
|
// NotificationClient client = new NotificationClient();
|
|
// client.send ??????
|
|
|
|
for (Notify notify : listNotifies) {
|
|
switch (notify.getType()) {
|
|
case USER_MESSAGE:
|
|
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);
|
|
|
|
// 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:
|
|
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;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
|
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<String> 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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|