@Override
public T deserialize(final SecurityContext securityContext, Class<T> type, S source) throws FrameworkException {
// TODO: check why this doesn't work for setProperty with plain uuid..
final App app = StructrApp.getInstance(securityContext);
Result<T> result = Result.EMPTY_RESULT;
// create and fill input map with source object
Map<String, Object> sourceMap = new LinkedHashMap<>();
sourceMap.put(propertyKey.jsonName(), source);
// try to convert input type to java type in order to create object correctly
PropertyMap convertedSourceMap = PropertyMap.inputTypeToJavaType(securityContext, type, sourceMap);
Object convertedSource = convertedSourceMap.get(propertyKey);
if (convertedSource != null) {
// FIXME: use uuid only here?
if (convertedSource instanceof JsonInput) {
Object value = ((JsonInput)convertedSource).getAttributes().get(propertyKey.jsonName());
if (value != null) {
result = app.nodeQuery(type).and(propertyKey, value.toString()).getResult();
}
} else if (convertedSource instanceof GraphObject) {
GraphObject obj = (GraphObject)convertedSource;
if (propertyKey != null) {
result = app.nodeQuery(type).and(propertyKey, obj.getProperty(propertyKey)).getResult();
} else {
// fetch property key for "id", may be different for AbstractNode and AbstractRelationship!
PropertyKey<String> idProperty = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(obj.getClass(), GraphObject.id.dbName());
result = new Result(app.get(obj.getProperty(idProperty)), false);
}
} else {
result = app.nodeQuery(type).and(propertyKey, convertedSource).getResult();
}
}
// just check for existance
int resultCount = result.size();
switch (resultCount) {
case 0 :
if ((convertedSource != null) && createIfNotExisting) {
// create node and return it
T newNode = app.create(type);
if (newNode != null) {
newNode.setProperty(propertyKey, convertedSource);
return newNode;
}