Removed old unneeded solution
parent
257ec29433
commit
84b44f9476
@ -1,71 +0,0 @@
|
||||
package org.gcube.gcat.moderation.thread.social.messages;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.gcube.social_networking.social_networking_client_library.MessageClient;
|
||||
import org.gcube.social_networking.socialnetworking.model.beans.Message;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class SocialMessage extends Thread {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SocialMessage.class);
|
||||
|
||||
public static final String ITEM_URL = "Item URL";
|
||||
|
||||
protected static final String RESPONSE_SUCCESS_KEY = "success";
|
||||
protected static final String RESPONSE_MESSAGE_KEY = "message";
|
||||
|
||||
protected final ObjectMapper objectMapper;
|
||||
|
||||
protected Message message;
|
||||
|
||||
public SocialMessage() throws Exception {
|
||||
super();
|
||||
this.objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
public Message getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(Message message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
logger.info("Going to send Message {}", message);
|
||||
// write message
|
||||
sendSocialMessage();
|
||||
} catch(Exception e) {
|
||||
logger.error("Error while executing post creation actions", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void sendSocialMessage() {
|
||||
try {
|
||||
MessageClient messageClient = new MessageClient();
|
||||
String ret = messageClient.writeMessage(message);
|
||||
|
||||
String messageString = objectMapper.writeValueAsString(message);
|
||||
logger.debug("The message that is going to be send is\n{}", messageString);
|
||||
|
||||
JsonNode jsonNode = objectMapper.readTree(ret);
|
||||
if(jsonNode.get(RESPONSE_SUCCESS_KEY).asBoolean()) {
|
||||
logger.info("Message sent : {}", messageString);
|
||||
} else {
|
||||
logger.info("Failed to write the message {}. Reason {}", messageString,
|
||||
jsonNode.get(RESPONSE_MESSAGE_KEY).asText());
|
||||
}
|
||||
} catch(Exception e) {
|
||||
logger.error("Unable to send the message : " + message.toString(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,217 +0,0 @@
|
||||
package org.gcube.gcat.moderation.thread.social.messages;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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;
|
||||
import org.gcube.gcat.social.SocialUsers;
|
||||
import org.gcube.gcat.utils.Constants;
|
||||
import org.gcube.social_networking.socialnetworking.model.beans.Message;
|
||||
import org.gcube.social_networking.socialnetworking.model.beans.Recipient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class SocialMessageModerationThread extends ModerationThread {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SocialMessageModerationThread.class);
|
||||
|
||||
protected StringBuffer getMainItemInfo(StringBuffer stringBuffer) {
|
||||
stringBuffer.append("Status: ");
|
||||
stringBuffer.append(cmItemStatus.getFancyValue());
|
||||
stringBuffer.append("\n");
|
||||
stringBuffer.append("Title: ");
|
||||
stringBuffer.append(itemTitle);
|
||||
stringBuffer.append("\n");
|
||||
stringBuffer.append("Name: ");
|
||||
stringBuffer.append(itemName);
|
||||
stringBuffer.append("\n");
|
||||
stringBuffer.append("ID: ");
|
||||
stringBuffer.append(itemID);
|
||||
stringBuffer.append("\n");
|
||||
stringBuffer.append("URL: ");
|
||||
if(cmItemStatus == CMItemStatus.APPROVED) {
|
||||
stringBuffer.append(itemURL);
|
||||
}else {
|
||||
stringBuffer.append(getModerationURL());
|
||||
}
|
||||
stringBuffer.append("\n\n");
|
||||
return stringBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the message for an item that is created/updated
|
||||
*/
|
||||
public void postItemToBeManaged() throws Exception {
|
||||
/*
|
||||
* An example of created message is:
|
||||
*
|
||||
* [mister x] created / updated the following item
|
||||
*
|
||||
* Title: RESTful Transaction Model
|
||||
* Name: my_first_restful_transaction_model
|
||||
* ID: e31a6ba8-66ef-47b8-b61f-99a1366b4a69
|
||||
* URL: https://data.dev.d4science.org/ctlg/devVRE/my_first_restful_transaction_model
|
||||
*
|
||||
* You are kindly requested to review it and decide either to APPROVE or REJECT it at <url of the item>
|
||||
*
|
||||
*/
|
||||
cmItemStatus = CMItemStatus.PENDING;
|
||||
String fullName = ckanUser.getNameSurname();
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
stringBuffer.append(fullName);
|
||||
stringBuffer.append(create ? " created " : " updated ");
|
||||
stringBuffer.append("the following item\n\n");
|
||||
stringBuffer = getMainItemInfo(stringBuffer);
|
||||
stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT it.");
|
||||
// stringBuffer.append("You are kindly requested to review it and decide either to APPROVE or REJECT at");
|
||||
// String manageURL = getManageURL();
|
||||
// stringBuffer.append(manageURL);
|
||||
postMessage(stringBuffer.toString());
|
||||
}
|
||||
|
||||
|
||||
public void postItemCreated() throws Exception {
|
||||
create = true;
|
||||
cmItemStatus = CMItemStatus.PENDING;
|
||||
postItemToBeManaged();
|
||||
}
|
||||
|
||||
public void postItemUpdated() throws Exception {
|
||||
create = false;
|
||||
cmItemStatus = CMItemStatus.PENDING;
|
||||
postItemToBeManaged();
|
||||
}
|
||||
|
||||
protected StringBuffer addUserWithRole(String fullName, String role, StringBuffer stringBuffer) {
|
||||
stringBuffer.append(fullName);
|
||||
stringBuffer.append(" [");
|
||||
stringBuffer.append(role);
|
||||
stringBuffer.append("] ");
|
||||
return stringBuffer;
|
||||
}
|
||||
|
||||
public void postItemManaged(String userMessage) throws Exception {
|
||||
create = false;
|
||||
String fullName = ckanUser.getNameSurname();
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
stringBuffer = addUserWithRole(fullName, Moderated.CATALOGUE_MODERATOR, stringBuffer);
|
||||
stringBuffer.append(cmItemStatus.getValue());
|
||||
stringBuffer.append(" the following item");
|
||||
if(userMessage!=null && userMessage.length()>0) {
|
||||
stringBuffer.append(" with this accompanying message\n\n");
|
||||
stringBuffer = getMainItemInfo(stringBuffer);
|
||||
stringBuffer.append(userMessage);
|
||||
}else {
|
||||
stringBuffer.append("\n\n");
|
||||
stringBuffer = getMainItemInfo(stringBuffer);
|
||||
}
|
||||
postMessage(stringBuffer.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postItemRejected(String userMessage) throws Exception {
|
||||
create = false;
|
||||
cmItemStatus = CMItemStatus.REJECTED;
|
||||
postItemManaged(userMessage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postItemApproved(String userMessage) throws Exception {
|
||||
create = false;
|
||||
cmItemStatus = CMItemStatus.APPROVED;
|
||||
postItemManaged(userMessage);
|
||||
}
|
||||
|
||||
protected String getSubject() {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
if(!create) {
|
||||
logger.trace("It's a reply");
|
||||
stringWriter.append("Re: ");
|
||||
}
|
||||
stringWriter.append("[Catalogue Service] ");
|
||||
stringWriter.append(itemTitle);
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
protected List<Recipient> getRecipients(Set<String> users) {
|
||||
List<Recipient> recipients = new ArrayList<>();
|
||||
for(String username : users) {
|
||||
recipients.add(new Recipient(username));
|
||||
}
|
||||
return recipients;
|
||||
}
|
||||
|
||||
protected Message getMessage(String messageString) throws Exception {
|
||||
Message message = new Message();
|
||||
message.setBody(messageString);
|
||||
message.setSubject(getSubject());
|
||||
Set<String> moderators = SocialUsers.getUsernamesByRole(Moderated.CATALOGUE_MODERATOR);
|
||||
List<Recipient> recipients = getRecipients(moderators);
|
||||
message.setRecipients(recipients);
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void postMessage(String messageString) throws Exception {
|
||||
SecretManager secretManager = SecretManagerProvider.instance.get();
|
||||
String username = secretManager.getUser().getUsername();
|
||||
Message message = getMessage(messageString);
|
||||
List<Recipient> recipients = message.getRecipients();
|
||||
recipients.add(new Recipient(username));
|
||||
Secret secret = Constants.getCatalogueSecret();
|
||||
secretManager.startSession(secret);
|
||||
try {
|
||||
sendMessage(message);
|
||||
}finally {
|
||||
secretManager.endSession();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postUserMessage(CMItemStatus cmItemStatus, String userMessage) throws Exception {
|
||||
this.create = false;
|
||||
this.cmItemStatus = cmItemStatus;
|
||||
String fullName = ckanUser.getNameSurname();
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
stringBuffer = addUserWithRole(fullName, itemAuthor ? "Author" : Moderated.CATALOGUE_MODERATOR, stringBuffer);
|
||||
stringBuffer.append("sent a message regarding the following item\n\n");
|
||||
stringBuffer = getMainItemInfo(stringBuffer);
|
||||
stringBuffer.append(userMessage);
|
||||
Message message = getMessage(stringBuffer.toString());
|
||||
SecretManager secretManager = SecretManagerProvider.instance.get();
|
||||
String username = secretManager.getUser().getUsername();
|
||||
List<Recipient> recipients = message.getRecipients();
|
||||
recipients.add(new Recipient(username));
|
||||
Secret secret = Constants.getCatalogueSecret();
|
||||
secretManager.startSession(secret);
|
||||
try {
|
||||
sendMessage(message);
|
||||
}finally {
|
||||
secretManager.endSession();
|
||||
}
|
||||
}
|
||||
|
||||
protected void sendMessage(Message message) throws Exception {
|
||||
SocialMessage socialMessage = new SocialMessage();
|
||||
socialMessage.setMessage(message);
|
||||
socialMessage.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createModerationThread() throws Exception {
|
||||
create = true;
|
||||
cmItemStatus = CMItemStatus.PENDING;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue