Search in sources :

Example 1 with DataFormatException

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.");
}
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)

Aggregations

HashMap (java.util.HashMap)1 ConstraintViolationException (org.structr.api.ConstraintViolationException)1 DataFormatException (org.structr.api.DataFormatException)1 NativeResult (org.structr.api.NativeResult)1 Node (org.structr.api.graph.Node)1 FrameworkException (org.structr.common.error.FrameworkException)1 GraphObject (org.structr.core.GraphObject)1 AbstractNode (org.structr.core.entity.AbstractNode)1 Security (org.structr.core.entity.Security)1 PrincipalOwnsNode (org.structr.core.entity.relationship.PrincipalOwnsNode)1