Refactored code and added update resource function
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@134625 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
9954274a9d
commit
fe3be05045
|
@ -41,13 +41,16 @@ import org.gcube.informationsystem.resourceregistry.resources.utils.Utility;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.node.JsonNodeType;
|
import com.fasterxml.jackson.databind.node.JsonNodeType;
|
||||||
import com.orientechnologies.orient.core.record.impl.ODocument;
|
import com.orientechnologies.orient.core.record.impl.ODocument;
|
||||||
import com.tinkerpop.blueprints.Direction;
|
import com.tinkerpop.blueprints.Direction;
|
||||||
import com.tinkerpop.blueprints.Edge;
|
import com.tinkerpop.blueprints.Edge;
|
||||||
|
import com.tinkerpop.blueprints.Element;
|
||||||
import com.tinkerpop.blueprints.Vertex;
|
import com.tinkerpop.blueprints.Vertex;
|
||||||
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
|
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
|
||||||
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
|
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
|
||||||
|
@ -59,10 +62,12 @@ 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
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory
|
private static Logger logger = LoggerFactory
|
||||||
.getLogger(EntityManagementImpl.class);
|
.getLogger(EntityManagementImpl.class);
|
||||||
|
|
||||||
public Vertex getEntity(OrientGraph orientGraph, UUID uuid,
|
protected Vertex getEntity(OrientGraph orientGraph, UUID uuid,
|
||||||
String entityType, Class<? extends Entity> entityClass)
|
String entityType, Class<? extends Entity> entityClass)
|
||||||
throws FacetNotFoundException, ResourceNotFoundException,
|
throws FacetNotFoundException, ResourceNotFoundException,
|
||||||
ResourceRegistryException {
|
ResourceRegistryException {
|
||||||
|
@ -75,7 +80,7 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
entityType = Resource.NAME;
|
entityType = Resource.NAME;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Utility.getEntityByUUID(orientGraph, entityType, uuid);
|
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());
|
||||||
|
@ -88,6 +93,37 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Vertex getEntity(OrientGraph orientGraph, JsonNode jsonNode,
|
||||||
|
String entityType, 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Header header = HeaderUtility.getHeader(jsonNode, false);
|
||||||
|
UUID resourceUUID = header.getUUID();
|
||||||
|
|
||||||
|
Vertex vertex = getEntity(orientGraph, resourceUUID, entityType,
|
||||||
|
entityClass);
|
||||||
|
|
||||||
|
return vertex;
|
||||||
|
}
|
||||||
|
|
||||||
public Edge getRelation(
|
public Edge getRelation(
|
||||||
OrientGraph orientGraph,
|
OrientGraph orientGraph,
|
||||||
UUID uuid,
|
UUID uuid,
|
||||||
|
@ -103,7 +139,7 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
relationType = ConsistsOf.NAME;
|
relationType = ConsistsOf.NAME;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Utility.getRelationByUUID(orientGraph, relationType, uuid);
|
return Utility.getElementByUUID(orientGraph, relationType, uuid, Edge.class);
|
||||||
} catch (ResourceRegistryException e) {
|
} catch (ResourceRegistryException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -112,6 +148,40 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Edge getRelation(
|
||||||
|
OrientGraph orientGraph,
|
||||||
|
JsonNode jsonNode,
|
||||||
|
String relationType,
|
||||||
|
@SuppressWarnings("rawtypes") Class<? extends Relation> relationClass)
|
||||||
|
throws JsonParseException, JsonMappingException, IOException,
|
||||||
|
ResourceRegistryException {
|
||||||
|
|
||||||
|
if (relationType == null || relationType.compareTo("") == 0) {
|
||||||
|
String error = String.format("Invalid %s type : %s",
|
||||||
|
relationClass.getSimpleName(), relationType);
|
||||||
|
throw new ResourceRegistryException(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
String classProperty = getClassProperty(jsonNode);
|
||||||
|
if (relationType.compareTo(classProperty) != 0) {
|
||||||
|
SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
|
||||||
|
try {
|
||||||
|
schemaManagement.getTypeSchema(relationType, classProperty);
|
||||||
|
} catch (SchemaNotFoundException e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Header header = HeaderUtility.getHeader(jsonNode, false);
|
||||||
|
UUID relationUUID = header.getUUID();
|
||||||
|
|
||||||
|
Edge edge = getRelation(orientGraph, relationUUID, relationType,
|
||||||
|
relationClass);
|
||||||
|
|
||||||
|
return edge;
|
||||||
|
}
|
||||||
|
|
||||||
private static String lowerCaseFirstCharacter(String string) {
|
private static String lowerCaseFirstCharacter(String string) {
|
||||||
return string.substring(0, 1).toLowerCase() + string.substring(1);
|
return string.substring(0, 1).toLowerCase() + string.substring(1);
|
||||||
}
|
}
|
||||||
|
@ -221,7 +291,7 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
|
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
|
||||||
if(ignoreKeys == null){
|
if (ignoreKeys == null) {
|
||||||
ignoreKeys = new HashSet<>();
|
ignoreKeys = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,7 +300,7 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
Entry<String, JsonNode> entry = fields.next();
|
Entry<String, JsonNode> entry = fields.next();
|
||||||
|
|
||||||
String key = entry.getKey();
|
String key = entry.getKey();
|
||||||
if (key.compareTo(Relation.HEADER_PROPERTY) == 0) {
|
if (key.compareTo(Entity.HEADER_PROPERTY) == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (key.startsWith("@") || key.startsWith("_")) {
|
if (key.startsWith("@") || key.startsWith("_")) {
|
||||||
|
@ -256,19 +326,41 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createRelations(OrientGraph orientGraph, Vertex resource,
|
private Map<String, Object> getVertexProperties(JsonNode node)
|
||||||
JsonNode relationArray,
|
throws ResourceRegistryException {
|
||||||
@SuppressWarnings("rawtypes") Class<? extends Relation> relation)
|
Set<String> ignoreKeys = new HashSet<>();
|
||||||
throws FacetNotFoundException, ResourceNotFoundException,
|
Map<String, Object> vertexProperties = null;
|
||||||
ResourceRegistryException {
|
try {
|
||||||
|
vertexProperties = 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);
|
||||||
|
}
|
||||||
|
return vertexProperties;
|
||||||
|
}
|
||||||
|
|
||||||
Iterator<JsonNode> iterator = relationArray.elements();
|
private Map<String, Object> getEdgeProperties(JsonNode node)
|
||||||
while (iterator.hasNext()) {
|
throws ResourceRegistryException {
|
||||||
JsonNode node = iterator.next();
|
Set<String> ignoreKeys = new HashSet<>();
|
||||||
|
ignoreKeys.add(Relation.TARGET_PROPERTY);
|
||||||
|
ignoreKeys.add(Relation.SOURCE_PROPERTY);
|
||||||
|
ignoreKeys.add(Relation.HEADER_PROPERTY);
|
||||||
|
|
||||||
/* Managing Target */
|
Map<String, Object> edgeProperties = null;
|
||||||
JsonNode target = node.get(Relation.TARGET_PROPERTY);
|
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);
|
||||||
|
}
|
||||||
|
return edgeProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Vertex getOrCreateTargetVertex(OrientGraph orientGraph,
|
||||||
|
@SuppressWarnings("rawtypes") Class<? extends Relation> relation,
|
||||||
|
JsonNode target) throws ResourceRegistryException {
|
||||||
Header targetHeader = null;
|
Header targetHeader = null;
|
||||||
try {
|
try {
|
||||||
targetHeader = HeaderUtility.getHeader(target, false);
|
targetHeader = HeaderUtility.getHeader(target, false);
|
||||||
|
@ -282,8 +374,7 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
targetVertex = createVertexEntity(orientGraph,
|
targetVertex = createVertexEntity(orientGraph,
|
||||||
getClassProperty(target), Facet.class,
|
getClassProperty(target), Facet.class,
|
||||||
target.toString(), true);
|
target.toString(), true);
|
||||||
targetHeader = targetVertex
|
targetHeader = targetVertex.getProperty(Facet.HEADER_PROPERTY);
|
||||||
.getProperty(Facet.HEADER_PROPERTY);
|
|
||||||
} else {
|
} else {
|
||||||
String error = String
|
String error = String
|
||||||
.format("%s %s must already exist. The UUID must be provided in the %s of %s json respresentation",
|
.format("%s %s must already exist. The UUID must be provided in the %s of %s json respresentation",
|
||||||
|
@ -315,21 +406,28 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
targetClass);
|
targetClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return targetVertex;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createRelations(OrientGraph orientGraph, Vertex resource,
|
||||||
|
JsonNode relationArray,
|
||||||
|
@SuppressWarnings("rawtypes") Class<? extends Relation> relation)
|
||||||
|
throws FacetNotFoundException, ResourceNotFoundException,
|
||||||
|
ResourceRegistryException {
|
||||||
|
|
||||||
|
Iterator<JsonNode> iterator = relationArray.elements();
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
JsonNode node = iterator.next();
|
||||||
|
|
||||||
|
/* Managing Target */
|
||||||
|
JsonNode target = node.get(Relation.TARGET_PROPERTY);
|
||||||
|
|
||||||
|
Vertex targetVertex = getOrCreateTargetVertex(orientGraph,
|
||||||
|
relation, target);
|
||||||
|
|
||||||
String relationType = getClassProperty(node);
|
String relationType = getClassProperty(node);
|
||||||
|
|
||||||
Set<String> ignoreKeys = new HashSet<>();
|
Map<String, Object> edgeProperties = getEdgeProperties(node);
|
||||||
ignoreKeys.add(Relation.TARGET_PROPERTY);
|
|
||||||
ignoreKeys.add(Relation.SOURCE_PROPERTY);
|
|
||||||
ignoreKeys.add(Relation.HEADER_PROPERTY);
|
|
||||||
|
|
||||||
Map<String, Object> 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,
|
createEdgeRelation(orientGraph, resource, targetVertex,
|
||||||
relationType, relation, edgeProperties, true);
|
relationType, relation, edgeProperties, true);
|
||||||
|
@ -390,15 +488,7 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
if (Resource.class.isAssignableFrom(entity)) {
|
if (Resource.class.isAssignableFrom(entity)) {
|
||||||
// Facet and relation are created in calling method
|
// Facet and relation are created in calling method
|
||||||
} else {
|
} else {
|
||||||
Set<String> ignoreKeys = new HashSet<>();
|
Map<String, Object> properties = getVertexProperties(jsonNode);
|
||||||
Map<String, Object> properties = null;
|
|
||||||
try {
|
|
||||||
properties = getPropertyMap(jsonNode, ignoreKeys);
|
|
||||||
} catch (Exception e) {
|
|
||||||
String error = "Error while parsing json to get Relation properties";
|
|
||||||
logger.error(error, e);
|
|
||||||
throw new ResourceRegistryException(error, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (String key : properties.keySet()) {
|
for (String key : properties.keySet()) {
|
||||||
try {
|
try {
|
||||||
|
@ -412,24 +502,6 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
Iterator<Entry<String, JsonNode>> iterator = jsonNode.fields();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
Entry<String, JsonNode> entry = iterator.next();
|
|
||||||
if (entry.getKey().compareTo(Facet.HEADER_PROPERTY) == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (entry.getKey().startsWith("@")
|
|
||||||
|| entry.getKey().startsWith("_")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
JsonNode value = entry.getValue();
|
|
||||||
if (!(value instanceof NullNode)) {
|
|
||||||
vertex.setProperty(entry.getKey(), value.asText());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ContextUtility.addToActualContext(orientGraph, vertex);
|
ContextUtility.addToActualContext(orientGraph, vertex);
|
||||||
|
@ -440,17 +512,15 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
orientGraph.commit();
|
orientGraph.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Created {} is {}",
|
logger.info("Created {} is {}", Vertex.class.getSimpleName(),
|
||||||
Vertex.class.getSimpleName(),
|
|
||||||
Utility.toJsonString((OrientVertex) vertex, true));
|
Utility.toJsonString((OrientVertex) vertex, true));
|
||||||
|
|
||||||
return vertex;
|
return vertex;
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.trace("Error while creating {} for {} ({}) using {}",
|
logger.trace("Error while creating {} for {} ({}) using {}",
|
||||||
Vertex.class.getSimpleName(),
|
Vertex.class.getSimpleName(), entity.getClass()
|
||||||
entity.getClass().getSimpleName(), entityType,
|
.getSimpleName(), entityType, jsonRepresentation, e);
|
||||||
jsonRepresentation, e);
|
|
||||||
|
|
||||||
if (orientGraph != null) {
|
if (orientGraph != null) {
|
||||||
orientGraph.rollback();
|
orientGraph.rollback();
|
||||||
|
@ -497,14 +567,10 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
|
|
||||||
if (jsonProperties != null && jsonProperties.compareTo("") != 0) {
|
if (jsonProperties != null && jsonProperties.compareTo("") != 0) {
|
||||||
try {
|
try {
|
||||||
Set<String> ignoreKeys = new HashSet<>();
|
|
||||||
ignoreKeys.add(Relation.SOURCE_PROPERTY);
|
|
||||||
ignoreKeys.add(Relation.TARGET_PROPERTY);
|
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
JsonNode jsonNode = mapper.readTree(jsonProperties);
|
JsonNode jsonNode = mapper.readTree(jsonProperties);
|
||||||
|
|
||||||
edgeProperties = getPropertyMap(jsonNode, ignoreKeys);
|
edgeProperties = getEdgeProperties(jsonNode);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ResourceRegistryException(
|
throw new ResourceRegistryException(
|
||||||
|
@ -538,6 +604,11 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
edgeProperties);
|
edgeProperties);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (relationType == null || relationType.compareTo("") == 0) {
|
||||||
|
throw new ResourceRegistryException(
|
||||||
|
Relation.class.getSimpleName()
|
||||||
|
+ " Type cannot be empty or null");
|
||||||
|
}
|
||||||
|
|
||||||
SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
|
SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
|
||||||
try {
|
try {
|
||||||
|
@ -547,12 +618,6 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
throw 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 between source and target
|
// TODO Check the relation compatibility between source and target
|
||||||
|
|
||||||
logger.trace("Creating {} ({}) beetween {} -> {}",
|
logger.trace("Creating {} ({}) beetween {} -> {}",
|
||||||
|
@ -583,8 +648,8 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
orientGraph.commit();
|
orientGraph.commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("{} with properties {} successfully created", relationType,
|
logger.info("{} with properties {} successfully created",
|
||||||
edgeProperties);
|
relationType, edgeProperties);
|
||||||
|
|
||||||
return edge;
|
return edge;
|
||||||
|
|
||||||
|
@ -609,6 +674,85 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param orientGraph
|
||||||
|
* @param relationJsonNode
|
||||||
|
* @param class1
|
||||||
|
* @throws ResourceRegistryException
|
||||||
|
*/
|
||||||
|
private Edge updateRelation(
|
||||||
|
OrientGraph orientGraph,
|
||||||
|
JsonNode jsonNode,
|
||||||
|
@SuppressWarnings("rawtypes") Class<? extends Relation> relationClass,
|
||||||
|
boolean deferredCommit) throws ResourceRegistryException {
|
||||||
|
|
||||||
|
logger.debug("Trying to update {} : {}", relationClass.getSimpleName(),
|
||||||
|
jsonNode);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
String relationType = getClassProperty(jsonNode);
|
||||||
|
|
||||||
|
if (relationType == null || relationType.compareTo("") == 0) {
|
||||||
|
throw new ResourceRegistryException(
|
||||||
|
Relation.class.getSimpleName()
|
||||||
|
+ " Type cannot be empty or null");
|
||||||
|
}
|
||||||
|
|
||||||
|
SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
|
||||||
|
try {
|
||||||
|
schemaManagement.getTypeSchema(relationType,
|
||||||
|
relationClass.getSimpleName());
|
||||||
|
} catch (SchemaNotFoundException e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
UUID uuid = org.gcube.informationsystem.impl.utils.Utility
|
||||||
|
.getUUIDFromJsonNode(jsonNode);
|
||||||
|
|
||||||
|
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){
|
||||||
|
UUID targetUUID = org.gcube.informationsystem.impl.utils.Utility
|
||||||
|
.getUUIDFromJsonNode(target);
|
||||||
|
updateFacet(orientGraph, targetUUID, target, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!deferredCommit) {
|
||||||
|
orientGraph.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("{} {} successfully updated",
|
||||||
|
relationType, jsonNode);
|
||||||
|
|
||||||
|
return edge;
|
||||||
|
|
||||||
|
} catch (ResourceRegistryException rre) {
|
||||||
|
logger.error("Error Updating {} {} ", relationClass, jsonNode, rre);
|
||||||
|
if (orientGraph != null) {
|
||||||
|
orientGraph.rollback();
|
||||||
|
}
|
||||||
|
throw rre;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error Creating {} {}", relationClass, jsonNode, e);
|
||||||
|
if (orientGraph != null) {
|
||||||
|
orientGraph.rollback();
|
||||||
|
}
|
||||||
|
throw new ResourceRegistryException(e);
|
||||||
|
} finally {
|
||||||
|
if (orientGraph != null && !deferredCommit) {
|
||||||
|
orientGraph.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String createFacet(String facetType, String jsonRepresentation)
|
public String createFacet(String facetType, String jsonRepresentation)
|
||||||
throws ResourceRegistryException {
|
throws ResourceRegistryException {
|
||||||
|
@ -658,6 +802,93 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Element updateProperties(Element element, JsonNode jsonNode)
|
||||||
|
throws ResourceRegistryException {
|
||||||
|
Set<String> oldKeys = element.getPropertyKeys();
|
||||||
|
|
||||||
|
Map<String, Object> properties;
|
||||||
|
if(element instanceof Vertex){
|
||||||
|
properties = getVertexProperties(jsonNode);
|
||||||
|
}else if(element instanceof Edge){
|
||||||
|
properties = getEdgeProperties(jsonNode);
|
||||||
|
}else{
|
||||||
|
String error = String.format("Error while updating {} properties", element.toString());
|
||||||
|
throw new ResourceRegistryException(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
oldKeys.removeAll(properties.keySet());
|
||||||
|
|
||||||
|
for (String key : properties.keySet()) {
|
||||||
|
try {
|
||||||
|
element.setProperty(key, properties.get(key));
|
||||||
|
} catch (Exception e) {
|
||||||
|
String error = String.format(
|
||||||
|
"Error while setting property %s : %s", key, properties
|
||||||
|
.get(key).toString());
|
||||||
|
logger.error(error);
|
||||||
|
throw new ResourceRegistryException(error, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String key : oldKeys) {
|
||||||
|
if (key.startsWith("@") || key.startsWith("_")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (key.compareTo(Facet.HEADER_PROPERTY) == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
element.removeProperty(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
String entityType = getClassProperty(jsonNode);
|
||||||
|
Vertex facet = getEntity(orientGraph, uuid, entityType, Facet.class);
|
||||||
|
|
||||||
|
facet = (Vertex) updateProperties(facet, jsonNode);
|
||||||
|
|
||||||
|
((OrientVertex) facet).save();
|
||||||
|
|
||||||
|
if(!deferredCommit){
|
||||||
|
orientGraph.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
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, jsonNode, fnfe);
|
||||||
|
if (orientGraph != null) {
|
||||||
|
orientGraph.rollback();
|
||||||
|
}
|
||||||
|
throw fnfe;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.debug("Unable to update {} with UUID {} usign {}",
|
||||||
|
Facet.NAME, uuid, jsonNode, e);
|
||||||
|
if (orientGraph != null) {
|
||||||
|
orientGraph.rollback();
|
||||||
|
}
|
||||||
|
throw new ResourceRegistryException("Error Updating Facet",
|
||||||
|
e.getCause());
|
||||||
|
} finally {
|
||||||
|
if (orientGraph != null && !deferredCommit) {
|
||||||
|
orientGraph.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String updateFacet(UUID uuid, String jsonRepresentation)
|
public String updateFacet(UUID uuid, String jsonRepresentation)
|
||||||
throws ResourceRegistryException {
|
throws ResourceRegistryException {
|
||||||
|
@ -669,93 +900,20 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
|
||||||
JsonNode jsonNode = mapper.readTree(jsonRepresentation);
|
|
||||||
|
|
||||||
orientGraph = ContextUtility
|
orientGraph = ContextUtility
|
||||||
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||||
|
|
||||||
String entityType = getClassProperty(jsonNode);
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
Vertex facet = getEntity(orientGraph, uuid, entityType, Facet.class);
|
JsonNode jsonNode = mapper.readTree(jsonRepresentation);
|
||||||
|
return updateFacet(orientGraph, uuid, jsonNode, false);
|
||||||
Set<String> oldKeys = facet.getPropertyKeys();
|
} catch (IOException e) {
|
||||||
|
|
||||||
|
|
||||||
Set<String> ignoreKeys = new HashSet<>();
|
|
||||||
Map<String, Object> properties = null;
|
|
||||||
try {
|
|
||||||
properties = getPropertyMap(jsonNode, ignoreKeys);
|
|
||||||
} catch (Exception e) {
|
|
||||||
String error = "Error while parsing json to get Relation properties";
|
|
||||||
logger.error(error, e);
|
|
||||||
throw new ResourceRegistryException(error, e);
|
|
||||||
}
|
|
||||||
|
|
||||||
oldKeys.removeAll(properties.keySet());
|
|
||||||
|
|
||||||
for (String key : properties.keySet()) {
|
|
||||||
try {
|
|
||||||
facet.setProperty(key, properties.get(key));
|
|
||||||
} catch (Exception e) {
|
|
||||||
String error = String.format(
|
|
||||||
"Error while setting property %s : %s", key,
|
|
||||||
properties.get(key).toString());
|
|
||||||
logger.error(error);
|
|
||||||
throw new ResourceRegistryException(error, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
Iterator<Entry<String, JsonNode>> iterator = jsonNode.fields();
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
|
|
||||||
Entry<String, JsonNode> entry = iterator.next();
|
|
||||||
String key = entry.getKey();
|
|
||||||
|
|
||||||
if (key.startsWith("_")) {
|
|
||||||
oldKeys.remove(key);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (key.compareTo(Facet.HEADER_PROPERTY) == 0) {
|
|
||||||
oldKeys.remove(key);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
JsonNode value = entry.getValue();
|
|
||||||
facet.setProperty(key, value.asText());
|
|
||||||
|
|
||||||
oldKeys.remove(key);
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
for (String key : oldKeys) {
|
|
||||||
if (key.startsWith("_")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (key.compareTo(Facet.HEADER_PROPERTY) == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
facet.removeProperty(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
((OrientVertex) facet).save();
|
|
||||||
orientGraph.commit();
|
|
||||||
|
|
||||||
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 {}",
|
logger.debug("Unable to update {} with UUID {} usign {}",
|
||||||
Facet.NAME, uuid, jsonRepresentation, fnfe);
|
Facet.NAME, uuid, jsonRepresentation, e);
|
||||||
if (orientGraph != null) {
|
if (orientGraph != null) {
|
||||||
orientGraph.rollback();
|
orientGraph.rollback();
|
||||||
}
|
}
|
||||||
throw fnfe;
|
throw new ResourceRegistryException("Error Updating Facet",
|
||||||
|
e.getCause());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.debug("Unable to update {} with UUID {} usign {}",
|
logger.debug("Unable to update {} with UUID {} usign {}",
|
||||||
Facet.NAME, uuid, jsonRepresentation, e);
|
Facet.NAME, uuid, jsonRepresentation, e);
|
||||||
|
@ -765,7 +923,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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1078,6 +1236,70 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String updateResource(String resourceType, String jsonRepresentation)
|
||||||
|
throws ResourceNotFoundException, ResourceRegistryException {
|
||||||
|
logger.debug("Trying to create {} using {}", resourceType,
|
||||||
|
jsonRepresentation);
|
||||||
|
|
||||||
|
OrientGraph orientGraph = null;
|
||||||
|
try {
|
||||||
|
orientGraph = ContextUtility
|
||||||
|
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
||||||
|
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
JsonNode jsonNode = mapper.readTree(jsonRepresentation);
|
||||||
|
|
||||||
|
Vertex resource = getEntity(orientGraph, jsonNode, resourceType, Resource.class);
|
||||||
|
|
||||||
|
String property = lowerCaseFirstCharacter(ConsistsOf.NAME);
|
||||||
|
if (jsonNode.has(property)) {
|
||||||
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
||||||
|
for (JsonNode relationJsonNode : jsonNodeArray) {
|
||||||
|
updateRelation(orientGraph, relationJsonNode,
|
||||||
|
ConsistsOf.class, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
logger.error("Unable to create {} ({}) using {}", Resource.NAME,
|
||||||
|
resourceType, jsonRepresentation, e);
|
||||||
|
if (orientGraph != null) {
|
||||||
|
orientGraph.rollback();
|
||||||
|
}
|
||||||
|
throw new ResourceRegistryException(e);
|
||||||
|
} finally {
|
||||||
|
if (orientGraph != null) {
|
||||||
|
orientGraph.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteResource(UUID uuid) throws ResourceNotFoundException,
|
public boolean deleteResource(UUID uuid) throws ResourceNotFoundException,
|
||||||
ResourceRegistryException {
|
ResourceRegistryException {
|
||||||
|
@ -1171,16 +1393,15 @@ public class EntityManagementImpl implements EntityManagement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean addResourceToContext(UUID uuid)
|
public boolean addResourceToContext(UUID uuid)
|
||||||
throws ResourceNotFoundException, ContextNotFoundException,
|
throws ResourceNotFoundException, ContextNotFoundException,
|
||||||
ResourceRegistryException {
|
ResourceRegistryException {
|
||||||
return addEntityToContext(Resource.class, uuid);
|
return addEntityToContext(Resource.class, uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean addFacetToContext(UUID uuid) throws FacetNotFoundException,
|
public boolean addFacetToContext(UUID uuid) throws FacetNotFoundException,
|
||||||
ContextNotFoundException, ResourceRegistryException {
|
ContextNotFoundException, ResourceRegistryException {
|
||||||
return addEntityToContext(Facet.class, uuid);
|
return addEntityToContext(Facet.class, uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue