diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java index 4d6bbe7..f13b151 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagement.java @@ -1,29 +1,122 @@ -/** - * - */ 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.context.ContextCreationException; 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; -/** - * @author Luca Frosini (ISTI - CNR) - */ -public interface ContextManagement { +import com.fasterxml.jackson.databind.JsonNode; +import com.tinkerpop.blueprints.Direction; +import com.tinkerpop.blueprints.Edge; +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 { + + 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; - - public boolean delete(UUID context) throws ContextNotFoundException, ContextException; + @Override + public String serialize() throws ResourceRegistryException { + return serializeAsJson().toString(); + } + + @Override + public JSONObject serializeAsJson() throws ResourceRegistryException { + + JSONObject context = serializeSelfOnly(); + + int count=0; + Iterable 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 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; + } -} \ No newline at end of file + + +} diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java index cd1f311..544a0e1 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementImpl.java @@ -30,7 +30,7 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertex; /** * @author Luca Frosini (ISTI - CNR) */ -public class ContextManagementImpl implements ContextManagement { +public class ContextManagementImpl implements OLDContextManagement { private static Logger logger = LoggerFactory .getLogger(ContextManagementImpl.class); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java index 2e66b26..468984f 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/ContextUtility.java @@ -140,9 +140,14 @@ public class ContextUtility { } public static OrientGraph getActualSecurityContextGraph( - PermissionMode permissionMode) throws ResourceRegistryException { + PermissionMode permissionMode, boolean forceAdmin) throws ResourceRegistryException { try { - UUID contextUUID = getActualContextUUID(); + UUID contextUUID = null; + if(forceAdmin) { + contextUUID = SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID; + }else { + contextUUID = getActualContextUUID(); + } return SecurityContextMapper.getSecurityContextGraph(contextUUID, permissionMode); } catch (ContextException ce) { logger.error("Unable to retrieve context.", ce); @@ -154,9 +159,14 @@ public class ContextUtility { } public static OrientGraphNoTx getActualSecurityContextGraphNoTx( - PermissionMode permissionMode) throws ResourceRegistryException { + PermissionMode permissionMode, boolean forceAdmin) throws ResourceRegistryException { try { - UUID contextUUID = getActualContextUUID(); + UUID contextUUID = null; + if(forceAdmin) { + contextUUID = SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID; + }else { + contextUUID = getActualContextUUID(); + } return SecurityContextMapper.getSecurityContextGraphNoTx(contextUUID, permissionMode); } catch (ContextException ce) { logger.error("Unable to retrieve context.", ce); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java new file mode 100644 index 0000000..a461d7c --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/IsParentOfManagement.java @@ -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 { + + 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); + } + +} diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/OLDContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/OLDContextManagement.java new file mode 100644 index 0000000..66443af --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/OLDContextManagement.java @@ -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; + + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java b/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java index 6564b16..248b220 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/context/SecurityContextMapper.java @@ -27,8 +27,8 @@ public abstract class SecurityContextMapper { .getLogger(SecurityContextMapper.class); // Used to persist Schemas - public 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 String ADMIN_SECURITY_CONTEXT = "00000000-0000-0000-0000-000000000000"; + protected static final UUID ADMIN_SECURITY_CONTEXT_UUID = UUID.fromString(ADMIN_SECURITY_CONTEXT); // Used to Persist Context and thei relations public static final String MANAGEMENT_SECURITY_CONTEXT = "ffffffff-ffff-ffff-ffff-ffffffffffff"; @@ -147,7 +147,7 @@ public abstract class SecurityContextMapper { return factory; } - public static OrientGraph getSecurityContextGraph( + protected static OrientGraph getSecurityContextGraph( UUID context, PermissionMode permissionMode) { OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false); OrientGraph orientGraph = factory.getTx(); @@ -158,7 +158,7 @@ public abstract class SecurityContextMapper { return orientGraph; } - public static OrientGraphNoTx getSecurityContextGraphNoTx( + protected static OrientGraphNoTx getSecurityContextGraphNoTx( UUID context, PermissionMode permissionMode) { OrientGraphFactory factory = getSecurityContextFactory(context, permissionMode, false); OrientGraphNoTx orientGraphNoTx = factory.getNoTx(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java index c33440a..995e697 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java @@ -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.schema.SchemaException; 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.dbinitialization.DatabaseIntializator; import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement; @@ -86,7 +85,9 @@ import com.tinkerpop.blueprints.util.StringFactory; */ public abstract class ERManagement { - 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 UNDERSCORE = "_"; @@ -105,6 +106,19 @@ public abstract class ERManagement { protected String erType; 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") public static ERManagement getERManagement(AccessType querableType) @@ -235,7 +249,7 @@ public abstract class ERManagement { checkJSON(); } - protected OClass getOClass() throws SchemaException { + protected OClass getOClass() throws SchemaException, ResourceRegistryException { if(oClass==null){ if(element!=null){ OrientElement orientElement = (OrientElement) element; @@ -325,7 +339,7 @@ public abstract class ERManagement { public abstract JSONObject serializeAsJson() throws ResourceRegistryException; - + protected abstract El reallyCreate() throws ERAlreadyPresentException, ResourceRegistryException; @@ -527,7 +541,7 @@ public abstract class ERManagement { public String all(boolean polymorphic) throws ResourceRegistryException { try { orientGraph = ContextUtility - .getActualSecurityContextGraph(PermissionMode.READER); + .getActualSecurityContextGraph(PermissionMode.READER, forceAdmin); return reallyGetAll(polymorphic); } catch (ResourceRegistryException e) { @@ -544,7 +558,7 @@ public abstract class ERManagement { public boolean exists() throws ERNotFoundException, ERAvailableInAnotherContextException, ResourceRegistryException { try { - orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.READER); + orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.READER, forceAdmin); getElement(); @@ -563,7 +577,7 @@ public abstract class ERManagement { public String create() throws ERAlreadyPresentException, ResourceRegistryException { try { - orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.WRITER); + orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin); element = internalCreate(); @@ -594,7 +608,7 @@ public abstract class ERManagement { ERAvailableInAnotherContextException, ResourceRegistryException { try { orientGraph = ContextUtility - .getActualSecurityContextGraph(PermissionMode.READER); + .getActualSecurityContextGraph(PermissionMode.READER, forceAdmin); getElement(); @@ -614,7 +628,7 @@ public abstract class ERManagement { ERAvailableInAnotherContextException, ResourceRegistryException { try { orientGraph = ContextUtility - .getActualSecurityContextGraph(PermissionMode.WRITER); + .getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin); element = internalUpdate(); orientGraph.commit(); @@ -646,9 +660,8 @@ public abstract class ERManagement { try { - orientGraph = SecurityContextMapper.getSecurityContextGraph( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER); + orientGraph = ContextUtility.getActualSecurityContextGraph( + PermissionMode.WRITER, true); boolean deleted = reallyDelete(); @@ -688,9 +701,8 @@ public abstract class ERManagement { accessType.getName(), uuid); try { - orientGraph = SecurityContextMapper.getSecurityContextGraph( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER); + orientGraph = ContextUtility.getActualSecurityContextGraph( + PermissionMode.WRITER, true); boolean added = internalAddToContext(); @@ -718,9 +730,8 @@ public abstract class ERManagement { accessType.getName(), uuid); try { - orientGraph = SecurityContextMapper.getSecurityContextGraph( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER); + orientGraph = ContextUtility.getActualSecurityContextGraph( + PermissionMode.WRITER, true); boolean removed = internalRemoveFromContext(); @@ -852,7 +863,7 @@ public abstract class ERManagement { map.put(key, object); } } 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 { String error = String.format( "Error while setting property %s : %s (%s)", key, properties .get(key).toString(), e.getMessage()); - logger.error(error); + staticLogger.error(error); throw new ResourceRegistryException(error, e); } } @@ -1010,7 +1021,7 @@ public abstract class ERManagement { } } - protected Collection getSuperclasses() throws SchemaException{ + protected Collection getSuperclasses() throws SchemaException, ResourceRegistryException { Collection allSuperClasses = getOClass().getAllSuperClasses(); Collection superClasses = new HashSet<>(); for(OClass oSuperClass : allSuperClasses){ diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java index dbe6a17..573168c 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/EntityManagement.java @@ -36,7 +36,7 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertexType; public abstract class EntityManagement extends ERManagement { - private static Logger logger = LoggerFactory + private static Logger staticLogger = LoggerFactory .getLogger(EntityManagement.class); protected EntityManagement(AccessType accessType) { @@ -60,6 +60,25 @@ public abstract class EntityManagement extends 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, ResourceRegistryException { @@ -190,7 +209,7 @@ public abstract class EntityManagement extends orientVertexType = ((OrientVertex) vertex).getType(); }catch (Exception e) { 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); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java index e5bc821..23de068 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/entity/ResourceManagement.java @@ -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.RelationManagement; import org.gcube.informationsystem.resourceregistry.utils.Utility; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.JsonNode; import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; @@ -43,9 +41,6 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertexType; */ public class ResourceManagement extends EntityManagement { - private static Logger logger = LoggerFactory - .getLogger(ResourceManagement.class); - public ResourceManagement() { super(AccessType.RESOURCE); } @@ -105,36 +100,14 @@ public class ResourceManagement extends EntityManagement { 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, 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, JSONObject isRelatedTo) throws ResourceRegistryException { - return addRelation(sourceResource, isRelatedTo, AccessType.IS_RELATED_TO); + return addRelation(sourceResource, isRelatedTo, AccessType.IS_RELATED_TO.lowerCaseFirstCharacter()); } @Override @@ -238,7 +211,7 @@ public class ResourceManagement extends EntityManagement { public String all(boolean polymorphic, Map constraint) throws ResourceRegistryException { try { orientGraph = ContextUtility - .getActualSecurityContextGraph(PermissionMode.READER); + .getActualSecurityContextGraph(PermissionMode.READER, forceAdmin); return reallyGetAll(polymorphic, constraint); } catch (ResourceRegistryException e) { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java index c7cb1b9..6aa312b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/er/relation/RelationManagement.java @@ -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.relation.RelationNotFoundException; 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.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement; import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement; import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement; import org.gcube.informationsystem.resourceregistry.utils.Utility; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.JsonNode; import com.tinkerpop.blueprints.Direction; @@ -54,9 +51,6 @@ import com.tinkerpop.blueprints.impls.orient.OrientGraph; public abstract class RelationManagement extends ERManagement { - private static Logger logger = LoggerFactory - .getLogger(RelationManagement.class); - protected final Class targetEntityClass; protected RelationManagement(AccessType accessType) { @@ -622,7 +616,7 @@ public abstract class RelationManagement extends throws ResourceRegistryException { try { orientGraph = ContextUtility - .getActualSecurityContextGraph(PermissionMode.WRITER); + .getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin); element = reallyCreate(sourceUUID, targetUUID); @@ -704,7 +698,7 @@ public abstract class RelationManagement extends throws ResourceRegistryException { try { orientGraph = ContextUtility - .getActualSecurityContextGraph(PermissionMode.READER); + .getActualSecurityContextGraph(PermissionMode.READER, forceAdmin); return reallyGetAllFrom(uuid, direction, polymorphic); } catch (ResourceRegistryException e) { @@ -724,9 +718,8 @@ public abstract class RelationManagement extends accessType.getName(), uuid); try { - orientGraph = SecurityContextMapper.getSecurityContextGraph( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER); + orientGraph = ContextUtility.getActualSecurityContextGraph( + PermissionMode.WRITER, true); boolean added = forcedAddToContext(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java index 0d18719..d98381d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/Access.java @@ -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.rest.AccessPath; 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.er.ERManagement; import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement; @@ -298,7 +298,7 @@ public class Access { public String getContext(@PathParam(ID_PATH_PARAM) String uuid) throws ContextNotFoundException, ContextException { 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)); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java index e80ec7d..270c0f8 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/rest/ContextManager.java @@ -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.ContextNotFoundException; 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.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,7 +43,7 @@ public class ContextManager { 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 diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaContextManagement.java index f24feb5..25ccbd9 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaContextManagement.java @@ -8,7 +8,7 @@ import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException; 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.types.TypeBinder.TypeDefinition; import org.slf4j.Logger; @@ -55,8 +55,7 @@ public class SchemaContextManagement implements SchemaManagement { OrientGraph orientGraph = null; try { - orientGraph = SecurityContextMapper - .getSecurityContextGraph(SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER); + orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.WRITER, true); ObjectMapper mapper = new ObjectMapper(); TypeDefinition typeDefinition = mapper.readValue(json, TypeDefinition.class); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java index fc9702e..d6e2301 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/schema/SchemaManagementImpl.java @@ -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.SchemaException; 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.types.TypeBinder; import org.gcube.informationsystem.types.TypeBinder.Property; @@ -86,18 +86,18 @@ public class SchemaManagementImpl implements SchemaManagement { } public static OClass getTypeSchema(String type, AccessType accessType) - throws SchemaException { + throws SchemaException, ResourceRegistryException { OrientGraphNoTx orientGraphNoTx = null; try { logger.debug("Getting {} Type {} schema", accessType != null ? accessType.getName() : "", type); - orientGraphNoTx = SecurityContextMapper.getSecurityContextGraphNoTx( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.READER); + orientGraphNoTx = ContextUtility.getActualSecurityContextGraphNoTx( + PermissionMode.READER, true); return getTypeSchema(orientGraphNoTx, type, accessType); - + } catch (ResourceRegistryException e) { + throw e; } finally { if (orientGraphNoTx != null) { orientGraphNoTx.shutdown(); @@ -178,9 +178,8 @@ public class SchemaManagementImpl implements SchemaManagement { TypeDefinition typeDefinition = mapper.readValue(jsonSchema, TypeDefinition.class); - orientGraphNoTx = SecurityContextMapper.getSecurityContextGraphNoTx( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER); + orientGraphNoTx = ContextUtility.getActualSecurityContextGraphNoTx( + PermissionMode.WRITER, true); OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata(); OSchema oSchema = oMetadata.getSchema(); @@ -337,9 +336,8 @@ public class SchemaManagementImpl implements SchemaManagement { throws SchemaNotFoundException, SchemaException { OrientGraphNoTx orientGraphNoTx = null; try { - orientGraphNoTx = SecurityContextMapper.getSecurityContextGraphNoTx( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, - PermissionMode.WRITER); + orientGraphNoTx = ContextUtility.getActualSecurityContextGraphNoTx( + PermissionMode.WRITER, true); OMetadata oMetadata = orientGraphNoTx.getRawGraph().getMetadata(); OSchema oSchema = oMetadata.getSchema(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/utils/Utility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/utils/Utility.java index c5d65be..93b8709 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/utils/Utility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/utils/Utility.java @@ -17,7 +17,7 @@ import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; 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.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -92,8 +92,7 @@ public class Utility { Class clz) throws ERNotFoundException, ResourceRegistryException { OrientGraphNoTx orientGraphNoTx = null; try { - orientGraphNoTx = SecurityContextMapper.getSecurityContextGraphNoTx( - SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.READER); + orientGraphNoTx = ContextUtility.getActualSecurityContextGraphNoTx(PermissionMode.READER, true); return Utility.getElementByUUID(orientGraphNoTx, elementType, uuid, clz); } finally { if (orientGraphNoTx != null) { diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementTest.java new file mode 100644 index 0000000..d1f1ac2 --- /dev/null +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/context/ContextManagementTest.java @@ -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 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> children = context.getChildren(); + for(IsParentOf 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)); + + } + +}