resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/er/ERManagement.java

212 lines
4.7 KiB
Java

/**
*
*/
package org.gcube.informationsystem.resourceregistry.er;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.gcube.informationsystem.impl.utils.Entities;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Element;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientElement;
/**
* @author Luca Frosini (ISTI - CNR)
*
*/
public class ERManagement {
private static Logger logger = LoggerFactory.getLogger(ERManagement.class);
public static String lowerCaseFirstCharacter(String string) {
return string.substring(0, 1).toLowerCase() + string.substring(1);
}
public static String getClassProperty(JsonNode jsonNode) {
if (jsonNode.has(Entities.CLASS_PROPERTY)) {
return jsonNode.get(Entities.CLASS_PROPERTY).asText();
}
return null;
}
public static Object getObjectFromElement(JsonNode value)
throws ResourceRegistryException {
JsonNodeType jsonNodeType = value.getNodeType();
switch (jsonNodeType) {
case OBJECT:
return EmbeddedMangement.getEmbeddedType(value);
case ARRAY:
List<Object> array = new ArrayList<>();
Iterator<JsonNode> arrayElement = value.elements();
while (arrayElement.hasNext()) {
JsonNode arrayNode = arrayElement.next();
Object objectNode = getObjectFromElement(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 null;
}
public static Map<String, Object> getPropertyMap(JsonNode jsonNode,
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()) {
Entry<String, JsonNode> entry = fields.next();
String key = entry.getKey();
if (ignoreKeys.contains(key)) {
continue;
}
for (String prefix : ignoreStartWith) {
if (key.startsWith(prefix)) {
continue OUTER_WHILE;
}
}
JsonNode value = entry.getValue();
Object object = null;
try {
object = getObjectFromElement(value);
if (object != null) {
map.put(key, object);
}
} catch (ResourceRegistryException e) {
logger.warn("An invalidy property has been provided. It will be ignored.");
}
}
return map;
}
public static Element updateProperties(Element element, JsonNode jsonNode, Set<String> ignoreKeys, Set<String> ignoreStartWithKeys)
throws ResourceRegistryException {
Set<String> oldKeys = element.getPropertyKeys();
Map<String, Object> properties;
if (element instanceof Vertex || element instanceof Edge) {
try {
properties = getPropertyMap(jsonNode, ignoreKeys, ignoreStartWithKeys);
} catch ( IOException e) {
throw new ResourceRegistryException(e);
}
} else {
String error = String.format("Error while updating {} properties",
element.toString());
throw new ResourceRegistryException(error);
}
oldKeys.removeAll(properties.keySet());
for (String key : properties.keySet()) {
try {
element.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);
}
}
OUTER_FOR: for (String key : oldKeys) {
if (ignoreKeys.contains(key)) {
continue;
}
for (String prefix : ignoreStartWithKeys) {
if (key.startsWith(prefix)) {
continue OUTER_FOR;
}
}
element.removeProperty(key);
}
((OrientElement) element).save();
return element;
}
}