Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new enum to specify the direction of a relationship #785

Merged
merged 4 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class Actor {
private Long id;
private String name;

@Relationship(type = "ACTS_IN", direction = "OUTGOING")
@Relationship(type = "ACTS_IN", direction = Relationship.Direction.OUTGOING)
private Set<Movie> movies = new HashSet<>();

public Actor() {
Expand All @@ -108,7 +108,7 @@ public class Movie {
private String title;
private int released;

@Relationship(type = "ACTS_IN", direction = "INCOMING")
@Relationship(type = "ACTS_IN", direction = Relationship.Direction.INCOMING)
Set<Actor> actors;

public Movie() {
Expand Down
30 changes: 24 additions & 6 deletions core/src/main/java/org/neo4j/ogm/annotation/Relationship.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,31 @@
@Inherited
public @interface Relationship {

/**
* Enumeration of the direction a relationship can take.
*/
enum Direction {

/**
* Describes an outgoing relationship.
*/
OUTGOING,

/**
* Describes an incoming relationship.
*/
INCOMING,

/**
* Describes an undirected relationship.
*/
UNDIRECTED

}

String TYPE = "type";
String DIRECTION = "direction";

String INCOMING = "INCOMING";
String OUTGOING = "OUTGOING";
String UNDIRECTED = "UNDIRECTED";

@ValueFor(TYPE)
String value() default "";

Expand All @@ -53,8 +71,8 @@

/**
* Direction of the relationship. Defaults to OUTGOING.
* Possible values are {@link #OUTGOING}, {@link #INCOMING}, {@link #UNDIRECTED}.
* Possible values are {@link Direction#OUTGOING}, {@link Direction#INCOMING} and {@link Direction#UNDIRECTED}.
*/
String direction() default OUTGOING;
Direction direction() default Direction.OUTGOING;

}
13 changes: 10 additions & 3 deletions core/src/main/java/org/neo4j/ogm/context/DirectedRelationship.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.neo4j.ogm.context;

import org.neo4j.ogm.annotation.Relationship;

/**
* Represents a relationship type along with a direction.
*
Expand All @@ -26,9 +28,14 @@
public class DirectedRelationship {

private String relationshipType;
private String relationshipDirection;
private Relationship.Direction relationshipDirection;

public DirectedRelationship(String relationshipType, String relationshipDirection) {
/**
*
* @param relationshipType The relationship type
* @param relationshipDirection The relationship direction
*/
public DirectedRelationship(String relationshipType, Relationship.Direction relationshipDirection) {
this.relationshipType = relationshipType;
this.relationshipDirection = relationshipDirection;
}
Expand All @@ -37,7 +44,7 @@ public String type() {
return relationshipType;
}

public String direction() {
public Relationship.Direction direction() {
return relationshipDirection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.neo4j.ogm.context;

import org.neo4j.ogm.annotation.Relationship.*;
michael-simons marked this conversation as resolved.
Show resolved Hide resolved

/**
* A DirectedRelationship mapping to objects of a particular type.
*
Expand All @@ -27,7 +29,7 @@ public class DirectedRelationshipForType extends DirectedRelationship {

Class type;

public DirectedRelationshipForType(String relationshipType, String relationshipDirection, Class type) {
public DirectedRelationshipForType(String relationshipType, Direction relationshipDirection, Class type) {
super(relationshipType, relationshipDirection);
this.type = type;
}
Expand Down
13 changes: 8 additions & 5 deletions core/src/main/java/org/neo4j/ogm/context/EntityCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package org.neo4j.ogm.context;

import org.neo4j.ogm.annotation.Relationship;
import org.neo4j.ogm.annotation.Relationship.*;
michael-simons marked this conversation as resolved.
Show resolved Hide resolved

import static java.util.Objects.*;
import static java.util.stream.Collectors.*;

Expand Down Expand Up @@ -52,7 +55,7 @@ class EntityCollector {
* @param target The element to add to the collection that will eventually be set on the owning type
*/
public void collectRelationship(Long sourceId, Class startPropertyType, String relationshipType,
String relationshipDirection, long relationshipId, long targetId, Object target) {
Relationship.Direction relationshipDirection, long relationshipId, long targetId, Object target) {
michael-simons marked this conversation as resolved.
Show resolved Hide resolved
record(sourceId, startPropertyType, relationshipType, relationshipDirection,
new TargetTriple(relationshipId, targetId, target));
}
Expand All @@ -68,13 +71,13 @@ public void collectRelationship(Long sourceId, Class startPropertyType, String r
* @param target The element to add to the collection that will eventually be set on the owning type
*/
public void collectRelationship(Long sourceId, Class startPropertyType, String relationshipType,
String relationshipDirection, long targetId, Object target) {
Direction relationshipDirection, long targetId, Object target) {
record(sourceId, startPropertyType, relationshipType, relationshipDirection,
new TargetTriple(targetId, target));
}

private void record(Long owningEntityId, Class startPropertyType, String relationshipType,
String relationshipDirection, TargetTriple triple) {
Direction relationshipDirection, TargetTriple triple) {
this.collected.computeIfAbsent(owningEntityId, k -> new HashMap<>());
DirectedRelationship directedRelationship = new DirectedRelationship(relationshipType, relationshipDirection);
this.collected.get(owningEntityId).computeIfAbsent(directedRelationship, k -> new HashMap<>());
Expand All @@ -89,7 +92,7 @@ public void forCollectedEntities(CollectedHandler handler) {

relationshipMap.forEach((relationship, targetTypeMap) -> {
String type = relationship.type();
String direction = relationship.direction();
Direction direction = relationship.direction();

targetTypeMap.forEach((targetType, entityTriples) -> {

Expand All @@ -106,7 +109,7 @@ public void forCollectedEntities(CollectedHandler handler) {

interface CollectedHandler {

void handle(Long sourceId, String type, String direction, Class targetType, Collection<Object> entities);
void handle(Long sourceId, String type, Direction direction, Class targetType, Collection<Object> entities);
}

/**
Expand Down
25 changes: 13 additions & 12 deletions core/src/main/java/org/neo4j/ogm/context/EntityGraphMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.function.Predicate;

import org.neo4j.ogm.annotation.Relationship;
import org.neo4j.ogm.annotation.Relationship.*;
michael-simons marked this conversation as resolved.
Show resolved Hide resolved
import org.neo4j.ogm.annotation.RelationshipEntity;
import org.neo4j.ogm.compiler.SrcTargetKey;
import org.neo4j.ogm.cypher.compiler.CompileContext;
Expand Down Expand Up @@ -145,7 +146,7 @@ public CompileContext map(Object entity, int horizon) {
AnnotationInfo annotationInfo = reInfo.annotationsInfo().get(RelationshipEntity.class);
String relationshipType = annotationInfo.get(RelationshipEntity.TYPE, null);
DirectedRelationship directedRelationship = new DirectedRelationship(relationshipType,
Relationship.OUTGOING);
Direction.OUTGOING);

RelationshipBuilder relationshipBuilder = getRelationshipBuilder(compiler, entity, directedRelationship,
false);
Expand Down Expand Up @@ -373,7 +374,7 @@ private void mapEntityReferences(final Object entity, NodeBuilder nodeBuilder, i
for (FieldInfo reader : srcInfo.relationshipFields()) {

String relationshipType = reader.relationshipType();
String relationshipDirection = reader.relationshipDirection();
Direction relationshipDirection = reader.relationshipDirection();
Class startNodeType = srcInfo.getUnderlyingClass();
Class endNodeType = DescriptorMappings.getType(reader.getTypeDescriptor());

Expand Down Expand Up @@ -445,11 +446,11 @@ private void mapEntityReferences(final Object entity, NodeBuilder nodeBuilder, i
*/
private boolean clearContextRelationships(CompileContext compileContext, Long identity, Class endNodeType,
DirectedRelationship directedRelationship) {
if (directedRelationship.direction().equals(Relationship.INCOMING)) {
if (directedRelationship.direction().equals(Direction.INCOMING)) {
michael-simons marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.debug("context-del: ({})<-[:{}]-()", identity, directedRelationship.type());
return compileContext.deregisterIncomingRelationships(identity, directedRelationship.type(), endNodeType,
metaData.isRelationshipEntity(endNodeType.getName()));
} else if (directedRelationship.direction().equals(Relationship.OUTGOING)) {
} else if (directedRelationship.direction().equals(Direction.OUTGOING)) {
LOGGER.debug("context-del: ({})-[:{}]->()", identity, directedRelationship.type());
return compileContext.deregisterOutgoingRelationships(identity, directedRelationship.type(), endNodeType);
} else {
Expand Down Expand Up @@ -771,14 +772,14 @@ private MappedRelationship createMappedRelationship(RelationshipBuilder relation
MappedRelationship mappedRelationshipIncoming = new MappedRelationship(relNodes.targetId,
relationshipBuilder.type(), relNodes.sourceId, isRelationshipEntity ? relationshipBuilder.reference() : null, relNodes.sourceType,
relNodes.targetType);
if (relationshipBuilder.hasDirection(Relationship.UNDIRECTED)) {
if (relationshipBuilder.hasDirection(Direction.UNDIRECTED)) {
if (mappingContext.containsRelationship(mappedRelationshipIncoming)) {
return mappedRelationshipIncoming;
}
return mappedRelationshipOutgoing;
}

if (relationshipBuilder.hasDirection(Relationship.INCOMING)) {
if (relationshipBuilder.hasDirection(Direction.INCOMING)) {
return mappedRelationshipIncoming;
}

Expand Down Expand Up @@ -809,7 +810,7 @@ private void mapRelatedEntity(NodeBuilder srcNodeBuilder,
CompileContext context = compiler.context();

boolean alreadyVisitedNode = context.visited(relNodes.target, horizon);
boolean selfReferentialUndirectedRelationship = relationshipBuilder.hasDirection(Relationship.UNDIRECTED)
boolean selfReferentialUndirectedRelationship = relationshipBuilder.hasDirection(Direction.UNDIRECTED)
&& relNodes.source.getClass() == relNodes.target.getClass();
boolean relationshipFromExplicitlyMappedObject = level == 1;

Expand Down Expand Up @@ -898,7 +899,7 @@ private void maybeCreateRelationship(CompileContext context, Long src, Relations
return;
}

if (relationshipBuilder.hasDirection(Relationship.INCOMING)) {
if (relationshipBuilder.hasDirection(Direction.INCOMING)) {
//Still create a mapped relationship from src->tgt but we need to reconcile the types too
//If its a rel entity then we want to rebase the startClass to the @StartNode of the rel entity and the endClass to the rel entity
if (metaData.isRelationshipEntity(tgtClass.getName())) {
Expand Down Expand Up @@ -983,7 +984,7 @@ private boolean isRelationshipEntity(Object potentialRelationshipEntity) {
* @return true if the relationship should be mapped both ways, false otherwise
*/
private boolean bothWayMappingRequired(Object srcObject, String relationshipType, Object tgtObject,
String relationshipDirection) {
Relationship.Direction relationshipDirection) {
boolean mapBothWays = false;

ClassInfo tgtInfo = metaData.classInfo(tgtObject);
Expand All @@ -993,9 +994,9 @@ private boolean bothWayMappingRequired(Object srcObject, String relationshipType
return false;
}
for (FieldInfo tgtRelReader : tgtInfo.relationshipFields()) {
String tgtRelationshipDirection = tgtRelReader.relationshipDirection();
if ((tgtRelationshipDirection.equals(Relationship.OUTGOING) || tgtRelationshipDirection
.equals(Relationship.INCOMING)) //The relationship direction must be explicitly incoming or outgoing
Direction tgtRelationshipDirection = tgtRelReader.relationshipDirection();
if ((tgtRelationshipDirection.equals(Direction.OUTGOING) || tgtRelationshipDirection
michael-simons marked this conversation as resolved.
Show resolved Hide resolved
.equals(Direction.INCOMING)) //The relationship direction must be explicitly incoming or outgoing
&& tgtRelReader.relationshipType().equals(
relationshipType)) { //The source must have the same relationship type to the target as the target to the source
//Moreover, the source must be related to the target and vice versa in the SAME direction
Expand Down
10 changes: 6 additions & 4 deletions core/src/main/java/org/neo4j/ogm/context/GraphEntityMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package org.neo4j.ogm.context;

import static java.util.stream.Collectors.*;
import static org.neo4j.ogm.annotation.Relationship.*;
import static org.neo4j.ogm.annotation.Relationship.Direction.*;
michael-simons marked this conversation as resolved.
Show resolved Hide resolved
import static org.neo4j.ogm.metadata.reflect.EntityAccessManager.*;

import java.lang.reflect.InvocationTargetException;
Expand All @@ -29,6 +29,8 @@
import java.util.function.Predicate;

import org.neo4j.ogm.annotation.EndNode;
import org.neo4j.ogm.annotation.Relationship;
import org.neo4j.ogm.annotation.Relationship.*;
michael-simons marked this conversation as resolved.
Show resolved Hide resolved
import org.neo4j.ogm.annotation.StartNode;
import org.neo4j.ogm.exception.core.MappingException;
import org.neo4j.ogm.metadata.ClassInfo;
Expand Down Expand Up @@ -524,7 +526,7 @@ private void mapOneToMany(Collection<Edge> oneToManyRelationships) {
* @return FieldWriter or null if none exists
*/
private FieldInfo findIterableWriter(Object instance, Object parameter, String relationshipType,
String relationshipDirection) {
Direction relationshipDirection) {
ClassInfo classInfo = metadata.classInfo(instance);
return EntityAccessManager
.getIterableField(classInfo, parameter.getClass(), relationshipType, relationshipDirection);
Expand All @@ -540,7 +542,7 @@ private FieldInfo findIterableWriter(Object instance, Object parameter, String r
* @param relationshipType the relationship type associated with these values
*/
private void mapOneToMany(Object instance, Class<?> valueType, Object values, String relationshipType,
String relationshipDirection) {
Direction relationshipDirection) {

ClassInfo classInfo = metadata.classInfo(instance);

Expand Down Expand Up @@ -645,7 +647,7 @@ private ClassInfo getRelationshipEntity(Edge edge) {
* @param relationshipDirection the direction of the relationship
* @return true if 'by' declares the specified relationship on 'to', false otherwise
*/
private boolean declaresRelationshipTo(Class to, Class by, String relationshipName, String relationshipDirection) {
private boolean declaresRelationshipTo(Class to, Class by, String relationshipName, Relationship.Direction relationshipDirection) {
michael-simons marked this conversation as resolved.
Show resolved Hide resolved
return EntityAccessManager
.getRelationalWriter(metadata.classInfo(by.getName()), relationshipName, relationshipDirection, to) != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.neo4j.ogm.context;

import org.neo4j.ogm.annotation.Relationship;
import org.neo4j.ogm.annotation.Relationship.*;
michael-simons marked this conversation as resolved.
Show resolved Hide resolved
import org.neo4j.ogm.cypher.compiler.RelationshipBuilder;

/**
Expand Down Expand Up @@ -61,11 +61,11 @@ public boolean equals(Long otherSrc, RelationshipBuilder builder, Long otherTgt)
Boolean singleton = builder.isSingleton();
if (this.rel.equals(builder.type())) {
if (singleton) {
if (builder.hasDirection(Relationship.OUTGOING)) {
if (builder.hasDirection(Direction.OUTGOING)) {
if (this.src.equals(otherSrc) && this.tgt.equals(otherTgt)) {
return true;
}
} else if (builder.hasDirection(Relationship.INCOMING)) {
} else if (builder.hasDirection(Direction.INCOMING)) {
if (this.src.equals(otherTgt) && this.tgt.equals(otherSrc)) {
return true;
}
Expand Down
13 changes: 7 additions & 6 deletions core/src/main/java/org/neo4j/ogm/cypher/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;

import org.neo4j.ogm.annotation.Relationship.*;
michael-simons marked this conversation as resolved.
Show resolved Hide resolved
import org.neo4j.ogm.cypher.function.DistanceComparison;
import org.neo4j.ogm.cypher.function.FilterFunction;
import org.neo4j.ogm.cypher.function.PropertyComparison;
Expand Down Expand Up @@ -91,7 +92,7 @@ public class Filter implements FilterWithRelationship {
/**
* The relationship direction from the parent entity to the nested property
*/
private String relationshipDirection;
private Direction relationshipDirection;

private AttributeConverter propertyConverter;

Expand Down Expand Up @@ -130,11 +131,11 @@ public static void setNameFromProperty(Filter filter, String propertyName) {
filter.propertyName = propertyName;
}

public String getRelationshipDirection() {
public Direction getRelationshipDirection() {
return relationshipDirection;
}

public void setRelationshipDirection(String relationshipDirection) {
public void setRelationshipDirection(Direction relationshipDirection) {
this.relationshipDirection = relationshipDirection;
}

Expand Down Expand Up @@ -270,7 +271,7 @@ public static class NestedPathSegment implements FilterWithRelationship {
private final Class propertyType;

private String relationshipType;
private String relationshipDirection;
private Direction relationshipDirection;
private String nestedEntityTypeLabel;
private boolean nestedRelationshipEntity;

Expand All @@ -295,11 +296,11 @@ public void setRelationshipType(String relationshipType) {
this.relationshipType = relationshipType;
}

public void setRelationshipDirection(String relationshipDirection) {
public void setRelationshipDirection(Direction relationshipDirection) {
this.relationshipDirection = relationshipDirection;
}

public String getRelationshipDirection() {
public Direction getRelationshipDirection() {
return relationshipDirection;
}

Expand Down
Loading