workspace-uploader/src/main/java/org/gcube/portlets/widgets/workspaceuploader/server/notification/NotificationsWorkspaceUploa...

182 lines
5.9 KiB
Java

/**
*
*/
package org.gcube.portlets.widgets.workspaceuploader.server.notification;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.gcube.application.framework.core.session.ASLSession;
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.workspace.WorkspaceItem;
import org.gcube.common.homelibrary.home.workspace.WorkspaceSharedFolder;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.portlets.widgets.workspaceuploader.server.util.WsUtil;
import org.gcube.portlets.widgets.workspaceuploader.shared.ContactModel;
/**
* The Class NotificationsProducer.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Aug 3, 2015
*/
public class NotificationsWorkspaceUploaderProducer {
protected ScopeBean scope;
protected static Logger logger = Logger.getLogger(NotificationsWorkspaceUploaderProducer.class);
protected NotificationsManager notificationsMng;
protected ASLSession aslSession;
protected String userId;
/**
* Instantiates a new notifications producer.
*
* @param aslSession the asl session
* @param request the request
*/
public NotificationsWorkspaceUploaderProducer(ASLSession aslSession, HttpServletRequest request) {
this.notificationsMng = getNotificationManager(aslSession, request);
this.aslSession = aslSession;
this.userId = aslSession.getUsername();
}
/**
* Gets the notification manager.
*
* @param session the session
* @param request the request
* @return the notification manager
*/
public NotificationsManager getNotificationManager(ASLSession session, HttpServletRequest request) {
try{
logger.trace("Create new NotificationsManager for user: "+session.getUsername());
logger.trace("New ApplicationNotificationsManager with portlet class name: "+WsUtil.NOTIFICATION_PORTLET_CLASS_ID);
SocialNetworkingSite site = new SocialNetworkingSite(request);
SocialNetworkingUser curser = new SocialNetworkingUser(session.getUsername(), session.getUserEmailAddress(), session.getUserFullName(), session.getUserAvatarId());
return new ApplicationNotificationsManager(site, session.getScope(), curser, WsUtil.NOTIFICATION_PORTLET_CLASS_ID);
}catch (Exception e) {
logger.error("An error occurred instancing ApplicationNotificationsManager for user: "+session.getUsername(),e);
return null;
}
}
/**
* Gets the notifications mng.
*
* @return the notifications mng
*/
public NotificationsManager getNotificationsMng() {
return notificationsMng;
}
/**
* Sets the notification mng.
*
* @param notificationMng the new notification mng
*/
public void setNotificationMng(NotificationsManager notificationMng) {
this.notificationsMng = notificationMng;
}
/**
* Gets the asl session.
*
* @return the asl session
*/
public ASLSession getAslSession() {
return aslSession;
}
/**
* Runs a new thread to notify the contacts passed in input.
*
* @param listContacts the list contacts
* @param workspaceItem the workspace item
* @param sharedFolder the shared folder
*/
public void notifyAddedItemToSharing(final List<ContactModel> listContacts, final WorkspaceItem workspaceItem, final WorkspaceSharedFolder sharedFolder) {
new Thread() {
@Override
public void run() {
// printContacts(listContacts);
logger.info("Send notifies added item in sharedfolder is running...");
//DEBUG
System.out.println("Send notifies added item in sharedfolder is running...");
for (ContactModel infoContactModel : listContacts) {
try{
//NOTIFIES ONLY THE USERS THAT ARE DIFFERENT FROM CURRENT USER
if(infoContactModel.getLogin().compareTo(userId)!=0){
logger.info("Sending notification to user "+infoContactModel.getLogin() +" added item "+workspaceItem.getName()+" in shared folder "+sharedFolder.getName());
boolean notify = notificationsMng.notifyAddedItem(infoContactModel.getLogin(), workspaceItem, sharedFolder);
if(!notify){
logger.error("An error occured when notify user: "+infoContactModel.getLogin());
}
}
}catch (Exception e) {
logger.error("An error occured in notifyAddedItemToSharing ", e);
}
}
logger.trace("notifies of added item in shared folder is completed");
}
}.start();
}
/**
* Runs a new thread to notify the contacts passed in input.
*
* @param listContacts the list contacts
* @param workspaceItem the workspace item
* @param sharedFolder the shared folder
*/
public void notifyUpdatedItemToSharing(final List<ContactModel> listContacts, final WorkspaceItem workspaceItem, final WorkspaceSharedFolder sharedFolder) {
new Thread() {
@Override
public void run() {
// printContacts(listContacts);
logger.info("Send notifies updated item in shared folder is running...");
for (ContactModel infoContactModel : listContacts) {
try{
//NOTIFIES ONLY THE USERS THAT ARE DIFFERENT FROM CURRENT USER
if(infoContactModel.getLogin().compareTo(userId)!=0){
logger.info("Sending notification to user "+infoContactModel.getLogin() +" updated item "+workspaceItem.getName()+" in shared folder "+sharedFolder.getName());
boolean notify = notificationsMng.notifyUpdatedItem(infoContactModel.getLogin(), workspaceItem, sharedFolder);
if(!notify){
logger.error("An error updated when notify user: "+infoContactModel.getLogin());
}
}
}catch (Exception e) {
logger.error("An error updated in notifyAddedItemToSharing ", e);
e.printStackTrace();
}
}
logger.trace("notifies of updated item in shared folder is completed");
}
}.start();
}
}