Search in sources :

Example 11 with SessionTransaction

use of org.structr.bolt.SessionTransaction in project structr by structr.

the class NodeWrapper method removeLabel.

@Override
public void removeLabel(final Label label) {
    assertNotStale();
    final SessionTransaction tx = db.getCurrentTransaction();
    final Map<String, Object> map = new HashMap<>();
    final String tenantIdentifier = db.getTenantIdentifier();
    map.put("id", id);
    tx.set("MATCH (n" + (tenantIdentifier != null ? ":" + tenantIdentifier : "") + ") WHERE ID(n) = {id} REMOVE n:" + label.name(), map);
    tx.modified(this);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SessionTransaction(org.structr.bolt.SessionTransaction)

Example 12 with SessionTransaction

use of org.structr.bolt.SessionTransaction in project structr by structr.

the class NodeWrapper method getRelationships.

@Override
public Iterable<Relationship> getRelationships(final Direction direction, final RelationshipType relationshipType) {
    assertNotStale();
    final RelationshipRelationshipMapper mapper = new RelationshipRelationshipMapper(db);
    final SessionTransaction tx = db.getCurrentTransaction();
    List<Relationship> list = getList(direction, relationshipType);
    if (list == null || dontUseCache) {
        final Map<String, Object> map = new HashMap<>();
        final String tenantIdentifier = db.getTenantIdentifier();
        map.put("id", id);
        switch(direction) {
            case BOTH:
                list = toList(Iterables.map(mapper, tx.getRelationships("MATCH (n" + (tenantIdentifier != null ? ":" + tenantIdentifier : "") + ")-[r:" + relationshipType.name() + "]-() WHERE ID(n) = {id} RETURN DISTINCT r", map)));
                break;
            case OUTGOING:
                list = toList(Iterables.map(mapper, tx.getRelationships("MATCH (n" + (tenantIdentifier != null ? ":" + tenantIdentifier : "") + ")-[r:" + relationshipType.name() + "]->() WHERE ID(n) = {id} RETURN DISTINCT r", map)));
                break;
            case INCOMING:
                list = toList(Iterables.map(mapper, tx.getRelationships("MATCH (n" + (tenantIdentifier != null ? ":" + tenantIdentifier : "") + ")<-[r:" + relationshipType.name() + "]-() WHERE ID(n) = {id} RETURN DISTINCT r", map)));
                break;
        }
        setList(direction, relationshipType, list);
    }
    return list;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SessionTransaction(org.structr.bolt.SessionTransaction) Relationship(org.structr.api.graph.Relationship) RelationshipRelationshipMapper(org.structr.bolt.mapper.RelationshipRelationshipMapper)

Example 13 with SessionTransaction

use of org.structr.bolt.SessionTransaction in project structr by structr.

the class NodeWrapper method hasRelationshipTo.

@Override
public boolean hasRelationshipTo(final RelationshipType type, final Node targetNode) {
    assertNotStale();
    AssociationList list = (AssociationList) getList(Direction.OUTGOING, type);
    if (list != null && !dontUseCache) {
        return list.containsAssociation(this, type, targetNode);
    } else {
        list = (AssociationList) getList(Direction.INCOMING, type);
        if (list != null && !dontUseCache) {
            return list.containsAssociation(targetNode, type, this);
        } else {
            final SessionTransaction tx = db.getCurrentTransaction();
            final Map<String, Object> params = new LinkedHashMap<>();
            final String tenantIdentifier = db.getTenantIdentifier();
            params.put("id1", getId());
            params.put("id2", targetNode.getId());
            try {
                // try to fetch existing relationship by node ID(s)
                // FIXME: this call can be very slow when lots of relationships exist
                tx.getLong("MATCH (n" + (tenantIdentifier != null ? ":" + tenantIdentifier : "") + ")-[r:" + type.name() + "]->(m) WHERE id(n) = {id1} AND id(m) = {id2} RETURN id(r)", params);
                // success
                return true;
            } catch (Throwable t) {
                return false;
            }
        }
    }
}
Also used : SessionTransaction(org.structr.bolt.SessionTransaction) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with SessionTransaction

use of org.structr.bolt.SessionTransaction in project structr by structr.

the class NodeWrapper method getRelationships.

@Override
public Iterable<Relationship> getRelationships(final Direction direction) {
    assertNotStale();
    final RelationshipRelationshipMapper mapper = new RelationshipRelationshipMapper(db);
    final SessionTransaction tx = db.getCurrentTransaction();
    List<Relationship> list = getList(direction, null);
    if (list == null || dontUseCache) {
        final Map<String, Object> map = new HashMap<>();
        final String tenantIdentifier = db.getTenantIdentifier();
        map.put("id", id);
        switch(direction) {
            case BOTH:
                return getRelationships();
            case OUTGOING:
                list = toList(Iterables.map(mapper, tx.getRelationships("MATCH (n" + (tenantIdentifier != null ? ":" + tenantIdentifier : "") + ")-[r]->() WHERE ID(n) = {id} RETURN DISTINCT r", map)));
                break;
            case INCOMING:
                list = toList(Iterables.map(mapper, tx.getRelationships("MATCH (n" + (tenantIdentifier != null ? ":" + tenantIdentifier : "") + ")<-[r]-() WHERE ID(n) = {id} RETURN DISTINCT r", map)));
                break;
        }
        setList(direction, null, list);
    }
    return list;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SessionTransaction(org.structr.bolt.SessionTransaction) Relationship(org.structr.api.graph.Relationship) RelationshipRelationshipMapper(org.structr.bolt.mapper.RelationshipRelationshipMapper)

Example 15 with SessionTransaction

use of org.structr.bolt.SessionTransaction in project structr by structr.

the class NodeWrapper method getRelationships.

@Override
public Iterable<Relationship> getRelationships() {
    assertNotStale();
    final RelationshipRelationshipMapper mapper = new RelationshipRelationshipMapper(db);
    final SessionTransaction tx = db.getCurrentTransaction();
    List<Relationship> list = getList(null, null);
    if (list == null || dontUseCache) {
        final Map<String, Object> map = new HashMap<>();
        final String tenantIdentifier = db.getTenantIdentifier();
        map.put("id", id);
        list = toList(Iterables.map(mapper, tx.getRelationships("MATCH (n" + (tenantIdentifier != null ? ":" + tenantIdentifier : "") + ")-[r]-() WHERE ID(n) = {id} RETURN DISTINCT r", map)));
        // store in cache
        setList(null, null, list);
    }
    return list;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SessionTransaction(org.structr.bolt.SessionTransaction) Relationship(org.structr.api.graph.Relationship) RelationshipRelationshipMapper(org.structr.bolt.mapper.RelationshipRelationshipMapper)

Aggregations

SessionTransaction (org.structr.bolt.SessionTransaction)17 HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)9 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Relationship (org.structr.api.graph.Relationship)3 RelationshipRelationshipMapper (org.structr.bolt.mapper.RelationshipRelationshipMapper)3 LinkedList (java.util.LinkedList)1 NoSuchRecordException (org.neo4j.driver.v1.exceptions.NoSuchRecordException)1 NotFoundException (org.structr.api.NotFoundException)1 Label (org.structr.api.graph.Label)1