statistical-algorithms-impo.../src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/server/social/AlgorithmNotification.java

104 lines
3.7 KiB
Java

package org.gcube.portlets.user.statisticalalgorithmsimporter.server.social;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager;
import org.gcube.applicationsupportlayer.social.NotificationsManager;
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite;
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
import org.gcube.portal.notifications.bean.GenericItemBean;
import org.gcube.portal.notifications.thread.MessageNotificationsThread;
import org.gcube.portlets.user.statisticalalgorithmsimporter.server.util.ServiceCredentials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* TDMNotification notification sharing TR, templates or rules
*
* @author Giancarlo Panichi
*
*
*/
public class AlgorithmNotification extends Thread {
private static Logger logger = LoggerFactory.getLogger(AlgorithmNotification.class);
private HttpServletRequest httpServletRequest;
private ServiceCredentials serviceCredentials;
private ArrayList<Recipient> recipients;
private String body;
public AlgorithmNotification(HttpServletRequest httpServletRequest, ServiceCredentials serviceCredentials,
ArrayList<Recipient> recipients, String body) {
this.serviceCredentials = serviceCredentials;
this.recipients = recipients;
this.httpServletRequest = httpServletRequest;
this.body=body;
}
public void run() {
algorithmPublicationEmail();
}
private void algorithmPublicationEmail() {
try {
Workspace workspace = HomeLibrary.getUserWorkspace(serviceCredentials.getUserName());
List<String> recipientIds = retrieveListAddressee();
List<GenericItemBean> recipients = retrieveRecipients();
String subject = "[SAI] New software publication requested";
String messageId;
messageId = workspace.getWorkspaceMessageManager().sendMessageToPortalLogins(subject, body,
new ArrayList<String>(), recipientIds);
logger.debug("Sending message notification to: " + recipientIds.toString());
SocialNetworkingSite site = new SocialNetworkingSite(httpServletRequest);
SocialNetworkingUser user = new SocialNetworkingUser(serviceCredentials.getUserName(),
serviceCredentials.getEmail(), serviceCredentials.getFullName(),
serviceCredentials.getUserAvatarURL());
NotificationsManager nm = new ApplicationNotificationsManager(site, serviceCredentials.getScope(), user);
Thread thread = new Thread(new MessageNotificationsThread(recipients, messageId, subject, body, nm));
thread.start();
} catch (InternalErrorException | WorkspaceFolderNotFoundException | HomeNotFoundException e) {
logger.error("AlgorithmPublicationEmail(): " + e.getLocalizedMessage());
e.printStackTrace();
}
}
private List<GenericItemBean> retrieveRecipients() {
List<GenericItemBean> genericItemBeanRecipients = new ArrayList<GenericItemBean>();
for (Recipient recipient : recipients) {
genericItemBeanRecipients.add(new GenericItemBean(recipient.getUser(), recipient.getUser(),
recipient.getName() + " " + recipient.getSurname(), ""));
}
return genericItemBeanRecipients;
}
private List<String> retrieveListAddressee() {
ArrayList<String> addressee = new ArrayList<String>();
for (Recipient recipient : recipients) {
addressee.add(recipient.getUser());
}
return addressee;
}
}