2016-12-19 14:59:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2016-12-27 11:27:01 +01:00
|
|
|
package org.gcube.informationsystem.resourceregistry.er.entity;
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
import org.codehaus.jettison.json.JSONArray;
|
|
|
|
import org.codehaus.jettison.json.JSONObject;
|
2017-01-23 17:41:11 +01:00
|
|
|
import org.gcube.informationsystem.model.AccessType;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.model.entity.Resource;
|
|
|
|
import org.gcube.informationsystem.model.relation.ConsistsOf;
|
|
|
|
import org.gcube.informationsystem.model.relation.IsRelatedTo;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.EntityAlreadyPresentException;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
|
2016-12-27 11:27:01 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.er.relation.ConsistsOfManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.er.relation.IsRelatedToManagement;
|
|
|
|
import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement;
|
2016-12-22 17:27:26 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.utils.Utility;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
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;
|
|
|
|
import com.tinkerpop.blueprints.impls.orient.OrientVertex;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class ResourceManagement extends EntityManagement<Resource> {
|
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory
|
|
|
|
.getLogger(ResourceManagement.class);
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
public ResourceManagement() {
|
|
|
|
super(Resource.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ResourceManagement(OrientGraph orientGraph) {
|
|
|
|
super(Resource.class, orientGraph);
|
|
|
|
}
|
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
|
|
|
public String serialize() throws ResourceRegistryException {
|
|
|
|
return serializeAsJson().toString();
|
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2017-01-13 12:21:35 +01:00
|
|
|
@Override
|
|
|
|
public JSONObject serializeAsJson() throws ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
|
|
|
|
JSONObject sourceResource = serializeSelfOnly();
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
/*
|
2017-01-17 16:53:24 +01:00
|
|
|
* Cannot get ConsistsOf edge only because is not polymorphic for a
|
|
|
|
* com.tinkerpop.blueprints.Vertex
|
2016-12-19 15:56:16 +01:00
|
|
|
* vertex.getEdges(Direction.OUT, ConsistsOf.NAME);
|
2017-01-17 16:53:24 +01:00
|
|
|
* TODO Looks for a different query
|
2016-12-19 15:56:16 +01:00
|
|
|
*/
|
2017-01-17 16:53:24 +01:00
|
|
|
Iterable<Edge> edges = getElement().getEdges(Direction.OUT);
|
|
|
|
for (Edge edge : edges) {
|
2016-12-19 14:59:27 +01:00
|
|
|
@SuppressWarnings("rawtypes")
|
2017-01-17 16:53:24 +01:00
|
|
|
RelationManagement relationManagement = RelationManagement
|
|
|
|
.getRelationManagement(orientGraph, edge);
|
|
|
|
if (relationManagement instanceof ConsistsOfManagement) {
|
|
|
|
JSONObject consistsOf = relationManagement
|
|
|
|
.serializeAsJson();
|
|
|
|
sourceResource = addConsistsOf(sourceResource, consistsOf);
|
|
|
|
|
2016-12-19 15:37:15 +01:00
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
/*
|
2017-01-17 16:53:24 +01:00
|
|
|
* This comment is just to show that IsRelatedTo is not serialized
|
|
|
|
* by default as desing choice and not because forget
|
|
|
|
*
|
2016-12-19 14:59:27 +01:00
|
|
|
* else if(orientEdgeType.isSubClassOf(IsRelatedTo.NAME)){
|
2017-01-17 16:53:24 +01:00
|
|
|
* JSONObject isRelatedTo = relationManagement
|
|
|
|
.serializeAsJson();
|
|
|
|
* sourceResource = addIsRelatedTo(sourceResource, isRelatedTo);
|
|
|
|
* }
|
2016-12-19 14:59:27 +01:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
return sourceResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static JSONObject addRelation(JSONObject sourceResource,
|
|
|
|
JSONObject relation, AccessType accessType)
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
String arrayKey = accessType.lowerCaseFirstCharacter();
|
|
|
|
JSONArray relationArray = null;
|
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
try {
|
2017-01-17 16:53:24 +01:00
|
|
|
if (sourceResource.has(arrayKey)) {
|
|
|
|
relationArray = sourceResource.getJSONArray(arrayKey);
|
|
|
|
} else {
|
|
|
|
relationArray = new JSONArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
relationArray.put(relation);
|
|
|
|
sourceResource.putOpt(arrayKey, relationArray);
|
|
|
|
} catch (Exception e) {
|
2016-12-19 14:59:27 +01:00
|
|
|
throw new ResourceRegistryException(e);
|
|
|
|
}
|
|
|
|
|
2017-01-17 16:53:24 +01:00
|
|
|
return sourceResource;
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
|
|
|
|
public static JSONObject addConsistsOf(JSONObject sourceResource,
|
|
|
|
JSONObject consistsOf) throws ResourceRegistryException {
|
|
|
|
return addRelation(sourceResource, consistsOf, AccessType.CONSISTS_OF);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static JSONObject addIsRelatedTo(JSONObject sourceResource,
|
|
|
|
JSONObject isRelatedTo) throws ResourceRegistryException {
|
|
|
|
return addRelation(sourceResource, isRelatedTo, AccessType.IS_RELATED_TO);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
@Override
|
|
|
|
public Vertex reallyCreate() throws EntityAlreadyPresentException,
|
|
|
|
ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
createVertex();
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
String property = AccessType.CONSISTS_OF.lowerCaseFirstCharacter();
|
2016-12-19 14:59:27 +01:00
|
|
|
if (jsonNode.has(property)) {
|
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
|
|
|
for (JsonNode consistOfJsonNode : jsonNodeArray) {
|
|
|
|
ConsistsOfManagement com = new ConsistsOfManagement(orientGraph);
|
|
|
|
com.setJSON(consistOfJsonNode);
|
2016-12-30 17:31:12 +01:00
|
|
|
com.reallyCreate(element);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
property = AccessType.IS_RELATED_TO.lowerCaseFirstCharacter();
|
2016-12-19 14:59:27 +01:00
|
|
|
if (jsonNode.has(property)) {
|
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
|
|
|
for (JsonNode relationJsonNode : jsonNodeArray) {
|
|
|
|
IsRelatedToManagement irtm = new IsRelatedToManagement(
|
|
|
|
orientGraph);
|
|
|
|
irtm.setJSON(relationJsonNode);
|
2016-12-30 17:31:12 +01:00
|
|
|
irtm.reallyCreate(element);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Vertex reallyUpdate() throws ResourceRegistryException {
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2016-12-19 14:59:27 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
String property = AccessType.CONSISTS_OF.lowerCaseFirstCharacter();
|
2016-12-19 14:59:27 +01:00
|
|
|
if (jsonNode.has(property)) {
|
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
|
|
|
for (JsonNode relationJsonNode : jsonNodeArray) {
|
|
|
|
ConsistsOfManagement com = new ConsistsOfManagement(orientGraph);
|
|
|
|
com.setJSON(relationJsonNode);
|
|
|
|
com.reallyUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
property = AccessType.IS_RELATED_TO.lowerCaseFirstCharacter();
|
2016-12-19 14:59:27 +01:00
|
|
|
if (jsonNode.has(property)) {
|
|
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
|
|
|
for (JsonNode relationJsonNode : jsonNodeArray) {
|
|
|
|
IsRelatedToManagement irtm = new IsRelatedToManagement(
|
|
|
|
orientGraph);
|
|
|
|
irtm.setJSON(relationJsonNode);
|
|
|
|
irtm.reallyUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
((OrientVertex) element).save();
|
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
|
|
|
}
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
2016-12-19 14:59:27 +01:00
|
|
|
@Override
|
|
|
|
public boolean reallyDelete() throws ResourceNotFoundException,
|
|
|
|
ResourceRegistryException {
|
2017-01-17 16:53:24 +01:00
|
|
|
// internalDeleteResource(orientGraph, uuid, null);
|
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
getElement();
|
2017-01-17 16:53:24 +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-01-17 16:53:24 +01:00
|
|
|
while (iterator.hasNext()) {
|
2016-12-19 14:59:27 +01:00
|
|
|
Edge edge = iterator.next();
|
|
|
|
OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType();
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
RelationManagement relationManagement = null;
|
2017-01-17 16:53:24 +01:00
|
|
|
if (orientEdgeType.isSubClassOf(IsRelatedTo.NAME)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
relationManagement = new IsRelatedToManagement(orientGraph);
|
2017-01-17 16:53:24 +01:00
|
|
|
} else if (orientEdgeType.isSubClassOf(ConsistsOf.NAME)) {
|
2016-12-19 14:59:27 +01:00
|
|
|
relationManagement = new ConsistsOfManagement(orientGraph);
|
2017-01-17 16:53:24 +01:00
|
|
|
} else {
|
2016-12-19 14:59:27 +01:00
|
|
|
logger.warn("{} is not a {} nor a {}. "
|
|
|
|
+ "This is really strange ad should not occur. "
|
|
|
|
+ "Please Investigate it.",
|
2017-01-17 16:53:24 +01:00
|
|
|
Utility.toJsonString(edge, true), IsRelatedTo.NAME,
|
|
|
|
ConsistsOf.NAME);
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
if (relationManagement != null) {
|
2016-12-30 17:31:12 +01:00
|
|
|
relationManagement.setElement(edge);
|
2016-12-19 14:59:27 +01:00
|
|
|
relationManagement.reallyDelete();
|
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
}
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2016-12-30 17:31:12 +01:00
|
|
|
element.remove();
|
2017-01-17 16:53:24 +01:00
|
|
|
|
2016-12-19 14:59:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|