2016-12-19 14:59:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2016-12-27 11:27:01 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.er.relation;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-01-12 11:13:09 +01:00
|
|
|
import java.util.ArrayList;
|
2017-01-17 16:53:24 +01:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
2016-12-19 14:59:27 +01:00
|
|
|
import java.util.Iterator;
|
2017-01-12 11:13:09 +01:00
|
|
|
import java.util.List;
|
2017-01-17 16:53:24 +01:00
|
|
|
import java.util.Map;
|
2016-12-19 14:59:27 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2017-01-11 12:06:50 +01:00
|
|
|
import org.codehaus.jettison.json.JSONArray;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.codehaus.jettison.json.JSONObject;
|
2017-03-29 16:53:37 +02:00
|
|
|
import org.gcube.informationsystem.model.AccessType;
|
2016-12-20 16:51:57 +01:00
|
|
|
import org.gcube.informationsystem.model.embedded.PropagationConstraint;
|
2016-12-20 18:36:17 +01:00
|
|
|
import org.gcube.informationsystem.model.embedded.PropagationConstraint.AddConstraint;
|
2016-12-20 16:51:57 +01:00
|
|
|
import org.gcube.informationsystem.model.embedded.PropagationConstraint.RemoveConstraint;
|
2016-12-19 14:59:27 +01:00
|
|
|
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.relation.Relation;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
|
2017-11-17 11:42:37 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.er.ERNotFoundException;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.relation.RelationNotFoundException;
|
2017-11-17 11:42:37 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaException;
|
2016-12-22 17:33:10 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.context.ContextUtility;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.context.SecurityContextMapper.PermissionMode;
|
2016-12-27 11:27:01 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
|
2017-11-17 12:31:22 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility;
|
2016-12-27 11:27:01 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.er.entity.EntityManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement;
|
2016-12-22 17:27:26 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.utils.Utility;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import com.tinkerpop.blueprints.Direction;
|
|
|
|
import com.tinkerpop.blueprints.Edge;
|
|
|
|
import com.tinkerpop.blueprints.Vertex;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientEdge;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("rawtypes")
|
2017-11-17 15:59:25 +01:00
|
|
|
public abstract class RelationManagement<R extends Relation, S extends EntityManagement, T extends EntityManagement>
|
|
|
|
extends ERManagement<R, Edge> {
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
protected final Class<? extends Entity> targetEntityClass;
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 18:41:02 +01:00
|
|
|
protected S sourceEntityManagement;
|
|
|
|
protected T targetEntityManagement;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-03-29 16:53:37 +02:00
|
|
|
protected RelationManagement(AccessType accessType) {
|
|
|
|
super(accessType);
|
2016-12-30 17:31:12 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
this.ignoreKeys.add(Relation.HEADER_PROPERTY);
|
|
|
|
this.ignoreKeys.add(Relation.TARGET_PROPERTY);
|
|
|
|
this.ignoreKeys.add(Relation.SOURCE_PROPERTY);
|
|
|
|
this.ignoreKeys.add(OrientBaseGraph.CONNECTION_IN.toLowerCase());
|
|
|
|
this.ignoreKeys.add(OrientBaseGraph.CONNECTION_OUT.toLowerCase());
|
|
|
|
this.ignoreKeys.add(OrientBaseGraph.CONNECTION_IN.toUpperCase());
|
|
|
|
this.ignoreKeys.add(OrientBaseGraph.CONNECTION_OUT.toUpperCase());
|
|
|
|
|
2017-03-29 16:53:37 +02:00
|
|
|
switch (accessType) {
|
2017-11-17 15:59:25 +01:00
|
|
|
case CONSISTS_OF:
|
|
|
|
this.targetEntityClass = Facet.class;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IS_RELATED_TO:
|
|
|
|
this.targetEntityClass = Resource.class;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
this.targetEntityClass = Resource.class;
|
|
|
|
break;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 17:34:29 +01:00
|
|
|
sourceEntityManagement = null;
|
|
|
|
targetEntityManagement = null;
|
2017-03-29 16:53:37 +02:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
|
2017-03-29 16:53:37 +02:00
|
|
|
protected RelationManagement(AccessType accessType, OrientGraph orientGraph) {
|
|
|
|
this(accessType);
|
2016-12-19 14:59:27 +01:00
|
|
|
this.orientGraph = orientGraph;
|
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 18:41:02 +01:00
|
|
|
/*
|
|
|
|
* Needed for ResourceManagement.serializeAsJson() function to check that sourceEntityManagement is the same of
|
|
|
|
* the instance is creating this RelationManagement. TODO Look for a workaround
|
|
|
|
*/
|
|
|
|
public S giveMeSourceEntityManagementAsIs() throws ResourceRegistryException {
|
|
|
|
return sourceEntityManagement;
|
|
|
|
}
|
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2017-11-17 18:10:35 +01:00
|
|
|
public S getSourceEntityManagement() throws ResourceRegistryException {
|
2017-11-17 17:34:29 +01:00
|
|
|
if (sourceEntityManagement == null) {
|
2017-11-17 15:59:25 +01:00
|
|
|
Vertex source = getElement().getVertex(Direction.OUT);
|
2017-11-17 17:34:29 +01:00
|
|
|
sourceEntityManagement = newSourceEntityManagement();
|
|
|
|
sourceEntityManagement.setElement(source);
|
2017-11-17 15:59:25 +01:00
|
|
|
}
|
2017-11-21 18:18:11 +01:00
|
|
|
sourceEntityManagement.setReload(reload);
|
2017-11-17 17:34:29 +01:00
|
|
|
return sourceEntityManagement;
|
2017-11-17 15:59:25 +01:00
|
|
|
}
|
2017-11-17 16:04:15 +01:00
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2017-11-17 17:34:29 +01:00
|
|
|
public T getTargetEntityManagement() throws ResourceRegistryException {
|
|
|
|
if (targetEntityManagement == null) {
|
2017-11-17 15:59:25 +01:00
|
|
|
Vertex target = getElement().getVertex(Direction.IN);
|
2017-11-17 17:34:29 +01:00
|
|
|
targetEntityManagement = newTargetEntityManagement();
|
|
|
|
targetEntityManagement.setElement(target);
|
2017-11-17 15:59:25 +01:00
|
|
|
}
|
2017-11-21 18:18:11 +01:00
|
|
|
targetEntityManagement.setReload(reload);
|
2017-11-17 17:34:29 +01:00
|
|
|
return targetEntityManagement;
|
2017-11-17 15:59:25 +01:00
|
|
|
}
|
2017-11-17 16:04:15 +01:00
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
public void setSourceEntityManagement(S sourceEntityManagement) {
|
2017-11-17 17:34:29 +01:00
|
|
|
this.sourceEntityManagement = sourceEntityManagement;
|
2017-11-17 15:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setTargetEntityManagement(T targetEntityManagement) {
|
2017-11-17 17:34:29 +01:00
|
|
|
this.targetEntityManagement = targetEntityManagement;
|
2017-11-17 15:59:25 +01:00
|
|
|
}
|
2017-11-17 16:04:15 +01:00
|
|
|
|
2017-01-12 11:13:09 +01:00
|
|
|
@Override
|
2016-12-19 14:59:27 +01:00
|
|
|
public String serialize() throws ResourceRegistryException {
|
2016-12-19 16:25:29 +01:00
|
|
|
return serializeAsJson().toString();
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-12 11:13:09 +01:00
|
|
|
@Override
|
2016-12-19 14:59:27 +01:00
|
|
|
public JSONObject serializeAsJson() throws ResourceRegistryException {
|
2017-11-17 11:42:37 +01:00
|
|
|
return serializeAsJson(true, true);
|
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
public JSONObject serializeAsJson(boolean includeSource, boolean includeTarget) throws ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
JSONObject relation = serializeSelfOnly();
|
2016-12-19 16:25:29 +01:00
|
|
|
|
|
|
|
try {
|
2017-11-17 15:59:25 +01:00
|
|
|
if (includeSource) {
|
2017-11-21 18:18:11 +01:00
|
|
|
EntityManagement sourceEntityManagement = getSourceEntityManagement();
|
|
|
|
relation.put(Relation.SOURCE_PROPERTY, sourceEntityManagement.serializeSelfOnly());
|
2017-11-17 11:42:37 +01:00
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
|
|
|
if (includeTarget) {
|
2017-11-21 18:18:11 +01:00
|
|
|
EntityManagement targetEntityManagement = getTargetEntityManagement();
|
|
|
|
relation.put(Relation.TARGET_PROPERTY, targetEntityManagement.serializeAsJson());
|
2017-11-17 11:42:37 +01:00
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-04-14 14:37:57 +02:00
|
|
|
} catch (ResourceRegistryException e) {
|
2017-11-23 09:39:21 +01:00
|
|
|
logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e);
|
2017-04-14 14:37:57 +02:00
|
|
|
throw e;
|
2017-03-28 16:02:23 +02:00
|
|
|
} catch (Exception e) {
|
2017-11-23 09:39:21 +01:00
|
|
|
logger.error("Unable to correctly serialize {}. {}", element, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE, e);
|
2017-04-14 14:37:57 +02:00
|
|
|
throw new ResourceRegistryException(e);
|
2016-12-19 16:25:29 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
return relation;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
protected Map<String, JSONObject> fullSerialize(Map<String, JSONObject> visitedSourceResources)
|
2017-01-17 16:53:24 +01:00
|
|
|
throws ResourceRegistryException {
|
2017-11-21 18:18:11 +01:00
|
|
|
|
|
|
|
Vertex source = getElement().getVertex(Direction.OUT);
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
String id = source.getId().toString();
|
|
|
|
|
|
|
|
JSONObject sourceResource = visitedSourceResources.get(id);
|
2017-11-21 18:18:11 +01:00
|
|
|
ResourceManagement resourceManagement = null;
|
2017-11-17 16:04:15 +01:00
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
if (sourceResource == null) {
|
2017-11-21 18:18:11 +01:00
|
|
|
resourceManagement = (ResourceManagement) ERManagementUtility.getEntityManagement(orientGraph, source);
|
2017-01-17 16:53:24 +01:00
|
|
|
if (this instanceof IsRelatedToManagement) {
|
|
|
|
sourceResource = resourceManagement.serializeAsJson();
|
|
|
|
} else if (this instanceof ConsistsOfManagement) {
|
|
|
|
sourceResource = resourceManagement.serializeSelfOnly();
|
|
|
|
} else {
|
2017-11-17 15:59:25 +01:00
|
|
|
String error = String.format(
|
2017-11-23 09:39:21 +01:00
|
|
|
"{%s is not a %s nor a %s. %s",
|
|
|
|
this, IsRelatedToManagement.class.getSimpleName(), ConsistsOfManagement.class.getSimpleName(),
|
|
|
|
Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2017-01-17 16:53:24 +01:00
|
|
|
throw new ResourceRegistryException(error);
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 18:18:11 +01:00
|
|
|
|
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
if (this instanceof IsRelatedToManagement) {
|
2017-11-17 15:59:25 +01:00
|
|
|
sourceResource = ResourceManagement.addIsRelatedTo(sourceResource, serializeAsJson());
|
2017-01-17 16:53:24 +01:00
|
|
|
} else if (this instanceof ConsistsOfManagement) {
|
2017-11-17 15:59:25 +01:00
|
|
|
sourceResource = ResourceManagement.addConsistsOf(sourceResource, serializeAsJson());
|
2017-01-17 16:53:24 +01:00
|
|
|
} else {
|
2017-11-17 15:59:25 +01:00
|
|
|
String error = String.format(
|
2017-11-23 09:39:21 +01:00
|
|
|
"{%s is not a %s nor a %s. %s",
|
|
|
|
this, IsRelatedToManagement.class.getSimpleName(), ConsistsOfManagement.class.getSimpleName(),
|
|
|
|
Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2017-01-17 16:53:24 +01:00
|
|
|
throw new ResourceRegistryException(error);
|
2017-01-13 12:21:35 +01:00
|
|
|
}
|
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
visitedSourceResources.put(id, sourceResource);
|
|
|
|
|
|
|
|
return visitedSourceResources;
|
2017-01-13 12:21:35 +01:00
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
@Override
|
|
|
|
protected Edge reallyCreate() throws ResourceRegistryException {
|
2016-12-20 18:36:17 +01:00
|
|
|
|
2017-11-17 17:34:29 +01:00
|
|
|
if (sourceEntityManagement == null) {
|
2017-11-17 15:59:25 +01:00
|
|
|
|
|
|
|
if (!jsonNode.has(Relation.SOURCE_PROPERTY)) {
|
|
|
|
throw new ResourceRegistryException("Error while creating relation. No source definition found");
|
2017-11-17 11:42:37 +01:00
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
UUID sourceUUID = org.gcube.informationsystem.impl.utils.Utility
|
|
|
|
.getUUIDFromJsonNode(jsonNode.get(Relation.SOURCE_PROPERTY));
|
2017-11-17 16:04:15 +01:00
|
|
|
|
2017-11-17 17:34:29 +01:00
|
|
|
sourceEntityManagement = newSourceEntityManagement();
|
|
|
|
sourceEntityManagement.setUUID(sourceUUID);
|
2016-12-20 18:36:17 +01:00
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 17:34:29 +01:00
|
|
|
if (targetEntityManagement == null) {
|
|
|
|
targetEntityManagement = newTargetEntityManagement();
|
2016-12-20 18:36:17 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
if (!jsonNode.has(Relation.TARGET_PROPERTY)) {
|
2017-11-17 15:59:25 +01:00
|
|
|
throw new ResourceRegistryException("Error while creating " + erType + ". No target definition found");
|
2017-11-17 11:42:37 +01:00
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
try {
|
2017-11-17 17:34:29 +01:00
|
|
|
targetEntityManagement.setJSON(jsonNode.get(Relation.TARGET_PROPERTY));
|
2017-11-17 15:59:25 +01:00
|
|
|
} catch (SchemaException e) {
|
2017-11-17 18:10:35 +01:00
|
|
|
StringBuilder errorMessage = new StringBuilder();
|
2017-11-17 11:42:37 +01:00
|
|
|
errorMessage.append("A ");
|
|
|
|
errorMessage.append(erType);
|
|
|
|
errorMessage.append(" can be only created beetween ");
|
2017-11-17 17:34:29 +01:00
|
|
|
errorMessage.append(sourceEntityManagement.getAccessType().getName());
|
2017-11-17 11:42:37 +01:00
|
|
|
errorMessage.append(" and ");
|
2017-11-17 17:34:29 +01:00
|
|
|
errorMessage.append(targetEntityManagement.getAccessType().getName());
|
2017-11-17 11:42:37 +01:00
|
|
|
throw new ResourceRegistryException(errorMessage.toString(), e);
|
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
try {
|
2017-11-17 17:34:29 +01:00
|
|
|
targetEntityManagement.getElement();
|
2017-11-17 11:42:37 +01:00
|
|
|
} catch (Exception e) {
|
2017-11-17 17:34:29 +01:00
|
|
|
targetEntityManagement.internalCreate();
|
2017-11-17 11:42:37 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 17:34:29 +01:00
|
|
|
logger.trace("Creating {} beetween {} -> {}", erType, getSourceEntityManagement().serialize(),
|
|
|
|
getTargetEntityManagement().serialize());
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 17:34:29 +01:00
|
|
|
Vertex source = (Vertex) getSourceEntityManagement().getElement();
|
|
|
|
Vertex target = (Vertex) getTargetEntityManagement().getElement();
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
element = orientGraph.addEdge(null, source, target, erType);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
ERManagement.updateProperties(oClass, element, jsonNode, ignoreKeys, ignoreStartWithKeys);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
logger.info("{} successfully created", erType);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
return element;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
protected abstract S newSourceEntityManagement() throws ResourceRegistryException;
|
2017-11-17 16:04:15 +01:00
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
protected abstract T newTargetEntityManagement() throws ResourceRegistryException;
|
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
2017-10-26 17:09:07 +02:00
|
|
|
protected Edge reallyUpdate() throws ResourceRegistryException {
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
logger.debug("Trying to update {} : {}", erType, jsonNode);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
Edge edge = getElement();
|
2017-11-17 15:59:25 +01:00
|
|
|
ERManagement.updateProperties(oClass, edge, jsonNode, ignoreKeys, ignoreStartWithKeys);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
if (accessType.compareTo(AccessType.CONSISTS_OF) == 0) {
|
2016-12-19 14:59:27 +01:00
|
|
|
JsonNode target = jsonNode.get(Relation.TARGET_PROPERTY);
|
|
|
|
if (target != null) {
|
|
|
|
FacetManagement fm = new FacetManagement(orientGraph);
|
|
|
|
fm.setJSON(target);
|
2017-10-26 17:09:07 +02:00
|
|
|
fm.internalUpdate();
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
logger.info("{} {} successfully updated", erType, jsonNode);
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
return edge;
|
|
|
|
|
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
2017-11-17 15:59:25 +01:00
|
|
|
protected boolean reallyAddToContext() throws ContextException, ResourceRegistryException {
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2016-12-20 18:36:17 +01:00
|
|
|
AddConstraint addConstraint = AddConstraint.unpropagate;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-20 18:36:17 +01:00
|
|
|
try {
|
2017-11-17 15:59:25 +01:00
|
|
|
PropagationConstraint propagationConstraint = Utility.getEmbedded(PropagationConstraint.class, element,
|
2016-12-20 18:36:17 +01:00
|
|
|
Relation.PROPAGATION_CONSTRAINT);
|
|
|
|
if (propagationConstraint.getAddConstraint() != null) {
|
|
|
|
addConstraint = propagationConstraint.getAddConstraint();
|
2017-11-17 15:59:25 +01:00
|
|
|
} else {
|
|
|
|
String error = String.format(
|
2017-11-23 09:39:21 +01:00
|
|
|
"%s.%s in %s is null. %s",
|
2017-11-17 15:59:25 +01:00
|
|
|
Relation.PROPAGATION_CONSTRAINT, PropagationConstraint.ADD_PROPERTY,
|
2017-11-23 09:39:21 +01:00
|
|
|
Utility.toJsonString(element, true), Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2017-04-18 09:44:58 +02:00
|
|
|
logger.error(error);
|
|
|
|
throw new ResourceRegistryException(error);
|
2016-12-20 18:36:17 +01:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2017-11-17 15:59:25 +01:00
|
|
|
String error = String.format(
|
2017-11-23 09:39:21 +01:00
|
|
|
"Error while getting %s from %s while performing AddToContext. %s",
|
|
|
|
Relation.PROPAGATION_CONSTRAINT, Utility.toJsonString(element, true),
|
|
|
|
Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2017-04-18 09:44:58 +02:00
|
|
|
logger.warn(error);
|
|
|
|
throw new ResourceRegistryException(error, e);
|
2016-12-20 18:36:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (addConstraint) {
|
2017-11-17 16:04:15 +01:00
|
|
|
case propagate:
|
|
|
|
/*
|
|
|
|
* The relation must be added only in the case the target vertex must be added.
|
|
|
|
* Otherwise we have a relation which point to an entity outside of the context.
|
|
|
|
*/
|
2017-11-17 17:34:29 +01:00
|
|
|
getTargetEntityManagement().internalAddToContext();
|
2017-11-17 16:04:15 +01:00
|
|
|
|
|
|
|
ContextUtility.addToActualContext(orientGraph, getElement());
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case unpropagate:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-12-20 18:36:17 +01:00
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
public boolean forcedAddToContext() throws ContextException, ResourceRegistryException {
|
|
|
|
|
2017-03-23 13:37:15 +01:00
|
|
|
getElement();
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-03-23 13:37:15 +01:00
|
|
|
/* Adding source to Context */
|
2017-11-17 17:34:29 +01:00
|
|
|
getSourceEntityManagement().internalAddToContext();
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-03-23 13:37:15 +01:00
|
|
|
/* Adding target to Context */
|
2017-11-17 17:34:29 +01:00
|
|
|
getTargetEntityManagement().internalAddToContext();
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-03-23 13:37:15 +01:00
|
|
|
ContextUtility.addToActualContext(orientGraph, getElement());
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-03-23 13:37:15 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
2017-11-17 15:59:25 +01:00
|
|
|
protected boolean reallyRemoveFromContext() throws ContextException, ResourceRegistryException {
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2016-12-20 18:36:17 +01:00
|
|
|
|
|
|
|
RemoveConstraint removeConstraint = RemoveConstraint.keep;
|
|
|
|
|
|
|
|
try {
|
2017-11-17 15:59:25 +01:00
|
|
|
PropagationConstraint propagationConstraint = Utility.getEmbedded(PropagationConstraint.class, element,
|
2016-12-20 18:36:17 +01:00
|
|
|
Relation.PROPAGATION_CONSTRAINT);
|
|
|
|
if (propagationConstraint.getRemoveConstraint() != null) {
|
|
|
|
removeConstraint = propagationConstraint.getRemoveConstraint();
|
2017-11-17 15:59:25 +01:00
|
|
|
} else {
|
|
|
|
String error = String.format(
|
2017-11-23 09:39:21 +01:00
|
|
|
"%s.%s in %s is null. %s",
|
2017-11-17 15:59:25 +01:00
|
|
|
Relation.PROPAGATION_CONSTRAINT, PropagationConstraint.REMOVE_PROPERTY,
|
2017-11-23 09:39:21 +01:00
|
|
|
Utility.toJsonString(element, true), Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2017-04-18 09:44:58 +02:00
|
|
|
logger.error(error);
|
|
|
|
throw new ResourceRegistryException(error);
|
2016-12-20 18:36:17 +01:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2017-11-17 15:59:25 +01:00
|
|
|
String error = String.format(
|
2017-11-23 09:39:21 +01:00
|
|
|
"Error while getting %s from %s while performing RemoveFromContext. %s",
|
|
|
|
Relation.PROPAGATION_CONSTRAINT, Utility.toJsonString(element, true),
|
|
|
|
Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2017-04-18 09:44:58 +02:00
|
|
|
logger.error(error);
|
|
|
|
throw new ResourceRegistryException(error, e);
|
2016-12-20 18:36:17 +01:00
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2016-12-27 15:31:39 +01:00
|
|
|
/*
|
2017-11-17 15:59:25 +01:00
|
|
|
* In any removeConstraint value the relation MUST be removed from context to
|
|
|
|
* avoid to have edge having a source outside of the context.
|
2016-12-27 15:31:39 +01:00
|
|
|
*/
|
2016-12-30 17:31:12 +01:00
|
|
|
ContextUtility.removeFromActualContext(orientGraph, element);
|
2016-12-20 18:36:17 +01:00
|
|
|
|
|
|
|
switch (removeConstraint) {
|
2017-11-17 16:04:15 +01:00
|
|
|
case cascade:
|
2017-11-17 17:34:29 +01:00
|
|
|
getTargetEntityManagement().internalRemoveFromContext();
|
2017-11-17 16:04:15 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case cascadeWhenOrphan:
|
2017-11-17 17:34:29 +01:00
|
|
|
Vertex target = (Vertex) getTargetEntityManagement().getElement();
|
2017-11-17 16:04:15 +01:00
|
|
|
|
|
|
|
Iterable<Edge> iterable = target.getEdges(Direction.IN);
|
|
|
|
Iterator<Edge> iterator = iterable.iterator();
|
|
|
|
int count = 0;
|
|
|
|
OrientEdge edge = null;
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
edge = (OrientEdge) iterator.next();
|
|
|
|
OrientEdge thisOrientEdge = (OrientEdge) getElement();
|
|
|
|
if (edge.compareTo(thisOrientEdge) != 0) {
|
|
|
|
if (thisOrientEdge.getOutVertex().compareTo(edge.getOutVertex()) != 0) {
|
|
|
|
count++;
|
|
|
|
break;
|
2017-03-23 13:37:15 +01:00
|
|
|
}
|
2017-11-17 16:04:15 +01:00
|
|
|
/*
|
|
|
|
* else{ ContextUtility.removeFromActualContext(orientGraph, edge); }
|
|
|
|
*/
|
2017-03-23 13:37:15 +01:00
|
|
|
}
|
2017-11-17 16:04:15 +01:00
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-11-17 16:04:15 +01:00
|
|
|
if (count > 0) {
|
|
|
|
logger.trace(
|
|
|
|
"{} point to {} which is not orphan ({} exists). Giving {} directive, it will be not remove from current context.",
|
|
|
|
element, target, edge, removeConstraint);
|
|
|
|
} else {
|
2017-11-17 17:34:29 +01:00
|
|
|
getTargetEntityManagement().internalRemoveFromContext();
|
2017-11-17 16:04:15 +01:00
|
|
|
}
|
|
|
|
break;
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 16:04:15 +01:00
|
|
|
case keep:
|
|
|
|
break;
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 16:04:15 +01:00
|
|
|
default:
|
|
|
|
break;
|
2017-04-14 14:37:57 +02:00
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 16:04:15 +01:00
|
|
|
return true;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
2017-11-17 15:59:25 +01:00
|
|
|
protected boolean reallyDelete() throws RelationNotFoundException, ResourceRegistryException {
|
|
|
|
logger.debug("Going to remove {} with UUID {}. Related {}s will be detached.", accessType.getName(), uuid,
|
|
|
|
targetEntityClass.getSimpleName());
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-20 16:51:57 +01:00
|
|
|
RemoveConstraint removeConstraint = RemoveConstraint.keep;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
try {
|
2017-11-17 15:59:25 +01:00
|
|
|
PropagationConstraint propagationConstraint = Utility.getEmbedded(PropagationConstraint.class, element,
|
2016-12-20 18:36:17 +01:00
|
|
|
Relation.PROPAGATION_CONSTRAINT);
|
|
|
|
if (propagationConstraint.getRemoveConstraint() != null) {
|
2016-12-20 16:51:57 +01:00
|
|
|
removeConstraint = propagationConstraint.getRemoveConstraint();
|
2017-11-17 15:59:25 +01:00
|
|
|
} else {
|
|
|
|
String error = String.format(
|
2017-11-23 09:39:21 +01:00
|
|
|
"%s.%s in %s is null. %s",
|
2017-11-17 15:59:25 +01:00
|
|
|
Relation.PROPAGATION_CONSTRAINT, PropagationConstraint.REMOVE_PROPERTY,
|
2017-11-23 09:39:21 +01:00
|
|
|
Utility.toJsonString(element, true), Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2017-04-18 09:44:58 +02:00
|
|
|
logger.error(error);
|
|
|
|
throw new ResourceRegistryException(error);
|
2016-12-20 16:51:57 +01:00
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
} catch (Exception e) {
|
2017-11-17 15:59:25 +01:00
|
|
|
logger.warn(
|
2017-11-23 09:39:21 +01:00
|
|
|
"Error while getting {} from {}. Assuming {}. {}",
|
|
|
|
Relation.PROPAGATION_CONSTRAINT, Utility.toJsonString(element, true), removeConstraint,
|
|
|
|
Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
|
2017-11-17 17:34:29 +01:00
|
|
|
Vertex target = (Vertex) getTargetEntityManagement().getElement();
|
2016-12-30 17:31:12 +01:00
|
|
|
element.remove();
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-20 16:51:57 +01:00
|
|
|
switch (removeConstraint) {
|
2017-11-17 16:04:15 +01:00
|
|
|
case cascade:
|
2017-11-17 17:34:29 +01:00
|
|
|
getTargetEntityManagement().internalDelete();
|
2017-11-17 16:04:15 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case cascadeWhenOrphan:
|
|
|
|
Iterable<Edge> iterable = target.getEdges(Direction.IN);
|
|
|
|
Iterator<Edge> iterator = iterable.iterator();
|
|
|
|
if (iterator.hasNext()) {
|
|
|
|
logger.trace("{} point to {} which is not orphan. Giving {} directive, it will be keep.", element,
|
|
|
|
target, removeConstraint);
|
|
|
|
} else {
|
2017-11-17 17:34:29 +01:00
|
|
|
getTargetEntityManagement().internalDelete();
|
2017-11-17 16:04:15 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case keep:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-27 17:22:19 +02:00
|
|
|
@SuppressWarnings("unused")
|
2017-11-17 15:59:25 +01:00
|
|
|
private String create(UUID sourceUUID, UUID targetUUID) throws ResourceRegistryException {
|
2016-12-19 14:59:27 +01:00
|
|
|
try {
|
2017-11-17 15:59:25 +01:00
|
|
|
orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.WRITER, forceAdmin);
|
|
|
|
|
2017-11-17 17:34:29 +01:00
|
|
|
getSourceEntityManagement().setUUID(sourceUUID);
|
|
|
|
getTargetEntityManagement().setUUID(targetUUID);
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-11-17 11:42:37 +01:00
|
|
|
element = reallyCreate();
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
orientGraph.commit();
|
|
|
|
|
|
|
|
return serialize();
|
|
|
|
|
|
|
|
} catch (ResourceRegistryException e) {
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.rollback();
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.rollback();
|
|
|
|
}
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
} finally {
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2017-11-17 15:59:25 +01:00
|
|
|
protected Collection<JSONObject> serializeEdges(Iterable<Edge> edges, boolean postFilterPolymorphic)
|
|
|
|
throws ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
Map<String, JSONObject> visitedSourceResources = new HashMap<>();
|
2017-01-13 12:21:35 +01:00
|
|
|
for (Edge edge : edges) {
|
|
|
|
if (postFilterPolymorphic && edge.getLabel().compareTo(erType) != 0) {
|
2017-01-12 11:13:09 +01:00
|
|
|
continue;
|
|
|
|
}
|
2017-11-17 16:04:15 +01:00
|
|
|
RelationManagement relationManagement = ERManagementUtility.getRelationManagement(orientGraph, edge);
|
2017-11-17 15:59:25 +01:00
|
|
|
visitedSourceResources = relationManagement.fullSerialize(visitedSourceResources);
|
2017-01-11 12:06:50 +01:00
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
return visitedSourceResources.values();
|
2017-01-12 11:13:09 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
protected String serializeJSONObjectList(Collection<JSONObject> list) {
|
2017-01-12 11:13:09 +01:00
|
|
|
JSONArray jsonArray = new JSONArray(list);
|
2017-01-11 12:06:50 +01:00
|
|
|
return jsonArray.toString();
|
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-11 12:06:50 +01:00
|
|
|
@Override
|
2017-11-17 15:59:25 +01:00
|
|
|
public String reallyGetAll(boolean polymorphic) throws ResourceRegistryException {
|
2017-01-11 17:11:28 +01:00
|
|
|
Iterable<Edge> edges = orientGraph.getEdgesOfClass(erType, polymorphic);
|
2017-01-17 16:53:24 +01:00
|
|
|
Collection<JSONObject> collection = serializeEdges(edges, false);
|
|
|
|
return serializeJSONObjectList(collection);
|
2017-01-11 12:06:50 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
public String reallyGetAllFrom(UUID uuid, Direction direction, boolean polymorphic)
|
|
|
|
throws ResourceRegistryException {
|
2017-01-11 12:06:50 +01:00
|
|
|
EntityManagement entityManagement = null;
|
|
|
|
try {
|
2017-11-17 12:31:22 +01:00
|
|
|
entityManagement = (EntityManagement) ERManagementUtility.getERManagementFromUUID(orientGraph, uuid);
|
2017-01-13 12:21:35 +01:00
|
|
|
} catch (ResourceRegistryException e) {
|
2017-01-11 12:06:50 +01:00
|
|
|
throw e;
|
2017-01-13 12:21:35 +01:00
|
|
|
} catch (Exception e) {
|
2017-11-17 15:59:25 +01:00
|
|
|
throw new ResourceRegistryException(
|
|
|
|
String.format("Provided UUID %s does not belogn to any %s", uuid.toString(), Entity.NAME));
|
2017-01-11 12:06:50 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-11 12:06:50 +01:00
|
|
|
Vertex vertex = (Vertex) entityManagement.getElement();
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-12 11:13:09 +01:00
|
|
|
List<JSONObject> list = new ArrayList<>();
|
2017-01-13 17:06:57 +01:00
|
|
|
Iterable<Edge> edges = vertex.getEdges(direction, erType);
|
2017-01-13 12:21:35 +01:00
|
|
|
list.addAll(serializeEdges(edges, !polymorphic));
|
2017-01-12 11:13:09 +01:00
|
|
|
|
|
|
|
return serializeJSONObjectList(list);
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-01-11 12:06:50 +01:00
|
|
|
}
|
2017-01-13 12:21:35 +01:00
|
|
|
|
2017-11-17 15:59:25 +01:00
|
|
|
public String allFrom(UUID uuid, Direction direction, boolean polymorphic) throws ResourceRegistryException {
|
2017-01-11 12:06:50 +01:00
|
|
|
try {
|
2017-11-17 15:59:25 +01:00
|
|
|
orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.READER, forceAdmin);
|
2017-01-11 12:06:50 +01:00
|
|
|
|
|
|
|
return reallyGetAllFrom(uuid, direction, polymorphic);
|
|
|
|
} catch (ResourceRegistryException e) {
|
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
} finally {
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2017-03-23 13:37:15 +01:00
|
|
|
@Override
|
2017-11-17 11:42:37 +01:00
|
|
|
public boolean addToContext() throws ERNotFoundException, ContextException {
|
2017-11-17 15:59:25 +01:00
|
|
|
logger.debug("Going to add {} with UUID {} to actual Context", accessType.getName(), uuid);
|
2017-03-23 13:37:15 +01:00
|
|
|
|
|
|
|
try {
|
2017-11-17 15:59:25 +01:00
|
|
|
orientGraph = ContextUtility.getActualSecurityContextGraph(PermissionMode.WRITER, true);
|
2017-03-23 13:37:15 +01:00
|
|
|
|
|
|
|
boolean added = forcedAddToContext();
|
|
|
|
|
|
|
|
orientGraph.commit();
|
2017-11-17 15:59:25 +01:00
|
|
|
logger.info("{} with UUID {} successfully added to actual Context", accessType.getName(), uuid);
|
2017-03-23 13:37:15 +01:00
|
|
|
|
|
|
|
return added;
|
|
|
|
} catch (Exception e) {
|
2017-11-17 15:59:25 +01:00
|
|
|
logger.error("Unable to add {} with UUID {} to actual Context", accessType.getName(), uuid, e);
|
2017-03-23 13:37:15 +01:00
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.rollback();
|
|
|
|
}
|
|
|
|
throw new ContextException(e);
|
|
|
|
} finally {
|
|
|
|
if (orientGraph != null) {
|
|
|
|
orientGraph.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 15:59:25 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|