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