2021-11-26 12:27:31 +01:00
|
|
|
package org.gcube.gcat.moderation.thread;
|
|
|
|
|
2022-07-26 11:18:21 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2022-07-12 11:36:48 +02:00
|
|
|
//import java.util.HashMap;
|
|
|
|
//import java.util.Map;
|
2022-05-19 12:06:59 +02:00
|
|
|
|
2021-11-26 12:27:31 +01:00
|
|
|
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
2022-07-26 11:18:21 +02:00
|
|
|
import org.gcube.common.authorization.utils.manager.SecretManager;
|
|
|
|
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
|
|
|
import org.gcube.gcat.api.configuration.CatalogueConfiguration;
|
2022-07-12 11:36:48 +02:00
|
|
|
//import org.gcube.common.authorization.utils.manager.SecretManager;
|
|
|
|
//import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
|
|
|
//import org.gcube.gcat.api.configuration.CatalogueConfiguration;
|
2021-12-02 11:50:18 +01:00
|
|
|
import org.gcube.gcat.api.moderation.CMItemStatus;
|
2022-05-27 17:20:52 +02:00
|
|
|
import org.gcube.gcat.moderation.thread.social.notifications.SocialNotificationModerationThread;
|
2021-11-26 12:27:31 +01:00
|
|
|
import org.gcube.gcat.persistence.ckan.CKANUser;
|
2022-07-12 11:36:48 +02:00
|
|
|
//import org.gcube.portlets.user.uriresolvermanager.UriResolverManager;
|
|
|
|
//import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryString.MODERATION_OP;
|
|
|
|
//import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryStringBuilder;
|
2022-07-26 11:18:21 +02:00
|
|
|
import org.gcube.portlets.user.uriresolvermanager.UriResolverManager;
|
|
|
|
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryString.MODERATION_OP;
|
|
|
|
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryStringBuilder;
|
2021-11-26 12:27:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public abstract class ModerationThread {
|
2022-04-07 16:24:46 +02:00
|
|
|
|
2021-11-26 12:27:31 +01:00
|
|
|
protected String itemID;
|
|
|
|
protected String itemName;
|
2022-04-06 17:15:46 +02:00
|
|
|
protected String itemTitle;
|
2022-03-31 15:49:22 +02:00
|
|
|
protected String itemURL;
|
2022-10-20 11:28:15 +02:00
|
|
|
protected String itemAuthorCkanUsername;
|
2022-05-19 12:06:59 +02:00
|
|
|
|
|
|
|
protected String moderationURL;
|
2022-04-07 16:24:46 +02:00
|
|
|
|
|
|
|
protected boolean create;
|
|
|
|
protected CMItemStatus cmItemStatus;
|
2022-04-06 17:15:46 +02:00
|
|
|
protected boolean itemAuthor;
|
2022-04-07 16:24:46 +02:00
|
|
|
|
2021-11-26 12:27:31 +01:00
|
|
|
protected CKANUser ckanUser;
|
|
|
|
protected ObjectMapper objectMapper;
|
2022-04-07 16:24:46 +02:00
|
|
|
|
2021-11-26 12:27:31 +01:00
|
|
|
public static ModerationThread getDefaultInstance() {
|
2022-03-29 15:17:30 +02:00
|
|
|
// return new FakeModerationThread();
|
2022-05-27 17:20:52 +02:00
|
|
|
// return new SocialMessageModerationThread();
|
|
|
|
return new SocialNotificationModerationThread();
|
2021-11-26 12:27:31 +01:00
|
|
|
}
|
2022-04-07 16:24:46 +02:00
|
|
|
|
2021-11-26 12:27:31 +01:00
|
|
|
public ModerationThread() {
|
|
|
|
this.objectMapper = new ObjectMapper();
|
2022-04-07 16:24:46 +02:00
|
|
|
this.itemAuthor = false;
|
|
|
|
this.create = false;
|
2022-05-19 12:06:59 +02:00
|
|
|
this.cmItemStatus = CMItemStatus.PENDING;
|
2021-11-26 12:27:31 +01:00
|
|
|
}
|
2022-04-07 16:24:46 +02:00
|
|
|
|
2022-04-06 17:15:46 +02:00
|
|
|
public void setItemCoordinates(String itemID, String itemName, String itemTitle, String itemURL) {
|
2021-11-26 12:27:31 +01:00
|
|
|
this.itemID = itemID;
|
|
|
|
this.itemName = itemName;
|
2022-04-06 17:15:46 +02:00
|
|
|
this.itemTitle = itemTitle;
|
2022-03-31 15:49:22 +02:00
|
|
|
this.itemURL = itemURL;
|
2021-11-26 12:27:31 +01:00
|
|
|
}
|
2022-04-06 17:15:46 +02:00
|
|
|
|
|
|
|
public void setItemAuthor(boolean itemAuthor) {
|
|
|
|
this.itemAuthor = itemAuthor;
|
|
|
|
}
|
|
|
|
|
2022-10-20 11:28:15 +02:00
|
|
|
public String getItemAuthorCkanUsername() {
|
|
|
|
return itemAuthorCkanUsername;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setItemAuthorCkanUsername(String itemAuthorCkanUsername) {
|
|
|
|
this.itemAuthorCkanUsername = itemAuthorCkanUsername;
|
|
|
|
}
|
|
|
|
|
2021-11-26 12:27:31 +01:00
|
|
|
public void setCKANUser(CKANUser ckanUser) {
|
|
|
|
this.ckanUser = ckanUser;
|
|
|
|
}
|
2022-04-07 16:24:46 +02:00
|
|
|
|
2022-05-19 12:06:59 +02:00
|
|
|
public String getModerationURL() {
|
2022-07-26 11:18:21 +02:00
|
|
|
if(moderationURL==null) {
|
|
|
|
try {
|
|
|
|
SecretManager secretManager = SecretManagerProvider.instance.get();
|
|
|
|
String context = secretManager.getContext();
|
|
|
|
UriResolverManager resolver = new UriResolverManager("CTLG");
|
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
params.put("gcube_scope", context); //e.g. /gcube/devsec/devVRE
|
|
|
|
params.put("entity_context", "organization");
|
|
|
|
params.put("entity_name", CatalogueConfiguration.getOrganizationName(context)); //e.g. devvre
|
|
|
|
|
|
|
|
CatalogueResolverQueryStringBuilder builder = new CatalogueResolverQueryStringBuilder(itemName); //item name under moderation
|
|
|
|
builder.itemStatus(cmItemStatus.name()). //e.g. pending, approved, rejected
|
|
|
|
moderation(MODERATION_OP.show);
|
|
|
|
|
|
|
|
String queryString = builder.buildQueryParametersToQueryString();
|
|
|
|
params.put(CatalogueResolverQueryStringBuilder.QUERY_STRING_PARAMETER, queryString);
|
|
|
|
|
|
|
|
moderationURL = resolver.getLink(params, true);
|
|
|
|
}catch (Exception e) {
|
|
|
|
return itemURL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return moderationURL;
|
2022-05-19 12:06:59 +02:00
|
|
|
}
|
|
|
|
|
2021-11-26 12:27:31 +01:00
|
|
|
/**
|
|
|
|
* The message is sent as gCat
|
|
|
|
* @param message
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2022-04-07 16:24:46 +02:00
|
|
|
protected abstract void postMessage(String message) throws Exception;
|
|
|
|
|
2021-11-26 12:27:31 +01:00
|
|
|
/**
|
|
|
|
* The message is sent as User
|
2022-04-07 16:24:46 +02:00
|
|
|
*
|
2021-11-26 12:27:31 +01:00
|
|
|
* @param cmItemStatus
|
|
|
|
* @param userMessage
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public abstract void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception;
|
2022-04-07 16:24:46 +02:00
|
|
|
|
|
|
|
protected abstract void createModerationThread() throws Exception;
|
|
|
|
|
|
|
|
public void postItemCreated() throws Exception {
|
2021-11-26 12:27:31 +01:00
|
|
|
createModerationThread();
|
2023-02-01 15:48:41 +01:00
|
|
|
this.create = true;
|
|
|
|
this.cmItemStatus = CMItemStatus.PENDING;
|
2022-03-31 15:49:22 +02:00
|
|
|
String fullName = ckanUser.getNameSurname();
|
2022-04-07 16:24:46 +02:00
|
|
|
String message = String.format(
|
|
|
|
"@**%s** created the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.",
|
2022-03-31 15:49:22 +02:00
|
|
|
fullName, itemName, itemID, cmItemStatus.getFancyValue());
|
2022-04-07 16:24:46 +02:00
|
|
|
postMessage(message);
|
2021-11-26 12:27:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void postItemUpdated() throws Exception {
|
2023-02-01 15:48:41 +01:00
|
|
|
this.cmItemStatus = CMItemStatus.PENDING;
|
2022-03-31 15:49:22 +02:00
|
|
|
String fullName = ckanUser.getNameSurname();
|
2022-04-07 16:24:46 +02:00
|
|
|
String message = String.format(
|
|
|
|
"@**%s** updated the item with name '%s' (id='%s'). The item is now in **%s** state and must be moderated.",
|
2022-04-06 17:15:46 +02:00
|
|
|
fullName, itemName, itemID, cmItemStatus.getFancyValue());
|
2022-04-07 16:24:46 +02:00
|
|
|
postMessage(message);
|
2021-11-26 12:27:31 +01:00
|
|
|
}
|
2022-04-07 16:24:46 +02:00
|
|
|
|
2021-11-26 12:27:31 +01:00
|
|
|
public void postItemRejected(String userMessage) throws Exception {
|
2023-02-01 15:48:41 +01:00
|
|
|
this.cmItemStatus = CMItemStatus.REJECTED;
|
2022-03-31 15:49:22 +02:00
|
|
|
String fullName = ckanUser.getNameSurname();
|
2022-04-07 16:24:46 +02:00
|
|
|
String message = String.format(
|
|
|
|
"@**%s** **%s** the item with name '%s' (id='%s'). The author can delete the item or update it to try to meet moderators requests if any.",
|
2022-03-31 15:49:22 +02:00
|
|
|
fullName, cmItemStatus.getFancyValue(), itemName, itemID);
|
2022-04-07 16:24:46 +02:00
|
|
|
postMessage(message);
|
2021-11-26 12:27:31 +01:00
|
|
|
postUserMessage(cmItemStatus, userMessage);
|
|
|
|
}
|
2022-04-07 16:24:46 +02:00
|
|
|
|
|
|
|
public void postItemApproved(String userMessage) throws Exception {
|
2023-02-01 15:48:41 +01:00
|
|
|
this.cmItemStatus = CMItemStatus.APPROVED;
|
2022-03-31 15:49:22 +02:00
|
|
|
String fullName = ckanUser.getNameSurname();
|
2022-04-07 16:24:46 +02:00
|
|
|
String message = String.format(
|
|
|
|
"@**%s** **%s** the item with name '%s' (id='%s'). The item is now available in the catalogue. The item is available at %s",
|
2022-03-31 15:49:22 +02:00
|
|
|
fullName, cmItemStatus.getFancyValue(), itemName, itemID, itemURL);
|
2022-04-07 16:24:46 +02:00
|
|
|
postMessage(message);
|
2021-11-26 12:27:31 +01:00
|
|
|
postUserMessage(cmItemStatus, userMessage);
|
|
|
|
}
|
2023-02-01 15:48:41 +01:00
|
|
|
|
|
|
|
public void postItemDeleted(CMItemStatus cmItemStatus) throws Exception {
|
|
|
|
this.cmItemStatus = cmItemStatus;
|
|
|
|
String fullName = ckanUser.getNameSurname();
|
|
|
|
String message = String.format(
|
|
|
|
"@**%s** deleted the item with name '%s' (id='%s')",
|
|
|
|
fullName, itemName, itemID, itemURL);
|
|
|
|
postMessage(message);
|
|
|
|
}
|
2021-11-26 12:27:31 +01:00
|
|
|
}
|