|
|
|
@ -4,13 +4,21 @@
|
|
|
|
|
package org.gcube.informationsystem.resourceregistry.resources.impl;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
import org.gcube.informationsystem.impl.utils.Entities;
|
|
|
|
|
import org.gcube.informationsystem.model.embedded.Embedded;
|
|
|
|
|
import org.gcube.informationsystem.model.entity.Entity;
|
|
|
|
|
import org.gcube.informationsystem.model.entity.Facet;
|
|
|
|
|
import org.gcube.informationsystem.model.entity.Resource;
|
|
|
|
|
import org.gcube.informationsystem.model.orientdb.impl.embedded.Header;
|
|
|
|
|
import org.gcube.informationsystem.model.relation.ConsistsOf;
|
|
|
|
|
import org.gcube.informationsystem.model.relation.IsRelatedTo;
|
|
|
|
|
import org.gcube.informationsystem.model.relation.Relation;
|
|
|
|
@ -26,9 +34,12 @@ import org.gcube.informationsystem.resourceregistry.resources.utils.Utility;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonParseException;
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.node.JsonNodeType;
|
|
|
|
|
import com.tinkerpop.blueprints.Edge;
|
|
|
|
|
import com.tinkerpop.blueprints.Vertex;
|
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
|
|
|
|
@ -94,15 +105,261 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String createVertexEntity(String entityType,
|
|
|
|
|
Class<? extends Entity> entity, String jsonRepresentation)
|
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
|
OrientGraph orientGraph = null;
|
|
|
|
|
private static String lowerCaseFirstCharacter(String string) {
|
|
|
|
|
return string.substring(0, 1).toLowerCase() + string.substring(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String getClassProperty(JsonNode jsonNode) {
|
|
|
|
|
if (jsonNode.has(Entities.CLASS_PROPERTY)) {
|
|
|
|
|
return jsonNode.get(Entities.CLASS_PROPERTY).asText();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Header getHeader(JsonNode jsonNode)
|
|
|
|
|
throws JsonParseException, JsonMappingException, IOException {
|
|
|
|
|
if (jsonNode.has(Resource.HEADER_PROPERTY)) {
|
|
|
|
|
JsonNode header = jsonNode.get(Resource.HEADER_PROPERTY);
|
|
|
|
|
if (header.isNull()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
return mapper.readValue(header.toString(), Header.class);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Entity checkTypeAndGetHeader(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;
|
|
|
|
|
}
|
|
|
|
|
boolean validType = false;
|
|
|
|
|
boolean notEmbedded = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Header can be ignored or managed. Actually is managed.
|
|
|
|
|
if(validType && notEmbedded){
|
|
|
|
|
Header header = null;
|
|
|
|
|
try {
|
|
|
|
|
header = getHeader(jsonNode);
|
|
|
|
|
} 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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Object getObejctFromElement(JsonNode value) throws ResourceRegistryException {
|
|
|
|
|
JsonNodeType jsonNodeType = value.getNodeType();
|
|
|
|
|
switch (jsonNodeType) {
|
|
|
|
|
case OBJECT:
|
|
|
|
|
// TODO
|
|
|
|
|
checkTypeAndGetHeader(value);
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
case ARRAY:
|
|
|
|
|
List<Object> array = new ArrayList<>();
|
|
|
|
|
Iterator<JsonNode> 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 null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Map<String, Object> getPropertyMap(JsonNode jsonNode,
|
|
|
|
|
Set<String> ignoreKeys) throws JsonProcessingException, IOException{
|
|
|
|
|
|
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
Iterator<Entry<String, JsonNode>> fields = jsonNode.fields();
|
|
|
|
|
while (fields.hasNext()) {
|
|
|
|
|
Entry<String, JsonNode> entry = fields.next();
|
|
|
|
|
|
|
|
|
|
String key = entry.getKey();
|
|
|
|
|
if (key.compareTo(Relation.HEADER_PROPERTY) == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (key.startsWith("@") || key.startsWith("_")) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (ignoreKeys.contains(key)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JsonNode value = entry.getValue();
|
|
|
|
|
Object object = null;
|
|
|
|
|
try {
|
|
|
|
|
object = getObejctFromElement(value);
|
|
|
|
|
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 createRelation(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);
|
|
|
|
|
|
|
|
|
|
Header targetHeader = null;
|
|
|
|
|
try {
|
|
|
|
|
targetHeader = getHeader(target);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
new ResourceRegistryException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Class<? extends Entity> targetClass = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Vertex targetVertex = null;
|
|
|
|
|
if (targetHeader == null) {
|
|
|
|
|
if (ConsistsOf.class.isAssignableFrom(relation)) {
|
|
|
|
|
targetVertex = createVertexEntity(
|
|
|
|
|
orientGraph, getClassProperty(target),
|
|
|
|
|
Facet.class,
|
|
|
|
|
target.toString(), true);
|
|
|
|
|
targetClass = Facet.class;
|
|
|
|
|
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);
|
|
|
|
|
logger.error(error);
|
|
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
// The target Entity was already created we just need to create
|
|
|
|
|
// the right relation
|
|
|
|
|
|
|
|
|
|
if(ConsistsOf.class.isAssignableFrom(relation)) {
|
|
|
|
|
targetClass = Facet.class;
|
|
|
|
|
}else if(IsRelatedTo.class.isAssignableFrom(relation)){
|
|
|
|
|
targetClass = Resource.class;
|
|
|
|
|
}else{
|
|
|
|
|
String error = String.format(
|
|
|
|
|
"%s Unsupported %s creation", relation.toString(),
|
|
|
|
|
Relation.NAME);
|
|
|
|
|
logger.error(error);
|
|
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String targetUUID = targetHeader.getUUID().toString();
|
|
|
|
|
String entityType = getClassProperty(target);
|
|
|
|
|
|
|
|
|
|
targetVertex = getEntity(orientGraph, targetUUID, entityType, targetClass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String relationType = getClassProperty(node);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Set<String> ignoreKeys = new HashSet<>();
|
|
|
|
|
ignoreKeys.add(Relation.TARGET_PROPERTY);
|
|
|
|
|
ignoreKeys.add(Relation.SOURCE_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,
|
|
|
|
|
relationType, relation, edgeProperties, true);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vertex createVertexEntity(String entityType,
|
|
|
|
|
Class<? extends Entity> entity, String jsonRepresentation,
|
|
|
|
|
boolean deferredCommit) throws ResourceRegistryException {
|
|
|
|
|
OrientGraph orientGraph = ContextUtility
|
|
|
|
|
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
|
|
|
|
return createVertexEntity(orientGraph, entityType, entity, jsonRepresentation,
|
|
|
|
|
deferredCommit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Vertex createVertexEntity(OrientGraph orientGraph, String entityType,
|
|
|
|
|
Class<? extends Entity> entity, String jsonRepresentation,
|
|
|
|
|
boolean deferredCommit) throws ResourceRegistryException {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
orientGraph = ContextUtility
|
|
|
|
|
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
|
|
|
|
|
try {
|
|
|
|
|
schemaManagement.getTypeSchema(entityType,
|
|
|
|
@ -113,33 +370,58 @@ 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
|
|
|
|
|
.format("Declared resourceType does not match with json representation %s!=%s",
|
|
|
|
|
entityType, type);
|
|
|
|
|
logger.trace(error);
|
|
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OrientVertex entityVertex = orientGraph.addVertex("class:"
|
|
|
|
|
OrientVertex vertex = orientGraph.addVertex("class:"
|
|
|
|
|
+ entityType);
|
|
|
|
|
|
|
|
|
|
Header entityHeader = getHeader(jsonNode);
|
|
|
|
|
if (entityHeader != null) {
|
|
|
|
|
vertex.setProperty(Entity.HEADER_PROPERTY, entityHeader);
|
|
|
|
|
} else {
|
|
|
|
|
entityHeader = HeaderUtility.addHeader(vertex, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Resource.class.isAssignableFrom(entity)) {
|
|
|
|
|
// TODO
|
|
|
|
|
// Facet and relation are created in calling method
|
|
|
|
|
} else {
|
|
|
|
|
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();
|
|
|
|
|
entityVertex.setProperty(entry.getKey(), value.asText());
|
|
|
|
|
vertex.setProperty(entry.getKey(), value.asText());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HeaderUtility.addHeader(entityVertex, null);
|
|
|
|
|
ContextUtility.addToActualContext(orientGraph, entityVertex);
|
|
|
|
|
ContextUtility.addToActualContext(orientGraph, vertex);
|
|
|
|
|
|
|
|
|
|
entityVertex.save();
|
|
|
|
|
orientGraph.commit();
|
|
|
|
|
vertex.save();
|
|
|
|
|
|
|
|
|
|
logger.trace("Created {} is {} orientVertexToJsonString", Vertex.class.getSimpleName(),
|
|
|
|
|
Utility.orientVertexToJsonString((OrientVertex) entityVertex, true));
|
|
|
|
|
|
|
|
|
|
return Utility.orientVertexToJsonString((OrientVertex) entityVertex, false);
|
|
|
|
|
//return Utility.orientVertexToJsonString((OrientVertex) entityVertex);
|
|
|
|
|
|
|
|
|
|
if (!deferredCommit) {
|
|
|
|
|
orientGraph.commit();
|
|
|
|
|
|
|
|
|
|
logger.trace("Created {} is {} orientVertexToJsonString",
|
|
|
|
|
Vertex.class.getSimpleName(), Utility
|
|
|
|
|
.orientVertexToJsonString(
|
|
|
|
|
(OrientVertex) vertex, true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return vertex;
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if (orientGraph != null) {
|
|
|
|
@ -148,17 +430,19 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
throw new ResourceRegistryException("Error Creating " + entityType
|
|
|
|
|
+ " with " + jsonRepresentation, e.getCause());
|
|
|
|
|
} finally {
|
|
|
|
|
if (orientGraph != null) {
|
|
|
|
|
if (orientGraph != null && !deferredCommit) {
|
|
|
|
|
orientGraph.shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String createEdgeRelation(String sourceUUID,
|
|
|
|
|
Class<? extends Entity> sourceClass, String targetUUID,
|
|
|
|
|
Class<? extends Entity> targetClass, String relationType,
|
|
|
|
|
String jsonProperties) throws FacetNotFoundException,
|
|
|
|
|
ResourceNotFoundException, ResourceRegistryException {
|
|
|
|
|
public Edge createEdgeRelation(
|
|
|
|
|
String sourceUUID, Class<? extends Entity> sourceClass,
|
|
|
|
|
String targetUUID, Class<? extends Entity> targetClass,
|
|
|
|
|
String relationType, @SuppressWarnings("rawtypes") Class<? extends Relation> relationBaseClass,
|
|
|
|
|
String jsonProperties)
|
|
|
|
|
throws FacetNotFoundException, ResourceNotFoundException,
|
|
|
|
|
ResourceRegistryException {
|
|
|
|
|
OrientGraph orientGraph = null;
|
|
|
|
|
|
|
|
|
|
if (relationType == null || relationType.compareTo("") == 0) {
|
|
|
|
@ -170,20 +454,63 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
orientGraph = ContextUtility
|
|
|
|
|
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
|
|
|
|
|
|
|
|
|
SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
|
|
|
|
|
try {
|
|
|
|
|
schemaManagement.getTypeSchema(relationType, Relation.NAME);
|
|
|
|
|
} catch (SchemaNotFoundException e) {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vertex source = getEntity(orientGraph, sourceUUID, null,
|
|
|
|
|
sourceClass);
|
|
|
|
|
Vertex target = getEntity(orientGraph, targetUUID, null,
|
|
|
|
|
targetClass);
|
|
|
|
|
|
|
|
|
|
Map<String, Object> edgeProperties = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
if (jsonProperties != null && jsonProperties.compareTo("") != 0) {
|
|
|
|
|
try {
|
|
|
|
|
Set<String> 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) {
|
|
|
|
|
new ResourceRegistryException(
|
|
|
|
|
"Error while setting Relation Properties", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return createEdgeRelation(orientGraph, source, target,
|
|
|
|
|
relationType, relationBaseClass, edgeProperties, false);
|
|
|
|
|
|
|
|
|
|
} catch(ResourceNotFoundException rnfe){
|
|
|
|
|
throw rnfe;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Edge createEdgeRelation(OrientGraph orientGraph,
|
|
|
|
|
Vertex source, Vertex target,
|
|
|
|
|
String relationType, @SuppressWarnings("rawtypes") Class<? extends Relation> relationBaseClass,
|
|
|
|
|
Map<String, Object> edgeProperties,
|
|
|
|
|
boolean deferredCommit) throws ResourceRegistryException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO Check if in and out types are compatible with the relation
|
|
|
|
|
// type as defined in relation type
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
|
|
|
|
|
try {
|
|
|
|
|
schemaManagement.getTypeSchema(relationType, relationBaseClass.getClass().getSimpleName());
|
|
|
|
|
} catch (SchemaNotFoundException 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 beetween source and target
|
|
|
|
|
|
|
|
|
|
logger.trace("Creating {} ({}) beetween {} -> {}",
|
|
|
|
|
Relation.class.getSimpleName(), relationType,
|
|
|
|
@ -192,43 +519,39 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
|
|
|
|
|
Edge edge = orientGraph.addEdge(null, source, target, relationType);
|
|
|
|
|
|
|
|
|
|
if (jsonProperties != null && jsonProperties.compareTo("") != 0) {
|
|
|
|
|
for(String key : edgeProperties.keySet()){
|
|
|
|
|
try {
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
JsonNode jsonNode = mapper.readTree(jsonProperties);
|
|
|
|
|
|
|
|
|
|
Iterator<Entry<String, JsonNode>> iterator = jsonNode
|
|
|
|
|
.fields();
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
|
Entry<String, JsonNode> entry = iterator.next();
|
|
|
|
|
try {
|
|
|
|
|
JsonNode value = entry.getValue();
|
|
|
|
|
edge.setProperty(entry.getKey(), value.asText());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new ResourceRegistryException(
|
|
|
|
|
"Error while setting property"
|
|
|
|
|
+ String.valueOf(entry), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
edge.setProperty(key, edgeProperties.get(key));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
new ResourceRegistryException(
|
|
|
|
|
"Error while setting Relation Properties", e);
|
|
|
|
|
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();
|
|
|
|
|
orientGraph.commit();
|
|
|
|
|
|
|
|
|
|
return Utility.orientEdgeToJsonString((OrientEdge) edge);
|
|
|
|
|
} catch (ResourceNotFoundException rnfe) {
|
|
|
|
|
throw rnfe;
|
|
|
|
|
if (!deferredCommit) {
|
|
|
|
|
orientGraph.commit();
|
|
|
|
|
}
|
|
|
|
|
return edge;
|
|
|
|
|
|
|
|
|
|
} catch (ResourceRegistryException rre) {
|
|
|
|
|
if (orientGraph!=null) {
|
|
|
|
|
orientGraph.rollback();
|
|
|
|
|
}
|
|
|
|
|
throw rre;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new ResourceNotFoundException(e.getMessage());
|
|
|
|
|
if (orientGraph!=null) {
|
|
|
|
|
orientGraph.rollback();
|
|
|
|
|
}
|
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
|
} finally {
|
|
|
|
|
if (orientGraph != null) {
|
|
|
|
|
if (orientGraph!= null && !deferredCommit) {
|
|
|
|
|
orientGraph.shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -237,7 +560,8 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
@Override
|
|
|
|
|
public String createFacet(String facetType, String jsonRepresentation)
|
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
|
return createVertexEntity(facetType, Facet.class, jsonRepresentation);
|
|
|
|
|
Vertex vertex = createVertexEntity(facetType, Facet.class, jsonRepresentation, false);
|
|
|
|
|
return Utility.orientVertexToJsonString((OrientVertex) vertex, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -257,8 +581,10 @@ 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.orientVertexToJsonString((OrientVertex) facet, true));
|
|
|
|
|
|
|
|
|
|
logger.trace("{} of type {} with UUID {} is {}", Facet.NAME,
|
|
|
|
|
facetType, uuid, Utility.orientVertexToJsonString(
|
|
|
|
|
(OrientVertex) facet, true));
|
|
|
|
|
|
|
|
|
|
return Utility.orientVertexToJsonString((OrientVertex) facet, true);
|
|
|
|
|
} catch (FacetNotFoundException fnfe) {
|
|
|
|
|
throw fnfe;
|
|
|
|
@ -322,11 +648,12 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
((OrientVertex) facet).save();
|
|
|
|
|
orientGraph.commit();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.trace("{} with UUID {} has been updated {}", Facet.NAME, uuid, Utility.orientVertexToJsonString((OrientVertex) facet, true));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Utility.orientVertexToJsonString((OrientVertex) facet, false);
|
|
|
|
|
logger.trace("{} with UUID {} has been updated {}", Facet.NAME,
|
|
|
|
|
uuid, Utility.orientVertexToJsonString(
|
|
|
|
|
(OrientVertex) facet, true));
|
|
|
|
|
|
|
|
|
|
return Utility
|
|
|
|
|
.orientVertexToJsonString((OrientVertex) facet, false);
|
|
|
|
|
|
|
|
|
|
} catch (FacetNotFoundException fnfe) {
|
|
|
|
|
if (orientGraph != null) {
|
|
|
|
@ -383,8 +710,11 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
String consistOfType, String jsonProperties)
|
|
|
|
|
throws FacetNotFoundException, ResourceNotFoundException,
|
|
|
|
|
ResourceRegistryException {
|
|
|
|
|
return createEdgeRelation(resourceUUID, Resource.class, facetUUID,
|
|
|
|
|
Facet.class, consistOfType, jsonProperties);
|
|
|
|
|
Edge edge = createEdgeRelation(resourceUUID, Resource.class,
|
|
|
|
|
facetUUID, Facet.class,
|
|
|
|
|
consistOfType, ConsistsOf.class,
|
|
|
|
|
jsonProperties);
|
|
|
|
|
return Utility.orientEdgeToJsonString((OrientEdge) edge, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -396,8 +726,8 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
orientGraph = ContextUtility
|
|
|
|
|
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
|
|
|
|
|
|
|
|
|
Edge edge = getRelation(orientGraph, consistOfUUID, ConsistsOf.NAME,
|
|
|
|
|
ConsistsOf.class);
|
|
|
|
|
Edge edge = getRelation(orientGraph, consistOfUUID,
|
|
|
|
|
ConsistsOf.NAME, ConsistsOf.class);
|
|
|
|
|
|
|
|
|
|
edge.remove();
|
|
|
|
|
orientGraph.commit();
|
|
|
|
@ -426,9 +756,11 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
String targetResourceUuid, String relatedToType,
|
|
|
|
|
String jsonProperties) throws ResourceNotFoundException,
|
|
|
|
|
ResourceRegistryException {
|
|
|
|
|
return createEdgeRelation(sourceResourceUuid, Resource.class,
|
|
|
|
|
targetResourceUuid, Resource.class, relatedToType,
|
|
|
|
|
Edge edge = createEdgeRelation(sourceResourceUuid, Resource.class,
|
|
|
|
|
targetResourceUuid, Resource.class,
|
|
|
|
|
relatedToType, IsRelatedTo.class,
|
|
|
|
|
jsonProperties);
|
|
|
|
|
return Utility.orientEdgeToJsonString((OrientEdge) edge, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -440,8 +772,8 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
orientGraph = ContextUtility
|
|
|
|
|
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
|
|
|
|
|
|
|
|
|
Edge edge = getRelation(orientGraph, relatedToUUID, IsRelatedTo.NAME,
|
|
|
|
|
IsRelatedTo.class);
|
|
|
|
|
Edge edge = getRelation(orientGraph, relatedToUUID,
|
|
|
|
|
IsRelatedTo.NAME, IsRelatedTo.class);
|
|
|
|
|
|
|
|
|
|
edge.remove();
|
|
|
|
|
orientGraph.commit();
|
|
|
|
@ -464,18 +796,49 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String marshallResource(Vertex vertex){
|
|
|
|
|
// TODO serialize all facets as weel
|
|
|
|
|
return Utility.orientVertexToJsonString((OrientVertex) vertex, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String createResource(String resourceType, String jsonRepresentation)
|
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
|
|
|
|
|
|
logger.trace("Trying to create {} : {}", resourceType,
|
|
|
|
|
jsonRepresentation);
|
|
|
|
|
|
|
|
|
|
OrientGraph orientGraph;
|
|
|
|
|
try {
|
|
|
|
|
JsonNode jsonNode = org.gcube.informationsystem.impl.utils.Utility.getJSONNode(jsonRepresentation);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
orientGraph = ContextUtility
|
|
|
|
|
.getActualSecurityContextGraph(PermissionMode.WRITER);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
createRelation(orientGraph, resource, jsonNodeArray, ConsistsOf.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
property = lowerCaseFirstCharacter(IsRelatedTo.NAME);
|
|
|
|
|
if (jsonNode.has(property)) {
|
|
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
|
|
|
|
createRelation(orientGraph, resource, jsonNodeArray, IsRelatedTo.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return marshallResource(resource);
|
|
|
|
|
|
|
|
|
|
} catch(ResourceRegistryException rre) {
|
|
|
|
|
throw rre;
|
|
|
|
|
} catch(Exception e){
|
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return createVertexEntity(resourceType, Resource.class,
|
|
|
|
|
jsonRepresentation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -495,11 +858,11 @@ public class EntityManagementImpl implements EntityManagement {
|
|
|
|
|
Vertex resource = getEntity(orientGraph, uuid, resourceType,
|
|
|
|
|
Resource.class);
|
|
|
|
|
|
|
|
|
|
// TODO get all attached facets
|
|
|
|
|
logger.trace("{} of type {} with UUID {} is {}", Resource.NAME, resourceType, uuid, Utility.orientVertexToJsonString((OrientVertex) resource, true));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Utility.orientVertexToJsonString((OrientVertex) resource, false);
|
|
|
|
|
logger.trace("{} of type {} with UUID {} is {}", Resource.NAME,
|
|
|
|
|
resourceType, uuid, Utility.orientVertexToJsonString(
|
|
|
|
|
(OrientVertex) resource, true));
|
|
|
|
|
|
|
|
|
|
return marshallResource(resource);
|
|
|
|
|
} catch (ResourceNotFoundException rnfe) {
|
|
|
|
|
throw rnfe;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
@ -523,6 +886,8 @@ 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();
|
|
|
|
|