Removed catch in resource marshalling

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@134820 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-25 14:48:35 +00:00
parent e4cbaf19d4
commit 1c9f4cec10
1 changed files with 63 additions and 60 deletions

View File

@ -65,16 +65,16 @@ public class EntityManagementImpl implements EntityManagement {
public static final Set<String> RELATION_IGNORE_KEYS; public static final Set<String> RELATION_IGNORE_KEYS;
public static final Set<String> RELATION_IGNORE_START_WITH_KEYS; public static final Set<String> RELATION_IGNORE_START_WITH_KEYS;
public static final Set<String> ENTITY_IGNORE_KEYS; public static final Set<String> ENTITY_IGNORE_KEYS;
public static final Set<String> ENTITY_IGNORE_START_WITH_KEYS; public static final Set<String> ENTITY_IGNORE_START_WITH_KEYS;
public static final Set<String> EMBEDDED_IGNORE_KEYS; public static final Set<String> EMBEDDED_IGNORE_KEYS;
public static final Set<String> EMBEDDED_IGNORE_START_WITH_KEYS; public static final Set<String> EMBEDDED_IGNORE_START_WITH_KEYS;
public static final String AT = "@"; public static final String AT = "@";
public static final String UNDERSCORE = "_"; public static final String UNDERSCORE = "_";
static { static {
RELATION_IGNORE_KEYS = new HashSet<String>(); RELATION_IGNORE_KEYS = new HashSet<String>();
RELATION_IGNORE_KEYS.add(Relation.HEADER_PROPERTY); RELATION_IGNORE_KEYS.add(Relation.HEADER_PROPERTY);
@ -84,31 +84,34 @@ public class EntityManagementImpl implements EntityManagement {
RELATION_IGNORE_KEYS.add(OrientBaseGraph.CONNECTION_OUT.toLowerCase()); RELATION_IGNORE_KEYS.add(OrientBaseGraph.CONNECTION_OUT.toLowerCase());
RELATION_IGNORE_KEYS.add(OrientBaseGraph.CONNECTION_IN.toUpperCase()); RELATION_IGNORE_KEYS.add(OrientBaseGraph.CONNECTION_IN.toUpperCase());
RELATION_IGNORE_KEYS.add(OrientBaseGraph.CONNECTION_OUT.toUpperCase()); RELATION_IGNORE_KEYS.add(OrientBaseGraph.CONNECTION_OUT.toUpperCase());
RELATION_IGNORE_START_WITH_KEYS = new HashSet<String>(); RELATION_IGNORE_START_WITH_KEYS = new HashSet<String>();
RELATION_IGNORE_START_WITH_KEYS.add(AT); RELATION_IGNORE_START_WITH_KEYS.add(AT);
RELATION_IGNORE_START_WITH_KEYS.add(UNDERSCORE); RELATION_IGNORE_START_WITH_KEYS.add(UNDERSCORE);
ENTITY_IGNORE_KEYS = new HashSet<String>(); ENTITY_IGNORE_KEYS = new HashSet<String>();
ENTITY_IGNORE_KEYS.add(Entity.HEADER_PROPERTY); ENTITY_IGNORE_KEYS.add(Entity.HEADER_PROPERTY);
ENTITY_IGNORE_START_WITH_KEYS = new HashSet<String>(); ENTITY_IGNORE_START_WITH_KEYS = new HashSet<String>();
ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_IN_PREFIX.toLowerCase()); ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_IN_PREFIX
ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_OUT_PREFIX.toLowerCase()); .toLowerCase());
ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_IN_PREFIX.toUpperCase()); ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_OUT_PREFIX
ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_OUT_PREFIX.toUpperCase()); .toLowerCase());
ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_IN_PREFIX
.toUpperCase());
ENTITY_IGNORE_START_WITH_KEYS.add(OrientVertex.CONNECTION_OUT_PREFIX
.toUpperCase());
ENTITY_IGNORE_START_WITH_KEYS.add(AT); ENTITY_IGNORE_START_WITH_KEYS.add(AT);
ENTITY_IGNORE_START_WITH_KEYS.add(UNDERSCORE); ENTITY_IGNORE_START_WITH_KEYS.add(UNDERSCORE);
EMBEDDED_IGNORE_KEYS = new HashSet<String>(); EMBEDDED_IGNORE_KEYS = new HashSet<String>();
EMBEDDED_IGNORE_START_WITH_KEYS = new HashSet<String>(); EMBEDDED_IGNORE_START_WITH_KEYS = new HashSet<String>();
ENTITY_IGNORE_START_WITH_KEYS.add(AT); ENTITY_IGNORE_START_WITH_KEYS.add(AT);
ENTITY_IGNORE_START_WITH_KEYS.add(UNDERSCORE); ENTITY_IGNORE_START_WITH_KEYS.add(UNDERSCORE);
} }
private static Logger logger = LoggerFactory private static Logger logger = LoggerFactory
.getLogger(EntityManagementImpl.class); .getLogger(EntityManagementImpl.class);
@ -148,7 +151,8 @@ public class EntityManagementImpl implements EntityManagement {
String classProperty = getClassProperty(jsonNode); String classProperty = getClassProperty(jsonNode);
try { try {
SchemaManagementImpl.getTypeSchema(orientGraph, classProperty, classProperty); SchemaManagementImpl.getTypeSchema(orientGraph, classProperty,
classProperty);
} catch (SchemaNotFoundException e) { } catch (SchemaNotFoundException e) {
throw e; throw e;
} }
@ -204,7 +208,8 @@ public class EntityManagementImpl implements EntityManagement {
String classProperty = getClassProperty(jsonNode); String classProperty = getClassProperty(jsonNode);
if (relationType.compareTo(classProperty) != 0) { if (relationType.compareTo(classProperty) != 0) {
try { try {
SchemaManagementImpl.getTypeSchema(orientGraph, relationType, classProperty); SchemaManagementImpl.getTypeSchema(orientGraph, relationType,
classProperty);
} catch (SchemaNotFoundException e) { } catch (SchemaNotFoundException e) {
throw e; throw e;
} }
@ -257,7 +262,7 @@ public class EntityManagementImpl implements EntityManagement {
throw new ResourceRegistryException( throw new ResourceRegistryException(
"An embedded object cannot have an Header"); "An embedded object cannot have an Header");
} }
ODocument oDocument = new ODocument(type); ODocument oDocument = new ODocument(type);
return oDocument.fromJSON(jsonNode.toString()); return oDocument.fromJSON(jsonNode.toString());
@ -324,36 +329,36 @@ public class EntityManagementImpl implements EntityManagement {
} }
public static Map<String, Object> getPropertyMap(JsonNode jsonNode, public static Map<String, Object> getPropertyMap(JsonNode jsonNode,
Set<String> ignoreKeys, Set<String> ignoreStartWith) throws JsonProcessingException, IOException { Set<String> ignoreKeys, Set<String> ignoreStartWith)
throws JsonProcessingException, IOException {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
if (ignoreKeys == null) { if (ignoreKeys == null) {
ignoreKeys = new HashSet<>(); ignoreKeys = new HashSet<>();
} }
if (ignoreStartWith == null) { if (ignoreStartWith == null) {
ignoreStartWith = new HashSet<>(); ignoreStartWith = new HashSet<>();
} }
Iterator<Entry<String, JsonNode>> fields = jsonNode.fields(); Iterator<Entry<String, JsonNode>> fields = jsonNode.fields();
OUTER_WHILE: OUTER_WHILE: while (fields.hasNext()) {
while (fields.hasNext()) {
Entry<String, JsonNode> entry = fields.next(); Entry<String, JsonNode> entry = fields.next();
String key = entry.getKey(); String key = entry.getKey();
if (ignoreKeys.contains(key)) { if (ignoreKeys.contains(key)) {
continue; continue;
} }
for(String prefix : ignoreStartWith){ for (String prefix : ignoreStartWith) {
if(key.startsWith(prefix)){ if (key.startsWith(prefix)) {
break OUTER_WHILE; break OUTER_WHILE;
} }
} }
JsonNode value = entry.getValue(); JsonNode value = entry.getValue();
Object object = null; Object object = null;
try { try {
@ -374,7 +379,8 @@ public class EntityManagementImpl implements EntityManagement {
throws ResourceRegistryException { throws ResourceRegistryException {
Map<String, Object> vertexProperties = null; Map<String, Object> vertexProperties = null;
try { try {
vertexProperties = getPropertyMap(node, ENTITY_IGNORE_KEYS, ENTITY_IGNORE_START_WITH_KEYS); vertexProperties = getPropertyMap(node, ENTITY_IGNORE_KEYS,
ENTITY_IGNORE_START_WITH_KEYS);
} catch (Exception e) { } catch (Exception e) {
String error = "Error while parsing json to get Relation properties"; String error = "Error while parsing json to get Relation properties";
logger.error(error, e); logger.error(error, e);
@ -387,7 +393,8 @@ public class EntityManagementImpl implements EntityManagement {
throws ResourceRegistryException { throws ResourceRegistryException {
Map<String, Object> edgeProperties = null; Map<String, Object> edgeProperties = null;
try { try {
edgeProperties = getPropertyMap(node, RELATION_IGNORE_KEYS, RELATION_IGNORE_START_WITH_KEYS); edgeProperties = getPropertyMap(node, RELATION_IGNORE_KEYS,
RELATION_IGNORE_START_WITH_KEYS);
} catch (Exception e) { } catch (Exception e) {
String error = "Error while parsing json to get Relation properties"; String error = "Error while parsing json to get Relation properties";
logger.error(error, e); logger.error(error, e);
@ -396,12 +403,11 @@ public class EntityManagementImpl implements EntityManagement {
return edgeProperties; return edgeProperties;
} }
private Element updateProperties(Element element, JsonNode jsonNode) private Element updateProperties(Element element, JsonNode jsonNode)
throws ResourceRegistryException { throws ResourceRegistryException {
Set<String> ignoreKeys = null; Set<String> ignoreKeys = null;
Set<String> ignoreStartWithKeys = null; Set<String> ignoreStartWithKeys = null;
Set<String> oldKeys = element.getPropertyKeys(); Set<String> oldKeys = element.getPropertyKeys();
Map<String, Object> properties; Map<String, Object> properties;
@ -433,25 +439,24 @@ public class EntityManagementImpl implements EntityManagement {
} }
} }
OUTER_FOR: OUTER_FOR: for (String key : oldKeys) {
for (String key : oldKeys) {
if (ignoreKeys.contains(key)) { if (ignoreKeys.contains(key)) {
continue; continue;
} }
for(String prefix : ignoreStartWithKeys){ for (String prefix : ignoreStartWithKeys) {
if(key.startsWith(prefix)){ if (key.startsWith(prefix)) {
break OUTER_FOR; break OUTER_FOR;
} }
} }
element.removeProperty(key); element.removeProperty(key);
} }
return element; return element;
} }
private Vertex getOrCreateTargetVertex(OrientGraph orientGraph, private Vertex getOrCreateTargetVertex(OrientGraph orientGraph,
@SuppressWarnings("rawtypes") Class<? extends Relation> relation, @SuppressWarnings("rawtypes") Class<? extends Relation> relation,
JsonNode target) throws ResourceRegistryException { JsonNode target) throws ResourceRegistryException {
@ -727,7 +732,8 @@ public class EntityManagementImpl implements EntityManagement {
private Edge updateRelation( private Edge updateRelation(
OrientGraph orientGraph, OrientGraph orientGraph,
JsonNode jsonNode, JsonNode jsonNode,
@SuppressWarnings("rawtypes") Class<? extends Relation> relationClass) throws ResourceRegistryException { @SuppressWarnings("rawtypes") Class<? extends Relation> relationClass)
throws ResourceRegistryException {
logger.debug("Trying to update {} : {}", relationClass.getSimpleName(), logger.debug("Trying to update {} : {}", relationClass.getSimpleName(),
jsonNode); jsonNode);
@ -1113,38 +1119,34 @@ public class EntityManagementImpl implements EntityManagement {
return true; return true;
} }
private static String marshallResource(OrientGraph orientGraph, Vertex vertex) throws JSONException { private static String marshallResource(OrientGraph orientGraph,
Vertex vertex) throws JSONException {
JSONObject jsonObject = Utility.toJsonObject((OrientVertex) vertex, JSONObject jsonObject = Utility.toJsonObject((OrientVertex) vertex,
true); true);
JSONArray consistsOfArray = new JSONArray(); JSONArray consistsOfArray = new JSONArray();
Iterable<Edge> edges = vertex.getEdges(Direction.OUT); Iterable<Edge> edges = vertex.getEdges(Direction.OUT);
for(Edge edge : edges) { for (Edge edge : edges) {
String edgeType = edge.getLabel(); String edgeType = edge.getLabel();
try { try {
SchemaManagementImpl.getTypeSchema(orientGraph, edgeType, ConsistsOf.NAME); SchemaManagementImpl.getTypeSchema(orientGraph, edgeType,
}catch (SchemaNotFoundException e) { ConsistsOf.NAME);
} catch (SchemaNotFoundException e) {
// This not an ConsistsOf Edge. it will be skipped // This not an ConsistsOf Edge. it will be skipped
continue; continue;
} }
try{
JSONObject jsonObjectEdge = Utility.toJsonObject(
(OrientEdge) edge, true);
Vertex facetVertex = edge.getVertex(Direction.IN);
jsonObjectEdge.put(Relation.TARGET_PROPERTY,
Utility.toJsonObject((OrientVertex) facetVertex, true));
consistsOfArray.put(jsonObjectEdge);
} catch (Exception e) { JSONObject jsonObjectEdge = Utility.toJsonObject((OrientEdge) edge,
logger.error("Unable to add a Relation to serilization", e); true);
continue; Vertex facetVertex = edge.getVertex(Direction.IN);
}
jsonObjectEdge.put(Relation.TARGET_PROPERTY,
Utility.toJsonObject((OrientVertex) facetVertex, true));
consistsOfArray.put(jsonObjectEdge);
} }
@ -1185,7 +1187,7 @@ public class EntityManagementImpl implements EntityManagement {
createRelations(orientGraph, resource, jsonNodeArray, createRelations(orientGraph, resource, jsonNodeArray,
IsRelatedTo.class); IsRelatedTo.class);
} }
orientGraph.commit(); orientGraph.commit();
String resourceString = marshallResource(orientGraph, resource); String resourceString = marshallResource(orientGraph, resource);
@ -1279,7 +1281,8 @@ public class EntityManagementImpl implements EntityManagement {
if (jsonNode.has(property)) { if (jsonNode.has(property)) {
JsonNode jsonNodeArray = jsonNode.get(property); JsonNode jsonNodeArray = jsonNode.get(property);
for (JsonNode relationJsonNode : jsonNodeArray) { for (JsonNode relationJsonNode : jsonNodeArray) {
updateRelation(orientGraph, relationJsonNode, ConsistsOf.class); updateRelation(orientGraph, relationJsonNode,
ConsistsOf.class);
} }
} }