From cfa9c0ed326710e123da8ce73d93bbadfe2744db Mon Sep 17 00:00:00 2001 From: "luca.frosini" Date: Thu, 17 Nov 2016 16:40:46 +0000 Subject: [PATCH] Added logs to better understanding what happen in case of failure git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@134334 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../resources/EntityManager.java | 20 +- .../resources/impl/EntityManagementImpl.java | 631 +++++++++++------- 2 files changed, 391 insertions(+), 260 deletions(-) diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/EntityManager.java b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/EntityManager.java index d203664..af8cf2b 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/EntityManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/EntityManager.java @@ -62,7 +62,7 @@ public class EntityManager { public String createFacet(@PathParam(TYPE_PATH_PARAM) String type, @QueryParam(EntityPath.DEFINITION_PARAM) String definition) throws EntityException, ResourceRegistryException { - logger.trace("requested facet creation for type {} defined by {} ", + logger.info("requested facet creation for type {} defined by {} ", type, definition); return entityManager.createFacet(type, definition); } @@ -84,7 +84,7 @@ public class EntityManager { public String updateFacet(@PathParam(ID_PATH_PARAM) String uuid, @QueryParam(EntityPath.DEFINITION_PARAM) String definition) throws FacetNotFoundException, ResourceRegistryException { - logger.trace("requested facet update for id {} with {}", uuid, + logger.info("requested facet update for id {} with {}", uuid, definition); return entityManager.updateFacet(UUID.fromString(uuid), definition); } @@ -102,7 +102,7 @@ public class EntityManager { @Path(EntityPath.FACET_PATH_PART + "/{" + ID_PATH_PARAM + "}") public boolean deleteFacet(@PathParam(ID_PATH_PARAM) String uuid) throws FacetNotFoundException, ResourceRegistryException { - logger.trace("Requested to delete Facet with id {}", uuid); + logger.info("Requested to delete Facet with id {}", uuid); return entityManager.deleteFacet(UUID.fromString(uuid)); } @@ -124,7 +124,7 @@ public class EntityManager { public String createResource(@PathParam(TYPE_PATH_PARAM) String type, @QueryParam(EntityPath.DEFINITION_PARAM) String definition) throws FacetNotFoundException, ResourceRegistryException { - logger.trace("requested resource creation for type {} with json {}", + logger.info("requested resource creation for type {} with json {}", type, definition); return entityManager.createResource(type, definition); } @@ -175,7 +175,7 @@ public class EntityManager { @QueryParam(EntityPath.PROPERTIES_PARAM) String properties) throws FacetNotFoundException, ResourceNotFoundException, ResourceRegistryException { - logger.trace( + logger.info( "requested to attach resource {} to facet {} ({} Type {}) with properties {}", resourceUUID, facetUUID, ConsistsOf.class.getSimpleName(), type, properties); @@ -195,7 +195,7 @@ public class EntityManager { @Path(EntityPath.CONSISTS_OF_PATH_PART + "/{" + ID_PATH_PARAM + "}") public boolean detachFacet(@PathParam(ID_PATH_PARAM) String consistOfUUID) throws ResourceRegistryException { - logger.trace("requested to detach {}", consistOfUUID); + logger.info("requested to detach {}", consistOfUUID); return entityManager.detachFacet(UUID.fromString(consistOfUUID)); } @@ -226,7 +226,7 @@ public class EntityManager { @QueryParam(EntityPath.TYPE_PARAM) String type, @QueryParam(EntityPath.PROPERTIES_PARAM) String properties) throws ResourceNotFoundException, ResourceRegistryException { - logger.trace( + logger.info( "requested to attach source {} {} and target {} {} ({} Type {}) with properties {}", Resource.NAME, sourceResourceUUID, Resource.NAME, targetResourceUUID, IsRelatedTo.class.getSimpleName(), type, @@ -248,7 +248,7 @@ public class EntityManager { @Path(EntityPath.IS_RELATED_TO_PATH_PART + "/{" + ID_PATH_PARAM + "}") public boolean detachResource(@PathParam(ID_PATH_PARAM) String relatedToUUID) throws ResourceRegistryException { - logger.trace("requested to detach {}", relatedToUUID); + logger.info("requested to detach {}", relatedToUUID); return entityManager.detachResource(UUID.fromString(relatedToUUID)); } @@ -258,7 +258,7 @@ public class EntityManager { public boolean addResourceToContext(@PathParam(ID_PATH_PARAM) String uuid) throws ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { - logger.trace("requested to add {} with UUID {} to current context {}", + logger.info("requested to add {} with UUID {} to current context {}", Resource.NAME, uuid, ContextUtility.getCurrentContext()); return entityManager.addResourceToContext(UUID.fromString(uuid)); } @@ -269,7 +269,7 @@ public class EntityManager { public boolean addFacetToContext(@PathParam(ID_PATH_PARAM) String uuid) throws FacetNotFoundException, ContextNotFoundException, ResourceRegistryException { - logger.trace("requested to add {} with UUID {} to current context {}", + logger.info("requested to add {} with UUID {} to current context {}", Facet.NAME, uuid, ContextUtility.getCurrentContext()); return entityManager.addFacetToContext(UUID.fromString(uuid)); } 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 7d2f095..2d86786 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 @@ -134,9 +134,9 @@ public class EntityManagementImpl implements EntityManagement { return null; } /* - ObjectMapper mapper = new ObjectMapper(); - Header header = mapper.readValue(headerNode.toString(), Header.class); - */ + * ObjectMapper mapper = new ObjectMapper(); Header header = + * mapper.readValue(headerNode.toString(), Header.class); + */ HeaderOrient headerOrient = new HeaderOrient(); headerOrient.fromJSON(headerNode.toString()); return headerOrient; @@ -144,101 +144,105 @@ public class EntityManagementImpl implements EntityManagement { return null; } - private static void checkEmbeddedType(JsonNode jsonNode) throws ResourceRegistryException { - if(jsonNode.has(Entities.CLASS_PROPERTY)){ + private static void checkEmbeddedType(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; } - + Header header = null; try { header = getHeader(jsonNode); - } catch (Exception e){ + } 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"); + throw new ResourceRegistryException( + "An embedded object cannot have an Header"); } - - if(header!=null){ + + 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"); + throw new ResourceRegistryException( + "An embedded object cannot have an Header"); } } } - - public static Object getObejctFromElement(JsonNode value) throws ResourceRegistryException { + + public static Object getObejctFromElement(JsonNode value) + throws ResourceRegistryException { JsonNodeType jsonNodeType = value.getNodeType(); switch (jsonNodeType) { - case OBJECT: - checkEmbeddedType(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); - } + case OBJECT: + checkEmbeddedType(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 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{ - + + 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; @@ -249,119 +253,124 @@ public class EntityManagementImpl implements EntityManagement { if (ignoreKeys.contains(key)) { continue; } - + JsonNode value = entry.getValue(); Object object = null; try { object = getObejctFromElement(value); - if(object!=null){ + 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 createRelations(OrientGraph orientGraph, Vertex resource, JsonNode relationArray, - @SuppressWarnings("rawtypes") Class relation) throws FacetNotFoundException, - ResourceNotFoundException, ResourceRegistryException { - + + private void createRelations(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) { throw new ResourceRegistryException(e); } - - + Vertex targetVertex = null; if (targetHeader == null) { if (ConsistsOf.class.isAssignableFrom(relation)) { - targetVertex = createVertexEntity( - orientGraph, getClassProperty(target), - Facet.class, target.toString(), true); - targetHeader = targetVertex.getProperty(Facet.HEADER_PROPERTY); + targetVertex = createVertexEntity(orientGraph, + getClassProperty(target), Facet.class, + target.toString(), true); + 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); + 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 { + } else { // The target Entity was already created we just need to create // the right relation Class targetClass = null; - - if(ConsistsOf.class.isAssignableFrom(relation)) { + + if (ConsistsOf.class.isAssignableFrom(relation)) { targetClass = Facet.class; - }else if(IsRelatedTo.class.isAssignableFrom(relation)){ + } else if (IsRelatedTo.class.isAssignableFrom(relation)) { targetClass = Resource.class; - }else{ - String error = String.format( - "%s Unsupported %s creation", relation.toString(), - Relation.NAME); + } else { + String error = String.format("%s Unsupported %s creation", + relation.toString(), Relation.NAME); logger.error(error); throw new ResourceRegistryException(error); } - + UUID targetUUID = targetHeader.getUUID(); String entityType = getClassProperty(target); - - targetVertex = getEntity(orientGraph, targetUUID, entityType, targetClass); + + 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){ + } 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, + + 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); + return createVertexEntity(orientGraph, entityType, entity, + jsonRepresentation, deferredCommit); } - - public Vertex createVertexEntity(OrientGraph orientGraph, String entityType, - Class entity, String jsonRepresentation, - boolean deferredCommit) throws ResourceRegistryException { - + + public Vertex createVertexEntity(OrientGraph orientGraph, + String entityType, Class entity, + String jsonRepresentation, boolean deferredCommit) + throws ResourceRegistryException { + + logger.trace("Going to create {} for {} ({}) using {}", + Vertex.class.getSimpleName(), + entity.getClass().getSimpleName(), entityType, + jsonRepresentation); + try { - + SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); try { schemaManagement.getTypeSchema(entityType, @@ -372,7 +381,7 @@ 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 @@ -382,9 +391,8 @@ public class EntityManagementImpl implements EntityManagement { throw new ResourceRegistryException(error); } - OrientVertex vertex = orientGraph.addVertex("class:" - + entityType); - + OrientVertex vertex = orientGraph.addVertex("class:" + entityType); + Header entityHeader = getHeader(jsonNode); if (entityHeader != null) { vertex.setProperty(Entity.HEADER_PROPERTY, entityHeader); @@ -406,28 +414,32 @@ public class EntityManagementImpl implements EntityManagement { continue; } JsonNode value = entry.getValue(); - if(!(value instanceof NullNode)){ + if (!(value instanceof NullNode)) { vertex.setProperty(entry.getKey(), value.asText()); } } } - + ContextUtility.addToActualContext(orientGraph, vertex); - + vertex.save(); if (!deferredCommit) { orientGraph.commit(); - - logger.trace("Created {} is {} orientVertexToJsonString", - Vertex.class.getSimpleName(), Utility - .toJsonString((OrientVertex) vertex, true)); } + logger.trace("Created {} is {}", + Vertex.class.getSimpleName(), + Utility.toJsonString((OrientVertex) vertex, true)); + return vertex; } catch (Exception e) { - logger.error("Error Creating {} with {}", entityType, jsonRepresentation, e); + logger.trace("Error while creating {} for {} ({}) using {}", + Vertex.class.getSimpleName(), + entity.getClass().getSimpleName(), entityType, + jsonRepresentation, e); + if (orientGraph != null) { orientGraph.rollback(); } @@ -441,15 +453,18 @@ public class EntityManagementImpl implements EntityManagement { } public Edge createEdgeRelation( - UUID sourceUUID, Class sourceClass, - UUID targetUUID, Class targetClass, - String relationType, @SuppressWarnings("rawtypes") Class relationBaseClass, - String jsonProperties) - throws FacetNotFoundException, ResourceNotFoundException, - ResourceRegistryException { - - logger.trace("Trying to create {} with {}", relationType, jsonProperties); - + UUID sourceUUID, + Class sourceClass, + UUID targetUUID, + Class targetClass, + String relationType, + @SuppressWarnings("rawtypes") Class relationBaseClass, + String jsonProperties) throws FacetNotFoundException, + ResourceNotFoundException, ResourceRegistryException { + + logger.trace("Trying to create {} with {}", relationType, + jsonProperties); + OrientGraph orientGraph = null; if (relationType == null || relationType.compareTo("") == 0) { @@ -465,60 +480,67 @@ public class EntityManagementImpl implements EntityManagement { 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) { throw new ResourceRegistryException( "Error while setting Relation Properties", e); } } - - return createEdgeRelation(orientGraph, source, target, + + return createEdgeRelation(orientGraph, source, target, relationType, relationBaseClass, edgeProperties, false); - - } catch(ResourceNotFoundException rnfe){ - logger.trace("Error Creating {} with {}", relationType, jsonProperties, rnfe); + + } catch (ResourceNotFoundException rnfe) { + logger.trace("Error Creating {} with {}", relationType, + jsonProperties, 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 { - - logger.info("Trying to create {} with {}", relationType, edgeProperties); + + public Edge createEdgeRelation( + OrientGraph orientGraph, + Vertex source, + Vertex target, + String relationType, + @SuppressWarnings("rawtypes") Class relationBaseClass, + Map edgeProperties, boolean deferredCommit) + throws ResourceRegistryException { + + logger.debug("Trying to create {} with {}", relationType, + edgeProperties); try { - + SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); try { - schemaManagement.getTypeSchema(relationType, relationBaseClass.getSimpleName()); + schemaManagement.getTypeSchema(relationType, + relationBaseClass.getSimpleName()); } catch (SchemaNotFoundException e) { throw e; } - + if (relationType == null || relationType.compareTo("") == 0) { - throw new ResourceRegistryException(Relation.class.getSimpleName() - + " Type cannot be empty or null"); + throw new ResourceRegistryException( + Relation.class.getSimpleName() + + " Type cannot be empty or null"); } - + // TODO Check the relation compatibility between source and target logger.trace("Creating {} ({}) beetween {} -> {}", @@ -528,44 +550,48 @@ public class EntityManagementImpl implements EntityManagement { Edge edge = orientGraph.addEdge(null, source, target, relationType); - for(String key : edgeProperties.keySet()){ + for (String key : edgeProperties.keySet()) { try { edge.setProperty(key, edgeProperties.get(key)); } catch (Exception e) { - String error = String.format("Error while setting property %s : %s", key, edgeProperties.get(key).toString()); + 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(); if (!deferredCommit) { orientGraph.commit(); } - - logger.info("{} with {} successfully created", relationType, edgeProperties); - + + logger.info("{} with {} successfully created", relationType, + edgeProperties); + return edge; } catch (ResourceRegistryException rre) { - logger.error("Error Creating {} with {}", relationType, edgeProperties, rre); - if (orientGraph!=null) { + logger.error("Error Creating {} with {}", relationType, + edgeProperties, rre); + if (orientGraph != null) { orientGraph.rollback(); } throw rre; } catch (Exception e) { - logger.error("Error Creating {} with {}", relationType, edgeProperties, e); - if (orientGraph!=null) { + logger.error("Error Creating {} with {}", relationType, + edgeProperties, e); + if (orientGraph != null) { orientGraph.rollback(); } throw new ResourceRegistryException(e); } finally { - if (orientGraph!= null && !deferredCommit) { + if (orientGraph != null && !deferredCommit) { orientGraph.shutdown(); } } @@ -574,7 +600,8 @@ public class EntityManagementImpl implements EntityManagement { @Override public String createFacet(String facetType, String jsonRepresentation) throws ResourceRegistryException { - Vertex vertex = createVertexEntity(facetType, Facet.class, jsonRepresentation, false); + Vertex vertex = createVertexEntity(facetType, Facet.class, + jsonRepresentation, false); return Utility.toJsonString((OrientVertex) vertex, false); } @@ -587,6 +614,10 @@ public class EntityManagementImpl implements EntityManagement { @Override public String readFacet(UUID uuid, String facetType) throws FacetNotFoundException, ResourceRegistryException { + + logger.debug("Going to read {} ({}) with UUID {}", Facet.NAME, + facetType, uuid); + OrientGraph orientGraph = null; try { @@ -595,13 +626,18 @@ 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.toJsonString((OrientVertex) facet, true)); + logger.info("{} of type {} with UUID {} is {}", Facet.NAME, + facetType, uuid, + Utility.toJsonString((OrientVertex) facet, true)); return Utility.toJsonString((OrientVertex) facet, true); } catch (FacetNotFoundException fnfe) { + logger.error("Unable to read {} ({}) with UUID {}", Facet.NAME, + facetType, uuid, fnfe); throw fnfe; } catch (Exception e) { + logger.error("Unable to read {} ({}) with UUID {}", Facet.NAME, + facetType, uuid, e); throw new ResourceRegistryException(e.getMessage()); } finally { if (orientGraph != null) { @@ -613,6 +649,10 @@ public class EntityManagementImpl implements EntityManagement { @Override public String updateFacet(UUID uuid, String jsonRepresentation) throws ResourceRegistryException { + + logger.debug("Trying to update {} with UUID {} usign {}", Facet.NAME, + uuid, jsonRepresentation); + OrientGraph orientGraph = null; try { @@ -661,17 +701,21 @@ public class EntityManagementImpl implements EntityManagement { ((OrientVertex) facet).save(); orientGraph.commit(); - logger.trace("{} with UUID {} has been updated {}", Facet.NAME, + logger.info("{} with UUID {} has been updated {}", Facet.NAME, uuid, Utility.toJsonString((OrientVertex) facet, true)); return Utility.toJsonString((OrientVertex) facet, false); } catch (FacetNotFoundException fnfe) { + logger.debug("Unable to update {} with UUID {} usign {}", + Facet.NAME, uuid, jsonRepresentation, fnfe); if (orientGraph != null) { orientGraph.rollback(); } throw fnfe; } catch (Exception e) { + logger.debug("Unable to update {} with UUID {} usign {}", + Facet.NAME, uuid, jsonRepresentation, e); if (orientGraph != null) { orientGraph.rollback(); } @@ -687,6 +731,9 @@ public class EntityManagementImpl implements EntityManagement { @Override public boolean deleteFacet(UUID uuid) throws FacetNotFoundException, ResourceRegistryException { + + logger.debug("Going to delete {} with UUID {}", Facet.NAME, uuid); + OrientGraph orientGraph = null; try { @@ -697,12 +744,20 @@ public class EntityManagementImpl implements EntityManagement { facet.remove(); orientGraph.commit(); + + logger.info("{} with UUID {} was successfully deleted.", + Facet.NAME, uuid); + } catch (FacetNotFoundException fnfe) { + logger.error("Unable to delete {} with UUID {}", Facet.NAME, uuid, + fnfe); if (orientGraph != null) { orientGraph.rollback(); } throw fnfe; } catch (Exception e) { + logger.error("Unable to delete {} with UUID {}", Facet.NAME, uuid, + e); if (orientGraph != null) { orientGraph.rollback(); } @@ -721,33 +776,50 @@ public class EntityManagementImpl implements EntityManagement { String consistOfType, String jsonProperties) throws FacetNotFoundException, ResourceNotFoundException, ResourceRegistryException { - Edge edge = createEdgeRelation(resourceUUID, Resource.class, - facetUUID, Facet.class, - consistOfType, ConsistsOf.class, - jsonProperties); + Edge edge = createEdgeRelation(resourceUUID, Resource.class, facetUUID, + Facet.class, consistOfType, ConsistsOf.class, jsonProperties); return Utility.toJsonString((OrientEdge) edge, false); } @Override - public boolean detachFacet(UUID consistOfUUID) + public boolean detachFacet(UUID consistsOfUUID) throws ResourceRegistryException { OrientGraph orientGraph = null; + logger.debug( + "Going to remove {} {} with UUID {}. {} will be detached from its {}.", + ConsistsOf.NAME, Relation.NAME, consistsOfUUID, Facet.NAME, + Resource.NAME); + try { orientGraph = ContextUtility .getActualSecurityContextGraph(PermissionMode.WRITER); - Edge edge = getRelation(orientGraph, consistOfUUID, + Edge edge = getRelation(orientGraph, consistsOfUUID, ConsistsOf.NAME, ConsistsOf.class); edge.remove(); orientGraph.commit(); + + logger.info( + "{} {} with UUID {} successfully removed. {} has been detached from its {}.", + ConsistsOf.NAME, Relation.NAME, consistsOfUUID, Facet.NAME, + Resource.NAME); + } catch (FacetNotFoundException fnfe) { + logger.error( + "Unable to remove {} {} with UUID {}. {} has not been detached from its {}.", + ConsistsOf.NAME, Relation.NAME, consistsOfUUID, Facet.NAME, + Resource.NAME); if (orientGraph != null) { orientGraph.rollback(); } throw fnfe; } catch (Exception e) { + logger.error( + "Unable to remove {} {} with UUID {}. {} has not been detached from its {}.", + ConsistsOf.NAME, Relation.NAME, consistsOfUUID, Facet.NAME, + Resource.NAME); if (orientGraph != null) { orientGraph.rollback(); } @@ -764,36 +836,53 @@ public class EntityManagementImpl implements EntityManagement { @Override public String attachResource(UUID sourceResourceUuid, - UUID targetResourceUuid, String relatedToType, - String jsonProperties) throws ResourceNotFoundException, - ResourceRegistryException { + UUID targetResourceUuid, String relatedToType, String jsonProperties) + throws ResourceNotFoundException, ResourceRegistryException { Edge edge = createEdgeRelation(sourceResourceUuid, Resource.class, - targetResourceUuid, Resource.class, - relatedToType, IsRelatedTo.class, - jsonProperties); + targetResourceUuid, Resource.class, relatedToType, + IsRelatedTo.class, jsonProperties); return Utility.toJsonString((OrientEdge) edge, false); } @Override - public boolean detachResource(UUID relatedToUUID) + public boolean detachResource(UUID isRelatedToUUID) throws ResourceRegistryException { + + logger.debug( + "Going to remove {} {} with UUID {}. Related {}s will be detached.", + IsRelatedTo.NAME, Relation.NAME, isRelatedToUUID, Resource.NAME); + OrientGraph orientGraph = null; try { orientGraph = ContextUtility .getActualSecurityContextGraph(PermissionMode.WRITER); - Edge edge = getRelation(orientGraph, relatedToUUID, + Edge edge = getRelation(orientGraph, isRelatedToUUID, IsRelatedTo.NAME, IsRelatedTo.class); edge.remove(); orientGraph.commit(); - } catch (FacetNotFoundException fnfe) { + + logger.info( + "{} {} with UUID {} successfully removed. Related {}s were detached.", + IsRelatedTo.NAME, Relation.NAME, isRelatedToUUID, + Resource.NAME); + + } catch (ResourceRegistryException rre) { + logger.error( + "Unable to remove {} {} with UUID. Related {}s will not be detached.", + IsRelatedTo.NAME, Relation.NAME, isRelatedToUUID, + Resource.NAME); if (orientGraph != null) { orientGraph.rollback(); } - throw fnfe; + throw rre; } catch (Exception e) { + logger.error( + "Unable to remove {} {} with UUID {}. Related {}s will not be detached.", + IsRelatedTo.NAME, Relation.NAME, isRelatedToUUID, + Resource.NAME); if (orientGraph != null) { orientGraph.rollback(); } @@ -807,76 +896,92 @@ public class EntityManagementImpl implements EntityManagement { return true; } - private static String marshallResource(Vertex vertex) throws JSONException{ - JSONObject jsonObject = Utility.toJsonObject((OrientVertex) vertex, true); - + private static String marshallResource(Vertex vertex) throws JSONException { + JSONObject jsonObject = Utility.toJsonObject((OrientVertex) vertex, + true); + JSONArray consistsOfArray = new JSONArray(); - + Iterable edges = vertex.getEdges(Direction.OUT); - - for(Edge edge : edges){ - + + for (Edge edge : edges) { + String edgeType = edge.getLabel(); - + try { SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); schemaManagement.getTypeSchema(edgeType, ConsistsOf.NAME); - - JSONObject jsonObjectEdge = Utility.toJsonObject((OrientEdge) edge, true); + + JSONObject jsonObjectEdge = Utility.toJsonObject( + (OrientEdge) edge, true); Vertex facetVertex = edge.getVertex(Direction.IN); - jsonObjectEdge.put(Relation.TARGET_PROPERTY, Utility.toJsonObject((OrientVertex) facetVertex, true)); + jsonObjectEdge.put(Relation.TARGET_PROPERTY, + Utility.toJsonObject((OrientVertex) facetVertex, true)); consistsOfArray.put(jsonObjectEdge); - + } catch (SchemaNotFoundException e) { // This not an ConsistsOf Edge. it will be skipped } - + } - - jsonObject.put(lowerCaseFirstCharacter(ConsistsOf.NAME), consistsOfArray); - + + jsonObject.put(lowerCaseFirstCharacter(ConsistsOf.NAME), + consistsOfArray); + return jsonObject.toString(); } - + @Override public String createResource(String resourceType, String jsonRepresentation) throws ResourceRegistryException { - - logger.trace("Trying to create {} : {}", resourceType, - jsonRepresentation); - + + logger.debug("Trying to create {} using {}", resourceType, + jsonRepresentation); + OrientGraph orientGraph = null; try { orientGraph = ContextUtility .getActualSecurityContextGraph(PermissionMode.WRITER); - - Vertex resource = createVertexEntity(orientGraph, resourceType, Resource.class, jsonRepresentation, true); - + + 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); - createRelations(orientGraph, resource, jsonNodeArray, ConsistsOf.class); + createRelations(orientGraph, resource, jsonNodeArray, + ConsistsOf.class); } - + property = lowerCaseFirstCharacter(IsRelatedTo.NAME); if (jsonNode.has(property)) { JsonNode jsonNodeArray = jsonNode.get(property); - createRelations(orientGraph, resource, jsonNodeArray, IsRelatedTo.class); + createRelations(orientGraph, resource, jsonNodeArray, + IsRelatedTo.class); } - + orientGraph.commit(); - - return marshallResource(resource); - - } catch(ResourceRegistryException rre) { + + String resourceString = marshallResource(resource); + + logger.info("{} ({}) successfully created {}", Resource.NAME, + resourceType, resourceString); + + return resourceString; + + } catch (ResourceRegistryException rre) { + logger.error("Unable to create {} ({}) using {}", Resource.NAME, + resourceType, jsonRepresentation, rre); if (orientGraph != null) { orientGraph.rollback(); } throw rre; - } catch(Exception e){ + } catch (Exception e) { + logger.error("Unable to create {} ({}) using {}", Resource.NAME, + resourceType, jsonRepresentation, e); if (orientGraph != null) { orientGraph.rollback(); } @@ -886,7 +991,7 @@ public class EntityManagementImpl implements EntityManagement { orientGraph.shutdown(); } } - + } @Override @@ -897,6 +1002,10 @@ public class EntityManagementImpl implements EntityManagement { @Override public String readResource(UUID uuid, String resourceType) throws ResourceNotFoundException { + + logger.debug("Going to read {} ({}) with UUID {}", Resource.NAME, + resourceType, uuid); + OrientGraph orientGraph = null; try { @@ -906,13 +1015,18 @@ public class EntityManagementImpl implements EntityManagement { Vertex resource = getEntity(orientGraph, uuid, resourceType, Resource.class); - logger.trace("{} of type {} with UUID {} is {}", Resource.NAME, - resourceType, uuid, Utility.toJsonString((OrientVertex) resource, true)); + logger.info("{} of type {} with UUID {} is {}", Resource.NAME, + resourceType, uuid, + Utility.toJsonString((OrientVertex) resource, true)); return marshallResource(resource); } catch (ResourceNotFoundException rnfe) { + logger.error("Unable to read {} ({}) with UUID {}", Resource.NAME, + resourceType, uuid, rnfe); throw rnfe; } catch (Exception e) { + logger.error("Unable to read {} ({}) with UUID {}", Resource.NAME, + resourceType, uuid, e); throw new ResourceNotFoundException(e.getMessage()); } finally { if (orientGraph != null) { @@ -922,8 +1036,11 @@ public class EntityManagementImpl implements EntityManagement { } @Override - public boolean deleteResource(UUID uuid) - throws ResourceNotFoundException, ResourceRegistryException { + public boolean deleteResource(UUID uuid) throws ResourceNotFoundException, + ResourceRegistryException { + + logger.debug("Going to delete {} with UUID {}", Resource.NAME, uuid); + OrientGraph orientGraph = null; try { @@ -933,17 +1050,25 @@ 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(); + + logger.info("{} with UUID {} was successfully deleted.", + Resource.NAME, uuid); + return true; - } catch (ResourceNotFoundException fnfe) { + } catch (ResourceNotFoundException rnfe) { + logger.error("Unable to delete {} with UUID {}", Resource.NAME, + uuid, rnfe); if (orientGraph != null) { orientGraph.rollback(); } - throw fnfe; + throw rnfe; } catch (Exception e) { + logger.error("Unable to delete {} with UUID {}", Resource.NAME, + uuid, e); if (orientGraph != null) { orientGraph.rollback(); } @@ -954,24 +1079,27 @@ public class EntityManagementImpl implements EntityManagement { } } } - - + protected boolean addEntityToContext(Class clz, UUID uuid) throws FacetNotFoundException, ResourceNotFoundException, ContextNotFoundException, ResourceRegistryException { - + + logger.debug("Going to add {} with UUID {} to actual Context", + clz.getSimpleName(), uuid); + OrientGraph orientGraph = null; try { orientGraph = SecurityContextMapper.getSecurityContextFactory( SecurityContextMapper.ADMIN_SECURITY_CONTEXT_UUID, PermissionMode.WRITER).getTx(); - + Vertex vertex = getEntity(orientGraph, uuid, null, clz); - - UUID contextUUID = ContextUtility.addToActualContext(orientGraph, vertex); - + + UUID contextUUID = ContextUtility.addToActualContext(orientGraph, + vertex); + if (Resource.class.isAssignableFrom(clz)) { Iterable facets = vertex.getVertices(Direction.OUT, ConsistsOf.NAME); @@ -979,12 +1107,16 @@ public class EntityManagementImpl implements EntityManagement { ContextUtility.addToActualContext(orientGraph, facet); } } - + orientGraph.commit(); - logger.info("{} with UUID {} successfully added to actual Context with UUID {}", clz.getSimpleName(), - uuid, contextUUID); + logger.info( + "{} with UUID {} successfully added to actual Context with UUID {}", + clz.getSimpleName(), uuid, contextUUID); return true; } catch (Exception e) { + logger.error( + "Unable to add {} with UUID {} successfully added to actual Context", + clz.getSimpleName(), uuid, e); if (orientGraph != null) { orientGraph.rollback(); } @@ -996,7 +1128,6 @@ public class EntityManagementImpl implements EntityManagement { } } - @Override public boolean addResourceToContext(UUID uuid) throws ResourceNotFoundException, ContextNotFoundException,