All Downloads are FREE. Search and download functionalities are using the official Maven repository.

angry1980.audio.dao.Neo4jNode Maven / Gradle / Ivy

There is a newer version: 0.0.10
Show newest version
package angry1980.audio.dao;

import angry1980.audio.model.Neo4jNodeType;
import angry1980.audio.model.Neo4jRelationType;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class Neo4jNode extends Neo4j {

    public Neo4jNode(GraphDatabaseService graphDB) {
        super(graphDB);
    }

    protected Optional getNode(GraphDatabaseService graphDB, long id){
        return getNode(graphDB, getType(), id);
    }

    protected  T getEntity(GraphDatabaseService graphDB, long id, Function f){
        return getNode(graphDB, id).map(f).orElse(null);
    }

    protected Node getOrCreateNode(GraphDatabaseService graphDB, long id){
        return getOrCreateNode(graphDB, id, Function.identity());
    }

    protected Node getOrCreateNode(GraphDatabaseService graphDB, long id, Function create){
        return getOrCreateNode(graphDB, getType(), id, create);
    }

    protected Node getOrCreateNode(GraphDatabaseService graphDB, Neo4jNodeType type, long id){
        return getOrCreateNode(graphDB, type, id, Function.identity());
    }

    protected Stream getAllNodes(GraphDatabaseService graphDB) {
        return getAllNodes(graphDB, getType());
    }

    protected  Collection getAllEntities(GraphDatabaseService graphDB, Function f) {
        return getAllNodes(graphDB).map(f).collect(Collectors.toList());
    }

    protected Stream getConnectedNodes(Node node, Neo4jRelationType type){
        return getConnections(node, type).map(r -> r.getEndNode());
    }

    protected  Collection getConnectedEntities(Node node, Neo4jRelationType type, Function f) {
        return getConnectedNodes(node, type).map(f).collect(Collectors.toList());
    }

    protected  Collection getConnectedEntities(GraphDatabaseService graphDB, Neo4jNodeType nodeType, long nodeId, Neo4jRelationType type, Function f) {
        return getNode(graphDB, nodeType, nodeId)
                .map(node -> getConnectedEntities(node, type, f))
                .orElseGet(() -> Collections.emptyList());
    }

    protected abstract Neo4jNodeType getType();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy