gcube-cms-suite/notifications-plugins/src/main/java/org/gcube/application/cms/notifications/manage/ManageNotifyUser.java

92 lines
3.3 KiB
Java

package org.gcube.application.cms.notifications.manage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.gcube.application.cms.notifications.NotificationGenericConstants.NOTIFICATION_TYPE;
import org.gcube.application.cms.notifications.config.NotificationFor;
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.portal.databook.shared.Post;
import org.gcube.social_networking.socialnetworking.model.beans.MessageInputBean;
import org.gcube.social_networking.socialnetworking.model.beans.PostInputBean;
import org.gcube.social_networking.socialnetworking.model.beans.Recipient;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ManageNotifyUser {
private NotificationFor notificationFor;
private User recipientUser;
private NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil;
public ManageNotifyUser(NotificationFor notificationFor, NMessagesPlaceholdersSubstitutorUtil nMPlaceholdersSUtil,
User recipientUser) {
this.notificationFor = notificationFor;
this.recipientUser = recipientUser;
this.nMPlaceholdersSUtil = nMPlaceholdersSUtil;
}
public void send() throws Exception {
SocialClients socialClients = new SocialClients();
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.info("Sending message...");
String subject = "Message";
String body = null;
subject = nMPlaceholdersSUtil.replacePlaceholder(notify.getPlaceholder_title());
body = nMPlaceholdersSUtil.replacePlaceholder(notify.getPlaceholder_msg());
log.debug("Sending subject: {}", subject);
log.debug("Sending body: {}", body);
List<Recipient> rec = Arrays.asList(new Recipient(recipientUser.getUsername()));
log.info("Sending message...");
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:
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<Notify> toNotify() {
List<Notify> listNotifies = new ArrayList<Notify>();
List<NotificationWhen> listWhen = notificationFor.getWhen();
for (NotificationWhen notificationWhen : listWhen) {
listNotifies.addAll(notificationWhen.getNotify().stream().filter(n -> n.getSend() == true)
.collect(Collectors.toList()));
}
return listNotifies;
}
}