com.lordofthejars.nosqlunit.neo4j.Neo4jLowLevelOps Maven / Gradle / Ivy
package com.lordofthejars.nosqlunit.neo4j;
import java.util.HashMap;
import java.util.Iterator;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.rest.graphdb.RestAPI;
import org.neo4j.rest.graphdb.RestGraphDatabase;
import org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
import org.neo4j.tooling.GlobalGraphOperations;
public class Neo4jLowLevelOps {
private static final String GET_ALL_NODES_QUERY = "MATCH (nodes) return nodes";
private static final String GET_ALL_RELATIONSHIPS_QUERY = "match ()-[relationships]-() return relationships";
private Neo4jLowLevelOps() {
super();
}
public static Iterator getAllRelationships(GraphDatabaseService graphDatabaseService) {
Iterator allRelationships = null;
if(isRemoteConnection(graphDatabaseService)) {
allRelationships = getAllRelationshipsFromRemote(graphDatabaseService);
} else {
allRelationships = getAllRelationshipsFromEmbedded(graphDatabaseService);
}
return allRelationships;
}
public static Iterator getAllNodes(GraphDatabaseService graphDatabaseService) {
Iterator allNodes = null;
if(isRemoteConnection(graphDatabaseService)) {
allNodes = getAllNodesFromRemote(graphDatabaseService);
} else {
allNodes = getAllNodesFromEmbedded(graphDatabaseService);
}
return allNodes;
}
private static boolean isRemoteConnection(GraphDatabaseService graphDatabaseService) {
return graphDatabaseService instanceof RestGraphDatabase;
}
private static Iterator getAllNodesFromEmbedded(GraphDatabaseService graphDatabaseService) {
GlobalGraphOperations globalGraphOperations = GlobalGraphOperations.at(graphDatabaseService);
return globalGraphOperations.getAllNodes().iterator();
}
private static Iterator getAllRelationshipsFromEmbedded(GraphDatabaseService graphDatabaseService) {
GlobalGraphOperations globalGraphOperations = GlobalGraphOperations.at(graphDatabaseService);
return globalGraphOperations.getAllRelationships().iterator();
}
private static Iterator getAllNodesFromRemote(GraphDatabaseService graphDatabaseService) {
RestAPI restAPI = ((RestGraphDatabase)graphDatabaseService).getRestAPI();
RestCypherQueryEngine restCypherQueryEngine = new RestCypherQueryEngine(restAPI);
Iterator iterator = restCypherQueryEngine.query(GET_ALL_NODES_QUERY, new HashMap()).to(Node.class).iterator();
return iterator;
}
private static Iterator getAllRelationshipsFromRemote(GraphDatabaseService graphDatabaseService) {
RestAPI restAPI = ((RestGraphDatabase)graphDatabaseService).getRestAPI();
RestCypherQueryEngine restCypherQueryEngine = new RestCypherQueryEngine(restAPI);
Iterator iterator = restCypherQueryEngine.query(GET_ALL_RELATIONSHIPS_QUERY, new HashMap()).to(Relationship.class).iterator();
return iterator;
}
}