use of org.structr.api.DataFormatException in project structr by structr.
the class CreateNodeCommand method createNode.
// ----- private methods -----
private Node createNode(final DatabaseService graphDb, final Principal user, final Set<String> labels, final Map<String, Object> properties) throws FrameworkException {
final Map<String, Object> parameters = new HashMap<>();
final Map<String, Object> ownsProperties = new HashMap<>();
final Map<String, Object> securityProperties = new HashMap<>();
final StringBuilder buf = new StringBuilder();
final String newUuid = (String) properties.get("id");
final String tenantId = graphDb.getTenantIdentifier();
if (user != null && user.shouldSkipSecurityRelationships() == false) {
buf.append("MATCH (u:Principal) WHERE id(u) = {userId}");
buf.append(" CREATE (u)-[o:OWNS {ownsProperties}]->(n");
if (tenantId != null) {
buf.append(":");
buf.append(tenantId);
}
for (final String label : labels) {
buf.append(":");
buf.append(label);
}
buf.append(" {nodeProperties})<-[s:SECURITY {securityProperties}]-(u)");
buf.append(" RETURN n");
// configure OWNS relationship
ownsProperties.put(GraphObject.id.dbName(), getNextUuid());
ownsProperties.put(GraphObject.type.dbName(), PrincipalOwnsNode.class.getSimpleName());
ownsProperties.put(AbstractRelationship.sourceId.dbName(), user.getUuid());
ownsProperties.put(AbstractRelationship.targetId.dbName(), newUuid);
// configure SECURITY relationship
securityProperties.put(Security.allowed.dbName(), new String[] { Permission.read.name(), Permission.write.name(), Permission.delete.name(), Permission.accessControl.name() });
securityProperties.put(GraphObject.id.dbName(), getNextUuid());
securityProperties.put(GraphObject.type.dbName(), Security.class.getSimpleName());
securityProperties.put(AbstractRelationship.sourceId.dbName(), user.getUuid());
securityProperties.put(AbstractRelationship.targetId.dbName(), newUuid);
// store properties in statement
parameters.put("userId", user.getId());
parameters.put("ownsProperties", ownsProperties);
parameters.put("securityProperties", securityProperties);
} else {
buf.append("CREATE (n");
if (tenantId != null) {
buf.append(":");
buf.append(tenantId);
}
for (final String label : labels) {
buf.append(":");
buf.append(label);
}
buf.append(" {nodeProperties})");
buf.append(" RETURN n");
}
// make properties available to Cypher statement
parameters.put("nodeProperties", properties);
final NativeResult result = graphDb.execute(buf.toString(), parameters);
try {
if (result.hasNext()) {
final Map<String, Object> data = result.next();
final Node newNode = (Node) data.get("n");
return newNode;
}
} catch (DataFormatException dex) {
throw new FrameworkException(422, dex.getMessage());
} catch (ConstraintViolationException qex) {
throw new FrameworkException(422, qex.getMessage());
}
throw new RuntimeException("Unable to create new node.");
}
Aggregations