Search in sources :

Example 1 with SessionTransaction

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

the class NodeWrapper method evaluateCustomQuery.

/**
 * Evaluate a custom query and return result as a boolean value
 *
 * @param customQuery
 * @param parameters
 * @return
 */
public boolean evaluateCustomQuery(final String customQuery, final Map<String, Object> parameters) {
    final SessionTransaction tx = db.getCurrentTransaction();
    boolean result = false;
    try {
        result = tx.getBoolean(customQuery, parameters);
    } catch (Exception ignore) {
    }
    return result;
}
Also used : SessionTransaction(org.structr.bolt.SessionTransaction)

Example 2 with SessionTransaction

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

the class EntityWrapper method delete.

@Override
public void delete() throws NotInTransactionException {
    assertNotStale();
    final SessionTransaction tx = db.getCurrentTransaction();
    final Map<String, Object> map = new HashMap<>();
    map.put("id", id);
    tx.set(getQueryPrefix() + " WHERE ID(n) = {id} DELETE n", map);
    tx.modified(this);
    stale = true;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SessionTransaction(org.structr.bolt.SessionTransaction)

Example 3 with SessionTransaction

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

the class EntityWrapper method assertNotStale.

// ----- protected methods -----
protected synchronized void assertNotStale() {
    if (stale) {
        // invalidate caches
        onRemoveFromCache();
        // if a node/rel was deleted in a previous transaction but the caller keeps a
        // reference to this entity, we need to make sure that the reference is fresh.
        final SessionTransaction tx = db.getCurrentTransaction();
        final Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        try {
            // update data
            data.clear();
            update(tx.getEntity(getQueryPrefix() + " WHERE ID(n) = {id} RETURN n", map).asMap());
        } catch (NoSuchRecordException nex) {
            throw new NotFoundException(nex);
        }
        stale = false;
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SessionTransaction(org.structr.bolt.SessionTransaction) NotFoundException(org.structr.api.NotFoundException) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException)

Example 4 with SessionTransaction

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

the class EntityWrapper method setProperties.

@Override
public void setProperties(final Map<String, Object> values) {
    assertNotStale();
    final Map<String, Object> map = new HashMap<>();
    final SessionTransaction tx = db.getCurrentTransaction();
    final String query = getQueryPrefix() + " WHERE ID(n) = {id} SET n += {properties}";
    // overwrite a potential "id" property
    map.put("id", id);
    map.put("properties", values);
    // execute query
    tx.set(query, map);
    // update data
    update(values);
    tx.modified(this);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SessionTransaction(org.structr.bolt.SessionTransaction)

Example 5 with SessionTransaction

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

the class NodeWrapper method createRelationshipTo.

@Override
public Relationship createRelationshipTo(final Node endNode, final RelationshipType relationshipType, final Map<String, Object> properties) {
    assertNotStale();
    dontUseCache = true;
    final SessionTransaction tx = db.getCurrentTransaction();
    final Map<String, Object> map = new HashMap<>();
    final NodeWrapper otherNode = (NodeWrapper) endNode;
    final String tenantIdentifier = db.getTenantIdentifier();
    final StringBuilder buf = new StringBuilder();
    map.put("id1", id);
    map.put("id2", endNode.getId());
    map.put("relProperties", properties);
    buf.append("MATCH (n");
    if (tenantIdentifier != null) {
        buf.append(":");
        buf.append(tenantIdentifier);
    }
    buf.append("), (m");
    if (tenantIdentifier != null) {
        buf.append(":");
        buf.append(tenantIdentifier);
    }
    buf.append(") WHERE ID(n) = {id1} AND ID(m) = {id2} ");
    buf.append("MERGE (n)-[r:");
    buf.append(relationshipType.name());
    buf.append("]->(m)");
    buf.append(" SET r += {relProperties} RETURN r");
    final org.neo4j.driver.v1.types.Relationship rel = tx.getRelationship(buf.toString(), map);
    tx.modified(this);
    tx.modified(otherNode);
    // clear caches
    ((NodeWrapper) endNode).relationshipCache.clear();
    relationshipCache.clear();
    return RelationshipWrapper.newInstance(db, rel);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SessionTransaction(org.structr.bolt.SessionTransaction)

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