Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #30 from mebibou/direction-branch
Browse files Browse the repository at this point in the history
Support of Direction.BOTH for a relationship
  • Loading branch information
Benoît Simard committed Jun 23, 2013
2 parents 3d02433 + 91b22f6 commit d8157db
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 30 deletions.
Expand Up @@ -41,10 +41,10 @@ public class User extends Neo4jModel {

public Blob avatar;

@Neo4jRelatedTo(value = "IS_FRIEND", direction = "OUTGOING", lazy = true)
@Neo4jRelatedTo(value = "IS_FRIEND", direction = Direction.OUTGOING, lazy = true)
public List<User> friends;

@Neo4jRelatedTo(value = "IS_FRIEND", direction = "INCOMING", lazy = true)
@Neo4jRelatedTo(value = "IS_FRIEND", direction = Direction.INCOMING, lazy = true)
public List<User> followers;

public String toString() {
Expand Down
6 changes: 4 additions & 2 deletions src/play/modules/neo4j/annotation/Neo4jRelatedTo.java
Expand Up @@ -23,6 +23,9 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.RelationshipType;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Neo4jRelatedTo {
Expand All @@ -31,6 +34,5 @@

boolean lazy() default true;

// can be "OUTGOING" or "INCOMING" but not both !
String direction() default "OUTGOING";
Direction direction() default Direction.BOTH;
}
5 changes: 3 additions & 2 deletions src/play/modules/neo4j/annotation/Neo4jUniqueRelation.java
Expand Up @@ -23,6 +23,8 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.neo4j.graphdb.Direction;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Neo4jUniqueRelation {
Expand All @@ -32,6 +34,5 @@
// active line saving mode for this element (to do an history, a time-line, like for blogs)
boolean line() default true;

// can be "OUTGOING" or "INCOMING" but not both !
String direction() default "OUTGOING";
Direction direction() default Direction.BOTH;
}
4 changes: 2 additions & 2 deletions src/play/modules/neo4j/model/Neo4jFactory.java
Expand Up @@ -208,7 +208,7 @@ public Neo4jModel saveAndIndex(Neo4jModel nodeWrapper) throws Neo4jException {
if (field.isAnnotationPresent(Neo4jRelatedTo.class) && field.get(nodeWrapper) != null) {
// we retrive annotation value
Neo4jRelatedTo neo4jRelatedTo = field.getAnnotation(Neo4jRelatedTo.class);
Direction relationDirection = Direction.valueOf(neo4jRelatedTo.direction());
Direction relationDirection = neo4jRelatedTo.direction();
RelationshipType relationType = DynamicRelationshipType.withName(neo4jRelatedTo.value());

// construct an hasmap of database relation from node with begin node / relation format.
Expand Down Expand Up @@ -271,7 +271,7 @@ public Neo4jModel saveAndIndex(Neo4jModel nodeWrapper) throws Neo4jException {
if (field.isAnnotationPresent(Neo4jUniqueRelation.class)) {
// we retrive annotation value
Neo4jUniqueRelation neo4jUnique = field.getAnnotation(Neo4jUniqueRelation.class);
Direction relationDirection = Direction.valueOf(neo4jUnique.direction());
Direction relationDirection = neo4jUnique.direction();
RelationshipType relationType = DynamicRelationshipType.withName(neo4jUnique.value());
// get GETTER method
String propertyName = field.getName().substring(0, 1).toUpperCase()
Expand Down
6 changes: 3 additions & 3 deletions src/play/modules/neo4j/model/Neo4jModel.java
Expand Up @@ -89,7 +89,7 @@ private void initializeRelations() {
field.set(
this,
Neo4jRelationFactory.getModelsFromRelation(relatedTo.value(),
"" + relatedTo.direction(), field, node));
relatedTo.direction(), field, node));
} catch (IllegalAccessException e) {
Logger.error(e.getMessage());
}
Expand All @@ -106,8 +106,8 @@ private void initializeRelations() {
try {
field.set(
this,
Neo4jRelationFactory.getModelFromUniqueRelation(uniqueRelation.value(), ""
+ uniqueRelation.direction(), field, node));
Neo4jRelationFactory.getModelFromUniqueRelation(uniqueRelation.value(),
uniqueRelation.direction(), field, node));
} catch (IllegalAccessException e) {
Logger.error(e.getMessage());
}
Expand Down
26 changes: 7 additions & 19 deletions src/play/modules/neo4j/relationship/Neo4jRelationFactory.java
Expand Up @@ -43,24 +43,18 @@ public class Neo4jRelationFactory {
* @param node
* @return
*/
public static <T extends Neo4jModel> List<T> getModelsFromRelation(String relationName, String direction,
public static <T extends Neo4jModel> List<T> getModelsFromRelation(String relationName, Direction direction,
Field field, Node node) {
// construction of the return type
List<T> list = new ArrayList();
if (node != null) {
try {
if (field.getType().isAssignableFrom(List.class)) {
node.getRelationships(Direction.valueOf(direction), DynamicRelationshipType.withName(relationName))
node.getRelationships(direction, DynamicRelationshipType.withName(relationName))
.iterator();
for (Relationship relation : node.getRelationships(Direction.valueOf(direction),
for (Relationship relation : node.getRelationships(direction,
DynamicRelationshipType.withName(relationName))) {
Node item = null;
if (direction.equalsIgnoreCase("OUTGOING")) {
item = relation.getEndNode();
}
else {
item = relation.getStartNode();
}
Node item = relation.getOtherNode(node);
T nodeWrapper = Neo4jModel.getByNode(item);
list.add(nodeWrapper);
}
Expand All @@ -85,21 +79,15 @@ public static <T extends Neo4jModel> List<T> getModelsFromRelation(String relati
* @param node
* @return
*/
public static <T extends Neo4jModel> T getModelFromUniqueRelation(String relationName, String direction,
public static <T extends Neo4jModel> T getModelFromUniqueRelation(String relationName, Direction direction,
Field field, Node node) {
T nodeWrapper = null;
try {
if (Neo4jModel.class.isAssignableFrom(field.getType())) {
for (Relationship relation : node.getRelationships(DynamicRelationshipType.withName(relationName),
Direction.valueOf(direction))) {
direction)) {
if (nodeWrapper == null) {
Node item = null;
if (direction.equalsIgnoreCase("OUTGOING")) {
item = relation.getEndNode();
}
else {
item = relation.getStartNode();
}
Node item = relation.getOtherNode(node);
nodeWrapper = Neo4jModel.getByNode(item);
Logger.debug("Loading neo4j single '" + relation.getType().name() + "-" + relation.getId()
+ "' (" + direction + ") node for node " + node.getId());
Expand Down

0 comments on commit d8157db

Please sign in to comment.