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:
parent
7a65a38438
commit
0b1bd22e4e
|
@ -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
|
||||
|
|
|
@ -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());
|
||||
|
@ -94,31 +95,24 @@ public class EntityManagementImpl implements EntityManagement {
|
|||
}
|
||||
|
||||
protected Vertex getEntity(OrientGraph orientGraph, JsonNode jsonNode,
|
||||
String entityType, Class<? extends Entity> entityClass)
|
||||
UUID uuid, Class<? extends Entity> 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;
|
||||
|
@ -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) {
|
||||
|
@ -716,11 +711,10 @@ public class EntityManagementImpl implements EntityManagement {
|
|||
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);
|
||||
}
|
||||
|
||||
|
@ -728,8 +722,7 @@ public class EntityManagementImpl implements EntityManagement {
|
|||
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<String> oldKeys = element.getPropertyKeys();
|
||||
|
||||
Map<String, Object> 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);
|
||||
}
|
||||
|
||||
|
@ -843,8 +837,8 @@ 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);
|
||||
|
@ -858,7 +852,7 @@ public class EntityManagementImpl implements EntityManagement {
|
|||
|
||||
((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();
|
||||
}
|
||||
|
|
|
@ -113,7 +113,6 @@ public class EntityManagementFactory implements Factory<EntityManagement> {
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean addFacetToContext(UUID uuid)
|
||||
throws FacetNotFoundException, ContextNotFoundException,
|
||||
|
@ -123,7 +122,7 @@ public class EntityManagementFactory implements Factory<EntityManagement> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String updateResource(String resourceType,
|
||||
public String updateResource(UUID resourceUUID,
|
||||
String jsonRepresentation)
|
||||
throws ResourceNotFoundException, ResourceRegistryException {
|
||||
// TODO Auto-generated method stub
|
||||
|
|
Loading…
Reference in New Issue