2022-05-26 13:35:33 +02:00
package org.gcube.gcat.moderation.thread.social.notifications ;
import java.net.URL ;
2023-02-01 15:48:41 +01:00
import java.util.HashSet ;
2022-05-26 13:35:33 +02:00
import java.util.Set ;
import org.gcube.common.authorization.utils.manager.SecretManager ;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider ;
import org.gcube.common.authorization.utils.secret.Secret ;
import org.gcube.gcat.api.moderation.CMItemStatus ;
import org.gcube.gcat.api.moderation.Moderated ;
import org.gcube.gcat.moderation.thread.ModerationThread ;
2022-05-27 12:14:07 +02:00
import org.gcube.gcat.persistence.ckan.CKANUser ;
2022-05-26 13:35:33 +02:00
import org.gcube.gcat.social.SocialUsers ;
import org.gcube.gcat.utils.Constants ;
import org.gcube.social_networking.social_networking_client_library.NotificationClient ;
import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEvent ;
import org.gcube.social_networking.socialnetworking.model.beans.catalogue.CatalogueEventType ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/ * *
* @author Luca Frosini ( ISTI - CNR )
* /
public class SocialNotificationModerationThread extends ModerationThread {
private static final Logger logger = LoggerFactory . getLogger ( SocialNotificationModerationThread . class ) ;
2022-05-27 17:20:52 +02:00
public static final String AUTHOR = " Author " ;
2022-05-26 13:35:33 +02:00
protected CatalogueEventType catalogueEventType ;
2022-05-27 17:20:52 +02:00
protected boolean comment ;
2022-05-26 13:35:33 +02:00
2023-02-01 15:48:41 +01:00
protected boolean notificationSentByGCat ;
protected boolean notificationToSelfOnly ;
2022-05-27 17:20:52 +02:00
public SocialNotificationModerationThread ( ) {
super ( ) ;
this . comment = false ;
2023-02-01 15:48:41 +01:00
this . notificationSentByGCat = false ;
this . notificationToSelfOnly = false ;
2022-05-26 13:35:33 +02:00
}
/ * *
* Create the message for an item that is created / updated
* /
2022-05-27 12:14:07 +02:00
protected void notifyItemToBeManaged ( ) throws Exception {
2022-05-26 13:35:33 +02:00
/ *
* An example of created message is :
*
2022-05-27 17:20:52 +02:00
* [ mister x ] created / updated the item " [TITLE] " . You are kindly requested to review it and decide either to APPROVE or REJECT it . [ Go to catalogue ]
2022-05-26 13:35:33 +02:00
*
* /
String fullName = ckanUser . getNameSurname ( ) ;
StringBuffer stringBuffer = new StringBuffer ( ) ;
2022-05-27 17:20:52 +02:00
if ( notificationSentByGCat ) {
stringBuffer . append ( fullName ) ;
}
2022-05-26 13:35:33 +02:00
stringBuffer . append ( create ? " created " : " updated " ) ;
2022-05-27 12:14:07 +02:00
stringBuffer . append ( " the item " ) ;
2022-05-27 17:20:52 +02:00
stringBuffer = addQuotedTitle ( stringBuffer ) ;
stringBuffer . append ( " . You are kindly requested to review it and decide either to APPROVE or REJECT it. " ) ;
2022-05-26 13:35:33 +02:00
postMessage ( stringBuffer . toString ( ) ) ;
2023-02-01 15:48:41 +01:00
notificationToSelfOnly = true ;
stringBuffer = new StringBuffer ( ) ;
stringBuffer . append ( " Thank you for submitting your item " ) ;
stringBuffer = addQuotedTitle ( stringBuffer ) ;
stringBuffer . append ( " to the catalogue. Please consider this notification as confirmation that your item has been successfully received and will be managed by the catalogue moderators. " ) ;
postMessage ( stringBuffer . toString ( ) ) ;
notificationToSelfOnly = false ;
2022-05-26 13:35:33 +02:00
}
2023-02-01 15:48:41 +01:00
protected void notifyItemDeleted ( ) throws Exception {
String fullName = ckanUser . getNameSurname ( ) ;
StringBuffer stringBuffer = new StringBuffer ( ) ;
if ( notificationSentByGCat ) {
stringBuffer . append ( fullName ) ;
}
stringBuffer . append ( " permanently deleted " ) ;
stringBuffer . append ( " the item " ) ;
stringBuffer = addQuotedTitle ( stringBuffer ) ;
stringBuffer . append ( " . " ) ;
postMessage ( stringBuffer . toString ( ) ) ;
}
2022-05-26 13:35:33 +02:00
2023-02-01 15:48:41 +01:00
@Override
2022-05-26 13:35:33 +02:00
public void postItemCreated ( ) throws Exception {
create = true ;
cmItemStatus = CMItemStatus . PENDING ;
catalogueEventType = CatalogueEventType . ITEM_SUBMITTED ;
notifyItemToBeManaged ( ) ;
}
2023-02-01 15:48:41 +01:00
@Override
2022-05-26 13:35:33 +02:00
public void postItemUpdated ( ) throws Exception {
create = false ;
cmItemStatus = CMItemStatus . PENDING ;
catalogueEventType = CatalogueEventType . ITEM_UPDATED ;
notifyItemToBeManaged ( ) ;
}
2022-05-27 17:20:52 +02:00
protected StringBuffer addUserWithRole ( String fullName , String role , StringBuffer stringBuffer , boolean addUserFullName ) {
if ( addUserFullName ) {
stringBuffer . append ( fullName ) ;
}
if ( role ! = null ) {
stringBuffer . append ( " [ " ) ;
stringBuffer . append ( role ) ;
stringBuffer . append ( " ] " ) ;
}
2022-05-26 13:35:33 +02:00
return stringBuffer ;
}
2022-05-27 17:20:52 +02:00
protected StringBuffer addUserWithRole ( String fullName , String role , StringBuffer stringBuffer ) {
return addUserWithRole ( fullName , role , stringBuffer , notificationSentByGCat ) ;
}
2022-05-26 13:35:33 +02:00
public void postItemManaged ( String userMessage ) throws Exception {
2022-05-27 12:14:07 +02:00
/ *
* [ mister x ] rejected the item " [TITLE] " with this accompanying message " [MESSAGE] " . To resubmit it [ Go to catalogue ]
*
* [ mister x ] approved the item " [TITLE] " with this accompanying message " [MESSAGE] " . [ Go to catalogue ]
* /
2023-02-01 15:48:41 +01:00
this . create = false ;
2022-05-26 13:35:33 +02:00
String fullName = ckanUser . getNameSurname ( ) ;
StringBuffer stringBuffer = new StringBuffer ( ) ;
stringBuffer = addUserWithRole ( fullName , Moderated . CATALOGUE_MODERATOR , stringBuffer ) ;
stringBuffer . append ( cmItemStatus . getValue ( ) ) ;
2022-05-27 17:20:52 +02:00
stringBuffer . append ( " the item " ) ;
stringBuffer = addQuotedTitle ( stringBuffer ) ;
2022-05-26 13:35:33 +02:00
if ( userMessage ! = null & & userMessage . length ( ) > 0 ) {
2022-05-27 17:20:52 +02:00
stringBuffer . append ( " with this accompanying message \" " ) ;
2022-05-26 13:35:33 +02:00
stringBuffer . append ( userMessage ) ;
2022-05-27 17:20:52 +02:00
stringBuffer . append ( " \" " ) ;
2022-05-27 12:14:07 +02:00
}
stringBuffer . append ( " . " ) ;
if ( cmItemStatus = = CMItemStatus . REJECTED ) {
stringBuffer . append ( " To resubmit it " ) ;
2022-05-26 13:35:33 +02:00
}
postMessage ( stringBuffer . toString ( ) ) ;
}
@Override
public void postItemRejected ( String userMessage ) throws Exception {
2023-02-01 15:48:41 +01:00
this . create = false ;
this . cmItemStatus = CMItemStatus . REJECTED ;
this . catalogueEventType = CatalogueEventType . ITEM_REJECTED ;
2022-05-26 13:35:33 +02:00
postItemManaged ( userMessage ) ;
}
@Override
public void postItemApproved ( String userMessage ) throws Exception {
2023-02-01 15:48:41 +01:00
this . create = false ;
this . cmItemStatus = CMItemStatus . APPROVED ;
this . catalogueEventType = CatalogueEventType . ITEM_PUBLISHED ;
2022-05-26 13:35:33 +02:00
postItemManaged ( userMessage ) ;
}
2023-02-01 15:48:41 +01:00
@Override
public void postItemDeleted ( CMItemStatus cmItemStatus ) throws Exception {
this . create = false ;
this . cmItemStatus = cmItemStatus ;
this . catalogueEventType = CatalogueEventType . ITEM_REMOVED ;
notifyItemDeleted ( ) ;
}
2022-05-27 17:20:52 +02:00
protected StringBuffer addQuotedTitle ( StringBuffer stringBuffer , String quotingCharacter ) {
stringBuffer . append ( quotingCharacter ) ;
stringBuffer . append ( itemTitle ) ;
stringBuffer . append ( quotingCharacter ) ;
return stringBuffer ;
}
protected StringBuffer addQuotedTitle ( StringBuffer stringBuffer ) {
return addQuotedTitle ( stringBuffer , " \" " ) ;
}
2022-05-26 13:35:33 +02:00
protected String getSubject ( ) {
2022-05-27 17:20:52 +02:00
StringBuffer stringBuffer = new StringBuffer ( ) ;
String fullName = ckanUser . getNameSurname ( ) ;
if ( ! comment ) {
2022-05-27 12:14:07 +02:00
switch ( catalogueEventType ) {
case ITEM_SUBMITTED :
2022-05-27 17:20:52 +02:00
stringBuffer . append ( fullName ) ;
stringBuffer . append ( " created the item " ) ;
2022-05-27 12:14:07 +02:00
break ;
case ITEM_UPDATED :
2022-05-27 17:20:52 +02:00
stringBuffer . append ( fullName ) ;
stringBuffer . append ( " updated the item " ) ;
2022-05-27 12:14:07 +02:00
break ;
case ITEM_REJECTED :
case ITEM_PUBLISHED :
2022-05-27 17:20:52 +02:00
addUserWithRole ( fullName , Moderated . CATALOGUE_MODERATOR , stringBuffer , true ) ;
stringBuffer . append ( cmItemStatus . getValue ( ) ) ;
stringBuffer . append ( " the item " ) ;
2022-05-27 12:14:07 +02:00
break ;
default :
break ;
}
} else {
2022-05-27 17:20:52 +02:00
addUserWithRole ( fullName , itemAuthor ? SocialNotificationModerationThread . AUTHOR : Moderated . CATALOGUE_MODERATOR , stringBuffer , true ) ;
stringBuffer . append ( " commented on the item " ) ;
2022-05-26 13:35:33 +02:00
}
2022-05-27 17:20:52 +02:00
stringBuffer = addQuotedTitle ( stringBuffer ) ;
return stringBuffer . toString ( ) ;
2022-05-26 13:35:33 +02:00
}
protected CatalogueEvent getCatalogueEvent ( String messageString ) throws Exception {
CatalogueEvent catalogueEvent = new CatalogueEvent ( ) ;
2022-05-27 17:20:52 +02:00
catalogueEvent . setType ( catalogueEventType ) ;
catalogueEvent . setNotifyText ( messageString ) ;
2022-05-27 12:14:07 +02:00
catalogueEvent . setItemId ( getSubject ( ) ) ;
2022-05-26 13:35:33 +02:00
if ( cmItemStatus = = CMItemStatus . APPROVED ) {
catalogueEvent . setItemURL ( new URL ( itemURL ) ) ;
} else {
catalogueEvent . setItemURL ( new URL ( getModerationURL ( ) ) ) ;
}
2023-02-01 15:48:41 +01:00
Set < String > users = new HashSet < > ( ) ;
2022-05-26 13:35:33 +02:00
2023-02-01 15:48:41 +01:00
if ( ! notificationToSelfOnly ) {
users . addAll ( SocialUsers . getUsernamesByRole ( Moderated . CATALOGUE_MODERATOR ) ) ;
if ( itemAuthorCkanUsername ! = null ) {
// Adding item author
users . add ( CKANUser . getUsernameFromCKANUsername ( itemAuthorCkanUsername ) ) ;
}
2022-10-20 12:16:48 +02:00
}
2022-10-20 11:28:15 +02:00
2023-02-01 15:48:41 +01:00
// Adding current user
users . add ( CKANUser . getUsernameFromCKANUsername ( ckanUser . getName ( ) ) ) ;
2022-05-27 12:14:07 +02:00
catalogueEvent . setIdsToNotify ( users . toArray ( new String [ users . size ( ) ] ) ) ;
catalogueEvent . setIdsAsGroup ( false ) ;
2022-05-26 13:35:33 +02:00
return catalogueEvent ;
}
@Override
protected void postMessage ( String messageString ) throws Exception {
CatalogueEvent catalogueEvent = getCatalogueEvent ( messageString ) ;
SecretManager secretManager = SecretManagerProvider . instance . get ( ) ;
Secret secret = Constants . getCatalogueSecret ( ) ;
2022-05-27 17:20:52 +02:00
if ( notificationSentByGCat ) {
secretManager . startSession ( secret ) ;
}
2022-05-26 13:35:33 +02:00
try {
sendNotification ( catalogueEvent ) ;
} finally {
2022-05-27 17:20:52 +02:00
if ( notificationSentByGCat ) {
secretManager . endSession ( ) ;
}
2022-05-26 13:35:33 +02:00
}
}
@Override
public void postUserMessage ( CMItemStatus cmItemStatus , String userMessage ) throws Exception {
2022-05-27 17:20:52 +02:00
/ *
* [ mister x ] ( [ Role ] ) commented on the item " [TITLE] " as follows " [MESSAGE] " . [ Go to catalogue ]
* /
2022-05-26 13:35:33 +02:00
this . create = false ;
this . cmItemStatus = cmItemStatus ;
2022-05-27 17:20:52 +02:00
this . comment = true ;
2022-05-27 12:14:07 +02:00
switch ( cmItemStatus ) {
case PENDING :
catalogueEventType = CatalogueEventType . ITEM_UPDATED ;
break ;
case APPROVED :
catalogueEventType = CatalogueEventType . ITEM_PUBLISHED ;
break ;
case REJECTED :
catalogueEventType = CatalogueEventType . ITEM_REJECTED ;
break ;
default :
break ;
}
2022-05-26 13:35:33 +02:00
String fullName = ckanUser . getNameSurname ( ) ;
StringBuffer stringBuffer = new StringBuffer ( ) ;
2022-05-27 17:20:52 +02:00
stringBuffer = addUserWithRole ( fullName , itemAuthor ? SocialNotificationModerationThread . AUTHOR : Moderated . CATALOGUE_MODERATOR , stringBuffer ) ;
stringBuffer . append ( " commented on the item " ) ;
stringBuffer = addQuotedTitle ( stringBuffer ) ;
stringBuffer . append ( " as follows \" " ) ;
2022-05-26 13:35:33 +02:00
stringBuffer . append ( userMessage ) ;
2022-05-27 17:20:52 +02:00
stringBuffer . append ( " \" . " ) ;
2022-05-26 13:35:33 +02:00
CatalogueEvent catalogueEvent = getCatalogueEvent ( stringBuffer . toString ( ) ) ;
SecretManager secretManager = SecretManagerProvider . instance . get ( ) ;
Secret secret = Constants . getCatalogueSecret ( ) ;
2022-05-27 17:20:52 +02:00
if ( notificationSentByGCat ) {
secretManager . startSession ( secret ) ;
}
2022-05-26 13:35:33 +02:00
try {
sendNotification ( catalogueEvent ) ;
} finally {
2022-05-27 17:20:52 +02:00
if ( notificationSentByGCat ) {
secretManager . endSession ( ) ;
}
2022-05-26 13:35:33 +02:00
}
}
protected void sendNotification ( CatalogueEvent catalogueEvent ) throws Exception {
2022-05-27 17:20:52 +02:00
Thread thread = new Thread ( ) {
public void run ( ) {
try {
2022-10-19 17:50:04 +02:00
logger . trace ( " {} is going to send the following notification {} " , SecretManagerProvider . instance . get ( ) . getUser ( ) . getUsername ( ) , catalogueEvent ) ;
2022-05-27 17:20:52 +02:00
NotificationClient nc = new NotificationClient ( ) ;
nc . sendCatalogueEvent ( catalogueEvent ) ;
} catch ( Exception e ) {
logger . error ( " Error while sending notification. " , e ) ;
}
}
} ;
thread . run ( ) ;
// thread.start();
2022-05-26 13:35:33 +02:00
}
@Override
protected void createModerationThread ( ) throws Exception {
create = true ;
cmItemStatus = CMItemStatus . PENDING ;
}
}