Search in sources :

Example 1 with NativeResult

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.");
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) HashMap(java.util.HashMap) NativeResult(org.structr.api.NativeResult) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) PrincipalOwnsNode(org.structr.core.entity.relationship.PrincipalOwnsNode) Security(org.structr.core.entity.Security) PrincipalOwnsNode(org.structr.core.entity.relationship.PrincipalOwnsNode) DataFormatException(org.structr.api.DataFormatException) ConstraintViolationException(org.structr.api.ConstraintViolationException) GraphObject(org.structr.core.GraphObject)

Example 2 with NativeResult

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;
}
Also used : NativeResult(org.structr.api.NativeResult) DatabaseService(org.structr.api.DatabaseService) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) GraphObjectMap(org.structr.core.GraphObjectMap) GenericProperty(org.structr.core.property.GenericProperty) Collection(java.util.Collection) GraphObject(org.structr.core.GraphObject) List(java.util.List) LinkedList(java.util.LinkedList)

Aggregations

NativeResult (org.structr.api.NativeResult)2 GraphObject (org.structr.core.GraphObject)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ConstraintViolationException (org.structr.api.ConstraintViolationException)1 DataFormatException (org.structr.api.DataFormatException)1 DatabaseService (org.structr.api.DatabaseService)1 Node (org.structr.api.graph.Node)1 FrameworkException (org.structr.common.error.FrameworkException)1 GraphObjectMap (org.structr.core.GraphObjectMap)1 AbstractNode (org.structr.core.entity.AbstractNode)1 Security (org.structr.core.entity.Security)1 PrincipalOwnsNode (org.structr.core.entity.relationship.PrincipalOwnsNode)1 GenericProperty (org.structr.core.property.GenericProperty)1