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

View File

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

View File

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

View File

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