202 lines
6.3 KiB
Java
202 lines
6.3 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.informationsystem.resourceregistry.er.entity;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import org.codehaus.jettison.json.JSONArray;
|
|
import org.codehaus.jettison.json.JSONException;
|
|
import org.codehaus.jettison.json.JSONObject;
|
|
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;
|
|
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
|
|
import org.gcube.informationsystem.resourceregistry.er.relation.ConsistsOfManagement;
|
|
import org.gcube.informationsystem.resourceregistry.er.relation.IsRelatedToManagement;
|
|
import org.gcube.informationsystem.resourceregistry.er.relation.RelationManagement;
|
|
import org.gcube.informationsystem.resourceregistry.utils.Utility;
|
|
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);
|
|
|
|
public ResourceManagement() {
|
|
super(Resource.class);
|
|
}
|
|
|
|
public ResourceManagement(OrientGraph orientGraph) {
|
|
super(Resource.class, orientGraph);
|
|
}
|
|
|
|
private static JSONObject marshallResource(OrientGraph orientGraph,
|
|
Vertex vertex) throws ResourceRegistryException {
|
|
JSONObject jsonObject = Utility.toJsonObject((OrientVertex) vertex,
|
|
true);
|
|
|
|
JSONArray consistsOfArray = new JSONArray();
|
|
/*
|
|
* JSONArray isRelatedToArray = new JSONArray();
|
|
*/
|
|
|
|
/*
|
|
* Due to orientdb bug cannot get ConsistsOf edge only because
|
|
* are filtered in hasNext() function
|
|
* vertex.getEdges(Direction.OUT, ConsistsOf.NAME);
|
|
*/
|
|
|
|
Iterable<Edge> edges = vertex.getEdges(Direction.OUT);
|
|
for(Edge edge : edges){
|
|
@SuppressWarnings("rawtypes")
|
|
RelationManagement relationManagement = RelationManagement.getRelationManagement(orientGraph, edge);
|
|
if(relationManagement instanceof ConsistsOfManagement){
|
|
JSONObject relationJsonObject = relationManagement.serializeAsJson();
|
|
consistsOfArray.put(relationJsonObject);
|
|
}
|
|
/*
|
|
* else if(orientEdgeType.isSubClassOf(IsRelatedTo.NAME)){
|
|
* isRelatedToArray.put(jsonObjectEdge); }
|
|
*/
|
|
}
|
|
|
|
try {
|
|
jsonObject.put(ERManagement.lowerCaseFirstCharacter(ConsistsOf.NAME),
|
|
consistsOfArray);
|
|
} catch (JSONException e) {
|
|
throw new ResourceRegistryException(e);
|
|
}
|
|
/*
|
|
* jsonObject.put(lowerCaseFirstCharacter(IsRelatedTo.NAME),
|
|
* isRelatedToArray);
|
|
*/
|
|
|
|
return jsonObject;
|
|
}
|
|
|
|
@Override
|
|
public String serialize() throws ResourceRegistryException {
|
|
return marshallResource(orientGraph, getVertex()).toString();
|
|
}
|
|
|
|
@Override
|
|
public JSONObject serializeAsJson() throws ResourceRegistryException {
|
|
return marshallResource(orientGraph, getVertex());
|
|
}
|
|
|
|
@Override
|
|
public Vertex reallyCreate() throws EntityAlreadyPresentException,
|
|
ResourceRegistryException {
|
|
createVertex();
|
|
|
|
String property = ERManagement.lowerCaseFirstCharacter(ConsistsOf.NAME);
|
|
if (jsonNode.has(property)) {
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
|
for (JsonNode consistOfJsonNode : jsonNodeArray) {
|
|
ConsistsOfManagement com = new ConsistsOfManagement(orientGraph);
|
|
com.setJSON(consistOfJsonNode);
|
|
com.reallyCreate(vertex);
|
|
}
|
|
}
|
|
|
|
property = ERManagement.lowerCaseFirstCharacter(IsRelatedTo.NAME);
|
|
if (jsonNode.has(property)) {
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
|
for (JsonNode relationJsonNode : jsonNodeArray) {
|
|
IsRelatedToManagement irtm = new IsRelatedToManagement(
|
|
orientGraph);
|
|
irtm.setJSON(relationJsonNode);
|
|
irtm.reallyCreate(vertex);
|
|
}
|
|
}
|
|
|
|
return vertex;
|
|
}
|
|
|
|
@Override
|
|
public Vertex reallyUpdate() throws ResourceRegistryException {
|
|
|
|
getVertex();
|
|
|
|
String property = ERManagement.lowerCaseFirstCharacter(ConsistsOf.NAME);
|
|
if (jsonNode.has(property)) {
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
|
for (JsonNode relationJsonNode : jsonNodeArray) {
|
|
ConsistsOfManagement com = new ConsistsOfManagement(orientGraph);
|
|
com.setJSON(relationJsonNode);
|
|
com.reallyUpdate();
|
|
}
|
|
}
|
|
|
|
property = ERManagement.lowerCaseFirstCharacter(IsRelatedTo.NAME);
|
|
if (jsonNode.has(property)) {
|
|
JsonNode jsonNodeArray = jsonNode.get(property);
|
|
for (JsonNode relationJsonNode : jsonNodeArray) {
|
|
IsRelatedToManagement irtm = new IsRelatedToManagement(
|
|
orientGraph);
|
|
irtm.setJSON(relationJsonNode);
|
|
irtm.reallyUpdate();
|
|
}
|
|
}
|
|
|
|
((OrientVertex) vertex).save();
|
|
|
|
return vertex;
|
|
}
|
|
|
|
@Override
|
|
public boolean reallyDelete() throws ResourceNotFoundException,
|
|
ResourceRegistryException {
|
|
//internalDeleteResource(orientGraph, uuid, null);
|
|
|
|
getVertex();
|
|
|
|
Iterable<Edge> iterable = vertex.getEdges(Direction.OUT);
|
|
Iterator<Edge> iterator = iterable.iterator();
|
|
while(iterator.hasNext()){
|
|
Edge edge = iterator.next();
|
|
OrientEdgeType orientEdgeType = ((OrientEdge) edge).getType();
|
|
@SuppressWarnings("rawtypes")
|
|
RelationManagement relationManagement = null;
|
|
if(orientEdgeType.isSubClassOf(IsRelatedTo.NAME)){
|
|
relationManagement = new IsRelatedToManagement(orientGraph);
|
|
}else if(orientEdgeType.isSubClassOf(ConsistsOf.NAME)){
|
|
relationManagement = new ConsistsOfManagement(orientGraph);
|
|
}else{
|
|
logger.warn("{} is not a {} nor a {}. "
|
|
+ "This is really strange ad should not occur. "
|
|
+ "Please Investigate it.",
|
|
Utility.toJsonString(edge, true),
|
|
IsRelatedTo.NAME, ConsistsOf.NAME);
|
|
}
|
|
if(relationManagement!=null){
|
|
relationManagement.setEdge(edge);
|
|
relationManagement.reallyDelete();
|
|
}
|
|
|
|
}
|
|
|
|
vertex.remove();
|
|
|
|
return true;
|
|
}
|
|
|
|
}
|