Fixing bug on properties

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@134345 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-11-17 17:28:05 +00:00
parent 14e35279bb
commit daf61bc522
2 changed files with 119 additions and 56 deletions

View File

@ -48,7 +48,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.fasterxml.jackson.databind.node.NullNode;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
@ -144,7 +144,7 @@ public class EntityManagementImpl implements EntityManagement {
return null;
}
private static void checkEmbeddedType(JsonNode jsonNode)
private static ODocument getEmbeddedType(JsonNode jsonNode)
throws ResourceRegistryException {
if (jsonNode.has(Entities.CLASS_PROPERTY)) {
// Complex type
@ -172,63 +172,67 @@ 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());
}
return null;
}
public static Object getObejctFromElement(JsonNode value)
throws ResourceRegistryException {
JsonNodeType jsonNodeType = value.getNodeType();
switch (jsonNodeType) {
case OBJECT:
checkEmbeddedType(value);
return null;
case ARRAY:
List<Object> array = new ArrayList<>();
Iterator<JsonNode> arrayElement = value.elements();
while (arrayElement.hasNext()) {
JsonNode arrayNode = arrayElement.next();
Object objectNode = getObejctFromElement(arrayNode);
if (objectNode != null) {
array.add(objectNode);
case OBJECT:
return getEmbeddedType(value);
case ARRAY:
List<Object> array = new ArrayList<>();
Iterator<JsonNode> arrayElement = value.elements();
while (arrayElement.hasNext()) {
JsonNode arrayNode = arrayElement.next();
Object objectNode = getObejctFromElement(arrayNode);
if (objectNode != null) {
array.add(objectNode);
}
}
}
return array;
case BINARY:
break;
case BOOLEAN:
return value.asBoolean();
case NULL:
break;
case NUMBER:
if (value.isDouble() || value.isFloat()) {
return value.asDouble();
}
if (value.isBigInteger() || value.isShort() || value.isInt()) {
return value.asInt();
}
if (value.isLong()) {
return value.asLong();
}
break;
case STRING:
return value.asText();
case MISSING:
break;
case POJO:
break;
default:
break;
return array;
case BINARY:
break;
case BOOLEAN:
return value.asBoolean();
case NULL:
break;
case NUMBER:
if (value.isDouble() || value.isFloat()) {
return value.asDouble();
}
if (value.isBigInteger() || value.isShort() || value.isInt()) {
return value.asInt();
}
if (value.isLong()) {
return value.asLong();
}
break;
case STRING:
return value.asText();
case MISSING:
break;
case POJO:
break;
default:
break;
}
return null;
@ -239,6 +243,10 @@ public class EntityManagementImpl implements EntityManagement {
Map<String, Object> map = new HashMap<>();
if(ignoreKeys == null){
ignoreKeys = new HashSet<>();
}
Iterator<Entry<String, JsonNode>> fields = jsonNode.fields();
while (fields.hasNext()) {
Entry<String, JsonNode> entry = fields.next();
@ -334,6 +342,7 @@ public class EntityManagementImpl implements EntityManagement {
Set<String> ignoreKeys = new HashSet<>();
ignoreKeys.add(Relation.TARGET_PROPERTY);
ignoreKeys.add(Relation.SOURCE_PROPERTY);
ignoreKeys.add(Relation.HEADER_PROPERTY);
Map<String, Object> edgeProperties = null;
try {
@ -403,6 +412,30 @@ public class EntityManagementImpl implements EntityManagement {
if (Resource.class.isAssignableFrom(entity)) {
// Facet and relation are created in calling method
} else {
Set<String> ignoreKeys = new HashSet<>();
Map<String, Object> properties = null;
try {
properties = getPropertyMap(jsonNode, ignoreKeys);
} catch (Exception e) {
String error = "Error while parsing json to get Relation properties";
logger.error(error, e);
throw new ResourceRegistryException(error, e);
}
for (String key : properties.keySet()) {
try {
vertex.setProperty(key, properties.get(key));
} catch (Exception e) {
String error = String.format(
"Error while setting property %s : %s", key,
properties.get(key).toString());
logger.error(error);
throw new ResourceRegistryException(error, e);
}
}
/*
Iterator<Entry<String, JsonNode>> iterator = jsonNode.fields();
while (iterator.hasNext()) {
Entry<String, JsonNode> entry = iterator.next();
@ -418,6 +451,7 @@ public class EntityManagementImpl implements EntityManagement {
vertex.setProperty(entry.getKey(), value.asText());
}
}
*/
}
ContextUtility.addToActualContext(orientGraph, vertex);
@ -522,7 +556,7 @@ public class EntityManagementImpl implements EntityManagement {
Map<String, Object> edgeProperties, boolean deferredCommit)
throws ResourceRegistryException {
logger.debug("Trying to create {} with {}", relationType,
logger.debug("Trying to create {} with properties {}", relationType,
edgeProperties);
try {
@ -571,20 +605,20 @@ public class EntityManagementImpl implements EntityManagement {
orientGraph.commit();
}
logger.info("{} with {} successfully created", relationType,
logger.info("{} with properties {} successfully created", relationType,
edgeProperties);
return edge;
} catch (ResourceRegistryException rre) {
logger.error("Error Creating {} with {}", relationType,
logger.error("Error Creating {} with properties {}", relationType,
edgeProperties, rre);
if (orientGraph != null) {
orientGraph.rollback();
}
throw rre;
} catch (Exception e) {
logger.error("Error Creating {} with {}", relationType,
logger.error("Error Creating {} with properties {}", relationType,
edgeProperties, e);
if (orientGraph != null) {
orientGraph.rollback();
@ -668,6 +702,33 @@ public class EntityManagementImpl implements EntityManagement {
Set<String> oldKeys = facet.getPropertyKeys();
Set<String> ignoreKeys = new HashSet<>();
Map<String, Object> properties = null;
try {
properties = getPropertyMap(jsonNode, ignoreKeys);
} catch (Exception e) {
String error = "Error while parsing json to get Relation properties";
logger.error(error, e);
throw new ResourceRegistryException(error, e);
}
oldKeys.removeAll(properties.keySet());
for (String key : properties.keySet()) {
try {
facet.setProperty(key, properties.get(key));
} catch (Exception e) {
String error = String.format(
"Error while setting property %s : %s", key,
properties.get(key).toString());
logger.error(error);
throw new ResourceRegistryException(error, e);
}
}
/*
Iterator<Entry<String, JsonNode>> iterator = jsonNode.fields();
while (iterator.hasNext()) {
@ -690,14 +751,15 @@ public class EntityManagementImpl implements EntityManagement {
oldKeys.remove(key);
}
*/
for (String key : oldKeys) {
if (key.startsWith("_")) {
continue;
}
facet.removeProperty(key);
}
((OrientVertex) facet).save();
orientGraph.commit();

View File

@ -13,6 +13,7 @@
<logger name="org.gcube.informationsystem" level="DEBUG" />
<logger name="org.gcube.informationsystem.resourceregistry.context" level="INFO" />
<logger name="org.gcube.informationsystem.resourceregistry.resources.utils" level="INFO" />
<logger name="org.gcube.informationsystem.resourceregistry.resources.impl" level="TRACE" />
<logger name="org.gcube.informationsystem.impl.utils.discovery" level="ERROR" />
<root level="WARN">