Refs #10238: Refactor Context Port Type

Task-Url: https://support.d4science.org/issues/10238

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@158532 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-11-15 18:31:49 +00:00
parent 3310893d0d
commit 996a00f10f
16 changed files with 385 additions and 113 deletions

View File

@ -1,29 +1,122 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.context; package org.gcube.informationsystem.resourceregistry.context;
import java.util.UUID; import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.entity.Context;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextCreationException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** import com.fasterxml.jackson.databind.JsonNode;
* @author Luca Frosini (ISTI - CNR) import com.tinkerpop.blueprints.Direction;
*/ import com.tinkerpop.blueprints.Edge;
public interface ContextManagement { import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
public String create(UUID parentContext, String name) throws ContextCreationException, ResourceRegistryException; public class ContextManagement extends EntityManagement<Context> {
private static Logger logger = LoggerFactory.getLogger(ContextManagement.class);
public String read(UUID context) throws ContextNotFoundException, ContextException; public ContextManagement() {
super(AccessType.CONTEXT);
forceAdmin = true;
}
public String rename(UUID context, String newName) throws ContextNotFoundException, ContextException; public ContextManagement(OrientGraph orientGraph) {
super(AccessType.CONTEXT, orientGraph);
forceAdmin = true;
}
public String move(UUID newParent, UUID contextToMove) throws ContextNotFoundException, ContextException; @Override
public String serialize() throws ResourceRegistryException {
public boolean delete(UUID context) throws ContextNotFoundException, ContextException; return serializeAsJson().toString();
}
@Override
public JSONObject serializeAsJson() throws ResourceRegistryException {
JSONObject context = serializeSelfOnly();
int count=0;
Iterable<Edge> parents = getElement().getEdges(Direction.IN);
for(Edge edge : parents){
if(++count>1) {
throw new ContextException("A " + Context.NAME + " can not have more than one parent");
}
ContextManagement contextManagement = new ContextManagement(orientGraph);
contextManagement.setElement(edge.getVertex(Direction.OUT));
JSONObject parent = contextManagement.serializeSelfOnly();
try {
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(orientGraph);
isParentOfManagement.setElement(edge);
JSONObject isParentOf = isParentOfManagement.serializeAsJson(false,false);
parent = addRelation(parent, isParentOf, Context.CHILDREN_PROPERTY);
context.putOpt(Context.PARENT_PROPERTY, parent);
} catch (JSONException e) {
logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", edge);
throw new ContextException("");
}
}
Iterable<Edge> childrenEdges = getElement().getEdges(Direction.OUT);
for (Edge edge : childrenEdges) {
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(orientGraph);
isParentOfManagement.setElement(edge);
try {
JSONObject isParentOf = isParentOfManagement.serializeAsJson();
context = addRelation(context, isParentOf, Context.CHILDREN_PROPERTY);
}catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", edge);
throw e;
}catch (Exception e) {
logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", edge);
throw new ResourceRegistryException(e);
}
}
return context;
}
@Override
protected Vertex reallyCreate() throws ERAlreadyPresentException, ResourceRegistryException {
createVertex();
String propertyName = Context.PARENT_PROPERTY;
if (jsonNode.has(propertyName)) {
JsonNode parentJsonNode = jsonNode.get(propertyName);
ContextManagement contextManagement = new ContextManagement(orientGraph);
contextManagement.setJSON(parentJsonNode);
Vertex parent = contextManagement.getElement();
IsParentOfManagement isParentOfManagement = new IsParentOfManagement(orientGraph);
isParentOfManagement.reallyCreate(parent, this.element);
}
return element;
}
@Override
protected Vertex reallyUpdate() throws ERNotFoundException, ResourceRegistryException {
// TODO Auto-generated method stub
return null;
}
@Override
protected boolean reallyDelete() throws ERNotFoundException, ResourceRegistryException {
// TODO Auto-generated method stub
return false;
}
}
}

View File

@ -30,7 +30,7 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertex;
/** /**
* @author Luca Frosini (ISTI - CNR) * @author Luca Frosini (ISTI - CNR)
*/ */
public class ContextManagementImpl implements ContextManagement { public class ContextManagementImpl implements OLDContextManagement {
private static Logger logger = LoggerFactory private static Logger logger = LoggerFactory
.getLogger(ContextManagementImpl.class); .getLogger(ContextManagementImpl.class);

View File

@ -140,9 +140,14 @@ public class ContextUtility {
} }
public static OrientGraph getActualSecurityContextGraph( public static OrientGraph getActualSecurityContextGraph(
PermissionMode permissionMode) throws ResourceRegistryException { PermissionMode permissionMode, boolean forceAdmin) throws ResourceRegistryException {
try { try {
UUID contextUUID = getActualContextUUID(); UUID contextUUID = null;
if(forceAdmin) {
contextUUID = SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID;
}else {
contextUUID = getActualContextUUID();
}
return SecurityContextMapper.getSecurityContextGraph(contextUUID, permissionMode); return SecurityContextMapper.getSecurityContextGraph(contextUUID, permissionMode);
} catch (ContextException ce) { } catch (ContextException ce) {
logger.error("Unable to retrieve context.", ce); logger.error("Unable to retrieve context.", ce);
@ -154,9 +159,14 @@ public class ContextUtility {
} }
public static OrientGraphNoTx getActualSecurityContextGraphNoTx( public static OrientGraphNoTx getActualSecurityContextGraphNoTx(
PermissionMode permissionMode) throws ResourceRegistryException { PermissionMode permissionMode, boolean forceAdmin) throws ResourceRegistryException {
try { try {
UUID contextUUID = getActualContextUUID(); UUID contextUUID = null;
if(forceAdmin) {
contextUUID = SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID;
}else {
contextUUID = getActualContextUUID();
}
return SecurityContextMapper.getSecurityContextGraphNoTx(contextUUID, permissionMode); return SecurityContextMapper.getSecurityContextGraphNoTx(contextUUID, permissionMode);
} catch (ContextException ce) { } catch (ContextException ce) {
logger.error("Unable to retrieve context.", ce); logger.error("Unable to retrieve context.", ce);

View File

@ -0,0 +1,70 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.context;
import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.model.AccessType;
import org.gcube.informationsystem.model.relation.IsParentOf;
import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
/**
* @author Luca Frosini (ISTI - CNR)
*/
@SuppressWarnings("rawtypes")
public class IsParentOfManagement extends RelationManagement<IsParentOf> {
public IsParentOfManagement() {
super(AccessType.IS_PARENT_OF);
}
public IsParentOfManagement(OrientGraph orientGraph) {
super(AccessType.IS_PARENT_OF, orientGraph);
}
@Override
public JSONObject serializeAsJson() throws ResourceRegistryException {
return serializeAsJson(false, true);
}
public JSONObject serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
JSONObject relation = serializeSelfOnly();
try {
Vertex source = element.getVertex(Direction.OUT);
ContextManagement sourceContextManagement = new ContextManagement(orientGraph);
sourceContextManagement.setElement(source);
if(includeSource) {
relation.put(Relation.SOURCE_PROPERTY, sourceContextManagement.serializeSelfOnly());
}
Vertex target = element.getVertex(Direction.IN);
ContextManagement targetContextManagement = new ContextManagement(orientGraph);
targetContextManagement.setElement(target);
if(includeTarget) {
relation.put(Relation.TARGET_PROPERTY, targetContextManagement.serializeSelfOnly());
}
} catch (ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", element, e);
throw e;
} catch (Exception e) {
logger.error("Unable to correctly serialize {}. This is really strange and should not occur.", element, e);
throw new ResourceRegistryException(e);
}
return relation;
}
public Edge reallyCreate(Vertex source, Vertex target) throws ResourceRegistryException {
return super.reallyCreate(source, target);
}
}

View File

@ -0,0 +1,29 @@
/**
*
*/
package org.gcube.informationsystem.resourceregistry.context;
import java.util.UUID;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextCreationException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public interface OLDContextManagement {
public String create(UUID parentContext, String name) throws ContextCreationException, ResourceRegistryException;
public String read(UUID context) throws ContextNotFoundException, ContextException;
public String rename(UUID context, String newName) throws ContextNotFoundException, ContextException;
public String move(UUID newParent, UUID contextToMove) throws ContextNotFoundException, ContextException;
public boolean delete(UUID context) throws ContextNotFoundException, ContextException;
}

View File

@ -27,8 +27,8 @@ public abstract class SecurityContextMapper {
.getLogger(SecurityContextMapper.class); .getLogger(SecurityContextMapper.class);
// Used to persist Schemas // Used to persist Schemas
public static final String ADMIN_SECURITY_CONTEXT = "00000000-0000-0000-0000-000000000000"; protected static final String ADMIN_SECURITY_CONTEXT = "00000000-0000-0000-0000-000000000000";
public static final UUID ADMIN_SECURITY_CONTEXT_UUID = UUID.fromString(ADMIN_SECURITY_CONTEXT); protected static final UUID ADMIN_SECURITY_CONTEXT_UUID = UUID.fromString(ADMIN_SECURITY_CONTEXT);
// Used to Persist Context and thei relations // Used to Persist Context and thei relations
public static final String MANAGEMENT_SECURITY_CONTEXT = "ffffffff-ffff-ffff-ffff-ffffffffffff"; public static final String MANAGEMENT_SECURITY_CONTEXT = "ffffffff-ffff-ffff-ffff-ffffffffffff";
@ -147,7 +147,7 @@ public abstract class SecurityContextMapper {
return factory; return factory;
} }
public static OrientGraph getSecurityContextGraph( protected static OrientGraph getSecurityContextGraph(
UUID context, PermissionMode permissionMode) { UUID context, PermissionMode permissionMode) {
OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false); OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false);
OrientGraph orientGraph = factory.getTx(); OrientGraph orientGraph = factory.getTx();
@ -158,7 +158,7 @@ public abstract class SecurityContextMapper {
return orientGraph; return orientGraph;
} }
public static OrientGraphNoTx getSecurityContextGraphNoTx( protected static OrientGraphNoTx getSecurityContextGraphNoTx(
UUID context, PermissionMode permissionMode) { UUID context, PermissionMode permissionMode) {
OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false); OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false);
OrientGraphNoTx orientGraphNoTx = factory.getNoTx(); OrientGraphNoTx orientGraphNoTx = factory.getNoTx();

View File

@ -46,7 +46,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.Rela
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseIntializator; import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseIntializator;
import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement; import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement;
@ -86,7 +85,9 @@ import com.tinkerpop.blueprints.util.StringFactory;
*/ */
public abstract class ERManagement<ERType extends ER, El extends Element> { public abstract class ERManagement<ERType extends ER, El extends Element> {
private static Logger logger = LoggerFactory.getLogger(ERManagement.class); protected Logger logger = LoggerFactory.getLogger(this.getClass());
private static Logger staticLogger = LoggerFactory.getLogger(ERManagement.class);
public final String AT = "@"; public final String AT = "@";
public final String UNDERSCORE = "_"; public final String UNDERSCORE = "_";
@ -105,6 +106,19 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
protected String erType; protected String erType;
protected El element; protected El element;
/**
* Yhis boolean is used to force the use of ADMIN user instead of the user of the context
*/
protected boolean forceAdmin;
public boolean isForceAdmin() {
return forceAdmin;
}
public void setForceAdmin(boolean forceAdmin) {
this.forceAdmin = forceAdmin;
}
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public static ERManagement getERManagement(AccessType querableType) public static ERManagement getERManagement(AccessType querableType)
@ -235,7 +249,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
checkJSON(); checkJSON();
} }
protected OClass getOClass() throws SchemaException { protected OClass getOClass() throws SchemaException, ResourceRegistryException {
if(oClass==null){ if(oClass==null){
if(element!=null){ if(element!=null){
OrientElement orientElement = (OrientElement) element; OrientElement orientElement = (OrientElement) element;
@ -325,7 +339,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public abstract JSONObject serializeAsJson() public abstract JSONObject serializeAsJson()
throws ResourceRegistryException; throws ResourceRegistryException;
protected abstract El reallyCreate() throws ERAlreadyPresentException, protected abstract El reallyCreate() throws ERAlreadyPresentException,
ResourceRegistryException; ResourceRegistryException;
@ -527,7 +541,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public String all(boolean polymorphic) throws ResourceRegistryException { public String all(boolean polymorphic) throws ResourceRegistryException {
try { try {
orientGraph = ContextUtility orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.READER); .getActualSecurityContextGraph(PermissionMode.READER, forceAdmin);
return reallyGetAll(polymorphic); return reallyGetAll(polymorphic);
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
@ -544,7 +558,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public boolean exists() throws ERNotFoundException, public boolean exists() throws ERNotFoundException,
ERAvailableInAnotherContextException, ResourceRegistryException { ERAvailableInAnotherContextException, ResourceRegistryException {
try { try {
orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.READER); orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.READER, forceAdmin);
getElement(); getElement();
@ -563,7 +577,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
public String create() throws ERAlreadyPresentException, ResourceRegistryException { public String create() throws ERAlreadyPresentException, ResourceRegistryException {
try { try {
orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.WRITER); orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin);
element = internalCreate(); element = internalCreate();
@ -594,7 +608,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
ERAvailableInAnotherContextException, ResourceRegistryException { ERAvailableInAnotherContextException, ResourceRegistryException {
try { try {
orientGraph = ContextUtility orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.READER); .getActualSecurityContextGraph(PermissionMode.READER, forceAdmin);
getElement(); getElement();
@ -614,7 +628,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
ERAvailableInAnotherContextException, ResourceRegistryException { ERAvailableInAnotherContextException, ResourceRegistryException {
try { try {
orientGraph = ContextUtility orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER); .getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin);
element = internalUpdate(); element = internalUpdate();
orientGraph.commit(); orientGraph.commit();
@ -646,9 +660,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
try { try {
orientGraph = SecurityContextMapper.getSecurityContextGraph( orientGraph = ContextUtility.getActualSecurityContextGraph(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER, true);
PermissionMode.WRITER);
boolean deleted = reallyDelete(); boolean deleted = reallyDelete();
@ -688,9 +701,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
accessType.getName(), uuid); accessType.getName(), uuid);
try { try {
orientGraph = SecurityContextMapper.getSecurityContextGraph( orientGraph = ContextUtility.getActualSecurityContextGraph(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER, true);
PermissionMode.WRITER);
boolean added = internalAddToContext(); boolean added = internalAddToContext();
@ -718,9 +730,8 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
accessType.getName(), uuid); accessType.getName(), uuid);
try { try {
orientGraph = SecurityContextMapper.getSecurityContextGraph( orientGraph = ContextUtility.getActualSecurityContextGraph(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER, true);
PermissionMode.WRITER);
boolean removed = internalRemoveFromContext(); boolean removed = internalRemoveFromContext();
@ -852,7 +863,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
map.put(key, object); map.put(key, object);
} }
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
logger.warn("An invalidy property has been provided. It will be ignored."); staticLogger.warn("An invalidy property has been provided. It will be ignored.");
} }
} }
@ -920,7 +931,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
String error = String.format( String error = String.format(
"Error while setting property %s : %s (%s)", key, properties "Error while setting property %s : %s (%s)", key, properties
.get(key).toString(), e.getMessage()); .get(key).toString(), e.getMessage());
logger.error(error); staticLogger.error(error);
throw new ResourceRegistryException(error, e); throw new ResourceRegistryException(error, e);
} }
} }
@ -1010,7 +1021,7 @@ public abstract class ERManagement<ERType extends ER, El extends Element> {
} }
} }
protected Collection<String> getSuperclasses() throws SchemaException{ protected Collection<String> getSuperclasses() throws SchemaException, ResourceRegistryException {
Collection<OClass> allSuperClasses = getOClass().getAllSuperClasses(); Collection<OClass> allSuperClasses = getOClass().getAllSuperClasses();
Collection<String> superClasses = new HashSet<>(); Collection<String> superClasses = new HashSet<>();
for(OClass oSuperClass : allSuperClasses){ for(OClass oSuperClass : allSuperClasses){

View File

@ -36,7 +36,7 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
public abstract class EntityManagement<E extends Entity> extends public abstract class EntityManagement<E extends Entity> extends
ERManagement<E, Vertex> { ERManagement<E, Vertex> {
private static Logger logger = LoggerFactory private static Logger staticLogger = LoggerFactory
.getLogger(EntityManagement.class); .getLogger(EntityManagement.class);
protected EntityManagement(AccessType accessType) { protected EntityManagement(AccessType accessType) {
@ -60,6 +60,25 @@ public abstract class EntityManagement<E extends Entity> extends
this.orientGraph = orientGraph; this.orientGraph = orientGraph;
} }
protected static JSONObject addRelation(JSONObject sourceResource,
JSONObject relation, String arrayKey)
throws ResourceRegistryException {
JSONArray relationArray = null;
try {
if (sourceResource.has(arrayKey)) {
relationArray = sourceResource.getJSONArray(arrayKey);
} else {
relationArray = new JSONArray();
}
relationArray.put(relation);
sourceResource.putOpt(arrayKey, relationArray);
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
return sourceResource;
}
protected Vertex createVertex() throws EntityAlreadyPresentException, protected Vertex createVertex() throws EntityAlreadyPresentException,
ResourceRegistryException { ResourceRegistryException {
@ -190,7 +209,7 @@ public abstract class EntityManagement<E extends Entity> extends
orientVertexType = ((OrientVertex) vertex).getType(); orientVertexType = ((OrientVertex) vertex).getType();
}catch (Exception e) { }catch (Exception e) {
String error = String.format("Unable to detect type of %s. This is really strage please contact the administrator.", vertex.toString()); String error = String.format("Unable to detect type of %s. This is really strage please contact the administrator.", vertex.toString());
logger.error(error, e); staticLogger.error(error, e);
throw new ResourceRegistryException(error); throw new ResourceRegistryException(error);
} }

View File

@ -22,8 +22,6 @@ import org.gcube.informationsystem.resourceregistry.er.relation.ConsistsOfManage
import org.gcube.informationsystem.resourceregistry.er.relation.IsRelatedToManagement; import org.gcube.informationsystem.resourceregistry.er.relation.IsRelatedToManagement;
import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement; import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement;
import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
@ -43,9 +41,6 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
*/ */
public class ResourceManagement extends EntityManagement<Resource> { public class ResourceManagement extends EntityManagement<Resource> {
private static Logger logger = LoggerFactory
.getLogger(ResourceManagement.class);
public ResourceManagement() { public ResourceManagement() {
super(AccessType.RESOURCE); super(AccessType.RESOURCE);
} }
@ -105,36 +100,14 @@ public class ResourceManagement extends EntityManagement<Resource> {
return sourceResource; return sourceResource;
} }
protected static JSONObject addRelation(JSONObject sourceResource,
JSONObject relation, AccessType accessType)
throws ResourceRegistryException {
String arrayKey = accessType.lowerCaseFirstCharacter();
JSONArray relationArray = null;
try {
if (sourceResource.has(arrayKey)) {
relationArray = sourceResource.getJSONArray(arrayKey);
} else {
relationArray = new JSONArray();
}
relationArray.put(relation);
sourceResource.putOpt(arrayKey, relationArray);
} catch (Exception e) {
throw new ResourceRegistryException(e);
}
return sourceResource;
}
public static JSONObject addConsistsOf(JSONObject sourceResource, public static JSONObject addConsistsOf(JSONObject sourceResource,
JSONObject consistsOf) throws ResourceRegistryException { JSONObject consistsOf) throws ResourceRegistryException {
return addRelation(sourceResource, consistsOf, AccessType.CONSISTS_OF); return addRelation(sourceResource, consistsOf, AccessType.CONSISTS_OF.lowerCaseFirstCharacter());
} }
public static JSONObject addIsRelatedTo(JSONObject sourceResource, public static JSONObject addIsRelatedTo(JSONObject sourceResource,
JSONObject isRelatedTo) throws ResourceRegistryException { JSONObject isRelatedTo) throws ResourceRegistryException {
return addRelation(sourceResource, isRelatedTo, AccessType.IS_RELATED_TO); return addRelation(sourceResource, isRelatedTo, AccessType.IS_RELATED_TO.lowerCaseFirstCharacter());
} }
@Override @Override
@ -238,7 +211,7 @@ public class ResourceManagement extends EntityManagement<Resource> {
public String all(boolean polymorphic, Map<String, String> constraint) throws ResourceRegistryException { public String all(boolean polymorphic, Map<String, String> constraint) throws ResourceRegistryException {
try { try {
orientGraph = ContextUtility orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.READER); .getActualSecurityContextGraph(PermissionMode.READER, forceAdmin);
return reallyGetAll(polymorphic, constraint); return reallyGetAll(polymorphic, constraint);
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {

View File

@ -27,15 +27,12 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException;
import org.gcube.informationsystem.resourceregistry.context.ContextUtility; import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.resourceregistry.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.ERManagement;
import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement; import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement;
import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement; import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement;
import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement; import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement;
import org.gcube.informationsystem.resourceregistry.utils.Utility; import org.gcube.informationsystem.resourceregistry.utils.Utility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Direction;
@ -54,9 +51,6 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph;
public abstract class RelationManagement<R extends Relation> extends public abstract class RelationManagement<R extends Relation> extends
ERManagement<R, Edge> { ERManagement<R, Edge> {
private static Logger logger = LoggerFactory
.getLogger(RelationManagement.class);
protected final Class<? extends Entity> targetEntityClass; protected final Class<? extends Entity> targetEntityClass;
protected RelationManagement(AccessType accessType) { protected RelationManagement(AccessType accessType) {
@ -622,7 +616,7 @@ public abstract class RelationManagement<R extends Relation> extends
throws ResourceRegistryException { throws ResourceRegistryException {
try { try {
orientGraph = ContextUtility orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER); .getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin);
element = reallyCreate(sourceUUID, targetUUID); element = reallyCreate(sourceUUID, targetUUID);
@ -704,7 +698,7 @@ public abstract class RelationManagement<R extends Relation> extends
throws ResourceRegistryException { throws ResourceRegistryException {
try { try {
orientGraph = ContextUtility orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.READER); .getActualSecurityContextGraph(PermissionMode.READER, forceAdmin);
return reallyGetAllFrom(uuid, direction, polymorphic); return reallyGetAllFrom(uuid, direction, polymorphic);
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
@ -724,9 +718,8 @@ public abstract class RelationManagement<R extends Relation> extends
accessType.getName(), uuid); accessType.getName(), uuid);
try { try {
orientGraph = SecurityContextMapper.getSecurityContextGraph( orientGraph = ContextUtility.getActualSecurityContextGraph(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER, true);
PermissionMode.WRITER);
boolean added = forcedAddToContext(); boolean added = forcedAddToContext();

View File

@ -31,7 +31,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.query.Invalid
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath; import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD; import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPCall.HTTPMETHOD;
import org.gcube.informationsystem.resourceregistry.context.ContextManagement; import org.gcube.informationsystem.resourceregistry.context.OLDContextManagement;
import org.gcube.informationsystem.resourceregistry.context.ContextManagementImpl; import org.gcube.informationsystem.resourceregistry.context.ContextManagementImpl;
import org.gcube.informationsystem.resourceregistry.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.ERManagement;
import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement; import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement;
@ -298,7 +298,7 @@ public class Access {
public String getContext(@PathParam(ID_PATH_PARAM) String uuid) public String getContext(@PathParam(ID_PATH_PARAM) String uuid)
throws ContextNotFoundException, ContextException { throws ContextNotFoundException, ContextException {
logger.info("Requested to read {} with id {} ", org.gcube.informationsystem.model.entity.Context.NAME, uuid); logger.info("Requested to read {} with id {} ", org.gcube.informationsystem.model.entity.Context.NAME, uuid);
ContextManagement contextManager = new ContextManagementImpl(); OLDContextManagement contextManager = new ContextManagementImpl();
return contextManager.read(UUID.fromString(uuid)); return contextManager.read(UUID.fromString(uuid));
} }

View File

@ -24,7 +24,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.context.Conte
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath; import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath;
import org.gcube.informationsystem.resourceregistry.context.ContextManagement; import org.gcube.informationsystem.resourceregistry.context.OLDContextManagement;
import org.gcube.informationsystem.resourceregistry.context.ContextManagementImpl; import org.gcube.informationsystem.resourceregistry.context.ContextManagementImpl;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -43,7 +43,7 @@ public class ContextManager {
public static final String ID_PATH_PARAM = "id"; public static final String ID_PATH_PARAM = "id";
protected ContextManagement contextManager = new ContextManagementImpl(); protected OLDContextManagement contextManager = new ContextManagementImpl();
/** /**
* e.g. PUT /resource-registry/context?name=myVRE&parentContextId=a2fe0030-7b3d-4617-ba37-532c0e4b778d * e.g. PUT /resource-registry/context?name=myVRE&parentContextId=a2fe0030-7b3d-4617-ba37-532c0e4b778d

View File

@ -8,7 +8,7 @@ import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper; import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.types.TypeBinder.TypeDefinition; import org.gcube.informationsystem.types.TypeBinder.TypeDefinition;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -55,8 +55,7 @@ public class SchemaContextManagement implements SchemaManagement {
OrientGraph orientGraph = null; OrientGraph orientGraph = null;
try { try {
orientGraph = SecurityContextMapper orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.WRITER, true);
.getSecurityContextGraph(SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
TypeDefinition typeDefinition = mapper.readValue(json, TypeDefinition.class); TypeDefinition typeDefinition = mapper.readValue(json, TypeDefinition.class);

View File

@ -19,7 +19,7 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegis
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper; import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.gcube.informationsystem.types.TypeBinder; import org.gcube.informationsystem.types.TypeBinder;
import org.gcube.informationsystem.types.TypeBinder.Property; import org.gcube.informationsystem.types.TypeBinder.Property;
@ -86,18 +86,18 @@ public class SchemaManagementImpl implements SchemaManagement {
} }
public static OClass getTypeSchema(String type, AccessType accessType) public static OClass getTypeSchema(String type, AccessType accessType)
throws SchemaException { throws SchemaException, ResourceRegistryException {
OrientGraphNoTx orientGraphNoTx = null; OrientGraphNoTx orientGraphNoTx = null;
try { try {
logger.debug("Getting {} Type {} schema", logger.debug("Getting {} Type {} schema",
accessType != null ? accessType.getName() : "", type); accessType != null ? accessType.getName() : "", type);
orientGraphNoTx = SecurityContextMapper.getSecurityContextGraphNoTx( orientGraphNoTx = ContextUtility.getActualSecurityContextGraphNoTx(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.READER, true);
PermissionMode.READER);
return getTypeSchema(orientGraphNoTx, type, accessType); return getTypeSchema(orientGraphNoTx, type, accessType);
} catch (ResourceRegistryException e) {
throw e;
} finally { } finally {
if (orientGraphNoTx != null) { if (orientGraphNoTx != null) {
orientGraphNoTx.shutdown(); orientGraphNoTx.shutdown();
@ -178,9 +178,8 @@ public class SchemaManagementImpl implements SchemaManagement {
TypeDefinition typeDefinition = mapper.readValue(jsonSchema, TypeDefinition typeDefinition = mapper.readValue(jsonSchema,
TypeDefinition.class); TypeDefinition.class);
orientGraphNoTx = SecurityContextMapper.getSecurityContextGraphNoTx( orientGraphNoTx = ContextUtility.getActualSecurityContextGraphNoTx(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER, true);
PermissionMode.WRITER);
OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata(); OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata();
OSchema oSchema = oMetadata.getSchema(); OSchema oSchema = oMetadata.getSchema();
@ -337,9 +336,8 @@ public class SchemaManagementImpl implements SchemaManagement {
throws SchemaNotFoundException, SchemaException { throws SchemaNotFoundException, SchemaException {
OrientGraphNoTx orientGraphNoTx = null; OrientGraphNoTx orientGraphNoTx = null;
try { try {
orientGraphNoTx = SecurityContextMapper.getSecurityContextGraphNoTx( orientGraphNoTx = ContextUtility.getActualSecurityContextGraphNoTx(
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER, true);
PermissionMode.WRITER);
OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata(); OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata();
OSchema oSchema = oMetadata.getSchema(); OSchema oSchema = oMetadata.getSchema();

View File

@ -17,7 +17,7 @@ import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException; import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper; import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode; import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -92,8 +92,7 @@ public class Utility {
Class<? extends El> clz) throws ERNotFoundException, ResourceRegistryException { Class<? extends El> clz) throws ERNotFoundException, ResourceRegistryException {
OrientGraphNoTx orientGraphNoTx = null; OrientGraphNoTx orientGraphNoTx = null;
try { try {
orientGraphNoTx = SecurityContextMapper.getSecurityContextGraphNoTx( orientGraphNoTx = ContextUtility.getActualSecurityContextGraphNoTx(PermissionMode.READER, true);
SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.READER);
return Utility.getElementByUUID(orientGraphNoTx, elementType, uuid, clz); return Utility.getElementByUUID(orientGraphNoTx, elementType, uuid, clz);
} finally { } finally {
if (orientGraphNoTx != null) { if (orientGraphNoTx != null) {

View File

@ -0,0 +1,78 @@
package org.gcube.informationsystem.resourceregistry.context;
import java.util.List;
import java.util.UUID;
import org.gcube.informationsystem.impl.entity.ContextImpl;
import org.gcube.informationsystem.impl.utils.ISMapper;
import org.gcube.informationsystem.model.entity.Context;
import org.gcube.informationsystem.model.relation.IsParentOf;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ContextManagementTest {
private static Logger logger = LoggerFactory
.getLogger(ContextManagementTest.class);
@Test
public void get() throws Exception {
// UUID uuid = UUID.fromString("34395f1c-ecb3-4d21-8abe-986ba1650919");
UUID uuid = UUID.fromString("9e5ed12d-54e9-4810-9133-cfbcad996981");
ContextManagement contextManagement = new ContextManagement();
contextManagement.setUUID(uuid);
String string = contextManagement.read();
logger.debug(string);
Context context = ISMapper.unmarshal(Context.class, string);
logger.debug("{}", ISMapper.marshal(context));
logger.debug("Parent : {}", ISMapper.marshal(context.getParent()));
for(IsParentOf<Context, Context> isParentOf : context.getChildren()){
logger.debug("Children : {}", ISMapper.marshal(isParentOf));
}
Context parent = context.getParent(); //.getSource();
Context sameOfContext = parent.getChildren().get(0).getTarget();
Assert.assertTrue(context==sameOfContext);
List<IsParentOf<Context, Context>> children = context.getChildren();
for(IsParentOf<Context, Context> child : children) {
Assert.assertTrue(child.getSource()==context);
}
}
@Test
public void testJava() throws Exception {
Context gcube = new ContextImpl("gcube");
logger.debug("gcube : {}", ISMapper.marshal(gcube));
Context devsec = new ContextImpl("devsec");
gcube.addChild(devsec);
logger.debug("devsec : {}", ISMapper.marshal(devsec));
Context devVRE = new ContextImpl("devVRE");
devsec.addChild(devVRE);
logger.debug("devVRE : {}", ISMapper.marshal(devVRE));
Context devNext = new ContextImpl("devNext");
gcube.addChild(devNext);
logger.debug("devNext : {}", ISMapper.marshal(devNext));
Context NextNext = new ContextImpl("NextNext");
devNext.addChild(NextNext);
logger.debug("NextNext : {}", ISMapper.marshal(NextNext));
}
}