From 93436452365903fdc327836f0bf00a7ac3a87edb Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Mon, 12 Sep 2016 12:45:40 +0000 Subject: [PATCH] Creating resource in the proper way git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@131287 82a268e6-3cf1-43bd-a215-b396298e98cf --- .settings/org.eclipse.wst.common.component | 4 +- pom.xml | 2 +- .../resources/impl/EntityManagementImpl.java | 537 +++++++++++++++--- .../resources/utils/ContextUtility.java | 8 +- .../resources/utils/HeaderUtility.java | 12 +- .../resources/utils/Utility.java | 25 +- .../impl/ContextManagementImplTest.java | 13 +- .../impl/EntityManagementImplTest.java | 5 +- 8 files changed, 494 insertions(+), 112 deletions(-) diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 1b3ac0a..9fb1bc0 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -4,13 +4,13 @@ - + uses uses - + uses diff --git a/pom.xml b/pom.xml index 9ec454b..b1d9e02 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.gcube.information-system resource-registry - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT Resource Registry Resource Registry war diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java index b372b12..7e502f5 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImpl.java @@ -4,13 +4,21 @@ package org.gcube.informationsystem.resourceregistry.resources.impl; import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import org.gcube.informationsystem.impl.utils.Entities; +import org.gcube.informationsystem.model.embedded.Embedded; import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.entity.Facet; import org.gcube.informationsystem.model.entity.Resource; +import org.gcube.informationsystem.model.orientdb.impl.embedded.Header; import org.gcube.informationsystem.model.relation.ConsistsOf; import org.gcube.informationsystem.model.relation.IsRelatedTo; import org.gcube.informationsystem.model.relation.Relation; @@ -26,9 +34,12 @@ import org.gcube.informationsystem.resourceregistry.resources.utils.Utility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.JsonNodeType; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientEdge; @@ -94,15 +105,261 @@ public class EntityManagementImpl implements EntityManagement { } - public String createVertexEntity(String entityType, - Class entity, String jsonRepresentation) - throws ResourceRegistryException { - OrientGraph orientGraph = null; + private static String lowerCaseFirstCharacter(String string) { + return string.substring(0, 1).toLowerCase() + string.substring(1); + } + private static String getClassProperty(JsonNode jsonNode) { + if (jsonNode.has(Entities.CLASS_PROPERTY)) { + return jsonNode.get(Entities.CLASS_PROPERTY).asText(); + } + return null; + } + + private static Header getHeader(JsonNode jsonNode) + throws JsonParseException, JsonMappingException, IOException { + if (jsonNode.has(Resource.HEADER_PROPERTY)) { + JsonNode header = jsonNode.get(Resource.HEADER_PROPERTY); + if (header.isNull()) { + return null; + } + ObjectMapper mapper = new ObjectMapper(); + return mapper.readValue(header.toString(), Header.class); + } + return null; + } + + private static Entity checkTypeAndGetHeader(JsonNode jsonNode) throws ResourceRegistryException { + if(jsonNode.has(Entities.CLASS_PROPERTY)){ + // Complex type + String type = getClassProperty(jsonNode); + + + SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); + try { + schemaManagement.getTypeSchema(type, Embedded.NAME); + } catch (SchemaNotFoundException e) { + throw e; + } + boolean validType = false; + boolean notEmbedded = false; + + + + // Header can be ignored or managed. Actually is managed. + if(validType && notEmbedded){ + Header header = null; + try { + header = getHeader(jsonNode); + } catch (Exception e){ + logger.warn("An invalid Header has been provided. An embedded object cannot have an Header. It will be ignored."); + //throw new ResourceRegistryException("An embedded object cannot have an Header"); + } + + if(header!=null){ + logger.warn("An embedded object cannot have an Header. It will be ignored."); + //throw new ResourceRegistryException("An embedded object cannot have an Header"); + } + } + } + + return null; + } + + public static Object getObejctFromElement(JsonNode value) throws ResourceRegistryException { + JsonNodeType jsonNodeType = value.getNodeType(); + switch (jsonNodeType) { + case OBJECT: + // TODO + checkTypeAndGetHeader(value); + return null; + + case ARRAY: + List array = new ArrayList<>(); + Iterator arrayElement = value.elements(); + while(arrayElement.hasNext()){ + JsonNode arrayNode = arrayElement.next(); + Object objectNode = getObejctFromElement(arrayNode); + if(objectNode!=null){ + array.add(objectNode); + } + } + return array; + + case BINARY: + break; + + case BOOLEAN: + return value.asBoolean(); + + case NULL: + break; + + case NUMBER: + if(value.isDouble() || value.isFloat()){ + return value.asDouble(); + } + if(value.isBigInteger() || value.isShort() || value.isInt()){ + return value.asInt(); + } + + if(value.isLong()){ + return value.asLong(); + } + break; + + case STRING: + return value.asText(); + + case MISSING: + break; + + case POJO: + break; + + default: + break; + } + + return null; + } + + + public static Map getPropertyMap(JsonNode jsonNode, + Set ignoreKeys) throws JsonProcessingException, IOException{ + + Map map = new HashMap<>(); + + Iterator> fields = jsonNode.fields(); + while (fields.hasNext()) { + Entry entry = fields.next(); + + String key = entry.getKey(); + if (key.compareTo(Relation.HEADER_PROPERTY) == 0) { + continue; + } + if (key.startsWith("@") || key.startsWith("_")) { + continue; + } + if (ignoreKeys.contains(key)) { + continue; + } + + JsonNode value = entry.getValue(); + Object object = null; + try { + object = getObejctFromElement(value); + if(object!=null){ + map.put(key, object); + } + } catch (ResourceRegistryException e) { + logger.warn("An invalidy property has been provided. It will be ignored."); + } + + + + } + + return map; + } + + private void createRelation(OrientGraph orientGraph, Vertex resource, JsonNode relationArray, + @SuppressWarnings("rawtypes") Class relation) throws FacetNotFoundException, + ResourceNotFoundException, ResourceRegistryException { + + Iterator iterator = relationArray.elements(); + while (iterator.hasNext()) { + JsonNode node = iterator.next(); + + /* Managing Target */ + JsonNode target = node.get(Relation.TARGET_PROPERTY); + + Header targetHeader = null; + try { + targetHeader = getHeader(target); + } catch (IOException e) { + new ResourceRegistryException(e); + } + + Class targetClass = null; + + + Vertex targetVertex = null; + if (targetHeader == null) { + if (ConsistsOf.class.isAssignableFrom(relation)) { + targetVertex = createVertexEntity( + orientGraph, getClassProperty(target), + Facet.class, + target.toString(), true); + targetClass = Facet.class; + targetHeader = targetVertex.getProperty(Facet.HEADER_PROPERTY); + } else { + String error = String.format( + "%s %s must already exist. The UUID must be provided in the %s of %s json respresentation", + Relation.TARGET_PROPERTY, Resource.NAME, + Header.NAME, IsRelatedTo.NAME); + logger.error(error); + throw new ResourceRegistryException(error); + } + }else { + // The target Entity was already created we just need to create + // the right relation + + if(ConsistsOf.class.isAssignableFrom(relation)) { + targetClass = Facet.class; + }else if(IsRelatedTo.class.isAssignableFrom(relation)){ + targetClass = Resource.class; + }else{ + String error = String.format( + "%s Unsupported %s creation", relation.toString(), + Relation.NAME); + logger.error(error); + throw new ResourceRegistryException(error); + } + + String targetUUID = targetHeader.getUUID().toString(); + String entityType = getClassProperty(target); + + targetVertex = getEntity(orientGraph, targetUUID, entityType, targetClass); + } + + String relationType = getClassProperty(node); + + + Set ignoreKeys = new HashSet<>(); + ignoreKeys.add(Relation.TARGET_PROPERTY); + ignoreKeys.add(Relation.SOURCE_PROPERTY); + + Map edgeProperties = null; + try { + edgeProperties = getPropertyMap(node, ignoreKeys); + }catch(Exception e){ + String error = "Error while parsing json to get Relation properties"; + logger.error(error, e); + throw new ResourceRegistryException(error, e); + } + + createEdgeRelation(orientGraph, resource, targetVertex, + relationType, relation, edgeProperties, true); + + } + } + + public Vertex createVertexEntity(String entityType, + Class entity, String jsonRepresentation, + boolean deferredCommit) throws ResourceRegistryException { + OrientGraph orientGraph = ContextUtility + .getActualSecurityContextGraph(PermissionMode.WRITER); + return createVertexEntity(orientGraph, entityType, entity, jsonRepresentation, + deferredCommit); + } + + public Vertex createVertexEntity(OrientGraph orientGraph, String entityType, + Class entity, String jsonRepresentation, + boolean deferredCommit) throws ResourceRegistryException { + try { - orientGraph = ContextUtility - .getActualSecurityContextGraph(PermissionMode.WRITER); - + SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); try { schemaManagement.getTypeSchema(entityType, @@ -113,33 +370,58 @@ public class EntityManagementImpl implements EntityManagement { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(jsonRepresentation); + + String type = getClassProperty(jsonNode); + if (type != null && type.compareTo(entityType) != 0) { + String error = String + .format("Declared resourceType does not match with json representation %s!=%s", + entityType, type); + logger.trace(error); + throw new ResourceRegistryException(error); + } - OrientVertex entityVertex = orientGraph.addVertex("class:" + OrientVertex vertex = orientGraph.addVertex("class:" + entityType); + Header entityHeader = getHeader(jsonNode); + if (entityHeader != null) { + vertex.setProperty(Entity.HEADER_PROPERTY, entityHeader); + } else { + entityHeader = HeaderUtility.addHeader(vertex, null); + } + if (Resource.class.isAssignableFrom(entity)) { - // TODO + // Facet and relation are created in calling method } else { Iterator> iterator = jsonNode.fields(); while (iterator.hasNext()) { Entry entry = iterator.next(); + if (entry.getKey().compareTo(Facet.HEADER_PROPERTY) == 0) { + continue; + } + if (entry.getKey().startsWith("@") + || entry.getKey().startsWith("_")) { + continue; + } JsonNode value = entry.getValue(); - entityVertex.setProperty(entry.getKey(), value.asText()); + vertex.setProperty(entry.getKey(), value.asText()); } } - HeaderUtility.addHeader(entityVertex, null); - ContextUtility.addToActualContext(orientGraph, entityVertex); + ContextUtility.addToActualContext(orientGraph, vertex); - entityVertex.save(); - orientGraph.commit(); + vertex.save(); - logger.trace("Created {} is {} orientVertexToJsonString", Vertex.class.getSimpleName(), - Utility.orientVertexToJsonString((OrientVertex) entityVertex, true)); - - return Utility.orientVertexToJsonString((OrientVertex) entityVertex, false); - //return Utility.orientVertexToJsonString((OrientVertex) entityVertex); - + if (!deferredCommit) { + orientGraph.commit(); + + logger.trace("Created {} is {} orientVertexToJsonString", + Vertex.class.getSimpleName(), Utility + .orientVertexToJsonString( + (OrientVertex) vertex, true)); + } + + return vertex; } catch (Exception e) { if (orientGraph != null) { @@ -148,17 +430,19 @@ public class EntityManagementImpl implements EntityManagement { throw new ResourceRegistryException("Error Creating " + entityType + " with " + jsonRepresentation, e.getCause()); } finally { - if (orientGraph != null) { + if (orientGraph != null && !deferredCommit) { orientGraph.shutdown(); } } } - public String createEdgeRelation(String sourceUUID, - Class sourceClass, String targetUUID, - Class targetClass, String relationType, - String jsonProperties) throws FacetNotFoundException, - ResourceNotFoundException, ResourceRegistryException { + public Edge createEdgeRelation( + String sourceUUID, Class sourceClass, + String targetUUID, Class targetClass, + String relationType, @SuppressWarnings("rawtypes") Class relationBaseClass, + String jsonProperties) + throws FacetNotFoundException, ResourceNotFoundException, + ResourceRegistryException { OrientGraph orientGraph = null; if (relationType == null || relationType.compareTo("") == 0) { @@ -170,20 +454,63 @@ public class EntityManagementImpl implements EntityManagement { orientGraph = ContextUtility .getActualSecurityContextGraph(PermissionMode.WRITER); - SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); - try { - schemaManagement.getTypeSchema(relationType, Relation.NAME); - } catch (SchemaNotFoundException e) { - throw e; - } - Vertex source = getEntity(orientGraph, sourceUUID, null, sourceClass); Vertex target = getEntity(orientGraph, targetUUID, null, targetClass); + + Map edgeProperties = new HashMap<>(); + + if (jsonProperties != null && jsonProperties.compareTo("") != 0) { + try { + Set ignoreKeys = new HashSet<>(); + ignoreKeys.add(Relation.SOURCE_PROPERTY); + ignoreKeys.add(Relation.TARGET_PROPERTY); + + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonNode = mapper.readTree(jsonProperties); + + edgeProperties = getPropertyMap(jsonNode, ignoreKeys); + + } catch (Exception e) { + new ResourceRegistryException( + "Error while setting Relation Properties", e); + } + } + + return createEdgeRelation(orientGraph, source, target, + relationType, relationBaseClass, edgeProperties, false); + + } catch(ResourceNotFoundException rnfe){ + throw rnfe; + } catch (Exception e) { + throw new ResourceRegistryException(e); + } + + } + + public Edge createEdgeRelation(OrientGraph orientGraph, + Vertex source, Vertex target, + String relationType, @SuppressWarnings("rawtypes") Class relationBaseClass, + Map edgeProperties, + boolean deferredCommit) throws ResourceRegistryException { + - // TODO Check if in and out types are compatible with the relation - // type as defined in relation type + try { + + SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); + try { + schemaManagement.getTypeSchema(relationType, relationBaseClass.getClass().getSimpleName()); + } catch (SchemaNotFoundException e) { + throw e; + } + + if (relationType == null || relationType.compareTo("") == 0) { + throw new ResourceRegistryException(Relation.class.getSimpleName() + + " Type cannot be empty or null"); + } + + // TODO Check the relation compatibility beetween source and target logger.trace("Creating {} ({}) beetween {} -> {}", Relation.class.getSimpleName(), relationType, @@ -192,43 +519,39 @@ public class EntityManagementImpl implements EntityManagement { Edge edge = orientGraph.addEdge(null, source, target, relationType); - if (jsonProperties != null && jsonProperties.compareTo("") != 0) { + for(String key : edgeProperties.keySet()){ try { - ObjectMapper mapper = new ObjectMapper(); - JsonNode jsonNode = mapper.readTree(jsonProperties); - - Iterator> iterator = jsonNode - .fields(); - while (iterator.hasNext()) { - Entry entry = iterator.next(); - try { - JsonNode value = entry.getValue(); - edge.setProperty(entry.getKey(), value.asText()); - } catch (Exception e) { - throw new ResourceRegistryException( - "Error while setting property" - + String.valueOf(entry), e); - } - } + edge.setProperty(key, edgeProperties.get(key)); } catch (Exception e) { - new ResourceRegistryException( - "Error while setting Relation Properties", e); + String error = String.format("Error while setting property %s : %s", key, edgeProperties.get(key).toString()); + logger.error(error); + throw new ResourceRegistryException(error, e); } } + HeaderUtility.addHeader(edge, null); ContextUtility.addToActualContext(orientGraph, edge); ((OrientEdge) edge).save(); - orientGraph.commit(); - return Utility.orientEdgeToJsonString((OrientEdge) edge); - } catch (ResourceNotFoundException rnfe) { - throw rnfe; + if (!deferredCommit) { + orientGraph.commit(); + } + return edge; + + } catch (ResourceRegistryException rre) { + if (orientGraph!=null) { + orientGraph.rollback(); + } + throw rre; } catch (Exception e) { - throw new ResourceNotFoundException(e.getMessage()); + if (orientGraph!=null) { + orientGraph.rollback(); + } + throw new ResourceRegistryException(e); } finally { - if (orientGraph != null) { + if (orientGraph!= null && !deferredCommit) { orientGraph.shutdown(); } } @@ -237,7 +560,8 @@ public class EntityManagementImpl implements EntityManagement { @Override public String createFacet(String facetType, String jsonRepresentation) throws ResourceRegistryException { - return createVertexEntity(facetType, Facet.class, jsonRepresentation); + Vertex vertex = createVertexEntity(facetType, Facet.class, jsonRepresentation, false); + return Utility.orientVertexToJsonString((OrientVertex) vertex, false); } @Override @@ -257,8 +581,10 @@ public class EntityManagementImpl implements EntityManagement { Vertex facet = getEntity(orientGraph, uuid, facetType, Facet.class); - logger.trace("{} of type {} with UUID {} is {}", Facet.NAME, facetType, uuid, Utility.orientVertexToJsonString((OrientVertex) facet, true)); - + logger.trace("{} of type {} with UUID {} is {}", Facet.NAME, + facetType, uuid, Utility.orientVertexToJsonString( + (OrientVertex) facet, true)); + return Utility.orientVertexToJsonString((OrientVertex) facet, true); } catch (FacetNotFoundException fnfe) { throw fnfe; @@ -322,11 +648,12 @@ public class EntityManagementImpl implements EntityManagement { ((OrientVertex) facet).save(); orientGraph.commit(); - - logger.trace("{} with UUID {} has been updated {}", Facet.NAME, uuid, Utility.orientVertexToJsonString((OrientVertex) facet, true)); - - - return Utility.orientVertexToJsonString((OrientVertex) facet, false); + logger.trace("{} with UUID {} has been updated {}", Facet.NAME, + uuid, Utility.orientVertexToJsonString( + (OrientVertex) facet, true)); + + return Utility + .orientVertexToJsonString((OrientVertex) facet, false); } catch (FacetNotFoundException fnfe) { if (orientGraph != null) { @@ -383,8 +710,11 @@ public class EntityManagementImpl implements EntityManagement { String consistOfType, String jsonProperties) throws FacetNotFoundException, ResourceNotFoundException, ResourceRegistryException { - return createEdgeRelation(resourceUUID, Resource.class, facetUUID, - Facet.class, consistOfType, jsonProperties); + Edge edge = createEdgeRelation(resourceUUID, Resource.class, + facetUUID, Facet.class, + consistOfType, ConsistsOf.class, + jsonProperties); + return Utility.orientEdgeToJsonString((OrientEdge) edge, false); } @Override @@ -396,8 +726,8 @@ public class EntityManagementImpl implements EntityManagement { orientGraph = ContextUtility .getActualSecurityContextGraph(PermissionMode.WRITER); - Edge edge = getRelation(orientGraph, consistOfUUID, ConsistsOf.NAME, - ConsistsOf.class); + Edge edge = getRelation(orientGraph, consistOfUUID, + ConsistsOf.NAME, ConsistsOf.class); edge.remove(); orientGraph.commit(); @@ -426,9 +756,11 @@ public class EntityManagementImpl implements EntityManagement { String targetResourceUuid, String relatedToType, String jsonProperties) throws ResourceNotFoundException, ResourceRegistryException { - return createEdgeRelation(sourceResourceUuid, Resource.class, - targetResourceUuid, Resource.class, relatedToType, + Edge edge = createEdgeRelation(sourceResourceUuid, Resource.class, + targetResourceUuid, Resource.class, + relatedToType, IsRelatedTo.class, jsonProperties); + return Utility.orientEdgeToJsonString((OrientEdge) edge, false); } @Override @@ -440,8 +772,8 @@ public class EntityManagementImpl implements EntityManagement { orientGraph = ContextUtility .getActualSecurityContextGraph(PermissionMode.WRITER); - Edge edge = getRelation(orientGraph, relatedToUUID, IsRelatedTo.NAME, - IsRelatedTo.class); + Edge edge = getRelation(orientGraph, relatedToUUID, + IsRelatedTo.NAME, IsRelatedTo.class); edge.remove(); orientGraph.commit(); @@ -464,18 +796,49 @@ public class EntityManagementImpl implements EntityManagement { return true; } + private static String marshallResource(Vertex vertex){ + // TODO serialize all facets as weel + return Utility.orientVertexToJsonString((OrientVertex) vertex, false); + } + + @Override public String createResource(String resourceType, String jsonRepresentation) throws ResourceRegistryException { + + logger.trace("Trying to create {} : {}", resourceType, + jsonRepresentation); + + OrientGraph orientGraph; try { - JsonNode jsonNode = org.gcube.informationsystem.impl.utils.Utility.getJSONNode(jsonRepresentation); - } catch (IOException e) { + orientGraph = ContextUtility + .getActualSecurityContextGraph(PermissionMode.WRITER); + + Vertex resource = createVertexEntity(orientGraph, resourceType, Resource.class, jsonRepresentation, true); + + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonNode = mapper.readTree(jsonRepresentation); + + String property = lowerCaseFirstCharacter(ConsistsOf.NAME); + if (jsonNode.has(property)) { + JsonNode jsonNodeArray = jsonNode.get(property); + createRelation(orientGraph, resource, jsonNodeArray, ConsistsOf.class); + } + + property = lowerCaseFirstCharacter(IsRelatedTo.NAME); + if (jsonNode.has(property)) { + JsonNode jsonNodeArray = jsonNode.get(property); + createRelation(orientGraph, resource, jsonNodeArray, IsRelatedTo.class); + } + + return marshallResource(resource); + + } catch(ResourceRegistryException rre) { + throw rre; + } catch(Exception e){ throw new ResourceRegistryException(e); } - - return createVertexEntity(resourceType, Resource.class, - jsonRepresentation); } @Override @@ -495,11 +858,11 @@ public class EntityManagementImpl implements EntityManagement { Vertex resource = getEntity(orientGraph, uuid, resourceType, Resource.class); - // TODO get all attached facets - logger.trace("{} of type {} with UUID {} is {}", Resource.NAME, resourceType, uuid, Utility.orientVertexToJsonString((OrientVertex) resource, true)); - - - return Utility.orientVertexToJsonString((OrientVertex) resource, false); + logger.trace("{} of type {} with UUID {} is {}", Resource.NAME, + resourceType, uuid, Utility.orientVertexToJsonString( + (OrientVertex) resource, true)); + + return marshallResource(resource); } catch (ResourceNotFoundException rnfe) { throw rnfe; } catch (Exception e) { @@ -523,6 +886,8 @@ public class EntityManagementImpl implements EntityManagement { Vertex resource = getEntity(orientGraph, uuid, null, Resource.class); // TODO remove attached facets if not managed from hooks + + resource.remove(); orientGraph.commit(); diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/ContextUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/ContextUtility.java index 65ad292..4a6e441 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/ContextUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/ContextUtility.java @@ -9,6 +9,7 @@ import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.impl.ScopeBean; import org.gcube.informationsystem.model.orientdb.impl.entity.Context; import org.gcube.informationsystem.model.orientdb.impl.entity.Entity; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException; import org.gcube.informationsystem.resourceregistry.dbinitialization.SecurityContext; @@ -77,15 +78,18 @@ public class ContextUtility { } public static OrientGraph getActualSecurityContextGraph( - PermissionMode permissionMode) throws Exception { + PermissionMode permissionMode) throws ResourceRegistryException { try { String contextID = getActualContextUUID(); OrientGraphFactory factory = SecurityContextMapper .getSecurityContextFactory(contextID, permissionMode); return factory.getTx(); + } catch (ContextException ce) { + logger.error("Unable to retrieve context.", ce); + throw ce; } catch (Exception e) { logger.error("Unable to retrieve context.", e); - throw e; + throw new ResourceRegistryException(e); } } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/HeaderUtility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/HeaderUtility.java index 5487b94..cc457c3 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/HeaderUtility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/HeaderUtility.java @@ -9,7 +9,8 @@ import java.util.Date; import java.util.UUID; import org.gcube.common.authorization.library.provider.AuthorizationProvider; -import org.gcube.common.authorization.library.provider.UserInfo; +import org.gcube.common.authorization.library.provider.ClientInfo; +import org.gcube.common.authorization.library.utils.Caller; import org.gcube.informationsystem.model.orientdb.impl.embedded.Header; import org.gcube.informationsystem.model.orientdb.impl.entity.Entity; import org.gcube.informationsystem.model.orientdb.impl.relation.Relation; @@ -39,10 +40,11 @@ public class HeaderUtility { String creator = org.gcube.informationsystem.model.embedded.Header.UNKNOWN_USER; try { - UserInfo userInfo = AuthorizationProvider.instance.get(); - String username = userInfo.getUserName(); - if (username != null && username.compareTo("") != 0) { - creator = username; + Caller caller = AuthorizationProvider.instance.get(); + ClientInfo clientInfo = caller.getClient(); + String clientId = clientInfo.getId(); + if (clientId != null && clientId.compareTo("") != 0) { + creator = clientId; } else { throw new Exception("Username null or empty"); } diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java index 8cf0b5d..69b8105 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/utils/Utility.java @@ -35,9 +35,19 @@ public class Utility { private static final Logger logger = LoggerFactory.getLogger(Utility.class); - public static String orientVertexToJsonString(OrientVertex orientVertex, boolean raw) { + public static String orientVertexToJsonString(OrientVertex orientVertex, + boolean raw) { ORecord oRecord = orientVertex.getRecord(); - if(raw){ + if (raw) { + return oRecord.toJSON(); + } + return oRecord.toJSON("class"); + } + + public static String orientEdgeToJsonString(OrientEdge orientEdge, + boolean raw) { + ORecord oRecord = orientEdge.getRecord(); + if (raw) { return oRecord.toJSON(); } return oRecord.toJSON("class"); @@ -68,11 +78,6 @@ public class Utility { } } - public static String orientEdgeToJsonString(OrientEdge orientEdge) { - ORecord oRecord = orientEdge.getRecord(); - return oRecord.toJSON(); - } - public static String edgeToJsonString(Edge edge, boolean raw) throws JSONException { if (raw) { @@ -124,10 +129,10 @@ public class Utility { Iterator iterator = vertexes.iterator(); Vertex entity = iterator.next(); - + logger.trace("{} with {} is : {}", entityType, uuid, Utility.vertexToJsonString(entity)); - + if (iterator.hasNext()) { throw new ResourceRegistryException("Found more than one " + entityType + " with uuid " + uuid @@ -162,7 +167,7 @@ public class Utility { Iterator iterator = edges.iterator(); Edge relation = iterator.next(); - logger.trace("{} with {} is : {}", relationType, uuid, + logger.trace("{} with {} is : {}", relationType, uuid, Utility.edgeToJsonString(relation)); if (iterator.hasNext()) { diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/ContextManagementImplTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/ContextManagementImplTest.java index f533fa7..9a452e6 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/ContextManagementImplTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/ContextManagementImplTest.java @@ -155,8 +155,8 @@ public class ContextManagementImplTest { } - @Test - public void devContextTest() throws ContextNotFoundException, ContextException, InternalException { + //@Test + public void createDevContext() throws ContextNotFoundException, ContextException, InternalException { String gcube = contextManagementImpl.create(null, "gcube"); logger.trace("/gcube : {}", gcube); String devsec = contextManagementImpl.create(gcube, "devsec"); @@ -179,6 +179,15 @@ public class ContextManagementImplTest { logger.debug("The DB should be now clean"); } + //@Test + public void removeDevContext() throws ContextNotFoundException, ContextException, InternalException { + contextManagementImpl.delete("508da10a-b8e7-414f-b176-65e227addcae"); + contextManagementImpl.delete("d729c993-32f9-4bcd-9311-d92f791e5e12"); + contextManagementImpl.delete("8bf286f8-a93c-4936-9826-91f1b1c2af34"); + contextManagementImpl.delete("7273c1c7-bc06-4d49-bade-19dc3ab322f7"); + contextManagementImpl.delete("132691d2-e22d-4e3b-b605-f8b2c94f2c77"); + } + @Test public void readTest() throws ContextNotFoundException, ContextException, InternalException { String A_1 = contextManagementImpl.create(null, "LLL"); diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImplTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImplTest.java index 88b81cf..7085fc3 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImplTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/resources/impl/EntityManagementImplTest.java @@ -233,10 +233,7 @@ public class EntityManagementImplTest { hostingNode.addFacet(cpuFacetImpl); - StringWriter stringWriter = new StringWriter(); - Entities.marshal(hostingNode, stringWriter); - - String json = entityManagementImpl.createResource(HostingNode.NAME, stringWriter.toString()); + String json = entityManagementImpl.createResource(HostingNode.NAME, Entities.marshal(hostingNode)); logger.debug("Created : {}", json); }