workspace-tree-widget/src/main/java/org/gcube/portlets/user/workspace/server/notifications/NotificationsProducer.java

441 lines
14 KiB
Java

/**
*
*/
package org.gcube.portlets.user.workspace.server.notifications;
import java.util.HashMap;
import java.util.List;
import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.application.framework.core.session.SessionManager;
import org.gcube.applicationsupportlayer.social.NotificationsManager;
import org.gcube.common.core.scope.GCUBEScope;
import org.gcube.common.core.utils.logging.GCUBEClientLog;
import org.gcube.portlets.user.homelibrary.home.workspace.WorkspaceFolder;
import org.gcube.portlets.user.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.portlets.user.homelibrary.home.workspace.WorkspaceSharedFolder;
import org.gcube.portlets.user.workspace.client.model.InfoContactModel;
import org.gcube.portlets.user.workspace.server.util.Util;
/**
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
*
*/
public class NotificationsProducer {
protected GCUBEClientLog gcubeLogger = new GCUBEClientLog(NotificationsProducer.class);
protected GCUBEScope scope;
protected NotificationsManager notificationsMng;
protected ASLSession aslSession;
protected String userId;
/**
*
* @param aslSession
*/
public NotificationsProducer(ASLSession aslSession) {
this.notificationsMng = Util.getNotificationManager(aslSession);
this.aslSession = aslSession;
this.userId = aslSession.getUsername();
}
public NotificationsManager getNotificationsMng() {
return notificationsMng;
}
public void setNotificationMng(NotificationsManager notificationMng) {
this.notificationsMng = notificationMng;
}
public ASLSession getAslSession() {
return aslSession;
}
/**
* Runs a new thread to notify the contacts passed in input
* @param listContacts
* @param sharedFolder
*/
public void notifyFolderSharing(final List<InfoContactModel> listContacts, final WorkspaceFolder sharedFolder) {
new Thread(){
@Override
public void run() {
gcubeLogger.trace("Send notifies folder sharing is running...");
for (InfoContactModel infoContactModel : listContacts) {
try{
//NOTIFIES ONLY THE USERS THAT ARE DIFFERENT FROM CURRENT USER
if(infoContactModel.getLogin().compareTo(userId)!=0){
gcubeLogger.trace("Sending notification new share folder "+sharedFolder.getName()+" for user "+infoContactModel.getLogin());
//DEBUG
System.out.println("Sending notification new share folder "+sharedFolder.getName()+" for user "+infoContactModel.getLogin());
boolean notify = notificationsMng.notifyFolderSharing(infoContactModel.getLogin(), sharedFolder);
if(!notify)
gcubeLogger.error("An error occured when notify user: "+infoContactModel.getLogin());
}
}catch (Exception e) {
gcubeLogger.error("An error occured in notifyFolderSharing ", e);
e.printStackTrace();
}
}
gcubeLogger.trace("notifies share folder is completed");
}
}.start();
}
/**
* Runs a new thread to notify the contacts passed in input
* @param listContacts
* @param sharedFolder
*/
public void notifyFolderRenamed(final List<InfoContactModel> listSharedContact, final String itemOldName, final String itemNewName, final String idsharedFolder) {
new Thread(){
@Override
public void run() {
gcubeLogger.trace("Send notifies shared folder was renamed is running...");
for (InfoContactModel infoContactModel : listSharedContact) {
try{
//NOTIFIES ONLY THE USERS THAT ARE DIFFERENT FROM CURRENT USER
if(infoContactModel.getLogin().compareTo(userId)!=0){
gcubeLogger.trace("Sending notification share folder "+itemOldName+" was renamed as " + itemNewName+ "for user "+infoContactModel.getLogin());
//DEBUG
System.out.println("Sending notification share folder "+itemOldName+" was renamed as " + itemNewName+ "for user "+infoContactModel.getLogin());
boolean notify = notificationsMng.notifyFolderRenaming(infoContactModel.getLogin(), itemOldName, itemNewName, idsharedFolder);
if(!notify)
gcubeLogger.error("An error occured when notify user: "+infoContactModel.getLogin());
}
}catch (Exception e) {
gcubeLogger.error("An error occured in notifyFolderRenamed ", e);
e.printStackTrace();
}
}
gcubeLogger.trace("notifies share folder was renamed is completed");
}
}.start();
}
/**
* Runs a new thread to notify the contacts passed in input
* @param listContacts
* @param sharedFolder
*/
public void notifyItemUpdated(final List<InfoContactModel> listSharedContact, final WorkspaceItem item, final WorkspaceFolder sharedFolder) {
new Thread(){
@Override
public void run() {
gcubeLogger.trace("Send notifies shared item was updated is running...");
for (InfoContactModel infoContactModel : listSharedContact) {
try{
//NOTIFIES ONLY THE USERS THAT ARE DIFFERENT FROM CURRENT USER
if(infoContactModel.getLogin().compareTo(userId)!=0){
gcubeLogger.trace("Sending notification to user "+infoContactModel.getLogin() + " updated item "+item.getName());
//DEBUG
System.out.println("Sending notification to user "+infoContactModel.getLogin() + " updated item "+item.getName());
boolean notify = notificationsMng.notifyUpdatedItem(infoContactModel.getLogin(), item, sharedFolder);
if(!notify)
gcubeLogger.error("An error occured when notify user: "+infoContactModel.getLogin());
}
}catch (Exception e) {
gcubeLogger.error("An error occured in notifyItemUpdated ", e);
e.printStackTrace();
}
}
gcubeLogger.trace("notifies shared item was updated is completed");
}
}.start();
}
/**
* Runs a new thread to notify the new contacts passed in input
* @param listSharedContact - list of contacts already shared
* @param listSharingContact - list of "new" contacts witch share
* @param sharedFolder - the shared folder
*/
public void notifyAddedUserToSharing(final List<InfoContactModel> listSharedContact, final List<InfoContactModel> listSharingContact, final WorkspaceSharedFolder sharedFolder) {
new Thread() {
@Override
public void run() {
try{
//CREATE TEMPORARY HASH
HashMap<String, InfoContactModel> hashLoginAlreadyShared = new HashMap<String, InfoContactModel>();
for (InfoContactModel infoContactModel : listSharedContact) {
hashLoginAlreadyShared.put(infoContactModel.getLogin(),infoContactModel);
}
if(listSharedContact==null)
return;
for (InfoContactModel infoContactModel : listSharingContact) {
if(hashLoginAlreadyShared.get(infoContactModel.getLogin())==null){ //if is new contact.. notifies share
//NOTIFIES ONLY THE USERS THAT ARE DIFFERENT FROM CURRENT USER
if(infoContactModel.getLogin().compareTo(userId)!=0){
for (InfoContactModel contact : listSharedContact) { //NOTIFIES ALREADY SHARED CONTACTS
try{
gcubeLogger.trace("Sending notification to user "+contact.getLogin()+", added user "+ infoContactModel.getLogin() +" to share folder "+sharedFolder.getName());
//DEBUG
// System.out.println("Sending notification added user "+ infoContactModel.getLogin() +" to share folder "+sharedFolder.getName() + " for user "+contact.getLogin());
boolean notify = notificationsMng.notifyFolderAddedUser(contact.getLogin(), sharedFolder, infoContactModel.getLogin());
if(!notify)
gcubeLogger.error("An error occured when notifies user: "+infoContactModel.getLogin());
}catch (Exception e) {
gcubeLogger.error("An error occured in notifyFolderAddedUser ", e);
e.printStackTrace();
}
}
}
}
}
}catch (Exception e) {
gcubeLogger.error("An error occured in notifyAddedUserToSharing ", e);
e.printStackTrace();
}
}
}.start();
}
/**
* Runs a new thread to notify the contacts passed in input
* @param listContacts
* @param unSharedFolder
*/
public void notifyFolderUnSharing(final List<InfoContactModel> listContacts, final WorkspaceFolder unSharedFolder) {
new Thread() {
@Override
public void run() {
// printContacts(listContacts);
gcubeLogger.trace("Send notifies folder un share is running...");
for (InfoContactModel infoContactModel : listContacts) {
try{
//NOTIFIES ONLY THE USERS THAT ARE DIFFERENT FROM CURRENT USER
if(infoContactModel.getLogin().compareTo(userId)!=0){
gcubeLogger.trace("Sending notification to user "+infoContactModel.getLogin() +" un shared folder "+unSharedFolder.getName());
//DEBUG
System.out.println("Sending notification to user "+infoContactModel.getLogin() +" un shared folder "+unSharedFolder.getName());
boolean notify = notificationsMng.notifyFolderRemovedUser(infoContactModel.getLogin(), unSharedFolder);
if(!notify)
gcubeLogger.error("An error occured when notifies user: "+infoContactModel.getLogin());
}
}catch (Exception e) {
gcubeLogger.error("An error occured in notifyFolderUnSharing ", e);
e.printStackTrace();
}
}
gcubeLogger.trace("notifies of un share notifications is completed");
}
}.start();
}
/**
* Runs a new thread to notify the contacts passed in input
* @param listContacts
* @param workspaceItem
*/
public void notifyAddedItemToSharing(final List<InfoContactModel> listContacts, final WorkspaceItem workspaceItem, final WorkspaceFolder sharedFolder) {
new Thread() {
@Override
public void run() {
// printContacts(listContacts);
gcubeLogger.trace("Send notifies added item in sharedfolder is running...");
//DEBUG
System.out.println("Send notifies added item in sharedfolder is running...");
for (InfoContactModel infoContactModel : listContacts) {
try{
//NOTIFIES ONLY THE USERS THAT ARE DIFFERENT FROM CURRENT USER
if(infoContactModel.getLogin().compareTo(userId)!=0){
gcubeLogger.trace("Sending notification to user "+infoContactModel.getLogin() +" added item "+workspaceItem.getName()+" in shared folder "+sharedFolder.getName());
//DEBUG
System.out.println("Sending notification to user "+infoContactModel.getLogin() +" added item "+workspaceItem.getName()+" in shared folder "+sharedFolder.getName());
//DEBUG
// System.out.println("Send notify folder un share user "+infoContactModel.getLogin());
boolean notify = notificationsMng.notifyAddedItem(infoContactModel.getLogin(), workspaceItem, sharedFolder);
if(!notify){
gcubeLogger.error("An error occured when notify user: "+infoContactModel.getLogin());
//DEBUG
System.out.println("An error occured when notify user: "+infoContactModel.getLogin());
}
}
}catch (Exception e) {
gcubeLogger.error("An error occured in notifyAddedItemToSharing ", e);
e.printStackTrace();
}
}
gcubeLogger.trace("notifies of added item in shared folder is completed");
//DEBUG
System.out.println("notifies of added item in shared folder is completed");
}
}.start();
}
/**
* Runs a new thread to notify the contacts passed in input
* @param listContacts
* @param sharedFolder
*/
public void notifyRemovedItemToSharing(final List<InfoContactModel> listContacts, final WorkspaceItem workspaceItem, final WorkspaceFolder sharedFolder) {
new Thread() {
@Override
public void run() {
// printContacts(listContacts);
gcubeLogger.trace("Sending notificationremove item in shared folder is running...");
System.out.println("Sending notification remove item in shared folder is running...");
for (InfoContactModel infoContactModel : listContacts) {
try{
//NOTIFIES ONLY THE USERS THAT ARE DIFFERENT FROM CURRENT USER
if(infoContactModel.getLogin().compareTo(userId)!=0){
gcubeLogger.trace("Sending notification to user "+infoContactModel.getLogin() +" removed item "+workspaceItem.getName()+" in shared folder "+sharedFolder.getName());
//DEBUG
System.out.println("Sending notification to user "+infoContactModel.getLogin() +" removed item "+workspaceItem.getName()+" in shared folder "+sharedFolder.getName());
boolean notify = notificationsMng.notifyRemovedItem(infoContactModel.getLogin(), workspaceItem, sharedFolder);
if(!notify){
gcubeLogger.error("An error occured when notify user: "+infoContactModel.getLogin());
//DEBUG
System.out.println("An error occured when notify user: "+infoContactModel.getLogin());
}
}
}catch (Exception e) {
gcubeLogger.error("An error occured in notifyRemovedItemToSharing ", e);
e.printStackTrace();
}
}
gcubeLogger.trace("notifies of removed item in shared folder is completed");
//DEBUG
System.out.println("notifies of removed item in shared folder is completed");
}
}.start();
}
//DEBUG
private void printContacts(List<InfoContactModel> listContacts){
System.out.println("Print contacts");
for (InfoContactModel infoContactModel : listContacts) {
System.out.println(infoContactModel);
}
System.out.println("End print contacts");
}
public static void main(String[] args) throws Exception
{
String sessionID = "1";
String user = "francesco.mangiacrapa";
String scopeString = "/gcube/devsec/devVRE";
String fullName = "Francesco Mangiacrapa";
GCUBEScope scope;
ASLSession session;
session = SessionManager.getInstance().getASLSession(sessionID, user);
scope = GCUBEScope.getScope(scopeString);
session.setScope(scope.toString());
session.setUserAvatarId(user + "Avatar");
session.setUserFullName(fullName);
NotificationsProducer feeder = new NotificationsProducer(session);
}
}