use of org.structr.common.error.PropertiesNotFoundToken in project structr by structr.
the class TypeAndValueDeserializationStrategy method deserialize.
@Override
public T deserialize(final SecurityContext securityContext, Class<T> type, S source, final Object context) throws FrameworkException {
final App app = StructrApp.getInstance(securityContext);
Result<T> result = Result.EMPTY_RESULT;
// default to UUID
if (propertyKey == null) {
propertyKey = GraphObject.id;
}
// 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 Map) {
Object value = ((Map<String, Object>) convertedSource).get(propertyKey.jsonName());
if (value != null) {
result = app.nodeQuery(type).and(propertyKey, value.toString()).getResult();
}
} else if (convertedSource instanceof GraphObject) {
final GraphObject obj = (GraphObject) convertedSource;
result = app.nodeQuery(type).and(propertyKey, obj.getProperty(propertyKey)).getResult();
} 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;
}
} else {
logger.warn("Unable to create node of type {} for property {}", new Object[] { type.getSimpleName(), propertyKey.jsonName() });
}
break;
case 1:
T obj = result.get(0);
// if(!type.getSimpleName().equals(node.getType())) {
if (!type.isAssignableFrom(obj.getClass())) {
throw new FrameworkException(422, "Node type mismatch", new TypeToken(obj.getClass(), propertyKey, type.getSimpleName()));
}
if (!convertedSourceMap.isEmpty()) {
// set properties on related node?
setProperties(securityContext, obj, convertedSourceMap);
}
return obj;
}
if (convertedSource != null) {
PropertyMap attributes = new PropertyMap();
attributes.put(propertyKey, convertedSource);
attributes.put(AbstractNode.type, type.getSimpleName());
throw new FrameworkException(404, "No node found for given properties", new PropertiesNotFoundToken(type.getSimpleName(), null, attributes));
}
return null;
}
use of org.structr.common.error.PropertiesNotFoundToken in project structr by structr.
the class TypeAndPropertySetDeserializationStrategy method deserialize.
private T deserialize(final SecurityContext securityContext, Class<T> type, final PropertyMap attributes) throws FrameworkException {
final App app = StructrApp.getInstance(securityContext);
if (attributes != null) {
Result<T> result = Result.EMPTY_RESULT;
// Check if properties contain the UUID attribute
if (attributes.containsKey(GraphObject.id)) {
result = new Result(app.getNodeById(attributes.get(GraphObject.id)), false);
} else {
boolean attributesComplete = true;
// Check if all property keys of the PropertySetNotion are present
for (PropertyKey key : propertyKeys) {
attributesComplete &= attributes.containsKey(key);
}
if (attributesComplete) {
final PropertyMap searchAttributes = new PropertyMap();
for (final PropertyKey key : attributes.keySet()) {
// a related node property
if (key.relatedType() == null) {
searchAttributes.put(key, attributes.get(key));
}
}
result = app.nodeQuery(type).and(searchAttributes).getResult();
}
}
// just check for existance
String errorMessage = null;
final int size = result.size();
switch(size) {
case 0:
if (createIfNotExisting) {
// create node and return it
T newNode = app.create(type, attributes);
if (newNode != null) {
return newNode;
}
}
errorMessage = "No node found for the given properties and auto-creation not enabled";
break;
case 1:
final T relatedNode = getTypedResult(result, type);
if (!attributes.isEmpty()) {
// set properties on related node?
setProperties(securityContext, relatedNode, attributes);
}
return relatedNode;
default:
errorMessage = "Found " + size + " nodes for given type and properties, property set is ambiguous";
logger.error(errorMessage + ". This is often due to wrong modeling, or you should consider creating a uniquness constraint for " + type.getName(), size);
break;
}
throw new FrameworkException(404, errorMessage, new PropertiesNotFoundToken(type.getSimpleName(), null, attributes));
}
return null;
}
use of org.structr.common.error.PropertiesNotFoundToken in project structr by structr.
the class SchemaDeserializationStrategy method deserialize.
private T deserialize(final SecurityContext securityContext, final Class<T> type, final PropertyMap attributes, final Object context) throws FrameworkException {
final App app = StructrApp.getInstance(securityContext);
if (attributes != null) {
Result<T> result = Result.EMPTY_RESULT;
// remove attributes that do not belong to the target node
final PropertyMap foreignProperties = new PropertyMap();
for (final Iterator<PropertyKey> it = attributes.keySet().iterator(); it.hasNext(); ) {
final PropertyKey key = it.next();
if (foreignPropertyKeys.contains(key)) {
// move entry to foreign map and remove from attributes
foreignProperties.put(key, attributes.get(key));
it.remove();
}
}
// retrieve and remove source type name (needed for foreign properties)
final String sourceTypeName = (String) ((Map) context).get("name");
// Check if properties contain the UUID attribute
if (attributes.containsKey(GraphObject.id)) {
result = new Result(app.getNodeById(attributes.get(GraphObject.id)), false);
} else {
boolean attributesComplete = true;
// Check if all property keys of the PropertySetNotion are present
for (PropertyKey key : identifyingPropertyKeys) {
attributesComplete &= attributes.containsKey(key);
}
if (attributesComplete) {
// collect only those key-value pairs that are needed to
// identify the correct schema node (do not use related
// attributes to search for nodes)
final PropertyMap identifyingKeyValues = new PropertyMap();
for (final PropertyKey key : identifyingPropertyKeys) {
identifyingKeyValues.put(key, attributes.get(key));
}
result = app.nodeQuery(type).and(identifyingKeyValues).getResult();
}
}
// test set notion attributes for relationship creation
Map<String, PropertyMap> notionPropertyMap = (Map<String, PropertyMap>) securityContext.getAttribute("notionProperties");
if (notionPropertyMap == null) {
notionPropertyMap = new HashMap<>();
securityContext.setAttribute("notionProperties", notionPropertyMap);
}
// just check for existance
String errorMessage = null;
final int size = result.size();
switch(size) {
case 0:
if (createIfNotExisting) {
// create node and return it
T newNode = app.create(type, attributes);
if (newNode != null) {
notionPropertyMap.put(getStorageKey(relationProperty, newNode, sourceTypeName), foreignProperties);
return newNode;
}
}
errorMessage = "No node found for the given properties and auto-creation not enabled";
break;
case 1:
final T typedResult = getTypedResult(result, type);
notionPropertyMap.put(getStorageKey(relationProperty, typedResult, sourceTypeName), foreignProperties);
// set properties on existing node (relationships)
for (final Entry<PropertyKey, Object> entry : attributes.entrySet()) {
typedResult.setProperty(entry.getKey(), entry.getValue());
}
return typedResult;
default:
errorMessage = "Found " + size + " nodes for given type and properties, property set is ambiguous";
logger.error("Found {} nodes for given type and properties, property set is ambiguous!\n" + "This is often due to wrong modeling, or you should consider creating a uniquness constraint for " + type.getName(), size);
break;
}
throw new FrameworkException(404, errorMessage, new PropertiesNotFoundToken(type.getSimpleName(), AbstractNode.base, attributes));
}
return null;
}
Aggregations