2019-11-04 18:01:20 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.instances.model.entity;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
2019-10-25 17:45:59 +02:00
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
2019-02-13 12:33:29 +01:00
|
|
|
import org.gcube.informationsystem.model.reference.entities.Resource;
|
|
|
|
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
|
|
|
|
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
|
2018-06-04 15:31:13 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2017-04-28 17:43:30 +02:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException;
|
2017-11-17 11:42:37 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
|
2019-11-04 18:01:20 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.model.relation.ConsistsOfManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.model.relation.IsRelatedToManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.instances.model.relation.RelationManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.security.SecurityContext;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.security.SecurityContext.PermissionMode;
|
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.OrientEdge;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientEdgeType;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class ResourceManagement extends EntityManagement<Resource> {
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
public ResourceManagement() {
|
2017-03-29 16:53:37 +02:00
|
|
|
super(AccessType.RESOURCE);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-11-28 17:34:43 +01:00
|
|
|
public ResourceManagement(SecurityContext workingContext, OrientGraph orientGraph) {
|
2017-11-30 18:06:08 +01:00
|
|
|
super(AccessType.RESOURCE, workingContext, orientGraph);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-17 11:42:37 +01:00
|
|
|
|
|
|
|
@Override
|
2018-06-04 15:31:13 +02:00
|
|
|
protected ResourceNotFoundException getSpecificElementNotFoundException(NotFoundException e) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new ResourceNotFoundException(e.getMessage(), e.getCause());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-11-30 18:06:08 +01:00
|
|
|
protected ResourceAvailableInAnotherContextException getSpecificERAvailableInAnotherContextException(
|
|
|
|
String message) {
|
2017-11-17 11:42:37 +01:00
|
|
|
return new ResourceAvailableInAnotherContextException(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected ResourceAlreadyPresentException getSpecificERAlreadyPresentException(String message) {
|
|
|
|
return new ResourceAlreadyPresentException(message);
|
|
|
|
}
|
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
|
|
|
public String serialize() throws ResourceRegistryException {
|
|
|
|
return serializeAsJson().toString();
|
|
|
|
}
|
2019-11-04 18:01:20 +01:00
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
2019-02-13 12:33:29 +01:00
|
|
|
public JsonNode serializeAsJson() throws ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2019-02-13 12:33:29 +01:00
|
|
|
JsonNode sourceResource = serializeSelfOnly();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
/*
|
2017-11-30 18:06:08 +01:00
|
|
|
* Cannot get ConsistsOf edge only because is not polymorphic for a
|
|
|
|
* com.tinkerpop.blueprints.Vertex vertex.getEdges(Direction.OUT,
|
|
|
|
* ConsistsOf.NAME); TODO Looks for a different query
|
2016-12-19 15:56:16 +01:00
|
|
|
*/
|
2017-04-14 14:37:57 +02:00
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
Iterable<Edge> edges = getElement().getEdges(Direction.OUT);
|
2017-11-30 18:06:08 +01:00
|
|
|
for(Edge edge : edges) {
|
2017-11-17 18:10:35 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
@SuppressWarnings("rawtypes")
|
2017-11-17 18:10:35 +01:00
|
|
|
RelationManagement relationManagement = getRelationManagement(edge);
|
2017-11-21 18:18:11 +01:00
|
|
|
relationManagement.setReload(reload);
|
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
if(relationManagement.giveMeSourceEntityManagementAsIs() == null) {
|
2017-11-17 18:10:35 +01:00
|
|
|
relationManagement.setSourceEntityManagement(this);
|
2017-11-17 18:41:02 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
if(relationManagement.giveMeSourceEntityManagementAsIs() != this) {
|
2017-11-17 18:10:35 +01:00
|
|
|
StringBuilder errorMessage = new StringBuilder();
|
|
|
|
errorMessage.append("SourceEntityManagement for ");
|
|
|
|
errorMessage.append(relationManagement.getClass().getSimpleName());
|
|
|
|
errorMessage.append(" is not the one expected. ");
|
|
|
|
errorMessage.append(Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
|
|
|
throw new ResourceRegistryException(errorMessage.toString());
|
|
|
|
}
|
2017-11-17 11:42:37 +01:00
|
|
|
|
2017-11-30 18:06:08 +01:00
|
|
|
if(relationManagement instanceof ConsistsOfManagement) {
|
2017-03-28 16:02:23 +02:00
|
|
|
try {
|
2019-02-13 12:33:29 +01:00
|
|
|
JsonNode consistsOf = relationManagement.serializeAsJson(true, true);
|
2017-03-28 16:02:23 +02:00
|
|
|
sourceResource = addConsistsOf(sourceResource, consistsOf);
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(ResourceRegistryException e) {
|
2017-11-23 09:39:21 +01:00
|
|
|
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2017-04-14 14:37:57 +02:00
|
|
|
throw e;
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(Exception e) {
|
2017-11-23 09:39:21 +01:00
|
|
|
logger.error("Unable to correctly serialize {}. {}", edge, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2017-04-14 14:37:57 +02:00
|
|
|
throw new ResourceRegistryException(e);
|
2017-03-28 16:02:23 +02:00
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2016-12-19 15:37:15 +01:00
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
/*
|
2017-11-30 18:06:08 +01:00
|
|
|
* This comment is just to show that IsRelatedTo is not serialized by default as
|
|
|
|
* design choice and not because forget
|
2017-01-17 16:53:24 +01:00
|
|
|
*
|
2019-02-13 12:33:29 +01:00
|
|
|
* else if(orientEdgeType.isSubClassOf(IsRelatedTo.NAME)){ JsonNode
|
2017-11-30 18:06:08 +01:00
|
|
|
* isRelatedTo = relationManagement.serializeAsJson(true, true); sourceResource
|
|
|
|
* = addIsRelatedTo(sourceResource, isRelatedTo); }
|
2016-12-19 14:59:27 +01:00
|
|
|
*/
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
return sourceResource;
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2019-02-13 12:33:29 +01:00
|
|
|
public static JsonNode addConsistsOf(JsonNode sourceResource, JsonNode consistsOf)
|
2017-11-30 18:06:08 +01:00
|
|
|
throws ResourceRegistryException {
|
2017-11-15 19:31:49 +01:00
|
|
|
return addRelation(sourceResource, consistsOf, AccessType.CONSISTS_OF.lowerCaseFirstCharacter());
|
2017-01-17 16:53:24 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2019-02-13 12:33:29 +01:00
|
|
|
public static JsonNode addIsRelatedTo(JsonNode sourceResource, JsonNode isRelatedTo)
|
2017-11-30 18:06:08 +01:00
|
|
|
throws ResourceRegistryException {
|
2017-11-15 19:31:49 +01:00
|
|
|
return addRelation(sourceResource, isRelatedTo, AccessType.IS_RELATED_TO.lowerCaseFirstCharacter());
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
@Override
|
2017-11-30 18:06:08 +01:00
|
|
|
protected Vertex reallyCreate() throws ResourceAlreadyPresentException, ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
createVertex();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
String property = AccessType.CONSISTS_OF.lowerCaseFirstCharacter();
|
2017-11-30 18:06:08 +01:00
|
|
|
if(jsonNode.has(property)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
2017-11-30 18:06:08 +01:00
|
|
|
for(JsonNode consistOfJsonNode : jsonNodeArray) {
|
2017-11-28 17:34:43 +01:00
|
|
|
ConsistsOfManagement com = new ConsistsOfManagement(getWorkingContext(), orientGraph);
|
2019-02-13 12:33:29 +01:00
|
|
|
com.setJsonNode(consistOfJsonNode);
|
2017-11-17 11:42:37 +01:00
|
|
|
com.setSourceEntityManagement(this);
|
|
|
|
com.internalCreate();
|
2017-11-17 18:10:35 +01:00
|
|
|
addToRelationManagement(com);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
property = AccessType.IS_RELATED_TO.lowerCaseFirstCharacter();
|
2017-11-30 18:06:08 +01:00
|
|
|
if(jsonNode.has(property)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
2017-11-30 18:06:08 +01:00
|
|
|
for(JsonNode relationJsonNode : jsonNodeArray) {
|
2017-11-28 17:34:43 +01:00
|
|
|
IsRelatedToManagement irtm = new IsRelatedToManagement(getWorkingContext(), orientGraph);
|
2019-02-13 12:33:29 +01:00
|
|
|
irtm.setJsonNode(relationJsonNode);
|
2017-11-17 11:42:37 +01:00
|
|
|
irtm.setSourceEntityManagement(this);
|
|
|
|
irtm.internalCreate();
|
2017-11-17 18:10:35 +01:00
|
|
|
addToRelationManagement(irtm);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
return element;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
@Override
|
2017-10-26 17:09:07 +02:00
|
|
|
protected Vertex reallyUpdate() throws ResourceNotFoundException, ResourceRegistryException {
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
String property = AccessType.CONSISTS_OF.lowerCaseFirstCharacter();
|
2017-11-30 18:06:08 +01:00
|
|
|
if(jsonNode.has(property)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
2017-11-30 18:06:08 +01:00
|
|
|
for(JsonNode relationJsonNode : jsonNodeArray) {
|
2017-11-28 17:34:43 +01:00
|
|
|
ConsistsOfManagement com = new ConsistsOfManagement(getWorkingContext(), orientGraph);
|
2019-02-13 12:33:29 +01:00
|
|
|
com.setJsonNode(relationJsonNode);
|
2017-10-26 17:09:07 +02:00
|
|
|
com.internalCreateOrUdate();
|
2017-11-17 18:10:35 +01:00
|
|
|
addToRelationManagement(com);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
property = AccessType.IS_RELATED_TO.lowerCaseFirstCharacter();
|
2017-11-30 18:06:08 +01:00
|
|
|
if(jsonNode.has(property)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
2017-11-30 18:06:08 +01:00
|
|
|
for(JsonNode relationJsonNode : jsonNodeArray) {
|
2017-11-28 17:34:43 +01:00
|
|
|
IsRelatedToManagement irtm = new IsRelatedToManagement(getWorkingContext(), orientGraph);
|
2019-02-13 12:33:29 +01:00
|
|
|
irtm.setJsonNode(relationJsonNode);
|
2017-10-26 17:09:07 +02:00
|
|
|
irtm.internalUpdate();
|
2017-11-17 18:10:35 +01:00
|
|
|
addToRelationManagement(irtm);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
return element;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2016-12-19 14:59:27 +01:00
|
|
|
@Override
|
2017-11-30 18:06:08 +01:00
|
|
|
protected boolean reallyDelete() throws ResourceNotFoundException, ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
// internalDeleteResource(orientGraph, uuid, null);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
Iterable<Edge> iterable = element.getEdges(Direction.OUT);
|
2016-12-19 14:59:27 +01:00
|
|
|
Iterator<Edge> iterator = iterable.iterator();
|
2017-11-30 18:06:08 +01:00
|
|
|
while(iterator.hasNext()) {
|
2017-11-17 11:42:37 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
Edge edge = iterator.next();
|
|
|
|
OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType();
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
RelationManagement relationManagement = null;
|
2017-11-30 18:06:08 +01:00
|
|
|
if(orientEdgeType.isSubClassOf(IsRelatedTo.NAME)) {
|
2017-11-28 17:34:43 +01:00
|
|
|
relationManagement = new IsRelatedToManagement(getWorkingContext(), orientGraph);
|
2017-11-30 18:06:08 +01:00
|
|
|
} else if(orientEdgeType.isSubClassOf(ConsistsOf.NAME)) {
|
2017-11-28 17:34:43 +01:00
|
|
|
relationManagement = new ConsistsOfManagement(getWorkingContext(), orientGraph);
|
2017-01-17 16:53:24 +01:00
|
|
|
} else {
|
2017-11-30 18:06:08 +01:00
|
|
|
logger.warn("{} is not a {} nor a {}. {}", Utility.toJsonString(edge, true), IsRelatedTo.NAME,
|
2017-11-23 09:39:21 +01:00
|
|
|
ConsistsOf.NAME, Utility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
if(relationManagement != null) {
|
2016-12-30 17:31:12 +01:00
|
|
|
relationManagement.setElement(edge);
|
2017-10-26 17:09:07 +02:00
|
|
|
relationManagement.internalDelete();
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
element.remove();
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-27 12:59:00 +02:00
|
|
|
|
2018-06-08 15:05:47 +02:00
|
|
|
public String all(boolean polymorphic) throws ResourceRegistryException {
|
2017-09-27 12:59:00 +02:00
|
|
|
try {
|
2017-11-28 17:34:43 +01:00
|
|
|
orientGraph = getWorkingContext().getGraph(PermissionMode.READER);
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2018-06-08 15:05:47 +02:00
|
|
|
return reallyGetAll(polymorphic);
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(ResourceRegistryException e) {
|
2017-09-27 12:59:00 +02:00
|
|
|
throw e;
|
2017-11-30 18:06:08 +01:00
|
|
|
} catch(Exception e) {
|
2017-09-27 12:59:00 +02:00
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
} finally {
|
2017-11-30 18:06:08 +01:00
|
|
|
if(orientGraph != null) {
|
2017-09-27 12:59:00 +02:00
|
|
|
orientGraph.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-30 18:06:08 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|