angry1980.neo4j.Template Maven / Gradle / Ivy
package angry1980.neo4j;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
public class Template {
private GraphDatabaseService graphDB;
public Template(GraphDatabaseService graphDB) {
this.graphDB = Objects.requireNonNull(graphDB);
}
public T execute(Function f){
try(Transaction tx = graphDB.beginTx()){
T result = f.apply(graphDB);
tx.success();
return result;
}
}
public void execute(Consumer c){
try(Transaction tx = graphDB.beginTx()){
c.accept(graphDB);
tx.success();
}
}
public > K executeQuery(K query){
return execute(graphDB -> {
return this.handle(graphDB, query);
});
}
public > K handle(K query) {
return handle(graphDB, query);
}
private > K handle(GraphDatabaseService graphDB, K query){
try (Result result = graphDB.execute(query.getQuery(), query.getParams())) {
return query.handle(result);
}
}
}