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 1d82ead..e5fbab0 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/resources/EntityManager.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/resources/EntityManager.java @@ -131,6 +131,18 @@ public class EntityManager { return entityManager.createResource(type, json); } + @POST + @Path(EntityPath.RESOURCE_PATH_PART + "/{" + ID_PATH_PARAM + "}") + @Consumes({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON }) + @Produces(MediaType.APPLICATION_JSON) + public String updateResource(@PathParam(ID_PATH_PARAM) String uuid, + String json) throws FacetNotFoundException, + ResourceRegistryException { + logger.info("requested resource update for id {} with {}", uuid, json); + return entityManager.updateResource(UUID.fromString(uuid), json); + } + + /** * e.g. DELETE * /resource-registry/entity/resource/67062c11-9c3a-4906-870d-7df6a43408b0 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 9a5e0ad..3d33f5d 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 @@ -62,8 +62,8 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertex; */ public class EntityManagementImpl implements EntityManagement { - // TODO This Class is too long please refactoring is needed - + // TODO This Class is too long please refactoring is needed + private static Logger logger = LoggerFactory .getLogger(EntityManagementImpl.class); @@ -80,7 +80,8 @@ public class EntityManagementImpl implements EntityManagement { entityType = Resource.NAME; } } - return Utility.getElementByUUID(orientGraph, entityType, uuid, Vertex.class); + return Utility.getElementByUUID(orientGraph, entityType, uuid, + Vertex.class); } catch (ResourceRegistryException e) { if (Facet.class.isAssignableFrom(entityClass)) { throw new FacetNotFoundException(e.getMessage()); @@ -92,38 +93,31 @@ public class EntityManagementImpl implements EntityManagement { } } - + protected Vertex getEntity(OrientGraph orientGraph, JsonNode jsonNode, - String entityType, Class entityClass) + UUID uuid, Class entityClass) throws JsonParseException, JsonMappingException, IOException, FacetNotFoundException, ResourceNotFoundException, ResourceRegistryException { - if (entityType == null || entityType.compareTo("") == 0) { - String error = String.format("Invalid %s type : %s", - entityClass.getSimpleName(), entityType); - throw new ResourceRegistryException(error); - } - String classProperty = getClassProperty(jsonNode); - if (entityType.compareTo(classProperty) != 0) { - SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); - try { - schemaManagement.getTypeSchema(entityType, classProperty); - } catch (SchemaNotFoundException e) { - throw e; - } + + SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); + try { + schemaManagement.getTypeSchema(classProperty, classProperty); + } catch (SchemaNotFoundException e) { + throw e; } Header header = HeaderUtility.getHeader(jsonNode, false); UUID resourceUUID = header.getUUID(); - Vertex vertex = getEntity(orientGraph, resourceUUID, entityType, + Vertex vertex = getEntity(orientGraph, resourceUUID, classProperty, entityClass); return vertex; } - + public Edge getRelation( OrientGraph orientGraph, UUID uuid, @@ -139,7 +133,8 @@ public class EntityManagementImpl implements EntityManagement { relationType = ConsistsOf.NAME; } } - return Utility.getElementByUUID(orientGraph, relationType, uuid, Edge.class); + return Utility.getElementByUUID(orientGraph, relationType, uuid, + Edge.class); } catch (ResourceRegistryException e) { throw e; } catch (Exception e) { @@ -170,7 +165,7 @@ public class EntityManagementImpl implements EntityManagement { } catch (SchemaNotFoundException e) { throw e; } - + } Header header = HeaderUtility.getHeader(jsonNode, false); @@ -712,24 +707,22 @@ public class EntityManagementImpl implements EntityManagement { Edge edge = Utility.getElementByUUID(orientGraph, relationType, uuid, Edge.class); - + edge = (Edge) updateProperties(edge, jsonNode); ((OrientEdge) edge).save(); - JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY); - if(target!=null){ + if (target != null) { UUID targetUUID = org.gcube.informationsystem.impl.utils.Utility - .getUUIDFromJsonNode(target); + .getUUIDFromJsonNode(target); updateFacet(orientGraph, targetUUID, target, true); } - + if (!deferredCommit) { orientGraph.commit(); } - logger.info("{} {} successfully updated", - relationType, jsonNode); + logger.info("{} {} successfully updated", relationType, jsonNode); return edge; @@ -807,12 +800,13 @@ public class EntityManagementImpl implements EntityManagement { Set oldKeys = element.getPropertyKeys(); Map properties; - if(element instanceof Vertex){ + if (element instanceof Vertex) { properties = getVertexProperties(jsonNode); - }else if(element instanceof Edge){ + } else if (element instanceof Edge) { properties = getEdgeProperties(jsonNode); - }else{ - String error = String.format("Error while updating {} properties", element.toString()); + } else { + String error = String.format("Error while updating {} properties", + element.toString()); throw new ResourceRegistryException(error); } @@ -842,9 +836,9 @@ public class EntityManagementImpl implements EntityManagement { return element; } - - - protected String updateFacet(OrientGraph orientGraph, UUID uuid, JsonNode jsonNode, boolean deferredCommit) + + protected String updateFacet(OrientGraph orientGraph, UUID uuid, + JsonNode jsonNode, boolean deferredCommit) throws ResourceRegistryException { logger.debug("Trying to update {} with UUID {} usign {}", Facet.NAME, uuid, jsonNode); @@ -857,8 +851,8 @@ public class EntityManagementImpl implements EntityManagement { facet = (Vertex) updateProperties(facet, jsonNode); ((OrientVertex) facet).save(); - - if(!deferredCommit){ + + if (!deferredCommit) { orientGraph.commit(); } @@ -923,7 +917,7 @@ public class EntityManagementImpl implements EntityManagement { throw new ResourceRegistryException("Error Updating Facet", e.getCause()); } finally { - if (orientGraph != null ) { + if (orientGraph != null) { orientGraph.shutdown(); } } @@ -1237,9 +1231,9 @@ public class EntityManagementImpl implements EntityManagement { } @Override - public String updateResource(String resourceType, String jsonRepresentation) + public String updateResource(UUID resourceUUID, String jsonRepresentation) throws ResourceNotFoundException, ResourceRegistryException { - logger.debug("Trying to create {} using {}", resourceType, + logger.debug("Trying to update {} using {}", resourceUUID, jsonRepresentation); OrientGraph orientGraph = null; @@ -1250,7 +1244,8 @@ public class EntityManagementImpl implements EntityManagement { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(jsonRepresentation); - Vertex resource = getEntity(orientGraph, jsonNode, resourceType, Resource.class); + Vertex resource = getEntity(orientGraph, jsonNode, resourceUUID, + Resource.class); String property = lowerCaseFirstCharacter(ConsistsOf.NAME); if (jsonNode.has(property)) { @@ -1262,33 +1257,27 @@ public class EntityManagementImpl implements EntityManagement { } - /* - * Updating resource means update only ConsistsOf and Facets - * passed as json. All the others ConsistsOf relations are keep - * as they are. All IsRelatedTo relations and related resources if - * any are ignored - */ - ((OrientVertex) resource).save(); orientGraph.commit(); String resourceString = marshallResource(resource); - logger.info("{} ({}) successfully updated {}", Resource.NAME, - resourceType, resourceString); + logger.info("{} with UUID {} has been updated {}", Resource.NAME, + resourceUUID, + Utility.toJsonString((OrientVertex) resource, true)); return resourceString; } catch (ResourceRegistryException rre) { - logger.error("Unable to create {} ({}) using {}", Resource.NAME, - resourceType, jsonRepresentation, rre); + logger.debug("Unable to update {} with UUID {} usign {}", + Resource.NAME, resourceUUID, jsonRepresentation, rre); if (orientGraph != null) { orientGraph.rollback(); } throw rre; } catch (Exception e) { - logger.error("Unable to create {} ({}) using {}", Resource.NAME, - resourceType, jsonRepresentation, e); + logger.debug("Unable to update {} with UUID {} usign {}", + Resource.NAME, resourceUUID, jsonRepresentation, e); if (orientGraph != null) { orientGraph.rollback(); } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/EntityManagementFactory.java b/src/test/java/org/gcube/informationsystem/resourceregistry/EntityManagementFactory.java index 1501a2d..ef443c6 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/EntityManagementFactory.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/EntityManagementFactory.java @@ -104,7 +104,7 @@ public class EntityManagementFactory implements Factory { // TODO Auto-generated method stub return false; } - + @Override public boolean addResourceToContext(UUID uuid) throws ResourceNotFoundException, ContextNotFoundException, @@ -113,7 +113,6 @@ public class EntityManagementFactory implements Factory { return false; } - @Override public boolean addFacetToContext(UUID uuid) throws FacetNotFoundException, ContextNotFoundException, @@ -123,7 +122,7 @@ public class EntityManagementFactory implements Factory { } @Override - public String updateResource(String resourceType, + public String updateResource(UUID resourceUUID, String jsonRepresentation) throws ResourceNotFoundException, ResourceRegistryException { // TODO Auto-generated method stub