State change has been integrated in update. Keep the dedicated code as

comment
This commit is contained in:
Luca Frosini 2024-11-12 16:55:55 +01:00
parent 47ade46b63
commit 6160f73bc2
3 changed files with 128 additions and 106 deletions

View File

@ -11,7 +11,6 @@ import java.util.UUID;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.WebApplicationException;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
@ -38,10 +37,8 @@ import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility;
import org.gcube.informationsystem.resourceregistry.contexts.ServerContextCache;
import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentOfManagement;
import org.gcube.informationsystem.resourceregistry.environments.Environment;
import org.gcube.informationsystem.resourceregistry.environments.Environment.PermissionMode;
import org.gcube.informationsystem.resourceregistry.environments.contexts.ContextEnvironment;
import org.gcube.informationsystem.resourceregistry.environments.instances.InstanceEnvironment;
import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
import org.gcube.informationsystem.resourceregistry.queries.operators.ComparisonOperator;
import org.gcube.informationsystem.resourceregistry.queries.operators.LogicalOperator;
import org.gcube.informationsystem.resourceregistry.rest.requests.RequestUtility;
@ -370,13 +367,23 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
}
protected void checkStateChange(ContextState newContextstate) {
if(newContextstate == ContextState.DELETED || newContextstate == ContextState.CREATED) {
throw new BadRequestException("A " + Context.NAME + " cannot be set to " + newContextstate.getState());
}
}
@Override
protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
// TODO Add role check
Set<String> allowedRoles = Environment.getAllOperationsAllowedRoles();
if(!workingEnvironment.isUserAllowed(allowedRoles)) {
throw new ForbiddenException("You are not allowed to update a " + Context.NAME +". Allowed roles are " + allowedRoles);
}
boolean parentChanged = false;
boolean nameChanged = false;
boolean stateChanged = false;
OVertex parent = null;
boolean found = false;
@ -428,15 +435,30 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
}
String oldName = getElement().getProperty(Context.NAME_PROPERTY);
String oldName = element.getProperty(Context.NAME_PROPERTY);
String newName = jsonNode.get(Context.NAME_PROPERTY).asText();
if (oldName.compareTo(newName) != 0) {
nameChanged = true;
name = newName;
nameChanged = true;
}
String oldStateString = element.getProperty(Context.STATE);
ContextState state = ContextState.fromString(oldStateString);
JsonNode stateNode = jsonNode.get(Context.STATE);
String newStateString = stateNode.asText();
if(newStateString.compareTo(oldStateString)!=0) {
state = ContextState.fromString(newStateString);
stateChanged = true;
}
if(stateChanged) {
checkStateChange(state);
}
if (parentChanged || nameChanged) {
checkContext(newParentContextManagement);
// TODO uncomment if we want temporary suspend the Context
// state = ContextState.SUSPENDED;
}
if (parentChanged) {
@ -675,71 +697,72 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
// Nothing to do
}
public String changeState() throws ResourceRegistryException {
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
try {
Environment environment = getWorkingEnvironment();
Set<String> allowedRoles = Environment.getAllOperationsAllowedRoles();
if(!workingEnvironment.isUserAllowed(allowedRoles)) {
throw new ForbiddenException("You are not allowed to change the state of a " + Context.NAME +". Allowed roles are " + allowedRoles);
}
oDatabaseDocument = environment.getDatabaseDocument(PermissionMode.WRITER);
oDatabaseDocument.begin();
getElement();
setAsEntryPoint();
setOperation(Operation.UPDATE);
String stateString = jsonNode.get(Context.STATE).asText();
ContextState state = ContextState.fromString(stateString);
if(state == ContextState.DELETED || state == ContextState.CREATED) {
throw new BadRequestException("A Context cannot be set to " + state.getState());
}
element.setProperty(Context.STATE, state.getState());
element.save();
oDatabaseDocument.commit();
// TODO Notify to subscriptionNotification
ServerContextCache cache = ServerContextCache.getInstance();
cache.cleanCache();
return readAsString();
} catch(ResourceRegistryException e) {
logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
if(oDatabaseDocument != null) {
oDatabaseDocument.rollback();
}
throw e;
} catch(BadRequestException e) {
logger.error("Unable to change the state to {} with UUID {}. Reason \"{}\"", Context.NAME, uuid, e.getMessage());
if(oDatabaseDocument != null) {
oDatabaseDocument.rollback();
}
throw e;
} catch(WebApplicationException e) {
logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
if(oDatabaseDocument != null) {
oDatabaseDocument.rollback();
}
throw e;
} catch(Exception e) {
logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
if(oDatabaseDocument != null) {
oDatabaseDocument.rollback();
}
throw new ResourceRegistryException(e);
} finally {
if(oDatabaseDocument != null) {
oDatabaseDocument.close();
}
if(current!=null) {
current.activateOnCurrentThread();
}
}
}
// public String changeState() throws ResourceRegistryException {
// ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
//
// try {
// Environment environment = getWorkingEnvironment();
// Set<String> allowedRoles = Environment.getAllOperationsAllowedRoles();
// if(!workingEnvironment.isUserAllowed(allowedRoles)) {
// throw new ForbiddenException("You are not allowed to change the state of a " + Context.NAME +". Allowed roles are " + allowedRoles);
// }
// oDatabaseDocument = environment.getDatabaseDocument(PermissionMode.WRITER);
// oDatabaseDocument.begin();
// getElement();
// setAsEntryPoint();
// setOperation(Operation.UPDATE);
//
// String stateString = jsonNode.get(Context.STATE).asText();
// ContextState state = ContextState.fromString(stateString);
// if(state == ContextState.DELETED || state == ContextState.CREATED) {
// throw new BadRequestException("A Context cannot be set to " + state.getState());
// }
// element.setProperty(Context.STATE, state.getState());
// element.save();
//
// oDatabaseDocument.commit();
//
// // TODO Notify to subscriptionNotification
//
// ServerContextCache cache = ServerContextCache.getInstance();
// cache.cleanCache();
//
// return readAsString();
//
// } catch(ResourceRegistryException e) {
// logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
// if(oDatabaseDocument != null) {
// oDatabaseDocument.rollback();
// }
// throw e;
// } catch(BadRequestException e) {
// logger.error("Unable to change the state to {} with UUID {}. Reason \"{}\"", Context.NAME, uuid, e.getMessage());
// if(oDatabaseDocument != null) {
// oDatabaseDocument.rollback();
// }
// throw e;
// } catch(WebApplicationException e) {
// logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
// if(oDatabaseDocument != null) {
// oDatabaseDocument.rollback();
// }
// throw e;
// } catch(Exception e) {
// logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
// if(oDatabaseDocument != null) {
// oDatabaseDocument.rollback();
// }
// throw new ResourceRegistryException(e);
// } finally {
// if(oDatabaseDocument != null) {
// oDatabaseDocument.close();
// }
//
// if(current!=null) {
// current.activateOnCurrentThread();
// }
//
// }
// }
}

View File

@ -5,7 +5,6 @@ import java.util.UUID;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@ -128,34 +127,34 @@ public class ContextManager extends BaseRest {
return contextManagement.createOrUpdate();
}
/**
* POST /contexts/{UUID}
* e.g. POST /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
*
* BODY: {...}
*
* The body contains the Contexts JSon representation
* only the state property will be evaluated in this
* method
*
*/
@POST
@Path("{" + ContextManager.CONTEXT_UUID_PATH_PARAMETER + "}")
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
public String changeState(@PathParam(ContextManager.CONTEXT_UUID_PATH_PARAMETER) String uuid, String json)
throws ResourceRegistryException {
logger.info("Requested to activate {} with UUID {}", Context.NAME, uuid);
setAccountingMethod(Method.UPDATE, Context.NAME);
ServerRequestInfo serverRequestInfo = initRequestInfo();
serverRequestInfo.setIncludeMeta(true);
serverRequestInfo.setAllMeta(true);
ContextManagement contextManagement = new ContextManagement();
contextManagement.setJson(json);
contextManagement.setUUID(UUID.fromString(uuid));
return contextManagement.changeState();
}
// /**
// * POST /contexts/{UUID}
// * e.g. POST /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
// *
// * BODY: {...}
// *
// * The body contains the Contexts JSon representation
// * only the state property will be evaluated in this
// * method
// *
// */
// @POST
// @Path("{" + ContextManager.CONTEXT_UUID_PATH_PARAMETER + "}")
// @Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
// public String changeState(@PathParam(ContextManager.CONTEXT_UUID_PATH_PARAMETER) String uuid, String json)
// throws ResourceRegistryException {
// logger.info("Requested to activate {} with UUID {}", Context.NAME, uuid);
// setAccountingMethod(Method.UPDATE, Context.NAME);
//
// ServerRequestInfo serverRequestInfo = initRequestInfo();
// serverRequestInfo.setIncludeMeta(true);
// serverRequestInfo.setAllMeta(true);
//
// ContextManagement contextManagement = new ContextManagement();
// contextManagement.setJson(json);
// contextManagement.setUUID(UUID.fromString(uuid));
// return contextManagement.changeState();
// }
/*
* DELETE /contexts/{UUID}

View File

@ -248,7 +248,7 @@ public class ContextStateTest extends ContextManagementTest {
String state = contextState.getState();
contextA1.setState(state);
contextManagement.setJson(ElementMapper.marshal(contextA1));
String contextString = contextManagement.changeState();
String contextString = contextManagement.update();
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
ContextState gotState = ContextState.fromString(contextA1.getState());
logger.info("Contexts {} {} expected state {} - got state {}", newContext, contextA1.getID(), contextState.getState(), gotState.getState());
@ -262,7 +262,7 @@ public class ContextStateTest extends ContextManagementTest {
contextA1.setState(state);
contextManagement.setJson(ElementMapper.marshal(contextA1));
try {
contextString = contextManagement.changeState();
contextString = contextManagement.update();
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
gotState = ContextState.fromString(contextA1.getState());
throw new Exception("It should be possibile set explicitly the context " + newContext + " " + contextA1.getID() + " as " + gotState.getState());
@ -276,7 +276,7 @@ public class ContextStateTest extends ContextManagementTest {
state = contextState.getState();
contextA1.setState(state);
contextManagement.setJson(ElementMapper.marshal(contextA1));
contextString = contextManagement.changeState();
contextString = contextManagement.update();
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
gotState = ContextState.fromString(contextA1.getState());
logger.info("Contexts {} {} expected state {} - got state {}", newContext, contextA1.getID(), contextState.getState(), gotState.getState());
@ -290,7 +290,7 @@ public class ContextStateTest extends ContextManagementTest {
contextA1.setState(state);
contextManagement.setJson(ElementMapper.marshal(contextA1));
try {
contextString = contextManagement.changeState();
contextString = contextManagement.update();
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
gotState = ContextState.fromString(contextA1.getState());
throw new Exception("It should be possibile set explicitly the context " + newContext + " " + contextA1.getID() + " as " + gotState.getState());