Search in sources :

Example 1 with NodeEntity

use of org.neo4j.ogm.annotation.NodeEntity in project neo4j-ogm by neo4j.

the class MappingContext method neighbours.

/**
 * Get related objects of an entity / relationship. Used in deletion scenarios.
 *
 * @param entity The entity to look neighbours for.
 * @return If entity is a relationship, end and start nodes. If entity is a node, the relations pointing to it.
 */
public Set<Object> neighbours(Object entity) {
    return optionalNativeId(entity).filter(id -> id >= 0).map(id -> {
        Set<Object> neighbours = new HashSet<>();
        Class<?> type = entity.getClass();
        if (!metaData.isRelationshipEntity(type.getName())) {
            if (getNodeEntity(id) != null) {
                // todo: refactor to create a list of mappedRelationships from a nodeEntity id.
                for (MappedRelationship mappedRelationship : relationshipRegister) {
                    if (mappedRelationship.getStartNodeId() == id || mappedRelationship.getEndNodeId() == id) {
                        Object affectedObject = mappedRelationship.getEndNodeId() == id ? getNodeEntity(mappedRelationship.getStartNodeId()) : getNodeEntity(mappedRelationship.getEndNodeId());
                        if (affectedObject != null) {
                            neighbours.add(affectedObject);
                        }
                    }
                }
            }
        } else if (relationshipEntityRegister.containsKey(id)) {
            ClassInfo classInfo = metaData.classInfo(type.getName());
            FieldInfo startNodeReader = classInfo.getStartNodeReader();
            FieldInfo endNodeReader = classInfo.getEndNodeReader();
            neighbours.add(startNodeReader.read(entity));
            neighbours.add(endNodeReader.read(entity));
        }
        return neighbours;
    }).orElseGet(Collections::emptySet);
}
Also used : Iterator(java.util.Iterator) IdStrategy(org.neo4j.ogm.id.IdStrategy) MetaData(org.neo4j.ogm.metadata.MetaData) Collection(java.util.Collection) Set(java.util.Set) InternalIdStrategy(org.neo4j.ogm.id.InternalIdStrategy) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Map(java.util.Map) UuidStrategy(org.neo4j.ogm.id.UuidStrategy) Optional(java.util.Optional) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) Queue(java.util.Queue) MappingException(org.neo4j.ogm.exception.core.MappingException) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) NodeEntity(org.neo4j.ogm.annotation.NodeEntity) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) EntityUtils(org.neo4j.ogm.utils.EntityUtils) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Collections(java.util.Collections) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Aggregations

ArrayDeque (java.util.ArrayDeque)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Queue (java.util.Queue)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 NodeEntity (org.neo4j.ogm.annotation.NodeEntity)1 MappingException (org.neo4j.ogm.exception.core.MappingException)1 IdStrategy (org.neo4j.ogm.id.IdStrategy)1 InternalIdStrategy (org.neo4j.ogm.id.InternalIdStrategy)1 UuidStrategy (org.neo4j.ogm.id.UuidStrategy)1 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)1