Changed updateResource method signature. Exposed REST API for Update Resource

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@134639 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-23 17:14:56 +00:00
parent 7a65a38438
commit 0b1bd22e4e
3 changed files with 58 additions and 58 deletions

View File

@ -131,6 +131,18 @@ public class EntityManager {
return entityManager.createResource(type, json); 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 * e.g. DELETE
* /resource-registry/entity/resource/67062c11-9c3a-4906-870d-7df6a43408b0 * /resource-registry/entity/resource/67062c11-9c3a-4906-870d-7df6a43408b0

View File

@ -62,8 +62,8 @@ import com.tinkerpop.blueprints.impls.orient.OrientVertex;
*/ */
public class EntityManagementImpl implements EntityManagement { 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 private static Logger logger = LoggerFactory
.getLogger(EntityManagementImpl.class); .getLogger(EntityManagementImpl.class);
@ -80,7 +80,8 @@ public class EntityManagementImpl implements EntityManagement {
entityType = Resource.NAME; entityType = Resource.NAME;
} }
} }
return Utility.getElementByUUID(orientGraph, entityType, uuid, Vertex.class); return Utility.getElementByUUID(orientGraph, entityType, uuid,
Vertex.class);
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
if (Facet.class.isAssignableFrom(entityClass)) { if (Facet.class.isAssignableFrom(entityClass)) {
throw new FacetNotFoundException(e.getMessage()); throw new FacetNotFoundException(e.getMessage());
@ -92,38 +93,31 @@ public class EntityManagementImpl implements EntityManagement {
} }
} }
protected Vertex getEntity(OrientGraph orientGraph, JsonNode jsonNode, protected Vertex getEntity(OrientGraph orientGraph, JsonNode jsonNode,
String entityType, Class<? extends Entity> entityClass) UUID uuid, Class<? extends Entity> entityClass)
throws JsonParseException, JsonMappingException, IOException, throws JsonParseException, JsonMappingException, IOException,
FacetNotFoundException, ResourceNotFoundException, FacetNotFoundException, ResourceNotFoundException,
ResourceRegistryException { 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); String classProperty = getClassProperty(jsonNode);
if (entityType.compareTo(classProperty) != 0) {
SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
try { try {
schemaManagement.getTypeSchema(entityType, classProperty); schemaManagement.getTypeSchema(classProperty, classProperty);
} catch (SchemaNotFoundException e) { } catch (SchemaNotFoundException e) {
throw e; throw e;
}
} }
Header header = HeaderUtility.getHeader(jsonNode, false); Header header = HeaderUtility.getHeader(jsonNode, false);
UUID resourceUUID = header.getUUID(); UUID resourceUUID = header.getUUID();
Vertex vertex = getEntity(orientGraph, resourceUUID, entityType, Vertex vertex = getEntity(orientGraph, resourceUUID, classProperty,
entityClass); entityClass);
return vertex; return vertex;
} }
public Edge getRelation( public Edge getRelation(
OrientGraph orientGraph, OrientGraph orientGraph,
UUID uuid, UUID uuid,
@ -139,7 +133,8 @@ public class EntityManagementImpl implements EntityManagement {
relationType = ConsistsOf.NAME; relationType = ConsistsOf.NAME;
} }
} }
return Utility.getElementByUUID(orientGraph, relationType, uuid, Edge.class); return Utility.getElementByUUID(orientGraph, relationType, uuid,
Edge.class);
} catch (ResourceRegistryException e) { } catch (ResourceRegistryException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
@ -170,7 +165,7 @@ public class EntityManagementImpl implements EntityManagement {
} catch (SchemaNotFoundException e) { } catch (SchemaNotFoundException e) {
throw e; throw e;
} }
} }
Header header = HeaderUtility.getHeader(jsonNode, false); Header header = HeaderUtility.getHeader(jsonNode, false);
@ -712,24 +707,22 @@ public class EntityManagementImpl implements EntityManagement {
Edge edge = Utility.getElementByUUID(orientGraph, relationType, Edge edge = Utility.getElementByUUID(orientGraph, relationType,
uuid, Edge.class); uuid, Edge.class);
edge = (Edge) updateProperties(edge, jsonNode); edge = (Edge) updateProperties(edge, jsonNode);
((OrientEdge) edge).save(); ((OrientEdge) edge).save();
JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY); JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY);
if(target!=null){ if (target != null) {
UUID targetUUID = org.gcube.informationsystem.impl.utils.Utility UUID targetUUID = org.gcube.informationsystem.impl.utils.Utility
.getUUIDFromJsonNode(target); .getUUIDFromJsonNode(target);
updateFacet(orientGraph, targetUUID, target, true); updateFacet(orientGraph, targetUUID, target, true);
} }
if (!deferredCommit) { if (!deferredCommit) {
orientGraph.commit(); orientGraph.commit();
} }
logger.info("{} {} successfully updated", logger.info("{} {} successfully updated", relationType, jsonNode);
relationType, jsonNode);
return edge; return edge;
@ -807,12 +800,13 @@ public class EntityManagementImpl implements EntityManagement {
Set<String> oldKeys = element.getPropertyKeys(); Set<String> oldKeys = element.getPropertyKeys();
Map<String, Object> properties; Map<String, Object> properties;
if(element instanceof Vertex){ if (element instanceof Vertex) {
properties = getVertexProperties(jsonNode); properties = getVertexProperties(jsonNode);
}else if(element instanceof Edge){ } else if (element instanceof Edge) {
properties = getEdgeProperties(jsonNode); properties = getEdgeProperties(jsonNode);
}else{ } else {
String error = String.format("Error while updating {} properties", element.toString()); String error = String.format("Error while updating {} properties",
element.toString());
throw new ResourceRegistryException(error); throw new ResourceRegistryException(error);
} }
@ -842,9 +836,9 @@ public class EntityManagementImpl implements EntityManagement {
return element; return element;
} }
protected String updateFacet(OrientGraph orientGraph, UUID uuid,
protected String updateFacet(OrientGraph orientGraph, UUID uuid, JsonNode jsonNode, boolean deferredCommit) JsonNode jsonNode, boolean deferredCommit)
throws ResourceRegistryException { throws ResourceRegistryException {
logger.debug("Trying to update {} with UUID {} usign {}", Facet.NAME, logger.debug("Trying to update {} with UUID {} usign {}", Facet.NAME,
uuid, jsonNode); uuid, jsonNode);
@ -857,8 +851,8 @@ public class EntityManagementImpl implements EntityManagement {
facet = (Vertex) updateProperties(facet, jsonNode); facet = (Vertex) updateProperties(facet, jsonNode);
((OrientVertex) facet).save(); ((OrientVertex) facet).save();
if(!deferredCommit){ if (!deferredCommit) {
orientGraph.commit(); orientGraph.commit();
} }
@ -923,7 +917,7 @@ public class EntityManagementImpl implements EntityManagement {
throw new ResourceRegistryException("Error Updating Facet", throw new ResourceRegistryException("Error Updating Facet",
e.getCause()); e.getCause());
} finally { } finally {
if (orientGraph != null ) { if (orientGraph != null) {
orientGraph.shutdown(); orientGraph.shutdown();
} }
} }
@ -1237,9 +1231,9 @@ public class EntityManagementImpl implements EntityManagement {
} }
@Override @Override
public String updateResource(String resourceType, String jsonRepresentation) public String updateResource(UUID resourceUUID, String jsonRepresentation)
throws ResourceNotFoundException, ResourceRegistryException { throws ResourceNotFoundException, ResourceRegistryException {
logger.debug("Trying to create {} using {}", resourceType, logger.debug("Trying to update {} using {}", resourceUUID,
jsonRepresentation); jsonRepresentation);
OrientGraph orientGraph = null; OrientGraph orientGraph = null;
@ -1250,7 +1244,8 @@ public class EntityManagementImpl implements EntityManagement {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(jsonRepresentation); 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); String property = lowerCaseFirstCharacter(ConsistsOf.NAME);
if (jsonNode.has(property)) { 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(); ((OrientVertex) resource).save();
orientGraph.commit(); orientGraph.commit();
String resourceString = marshallResource(resource); String resourceString = marshallResource(resource);
logger.info("{} ({}) successfully updated {}", Resource.NAME, logger.info("{} with UUID {} has been updated {}", Resource.NAME,
resourceType, resourceString); resourceUUID,
Utility.toJsonString((OrientVertex) resource, true));
return resourceString; return resourceString;
} catch (ResourceRegistryException rre) { } catch (ResourceRegistryException rre) {
logger.error("Unable to create {} ({}) using {}", Resource.NAME, logger.debug("Unable to update {} with UUID {} usign {}",
resourceType, jsonRepresentation, rre); Resource.NAME, resourceUUID, jsonRepresentation, rre);
if (orientGraph != null) { if (orientGraph != null) {
orientGraph.rollback(); orientGraph.rollback();
} }
throw rre; throw rre;
} catch (Exception e) { } catch (Exception e) {
logger.error("Unable to create {} ({}) using {}", Resource.NAME, logger.debug("Unable to update {} with UUID {} usign {}",
resourceType, jsonRepresentation, e); Resource.NAME, resourceUUID, jsonRepresentation, e);
if (orientGraph != null) { if (orientGraph != null) {
orientGraph.rollback(); orientGraph.rollback();
} }

View File

@ -104,7 +104,7 @@ public class EntityManagementFactory implements Factory<EntityManagement> {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return false; return false;
} }
@Override @Override
public boolean addResourceToContext(UUID uuid) public boolean addResourceToContext(UUID uuid)
throws ResourceNotFoundException, ContextNotFoundException, throws ResourceNotFoundException, ContextNotFoundException,
@ -113,7 +113,6 @@ public class EntityManagementFactory implements Factory<EntityManagement> {
return false; return false;
} }
@Override @Override
public boolean addFacetToContext(UUID uuid) public boolean addFacetToContext(UUID uuid)
throws FacetNotFoundException, ContextNotFoundException, throws FacetNotFoundException, ContextNotFoundException,
@ -123,7 +122,7 @@ public class EntityManagementFactory implements Factory<EntityManagement> {
} }
@Override @Override
public String updateResource(String resourceType, public String updateResource(UUID resourceUUID,
String jsonRepresentation) String jsonRepresentation)
throws ResourceNotFoundException, ResourceRegistryException { throws ResourceNotFoundException, ResourceRegistryException {
// TODO Auto-generated method stub // TODO Auto-generated method stub