Allowing Catalogue-Managers to keep Moderation Extra Properties

migrating_to_smartgears_4
Luca Frosini 2 years ago
parent e6856b7653
commit 9b7a08d66b

@ -17,6 +17,7 @@ import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotAllowedException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import org.apache.http.MethodNotSupportedException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
@ -129,6 +130,17 @@ public class CKANPackage extends CKAN implements Moderated {
protected ModerationThread moderationThread;
/**
* By default extra properties used for moderation are removed
* from the item representation.
* So that, the default value of this field is false.
*
* A Catalogue-Manager can request to keep such properties
* for debugging purposes.
*
*/
protected boolean keepModerationExtraProperties;
public CKANPackage() {
super();
@ -146,9 +158,17 @@ public class CKANPackage extends CKAN implements Moderated {
ckanUser = CKANUserCache.getCurrrentCKANUser();
updateOperation = false;
keepModerationExtraProperties = true;
}
public void setKeepModerationExtraProperties(boolean keepModerationExtraProperties) {
if(ckanUser.getRole().ordinal()>=Role.MANAGER.ordinal() && keepModerationExtraProperties) {
this.keepModerationExtraProperties = keepModerationExtraProperties;
}
}
protected CKANOrganization checkGotOrganization(String gotOrganization) throws ForbiddenException {
if(!configuration.getSupportedOrganizations().contains(gotOrganization)) {
String error = String.format(
@ -192,24 +212,28 @@ public class CKANPackage extends CKAN implements Moderated {
// Removing all Content Moderation Keys
if(jsonNode.has(EXTRAS_KEY)) {
ArrayNode extras = (ArrayNode) jsonNode.get(EXTRAS_KEY);
// It is not possible to remove the element of an array while iterating it.
// We need to create a new array only with valie elements;
ArrayNode newExtras = mapper.createArrayNode();
boolean foundOne = false;
for(int i=0; i<extras.size(); i++) {
JsonNode extra = extras.get(i);
if(extra.has(EXTRAS_KEY_KEY) &&
extra.get(EXTRAS_KEY_KEY)!=null &&
extra.get(EXTRAS_KEY_KEY).asText().startsWith(Moderated.SYSTEM_CM_PREFIX)) {
foundOne = true;
}else {
newExtras.add(extra.deepCopy());
if(ckanUser.getRole().ordinal()>=Role.MANAGER.ordinal() && keepModerationExtraProperties) {
logger.trace("The user is a {} which requested to keep Moderation extra properties.", ckanUser.getRole());
}else {
ArrayNode extras = (ArrayNode) jsonNode.get(EXTRAS_KEY);
// It is not possible to remove the element of an array while iterating it.
// We need to create a new array only with valid elements;
ArrayNode newExtras = mapper.createArrayNode();
boolean foundOne = false;
for(int i=0; i<extras.size(); i++) {
JsonNode extra = extras.get(i);
if(extra.has(EXTRAS_KEY_KEY) &&
extra.get(EXTRAS_KEY_KEY)!=null &&
extra.get(EXTRAS_KEY_KEY).asText().startsWith(Moderated.SYSTEM_CM_PREFIX)) {
foundOne = true;
}else {
newExtras.add(extra.deepCopy());
}
}
if(foundOne) {
((ObjectNode) jsonNode).replace(EXTRAS_KEY, newExtras);
}
}
if(foundOne) {
((ObjectNode) jsonNode).replace(EXTRAS_KEY, newExtras);
}
}
return jsonNode;
@ -806,6 +830,9 @@ public class CKANPackage extends CKAN implements Moderated {
try {
readItem();
checkModerationRead();
// TODO check keepModerationExtraProperties
// uriInfo.getQueryParameters().get(KEEP);
return getAsCleanedString(result);
} catch(WebApplicationException e) {
throw e;

Loading…
Cancel
Save