2015-10-02 11:46:32 +02:00
/ * *
2016-05-31 15:53:54 +02:00
*
2015-10-02 11:46:32 +02:00
* /
package org.gcube.portlets.widgets.workspaceuploader.server.notification ;
import java.util.List ;
2016-05-31 15:53:54 +02:00
import javax.servlet.http.HttpServletRequest ;
2016-07-14 14:30:46 +02:00
import javax.servlet.http.HttpSession ;
2016-05-31 15:53:54 +02:00
2016-06-01 18:43:17 +02:00
import org.gcube.applicationsupportlayer.social.ApplicationNotificationsManager ;
2015-10-02 11:46:32 +02:00
import org.gcube.applicationsupportlayer.social.NotificationsManager ;
2018-06-26 18:37:52 +02:00
import org.gcube.applicationsupportlayer.social.shared.SocialFileItem ;
2016-06-01 18:43:17 +02:00
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingSite ;
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser ;
2018-06-26 18:37:52 +02:00
import org.gcube.applicationsupportlayer.social.shared.SocialSharedFolder ;
2016-11-24 12:28:48 +01:00
import org.gcube.common.portal.PortalContext ;
2015-10-02 11:46:32 +02:00
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.portlets.widgets.workspaceuploader.server.util.WsUtil ;
import org.gcube.portlets.widgets.workspaceuploader.shared.ContactModel ;
2016-11-24 12:28:48 +01:00
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
2018-06-26 18:37:52 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2015-10-02 11:46:32 +02:00
/ * *
* The Class NotificationsProducer .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
* Aug 3 , 2015
* /
public class NotificationsWorkspaceUploaderProducer {
protected ScopeBean scope ;
2016-05-31 15:53:54 +02:00
2018-06-26 18:37:52 +02:00
protected static Logger logger = LoggerFactory . getLogger ( NotificationsWorkspaceUploaderProducer . class ) ;
2016-05-31 15:53:54 +02:00
2015-10-02 11:46:32 +02:00
protected NotificationsManager notificationsMng ;
2016-11-24 12:28:48 +01:00
2016-12-22 15:03:33 +01:00
protected String username ;
2015-10-02 11:46:32 +02:00
/ * *
2016-07-14 14:30:46 +02:00
* Instantiates a new notifications workspace uploader producer .
2015-10-02 11:46:32 +02:00
*
2018-06-26 18:37:52 +02:00
* @param scopeGroupId the scope group id
2016-07-14 14:30:46 +02:00
* @param httpSession the http session
2016-05-31 15:53:54 +02:00
* @param request the request
2015-10-02 11:46:32 +02:00
* /
2016-11-24 12:28:48 +01:00
public NotificationsWorkspaceUploaderProducer ( String scopeGroupId , HttpSession httpSession , HttpServletRequest request ) {
this . notificationsMng = getNotificationManager ( scopeGroupId , httpSession , request ) ;
PortalContext pContext = PortalContext . getConfiguration ( ) ;
GCubeUser currUser = pContext . getCurrentUser ( request ) ;
2016-12-22 15:03:33 +01:00
this . username = currUser . getUsername ( ) ;
2015-10-02 11:46:32 +02:00
}
2016-07-14 14:30:46 +02:00
/ * *
* Gets the notification manager .
*
2018-06-26 18:37:52 +02:00
* @param scopeGroupId the scope group id
* @param httpSession the http session
2016-07-14 14:30:46 +02:00
* @param request the request
* @return the notification manager
* /
2016-11-24 12:28:48 +01:00
public NotificationsManager getNotificationManager ( String scopeGroupId , HttpSession httpSession , HttpServletRequest request ) {
2016-07-14 14:30:46 +02:00
NotificationsManager notifMng = ( NotificationsManager ) httpSession . getAttribute ( WsUtil . NOTIFICATION_MANAGER_UPLOADER ) ;
2016-11-24 12:28:48 +01:00
PortalContext pContext = PortalContext . getConfiguration ( ) ;
GCubeUser currUser = pContext . getCurrentUser ( request ) ;
2016-07-14 14:30:46 +02:00
if ( notifMng = = null ) {
try {
2016-12-22 15:03:33 +01:00
2016-11-24 12:28:48 +01:00
String scope = pContext . getCurrentScope ( scopeGroupId ) ;
logger . trace ( " Create new NotificationsManager for user: " + currUser . getUsername ( ) + " Scope= " + scope ) ;
2016-07-14 14:30:46 +02:00
logger . trace ( " New ApplicationNotificationsManager with portlet class name: " + WsUtil . NOTIFICATION_PORTLET_CLASS_ID ) ;
logger . info ( " Request URI: " + request . getRequestURI ( ) ) ;
SocialNetworkingSite site = new SocialNetworkingSite ( request ) ;
2016-11-24 12:28:48 +01:00
SocialNetworkingUser curser = new SocialNetworkingUser ( currUser . getUsername ( ) , currUser . getEmail ( ) , currUser . getFullname ( ) , currUser . getUserAvatarURL ( ) ) ;
2016-12-22 15:03:33 +01:00
2016-11-24 12:28:48 +01:00
notifMng = new ApplicationNotificationsManager ( site , scope , curser , WsUtil . NOTIFICATION_PORTLET_CLASS_ID ) ;
2016-07-14 14:30:46 +02:00
httpSession . setAttribute ( WsUtil . NOTIFICATION_MANAGER_UPLOADER , notifMng ) ;
} catch ( Exception e ) {
2016-11-24 12:28:48 +01:00
logger . error ( " An error occurred instancing ApplicationNotificationsManager for user: " + currUser . getUsername ( ) , e ) ;
2016-07-14 14:30:46 +02:00
}
}
return notifMng ;
2016-06-01 18:43:17 +02:00
}
2015-10-02 11:46:32 +02:00
/ * *
* 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 ;
}
2016-12-22 15:03:33 +01:00
2018-06-26 18:37:52 +02:00
2015-10-02 11:46:32 +02:00
/ * *
* Runs a new thread to notify the contacts passed in input .
*
* @param listContacts the list contacts
2018-06-26 18:37:52 +02:00
* @param item the item
2015-10-02 11:46:32 +02:00
* @param sharedFolder the shared folder
* /
2018-06-26 18:37:52 +02:00
public void notifyAddedItemToSharing ( final List < ContactModel > listContacts , final SocialFileItem socialItem , final SocialSharedFolder socialSharedFolder ) {
2015-10-02 11:46:32 +02:00
new Thread ( ) {
@Override
public void run ( ) {
// printContacts(listContacts);
logger . info ( " Send notifies added item in sharedfolder is running... " ) ;
//DEBUG
2017-01-20 14:26:38 +01:00
//System.out.println("Send notifies added item in sharedfolder is running...");
2015-10-02 11:46:32 +02:00
for ( ContactModel infoContactModel : listContacts ) {
try {
//NOTIFIES ONLY THE USERS THAT ARE DIFFERENT FROM CURRENT USER
2016-12-22 15:03:33 +01:00
if ( infoContactModel . getLogin ( ) . compareTo ( username ) ! = 0 ) {
2018-06-26 18:37:52 +02:00
logger . info ( " Sending notification to user " + infoContactModel . getLogin ( ) + " added item " + socialItem . getName ( ) + " in shared folder " + socialSharedFolder . getName ( ) ) ;
boolean notify = notificationsMng . notifyAddedItem ( infoContactModel . getLogin ( ) , socialItem , socialSharedFolder ) ;
if ( ! notify ) {
logger . error ( " An error occured when notify user: " + infoContactModel . getLogin ( ) ) ;
}
2015-10-02 11:46:32 +02:00
}
} catch ( Exception e ) {
logger . error ( " An error occured in notifyAddedItemToSharing " , e ) ;
}
}
2016-05-31 15:53:54 +02:00
2015-10-02 11:46:32 +02:00
logger . trace ( " notifies of added item in shared folder is completed " ) ;
}
} . start ( ) ;
}
2016-05-31 15:53:54 +02:00
2015-10-02 11:46:32 +02:00
/ * *
* 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
* /
2018-06-26 18:37:52 +02:00
public void notifyUpdatedItemToSharing ( final List < ContactModel > listContacts , final SocialFileItem socialItem , final SocialSharedFolder socialSharedFolder ) {
2015-10-02 11:46:32 +02:00
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
2016-12-22 15:03:33 +01:00
if ( infoContactModel . getLogin ( ) . compareTo ( username ) ! = 0 ) {
2018-06-25 12:42:41 +02:00
//TODO notificationsMng.notifyUpdatedItem(infoContactModel.getLogin(), workspaceItem, sharedFolder);
2018-06-26 18:37:52 +02:00
logger . info ( " Sending notification to user " + infoContactModel . getLogin ( ) + " updated item " + socialItem . getName ( ) + " in shared folder " + socialSharedFolder . getName ( ) ) ;
boolean notify = notificationsMng . notifyUpdatedItem ( infoContactModel . getLogin ( ) , socialItem , socialSharedFolder ) ;
2015-10-02 11:46:32 +02:00
if ( ! notify ) {
logger . error ( " An error updated when notify user: " + infoContactModel . getLogin ( ) ) ;
2018-06-26 18:37:52 +02:00
}
2015-10-02 11:46:32 +02:00
}
} catch ( Exception e ) {
logger . error ( " An error updated in notifyAddedItemToSharing " , e ) ;
}
}
logger . trace ( " notifies of updated item in shared folder is completed " ) ;
}
} . start ( ) ;
}
2016-05-31 15:53:54 +02:00
2015-10-02 11:46:32 +02:00
}