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);
}
Aggregations