Serializing Resource with facets

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@131456 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-09-16 13:35:44 +00:00
parent 328f8ad8a4
commit 08c6201212
4 changed files with 69 additions and 94 deletions

View File

@ -6,7 +6,6 @@ package org.gcube.informationsystem.resourceregistry.resources.impl;
import java.util.Iterator; import java.util.Iterator;
import java.util.UUID; import java.util.UUID;
import org.codehaus.jettison.json.JSONException;
import org.gcube.informationsystem.model.embedded.Header; import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.orientdb.impl.entity.Context; import org.gcube.informationsystem.model.orientdb.impl.entity.Context;
import org.gcube.informationsystem.model.relation.IsParentOf; import org.gcube.informationsystem.model.relation.IsParentOf;
@ -161,8 +160,7 @@ public class ContextManagementImpl implements ContextManagement {
SecurityContext.addToSecurityContext(orientGraph, SecurityContext.addToSecurityContext(orientGraph,
context.asVertex(), uuidString); context.asVertex(), uuidString);
String contextJsonString = Utility.vertexToJsonString(context String contextJsonString = Utility.toJsonString(context.asVertex());
.asVertex());
logger.trace("Creating {}", contextJsonString); logger.trace("Creating {}", contextJsonString);
orientGraph.commit(); orientGraph.commit();
@ -187,12 +185,7 @@ public class ContextManagementImpl implements ContextManagement {
OrientGraph orientGraph = SecurityContextMapper OrientGraph orientGraph = SecurityContextMapper
.getSecurityContextFactory(null, PermissionMode.READER).getTx(); .getSecurityContextFactory(null, PermissionMode.READER).getTx();
Vertex context = getContext(orientGraph, contextUUID); Vertex context = getContext(orientGraph, contextUUID);
try { return Utility.toJsonString(context);
return Utility.vertexToJsonString(context, false);
} catch (JSONException e) {
throw new ContextException(e.getCause());
}
} }
@Override @Override
@ -230,7 +223,7 @@ public class ContextManagementImpl implements ContextManagement {
orientGraph.commit(); orientGraph.commit();
String contextJsonString = Utility.vertexToJsonString(context); String contextJsonString = Utility.toJsonString(context);
logger.info("Context renamed {}", contextJsonString); logger.info("Context renamed {}", contextJsonString);
orientGraph.shutdown(); orientGraph.shutdown();
@ -253,7 +246,7 @@ public class ContextManagementImpl implements ContextManagement {
.getSecurityContextFactory(null, PermissionMode.WRITER).getTx(); .getSecurityContextFactory(null, PermissionMode.WRITER).getTx();
Vertex context = getContext(orientGraph, contextToMoveUUID); Vertex context = getContext(orientGraph, contextToMoveUUID);
logger.trace("Context to move {}", Utility.vertexToJsonString(context)); logger.trace("Context to move {}", Utility.toJsonString(context));
checkContext(orientGraph, newParentUUID, checkContext(orientGraph, newParentUUID,
context.getProperty(Context.NAME_PROPERTY).toString()); context.getProperty(Context.NAME_PROPERTY).toString());
@ -270,16 +263,14 @@ public class ContextManagementImpl implements ContextManagement {
if (newParentUUID != null) { if (newParentUUID != null) {
Vertex parent = getContext(orientGraph, newParentUUID); Vertex parent = getContext(orientGraph, newParentUUID);
logger.trace("New Parent Context {}", logger.trace("New Parent Context {}",
Utility.vertexToJsonString(parent)); Utility.toJsonString(parent));
orientGraph.addEdge(null, parent, context, IsParentOf.NAME); orientGraph.addEdge(null, parent, context, IsParentOf.NAME);
} }
orientGraph.commit(); orientGraph.commit();
context = getContext(orientGraph, contextToMoveUUID); context = getContext(orientGraph, contextToMoveUUID);
String contextJsonString = Utility.vertexToJsonString(context); String contextJsonString = Utility.toJsonString(context);
logger.info("Context moved {}", contextJsonString); logger.info("Context moved {}", contextJsonString);
orientGraph.shutdown(); orientGraph.shutdown();
@ -297,7 +288,7 @@ public class ContextManagementImpl implements ContextManagement {
.getSecurityContextFactory(null, PermissionMode.WRITER).getTx(); .getSecurityContextFactory(null, PermissionMode.WRITER).getTx();
Vertex context = getContext(orientGraph, uuid); Vertex context = getContext(orientGraph, uuid);
logger.trace("Context to be delete {}", logger.trace("Context to be delete {}",
Utility.vertexToJsonString(context)); Utility.toJsonString(context));
Iterable<Edge> edges = context.getEdges(Direction.OUT, IsParentOf.NAME); Iterable<Edge> edges = context.getEdges(Direction.OUT, IsParentOf.NAME);
if (edges != null && edges.iterator().hasNext()) { if (edges != null && edges.iterator().hasNext()) {

View File

@ -13,6 +13,9 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.impl.utils.Entities; import org.gcube.informationsystem.impl.utils.Entities;
import org.gcube.informationsystem.model.embedded.Embedded; import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.embedded.Header; import org.gcube.informationsystem.model.embedded.Header;
@ -40,6 +43,7 @@ import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeType; import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientEdge; import com.tinkerpop.blueprints.impls.orient.OrientEdge;
@ -129,49 +133,40 @@ public class EntityManagementImpl implements EntityManagement {
return null; return null;
} }
private static Entity checkTypeAndGetHeader(JsonNode jsonNode) throws ResourceRegistryException { private static void checkEmbeddedType(JsonNode jsonNode) throws ResourceRegistryException {
if(jsonNode.has(Entities.CLASS_PROPERTY)){ if(jsonNode.has(Entities.CLASS_PROPERTY)){
// Complex type // Complex type
String type = getClassProperty(jsonNode); String type = getClassProperty(jsonNode);
SchemaManagementImpl schemaManagement = new SchemaManagementImpl(); SchemaManagementImpl schemaManagement = new SchemaManagementImpl();
try { try {
schemaManagement.getTypeSchema(type, Embedded.NAME); schemaManagement.getTypeSchema(type, Embedded.NAME);
} catch (SchemaNotFoundException e) { } catch (SchemaNotFoundException e) {
throw e; throw e;
} }
boolean validType = false;
boolean notEmbedded = false;
Header header = null;
// Header can be ignored or managed. Actually is managed.
if(validType && notEmbedded){
org.gcube.informationsystem.model.embedded.Header header = null;
try { try {
header = getHeader(jsonNode); header = getHeader(jsonNode);
} catch (Exception e){ } catch (Exception e){
logger.warn("An invalid Header has been provided. An embedded object cannot have an Header. It will be ignored."); 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"); throw new ResourceRegistryException("An embedded object cannot have an Header");
} }
if(header!=null){ if(header!=null){
logger.warn("An embedded object cannot have an Header. It will be ignored."); logger.warn("An embedded object cannot have an Header. It will be ignored.");
//throw new ResourceRegistryException("An embedded object cannot have an Header"); throw new ResourceRegistryException("An embedded object cannot have an Header");
}
}
} }
return null; }
} }
public static Object getObejctFromElement(JsonNode value) throws ResourceRegistryException { public static Object getObejctFromElement(JsonNode value) throws ResourceRegistryException {
JsonNodeType jsonNodeType = value.getNodeType(); JsonNodeType jsonNodeType = value.getNodeType();
switch (jsonNodeType) { switch (jsonNodeType) {
case OBJECT: case OBJECT:
// TODO checkEmbeddedType(value);
checkTypeAndGetHeader(value);
return null; return null;
case ARRAY: case ARRAY:
@ -514,8 +509,8 @@ public class EntityManagementImpl implements EntityManagement {
logger.trace("Creating {} ({}) beetween {} -> {}", logger.trace("Creating {} ({}) beetween {} -> {}",
Relation.class.getSimpleName(), relationType, Relation.class.getSimpleName(), relationType,
Utility.vertexToJsonString(source), Utility.toJsonString(source),
Utility.vertexToJsonString(target)); Utility.toJsonString(target));
Edge edge = orientGraph.addEdge(null, source, target, relationType); Edge edge = orientGraph.addEdge(null, source, target, relationType);
@ -610,8 +605,8 @@ public class EntityManagementImpl implements EntityManagement {
orientGraph = ContextUtility orientGraph = ContextUtility
.getActualSecurityContextGraph(PermissionMode.WRITER); .getActualSecurityContextGraph(PermissionMode.WRITER);
// TODO get facetType from json String entityType = getClassProperty(jsonNode);
Vertex facet = getEntity(orientGraph, uuid, Facet.NAME, Facet.class); Vertex facet = getEntity(orientGraph, uuid, entityType, Facet.class);
Set<String> oldKeys = facet.getPropertyKeys(); Set<String> oldKeys = facet.getPropertyKeys();
@ -796,9 +791,24 @@ public class EntityManagementImpl implements EntityManagement {
return true; return true;
} }
private static String marshallResource(Vertex vertex){ private static String marshallResource(Vertex vertex) throws JSONException{
// TODO serialize all facets as weel JSONObject jsonObject = Utility.toJsonObject(vertex, false);
return Utility.orientVertexToJsonString((OrientVertex) vertex, false);
JSONArray jsonArray = new JSONArray();
Iterable<Edge> edges = vertex.getEdges(Direction.OUT, ConsistsOf.NAME);
for(Edge edge : edges){
JSONObject jsonObjectEdge = Utility.toJsonObject(edge, false);
Vertex facetVertex = edge.getVertex(Direction.OUT);
jsonObjectEdge.put(Relation.TARGET_PROPERTY, Utility.toJsonObject(facetVertex, false));
jsonArray.put(jsonObjectEdge);
}
jsonObject.append(lowerCaseFirstCharacter(ConsistsOf.NAME), jsonArray);
return jsonObject.toString();
} }
@ -899,7 +909,6 @@ public class EntityManagementImpl implements EntityManagement {
// TODO remove attached facets if not managed from hooks // TODO remove attached facets if not managed from hooks
resource.remove(); resource.remove();
orientGraph.commit(); orientGraph.commit();

View File

@ -119,7 +119,7 @@ public class ContextUtility {
Vertex context = iterator.next(); Vertex context = iterator.next();
logger.trace("Context Representing Vertex : {}", logger.trace("Context Representing Vertex : {}",
Utility.vertexToJsonString(context)); Utility.toJsonString(context));
if (iterator.hasNext()) { if (iterator.hasNext()) {
throw new ContextNotFoundException( throw new ContextNotFoundException(

View File

@ -8,6 +8,7 @@ import java.util.Iterator;
import java.util.Set; import java.util.Set;
import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.gcube.informationsystem.model.embedded.Header; import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.entity.Entity; import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.relation.Relation; import org.gcube.informationsystem.model.relation.Relation;
@ -20,6 +21,7 @@ import org.slf4j.LoggerFactory;
import com.orientechnologies.orient.core.record.ORecord; import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientEdge; import com.tinkerpop.blueprints.impls.orient.OrientEdge;
import com.tinkerpop.blueprints.impls.orient.OrientGraph; import com.tinkerpop.blueprints.impls.orient.OrientGraph;
@ -35,8 +37,7 @@ public class Utility {
private static final Logger logger = LoggerFactory.getLogger(Utility.class); private static final Logger logger = LoggerFactory.getLogger(Utility.class);
public static String orientVertexToJsonString(OrientVertex orientVertex, public static String orientVertexToJsonString(OrientVertex orientVertex, boolean raw) {
boolean raw) {
ORecord oRecord = orientVertex.getRecord(); ORecord oRecord = orientVertex.getRecord();
if (raw) { if (raw) {
return oRecord.toJSON(); return oRecord.toJSON();
@ -44,8 +45,7 @@ public class Utility {
return oRecord.toJSON("class"); return oRecord.toJSON("class");
} }
public static String orientEdgeToJsonString(OrientEdge orientEdge, public static String orientEdgeToJsonString(OrientEdge orientEdge, boolean raw) {
boolean raw) {
ORecord oRecord = orientEdge.getRecord(); ORecord oRecord = orientEdge.getRecord();
if (raw) { if (raw) {
return oRecord.toJSON(); return oRecord.toJSON();
@ -53,53 +53,29 @@ public class Utility {
return oRecord.toJSON("class"); return oRecord.toJSON("class");
} }
public static String vertexToJsonString(Vertex vertex, boolean raw) public static JSONObject toJsonObject(Element element, boolean raw) throws JSONException {
throws JSONException { if(raw){
if (raw) { return GraphSONUtility.jsonFromElement(element,
return GraphSONUtility.jsonFromElement(vertex, element.getPropertyKeys(), GraphSONMode.EXTENDED);
vertex.getPropertyKeys(), GraphSONMode.EXTENDED).toString(); }else{
} else { Set<String> keys = new HashSet<>(element.getPropertyKeys());
Set<String> keys = new HashSet<>(vertex.getPropertyKeys()); for (String key : element.getPropertyKeys()) {
for (String key : vertex.getPropertyKeys()) {
if (key.startsWith("_")) { if (key.startsWith("_")) {
keys.remove(key); keys.remove(key);
} }
} }
return GraphSONUtility.jsonFromElement(vertex, keys, return GraphSONUtility.jsonFromElement(element, keys,
GraphSONMode.COMPACT).toString(); GraphSONMode.COMPACT);
} }
} }
public static String vertexToJsonString(Vertex vertex) {
public static String toJsonString(Element element) {
try { try {
return vertexToJsonString(vertex, true); return toJsonObject(element, true).toString();
} catch (Exception e) { } catch (Exception e) {
return String.valueOf(vertex); return String.valueOf(element);
}
}
public static String edgeToJsonString(Edge edge, boolean raw)
throws JSONException {
if (raw) {
return GraphSONUtility.jsonFromElement(edge,
edge.getPropertyKeys(), GraphSONMode.EXTENDED).toString();
} else {
Set<String> keys = new HashSet<>(edge.getPropertyKeys());
for (String key : edge.getPropertyKeys()) {
if (key.startsWith("_")) {
keys.remove(key);
}
}
return GraphSONUtility.jsonFromElement(edge, keys,
GraphSONMode.COMPACT).toString();
}
}
public static String edgeToJsonString(Edge edge) {
try {
return edgeToJsonString(edge, true);
} catch (Exception e) {
return String.valueOf(edge);
} }
} }
@ -130,8 +106,7 @@ public class Utility {
Iterator<Vertex> iterator = vertexes.iterator(); Iterator<Vertex> iterator = vertexes.iterator();
Vertex entity = iterator.next(); Vertex entity = iterator.next();
logger.trace("{} with {} is : {}", entityType, uuid, logger.trace("{} with {} is : {}", entityType, uuid, Utility.toJsonString(entity));
Utility.vertexToJsonString(entity));
if (iterator.hasNext()) { if (iterator.hasNext()) {
throw new ResourceRegistryException("Found more than one " throw new ResourceRegistryException("Found more than one "
@ -168,7 +143,7 @@ public class Utility {
Edge relation = iterator.next(); Edge relation = iterator.next();
logger.trace("{} with {} is : {}", relationType, uuid, logger.trace("{} with {} is : {}", relationType, uuid,
Utility.edgeToJsonString(relation)); Utility.toJsonString(relation));
if (iterator.hasNext()) { if (iterator.hasNext()) {
throw new ResourceRegistryException("Found more than one " throw new ResourceRegistryException("Found more than one "