use of org.structr.api.NativeResult 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.");
}
use of org.structr.api.NativeResult in project structr by structr.
the class CypherQueryCommand method execute.
public List<GraphObject> execute(String query, Map<String, Object> parameters, boolean includeHiddenAndDeleted, boolean publicOnly) throws FrameworkException {
DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
RelationshipFactory relFactory = new RelationshipFactory(securityContext);
NodeFactory nodeFactory = new NodeFactory(securityContext);
List<GraphObject> resultList = new LinkedList<>();
// graphdb can be null..
if (graphDb != null) {
try (final NativeResult result = graphDb.execute(query, parameters != null ? parameters : Collections.emptyMap())) {
while (result.hasNext()) {
final Map<String, Object> row = result.next();
for (Entry<String, Object> entry : row.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
final Object obj = handleObject(nodeFactory, relFactory, key, value, includeHiddenAndDeleted, publicOnly, 0);
if (obj != null) {
if (obj instanceof GraphObject) {
resultList.add((GraphObject) obj);
} else if (obj instanceof Collection) {
final List<Object> nonGraphObjectResult = new LinkedList<>();
for (final Object item : ((Collection) obj)) {
if (item instanceof GraphObject) {
resultList.add((GraphObject) item);
} else {
nonGraphObjectResult.add(item);
}
}
if (!nonGraphObjectResult.isEmpty()) {
// Wrap non-graph-objects in simple list
final GraphObjectMap graphObject = new GraphObjectMap();
graphObject.setProperty(new GenericProperty(key), nonGraphObjectResult);
resultList.add(graphObject);
}
} else {
logger.warn("Unable to handle Cypher query result object of type {}, ignoring.", obj.getClass().getName());
}
}
}
}
}
}
return resultList;
}
Aggregations