State change has been integrated in update. Keep the dedicated code as
comment
This commit is contained in:
parent
47ade46b63
commit
6160f73bc2
|
@ -11,7 +11,6 @@ import java.util.UUID;
|
||||||
import javax.ws.rs.BadRequestException;
|
import javax.ws.rs.BadRequestException;
|
||||||
import javax.ws.rs.ForbiddenException;
|
import javax.ws.rs.ForbiddenException;
|
||||||
import javax.ws.rs.NotAuthorizedException;
|
import javax.ws.rs.NotAuthorizedException;
|
||||||
import javax.ws.rs.WebApplicationException;
|
|
||||||
|
|
||||||
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
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.ServerContextCache;
|
||||||
import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentOfManagement;
|
import org.gcube.informationsystem.resourceregistry.contexts.relations.IsParentOfManagement;
|
||||||
import org.gcube.informationsystem.resourceregistry.environments.Environment;
|
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.contexts.ContextEnvironment;
|
||||||
import org.gcube.informationsystem.resourceregistry.environments.instances.InstanceEnvironment;
|
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.ComparisonOperator;
|
||||||
import org.gcube.informationsystem.resourceregistry.queries.operators.LogicalOperator;
|
import org.gcube.informationsystem.resourceregistry.queries.operators.LogicalOperator;
|
||||||
import org.gcube.informationsystem.resourceregistry.rest.requests.RequestUtility;
|
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
|
@Override
|
||||||
protected OVertex reallyUpdate() throws NotFoundException, ResourceRegistryException {
|
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 parentChanged = false;
|
||||||
boolean nameChanged = false;
|
boolean nameChanged = false;
|
||||||
|
boolean stateChanged = false;
|
||||||
|
|
||||||
OVertex parent = null;
|
OVertex parent = null;
|
||||||
boolean found = false;
|
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();
|
String newName = jsonNode.get(Context.NAME_PROPERTY).asText();
|
||||||
if (oldName.compareTo(newName) != 0) {
|
if (oldName.compareTo(newName) != 0) {
|
||||||
nameChanged = true;
|
|
||||||
name = newName;
|
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) {
|
if (parentChanged || nameChanged) {
|
||||||
checkContext(newParentContextManagement);
|
checkContext(newParentContextManagement);
|
||||||
|
// TODO uncomment if we want temporary suspend the Context
|
||||||
|
// state = ContextState.SUSPENDED;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parentChanged) {
|
if (parentChanged) {
|
||||||
|
@ -675,71 +697,72 @@ public class ContextManagement extends EntityElementManagement<Context, EntityTy
|
||||||
// Nothing to do
|
// Nothing to do
|
||||||
}
|
}
|
||||||
|
|
||||||
public String changeState() throws ResourceRegistryException {
|
// public String changeState() throws ResourceRegistryException {
|
||||||
ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
// ODatabaseDocument current = ContextUtility.getCurrentODatabaseDocumentFromThreadLocal();
|
||||||
|
//
|
||||||
try {
|
// try {
|
||||||
Environment environment = getWorkingEnvironment();
|
// Environment environment = getWorkingEnvironment();
|
||||||
Set<String> allowedRoles = Environment.getAllOperationsAllowedRoles();
|
// Set<String> allowedRoles = Environment.getAllOperationsAllowedRoles();
|
||||||
if(!workingEnvironment.isUserAllowed(allowedRoles)) {
|
// if(!workingEnvironment.isUserAllowed(allowedRoles)) {
|
||||||
throw new ForbiddenException("You are not allowed to change the state of a " + Context.NAME +". Allowed roles are " + 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 = environment.getDatabaseDocument(PermissionMode.WRITER);
|
||||||
oDatabaseDocument.begin();
|
// oDatabaseDocument.begin();
|
||||||
getElement();
|
// getElement();
|
||||||
setAsEntryPoint();
|
// setAsEntryPoint();
|
||||||
setOperation(Operation.UPDATE);
|
// setOperation(Operation.UPDATE);
|
||||||
|
//
|
||||||
String stateString = jsonNode.get(Context.STATE).asText();
|
// String stateString = jsonNode.get(Context.STATE).asText();
|
||||||
ContextState state = ContextState.fromString(stateString);
|
// ContextState state = ContextState.fromString(stateString);
|
||||||
if(state == ContextState.DELETED || state == ContextState.CREATED) {
|
// if(state == ContextState.DELETED || state == ContextState.CREATED) {
|
||||||
throw new BadRequestException("A Context cannot be set to " + state.getState());
|
// throw new BadRequestException("A Context cannot be set to " + state.getState());
|
||||||
}
|
// }
|
||||||
element.setProperty(Context.STATE, state.getState());
|
// element.setProperty(Context.STATE, state.getState());
|
||||||
element.save();
|
// element.save();
|
||||||
|
//
|
||||||
oDatabaseDocument.commit();
|
// oDatabaseDocument.commit();
|
||||||
|
//
|
||||||
// TODO Notify to subscriptionNotification
|
// // TODO Notify to subscriptionNotification
|
||||||
|
//
|
||||||
ServerContextCache cache = ServerContextCache.getInstance();
|
// ServerContextCache cache = ServerContextCache.getInstance();
|
||||||
cache.cleanCache();
|
// cache.cleanCache();
|
||||||
|
//
|
||||||
return readAsString();
|
// return readAsString();
|
||||||
|
//
|
||||||
} catch(ResourceRegistryException e) {
|
// } catch(ResourceRegistryException e) {
|
||||||
logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
|
// logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
|
||||||
if(oDatabaseDocument != null) {
|
// if(oDatabaseDocument != null) {
|
||||||
oDatabaseDocument.rollback();
|
// oDatabaseDocument.rollback();
|
||||||
}
|
// }
|
||||||
throw e;
|
// throw e;
|
||||||
} catch(BadRequestException e) {
|
// } catch(BadRequestException e) {
|
||||||
logger.error("Unable to change the state to {} with UUID {}. Reason \"{}\"", Context.NAME, uuid, e.getMessage());
|
// logger.error("Unable to change the state to {} with UUID {}. Reason \"{}\"", Context.NAME, uuid, e.getMessage());
|
||||||
if(oDatabaseDocument != null) {
|
// if(oDatabaseDocument != null) {
|
||||||
oDatabaseDocument.rollback();
|
// oDatabaseDocument.rollback();
|
||||||
}
|
// }
|
||||||
throw e;
|
// throw e;
|
||||||
} catch(WebApplicationException e) {
|
// } catch(WebApplicationException e) {
|
||||||
logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
|
// logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
|
||||||
if(oDatabaseDocument != null) {
|
// if(oDatabaseDocument != null) {
|
||||||
oDatabaseDocument.rollback();
|
// oDatabaseDocument.rollback();
|
||||||
}
|
// }
|
||||||
throw e;
|
// throw e;
|
||||||
} catch(Exception e) {
|
// } catch(Exception e) {
|
||||||
logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
|
// logger.error("Unable to change the state to {} with UUID {}", Context.NAME, uuid, e);
|
||||||
if(oDatabaseDocument != null) {
|
// if(oDatabaseDocument != null) {
|
||||||
oDatabaseDocument.rollback();
|
// oDatabaseDocument.rollback();
|
||||||
}
|
// }
|
||||||
throw new ResourceRegistryException(e);
|
// throw new ResourceRegistryException(e);
|
||||||
} finally {
|
// } finally {
|
||||||
if(oDatabaseDocument != null) {
|
// if(oDatabaseDocument != null) {
|
||||||
oDatabaseDocument.close();
|
// oDatabaseDocument.close();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
if(current!=null) {
|
// if(current!=null) {
|
||||||
current.activateOnCurrentThread();
|
// current.activateOnCurrentThread();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import java.util.UUID;
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.DELETE;
|
import javax.ws.rs.DELETE;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.POST;
|
|
||||||
import javax.ws.rs.PUT;
|
import javax.ws.rs.PUT;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
|
@ -128,34 +127,34 @@ public class ContextManager extends BaseRest {
|
||||||
return contextManagement.createOrUpdate();
|
return contextManagement.createOrUpdate();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// /**
|
||||||
* POST /contexts/{UUID}
|
// * POST /contexts/{UUID}
|
||||||
* e.g. POST /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
|
// * e.g. POST /contexts/c0f314e7-2807-4241-a792-2a6c79ed4fd0
|
||||||
*
|
// *
|
||||||
* BODY: {...}
|
// * BODY: {...}
|
||||||
*
|
// *
|
||||||
* The body contains the Contexts JSon representation
|
// * The body contains the Contexts JSon representation
|
||||||
* only the state property will be evaluated in this
|
// * only the state property will be evaluated in this
|
||||||
* method
|
// * method
|
||||||
*
|
// *
|
||||||
*/
|
// */
|
||||||
@POST
|
// @POST
|
||||||
@Path("{" + ContextManager.CONTEXT_UUID_PATH_PARAMETER + "}")
|
// @Path("{" + ContextManager.CONTEXT_UUID_PATH_PARAMETER + "}")
|
||||||
@Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
|
// @Consumes({MediaType.TEXT_PLAIN, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8})
|
||||||
public String changeState(@PathParam(ContextManager.CONTEXT_UUID_PATH_PARAMETER) String uuid, String json)
|
// public String changeState(@PathParam(ContextManager.CONTEXT_UUID_PATH_PARAMETER) String uuid, String json)
|
||||||
throws ResourceRegistryException {
|
// throws ResourceRegistryException {
|
||||||
logger.info("Requested to activate {} with UUID {}", Context.NAME, uuid);
|
// logger.info("Requested to activate {} with UUID {}", Context.NAME, uuid);
|
||||||
setAccountingMethod(Method.UPDATE, Context.NAME);
|
// setAccountingMethod(Method.UPDATE, Context.NAME);
|
||||||
|
//
|
||||||
ServerRequestInfo serverRequestInfo = initRequestInfo();
|
// ServerRequestInfo serverRequestInfo = initRequestInfo();
|
||||||
serverRequestInfo.setIncludeMeta(true);
|
// serverRequestInfo.setIncludeMeta(true);
|
||||||
serverRequestInfo.setAllMeta(true);
|
// serverRequestInfo.setAllMeta(true);
|
||||||
|
//
|
||||||
ContextManagement contextManagement = new ContextManagement();
|
// ContextManagement contextManagement = new ContextManagement();
|
||||||
contextManagement.setJson(json);
|
// contextManagement.setJson(json);
|
||||||
contextManagement.setUUID(UUID.fromString(uuid));
|
// contextManagement.setUUID(UUID.fromString(uuid));
|
||||||
return contextManagement.changeState();
|
// return contextManagement.changeState();
|
||||||
}
|
// }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* DELETE /contexts/{UUID}
|
* DELETE /contexts/{UUID}
|
||||||
|
|
|
@ -248,7 +248,7 @@ public class ContextStateTest extends ContextManagementTest {
|
||||||
String state = contextState.getState();
|
String state = contextState.getState();
|
||||||
contextA1.setState(state);
|
contextA1.setState(state);
|
||||||
contextManagement.setJson(ElementMapper.marshal(contextA1));
|
contextManagement.setJson(ElementMapper.marshal(contextA1));
|
||||||
String contextString = contextManagement.changeState();
|
String contextString = contextManagement.update();
|
||||||
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
|
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
|
||||||
ContextState gotState = ContextState.fromString(contextA1.getState());
|
ContextState gotState = ContextState.fromString(contextA1.getState());
|
||||||
logger.info("Contexts {} {} expected state {} - got state {}", newContext, contextA1.getID(), contextState.getState(), gotState.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);
|
contextA1.setState(state);
|
||||||
contextManagement.setJson(ElementMapper.marshal(contextA1));
|
contextManagement.setJson(ElementMapper.marshal(contextA1));
|
||||||
try {
|
try {
|
||||||
contextString = contextManagement.changeState();
|
contextString = contextManagement.update();
|
||||||
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
|
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
|
||||||
gotState = ContextState.fromString(contextA1.getState());
|
gotState = ContextState.fromString(contextA1.getState());
|
||||||
throw new Exception("It should be possibile set explicitly the context " + newContext + " " + contextA1.getID() + " as " + gotState.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();
|
state = contextState.getState();
|
||||||
contextA1.setState(state);
|
contextA1.setState(state);
|
||||||
contextManagement.setJson(ElementMapper.marshal(contextA1));
|
contextManagement.setJson(ElementMapper.marshal(contextA1));
|
||||||
contextString = contextManagement.changeState();
|
contextString = contextManagement.update();
|
||||||
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
|
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
|
||||||
gotState = ContextState.fromString(contextA1.getState());
|
gotState = ContextState.fromString(contextA1.getState());
|
||||||
logger.info("Contexts {} {} expected state {} - got state {}", newContext, contextA1.getID(), contextState.getState(), gotState.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);
|
contextA1.setState(state);
|
||||||
contextManagement.setJson(ElementMapper.marshal(contextA1));
|
contextManagement.setJson(ElementMapper.marshal(contextA1));
|
||||||
try {
|
try {
|
||||||
contextString = contextManagement.changeState();
|
contextString = contextManagement.update();
|
||||||
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
|
contextA1 = ElementMapper.unmarshal(Context.class, contextString);
|
||||||
gotState = ContextState.fromString(contextA1.getState());
|
gotState = ContextState.fromString(contextA1.getState());
|
||||||
throw new Exception("It should be possibile set explicitly the context " + newContext + " " + contextA1.getID() + " as " + gotState.getState());
|
throw new Exception("It should be possibile set explicitly the context " + newContext + " " + contextA1.getID() + " as " + gotState.getState());
|
||||||
|
|
Loading…
Reference in New Issue