Skip to content
Permalink
Browse files
Merge pull request #95 from isururanawaka/test_framework
use try blocks to create neo4j driver sessions
  • Loading branch information
isururanawaka committed May 17, 2022
2 parents fe0f5f7 + 3590ffa commit b3748a489f57dd590e8f325a4ca0fa06d3c45b2a
Showing 1 changed file with 67 additions and 82 deletions.
@@ -54,124 +54,109 @@ public void init(String uri, String userName, String password) {
}

public List<Record> searchNodes(String query) {
Session session = driver.session();
if (!session.isOpen()) {
session = resume();
try (Session session = driver.session()) {
Result result = session.run(query);
return result.list();
}
Result result = session.run(query);
return result.list();
}

public List<Record> searchNodes(Map<String, Object> properties, String query) {
Session session = driver.session();
if (!session.isOpen()) {
session = resume();
try (Session session = driver.session()) {
Result result = session.run(query, properties);
return result.list();
}
Result result = session.run(query, properties);
return result.list();
}

public void mergeNode(Map<String, Object> properties, String label, String userId, String entityId,
String tenantId) {
Session session = driver.session();
if (!session.isOpen()) {
session = resume();
try (Session session = driver.session()) {
Map<String, Object> parameters = new HashMap<>();
properties.put("entityId", entityId);
properties.put("tenantId", tenantId);
parameters.put("props", properties);
parameters.put("username", userId);
parameters.put("entityId", entityId);
parameters.put("tenantId", tenantId);
Transaction tx = session.beginTransaction();
tx.run("MATCH (u:User) where u.username = $username AND u.tenantId = $tenantId " +
" MERGE (n:" + label + " {entityId: $entityId,tenantId: $tenantId}) ON MATCH SET n += $props ON CREATE SET n += $props" +
" MERGE (n)-[r2:SHARED_WITH {permission:'OWNER'}]->(u) return n", parameters);
tx.commit();
tx.close();
}
Map<String, Object> parameters = new HashMap<>();
properties.put("entityId", entityId);
properties.put("tenantId", tenantId);
parameters.put("props", properties);
parameters.put("username", userId);
parameters.put("entityId", entityId);
parameters.put("tenantId", tenantId);
Transaction tx = session.beginTransaction();
tx.run("MATCH (u:User) where u.username = $username AND u.tenantId = $tenantId " +
" MERGE (n:" + label + " {entityId: $entityId,tenantId: $tenantId}) ON MATCH SET n += $props ON CREATE SET n += $props" +
" MERGE (n)-[r2:SHARED_WITH {permission:'OWNER'}]->(u) return n", parameters);
tx.commit();
tx.close();
}

public void mergeNodesWithParentChildRelationShip(Map<String, Object> childProperties, Map<String, Object> parentProperties,
String childLabel, String parentLablel, String userId, String childEntityId,
String parentEntityId,
String tenantId) {
Session session = driver.session();
if (!session.isOpen()) {
session = resume();
try (Session session = driver.session()) {
Map<String, Object> parameters = new HashMap<>();
childProperties.put("childEntityId", childEntityId);
childProperties.put("tenantId", tenantId);
parentProperties.put("parentEntityId", parentEntityId);
parentProperties.put("tenantId", tenantId);
parameters.put("childProps", childProperties);
parameters.put("parentProps", parentProperties);
parameters.put("username", userId);
parameters.put("childEntityId", childEntityId);
parameters.put("parentEntityId", parentEntityId);
parameters.put("tenantId", tenantId);
Transaction tx = session.beginTransaction();
tx.run("MATCH (u:User) where u.username = $username AND u.tenantId = $tenantId " +
" MERGE (p:" + parentLablel + " {entityId: $parentEntityId,tenantId: $tenantId}) ON MATCH SET p += $parentProps ON CREATE SET p += $parentProps" +
" MERGE (c:" + childLabel + " {entityId: $childEntityId,tenantId: $tenantId}) ON MATCH SET c += $childProps ON CREATE SET c += $childProps" +
" MERGE (c)-[:SHARED_WITH {permission:'OWNER'}]->(u)" +
" MERGE (p)-[:SHARED_WITH {permission:'OWNER'}]->(u)" +
" MERGE (c)-[:CHILD_OF]->(p) return c", parameters);
tx.commit();
tx.close();
}
Map<String, Object> parameters = new HashMap<>();
childProperties.put("childEntityId", childEntityId);
childProperties.put("tenantId", tenantId);
parentProperties.put("parentEntityId", parentEntityId);
parentProperties.put("tenantId", tenantId);
parameters.put("childProps", childProperties);
parameters.put("parentProps", parentProperties);
parameters.put("username", userId);
parameters.put("childEntityId", childEntityId);
parameters.put("parentEntityId", parentEntityId);
parameters.put("tenantId", tenantId);
Transaction tx = session.beginTransaction();
tx.run("MATCH (u:User) where u.username = $username AND u.tenantId = $tenantId " +
" MERGE (p:" + parentLablel + " {entityId: $parentEntityId,tenantId: $tenantId}) ON MATCH SET p += $parentProps ON CREATE SET p += $parentProps" +
" MERGE (c:" + childLabel + " {entityId: $childEntityId,tenantId: $tenantId}) ON MATCH SET c += $childProps ON CREATE SET c += $childProps" +
" MERGE (c)-[:SHARED_WITH {permission:'OWNER'}]->(u)" +
" MERGE (p)-[:SHARED_WITH {permission:'OWNER'}]->(u)" +
" MERGE (c)-[:CHILD_OF]->(p) return c", parameters);
tx.commit();
tx.close();
}



public void deleteNode(String label, String entityId,
String tenantId) {
Session session = driver.session();
if (!session.isOpen()) {
session = resume();
try (Session session = driver.session()) {
Map<String, Object> parameters = new HashMap<>();
parameters.put("entityId", entityId);
parameters.put("tenantId", tenantId);
Transaction tx = session.beginTransaction();
tx.run("MATCH (n:" + label + ") where n.entityId= $entityId AND n.tenantId= $tenantId detach delete n", parameters);
tx.commit();
tx.close();
}
Map<String, Object> parameters = new HashMap<>();
parameters.put("entityId", entityId);
parameters.put("tenantId", tenantId);
Transaction tx = session.beginTransaction();
tx.run("MATCH (n:" + label + ") where n.entityId= $entityId AND n.tenantId= $tenantId detach delete n", parameters);
tx.commit();
tx.close();
}

public void runTransactionalQuery(Map<String, Object> parameters, String query) {
Session session = driver.session();
if (!session.isOpen()) {
session = resume();
try (Session session = driver.session()) {
Transaction tx = session.beginTransaction();
Result result = tx.run(query, parameters);
tx.commit();
tx.close();
}
Transaction tx = session.beginTransaction();
Result result = tx.run(query, parameters);
tx.commit();
tx.close();
}


public void runTransactionalQuery(String query) {
Session session = driver.session();
Transaction tx = session.beginTransaction();
Result result = tx.run(query);
tx.commit();
tx.close();
try (Session session = driver.session()) {
Transaction tx = session.beginTransaction();
Result result = tx.run(query);
tx.commit();
tx.close();
}
}

public void createMetadataNode(String parentLabel, String parentIdName, String parentIdValue,
String userId, String key, String value) {
Session session = driver.session();
if (!session.isOpen()) {
session = resume();
try(Session session = driver.session()) {
Transaction tx = session.beginTransaction();
tx.run("match (u:User)-[r1:MEMBER_OF]->(g:Group)<-[r2:SHARED_WITH]-(s:" + parentLabel + ") where u.userId='" + userId +
"' and s." + parentIdName + "='" + parentIdValue +
"' merge (m:Metadata)<-[r3:HAS_METADATA]-(s) set m." + key + "='" + value + "' return m");
tx.commit();
tx.close();
}

Transaction tx = session.beginTransaction();
tx.run("match (u:User)-[r1:MEMBER_OF]->(g:Group)<-[r2:SHARED_WITH]-(s:" + parentLabel + ") where u.userId='" + userId +
"' and s." + parentIdName + "='" + parentIdValue +
"' merge (m:Metadata)<-[r3:HAS_METADATA]-(s) set m." + key + "='" + value + "' return m");
tx.commit();
tx.close();
}

public boolean isOpen() {

0 comments on commit b3748a4

Please sign in to comment.