diff --git a/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jHelper.java b/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jHelper.java index e2b4faa12..378c12632 100644 --- a/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jHelper.java +++ b/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jHelper.java @@ -20,11 +20,13 @@ public abstract class Neo4jHelper { return new GraphDatabaseUtil(gds).getOrCreateSubReferenceNode(subRefRelType); } -// public static long count(Class entityClass, GraphDatabaseService gds) { -// Node subrefNode = findSubreferenceNode(Person.class, gds); -// // If the subref node is new, there are 0 instances of this entity class -// int count = ((Integer) subrefNode.getProperty(SUBREFERENCE_NODE_COUNTER_KEY, 0)); -// return count; -// } + public static long count(Class entityClass, GraphDatabaseService gds) { + // TODO: Need to figure out what to do here + // Node subrefNode = findSubreferenceNode(Person.class, gds); + // If the subref node is new, there are 0 instances of this entity class + // int count = ((Integer) subrefNode.getProperty(SUBREFERENCE_NODE_COUNTER_KEY, 0)); + // return count; + return 1; + } } diff --git a/src/test/java/org/springframework/persistence/test/Person.java b/src/test/java/org/springframework/persistence/test/Person.java index e3e52ba7c..7e2e3275a 100644 --- a/src/test/java/org/springframework/persistence/test/Person.java +++ b/src/test/java/org/springframework/persistence/test/Person.java @@ -5,6 +5,8 @@ import org.springframework.persistence.graph.GraphEntity; @GraphEntity public class Person { + private Long id; + private String name; private int age; diff --git a/src/test/java/org/springframework/persistence/test/Person_Graph_Entity.aj b/src/test/java/org/springframework/persistence/test/Person_Graph_Entity.aj new file mode 100644 index 000000000..b33431f30 --- /dev/null +++ b/src/test/java/org/springframework/persistence/test/Person_Graph_Entity.aj @@ -0,0 +1,81 @@ +package org.springframework.persistence.test; + +import java.util.ArrayList; +import java.util.List; + +import org.neo4j.graphdb.Direction; +import org.neo4j.graphdb.GraphDatabaseService; +import org.neo4j.graphdb.Node; +import org.neo4j.graphdb.Relationship; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Configurable; +import org.springframework.persistence.graph.neo4j.Neo4jHelper; + +/** + * EXAMPLE OF CODE THAT SHOULD BE GENERATED BY ROO BESIDES EACH GRAPHENTITY CLASS + * + * Note: Combines X_Roo_Entity with X_Roo_Finder, as + * we need only a single aspect for entities. + * @author rodjohnson + * + */ +privileged aspect Person_Graph_Entity { + + // TODO should be a better way of getting this? Could at least pull out gdsholder class + private static GraphDatabaseService graphDatabaseService() { + return new GdsHolder().gds; + } + + @Configurable + public static class GdsHolder { + @Autowired + public GraphDatabaseService gds; + } + + /** + * Add constructor that takes node. + * @param node + */ + public Person.new(Node node) { + setUnderlyingNode(node); + } + + public Long Person.getId() { + return getUnderlyingNode().getId(); + } + + + public static long Person.countPeople() { + return Neo4jHelper.count(Person.class, Person_Graph_Entity.graphDatabaseService()); + } + + public static List Person.findAllPeople() { + Node subrefNode = Neo4jHelper.findSubreferenceNode(Person.class, graphDatabaseService()); + // TODO Neo4j should add lazy list on top of graph + List people = new ArrayList((int) countPeople()); + for (Relationship rel : subrefNode.getRelationships(Neo4jHelper.INSTANCE_OF_RELATIONSHIP_TYPE, Direction.INCOMING)) { + Node personNode = rel.getStartNode(); + people.add(new Person(personNode)); + } + return people; + } + + public static Person Person.findPerson(Long id) { + Node n = Person_Graph_Entity.graphDatabaseService().getNodeById(id); + Person found = new Person(n); + return found; + } + + + // Pluggable query executors/resolvers, discussed with PL +// public static Person.findFooBars(int a, int b) { +// return executeQuery("foobar", a, b); +// // First look for String, then for method +// // QueryInterceptionResolver +// } + +// public static List Person.findPersonEntries(int firstResult, +// int maxResults) { +// throw new UnsupportedOperationException(); +// } +} diff --git a/src/test/java/org/springframework/persistence/test/graph/Neo4jGraphPersistenceTest.java b/src/test/java/org/springframework/persistence/test/graph/Neo4jGraphPersistenceTest.java index 9448891e6..ea73cf902 100644 --- a/src/test/java/org/springframework/persistence/test/graph/Neo4jGraphPersistenceTest.java +++ b/src/test/java/org/springframework/persistence/test/graph/Neo4jGraphPersistenceTest.java @@ -2,10 +2,15 @@ package org.springframework.persistence.test.graph; import junit.framework.Assert; +import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.neo4j.graphdb.GraphDatabaseService; +import org.neo4j.graphdb.Node; +import org.neo4j.graphdb.Transaction; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.persistence.graph.neo4j.NodeBacked; +import org.springframework.persistence.support.EntityInstantiator; import org.springframework.persistence.test.Person; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; @@ -17,12 +22,18 @@ import org.springframework.transaction.annotation.Transactional; @ContextConfiguration public class Neo4jGraphPersistenceTest { + @Autowired + private EntityInstantiator nodeInstantiator; + @Autowired protected GraphDatabaseService graphDatabaseService; + private static Long insertedId = 0L; + @Test - public void testGraphDatabaseServiceCreatedAndAutowired() { + public void testStuffWasAutowired() { Assert.assertNotNull( graphDatabaseService ); + Assert.assertNotNull( nodeInstantiator ); } @Test @@ -31,8 +42,39 @@ public class Neo4jGraphPersistenceTest { public void testUserConstructor() { int age = 39; Person p = new Person("Rod", age); - Assert.assertEquals(p.getUnderlyingNode().getProperty("Person.name"), p.getName()); - Assert.assertEquals(age, p.getAge()); + Assert.assertEquals(p.getName(), p.getUnderlyingNode().getProperty("Person.name")); + Assert.assertEquals(p.getAge(), p.getUnderlyingNode().getProperty("Person.age")); + insertedId = p.getId(); + } + + @Test + @Transactional + public void testInstantiatedFinder() { + Node n = findPersonTestNode(); + Person found = nodeInstantiator.createEntityFromState(n, Person.class); + Assert.assertEquals("Rod", found.getUnderlyingNode().getProperty("Person.name")); + Assert.assertEquals(39, found.getUnderlyingNode().getProperty("Person.age")); + } + + @Test + public void printNeo4jData() { + StringBuilder ret = new StringBuilder(); + for (Node n : graphDatabaseService.getAllNodes()) { + ret.append("ID: " + n.getId() + " ["); + int x = 0; + for (String prop : n.getPropertyKeys()) { + if (x++ > 0) { + ret.append(", "); + } + ret.append(prop + "=" + n.getProperty(prop)); + } + ret.append("] "); + } + System.out.println("*** NEO4J DATA: " + ret); + } + + private Node findPersonTestNode() { + return graphDatabaseService.getNodeById(insertedId); } } diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties index 0d19d218f..efb37749a 100644 --- a/src/test/resources/log4j.properties +++ b/src/test/resources/log4j.properties @@ -16,7 +16,7 @@ log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n -log4j.category.org.springframework=DEBUG +log4j.category.org.springframework=WARN log4j.category.org.springframework.data=TRACE log4j.category.org.springframework.datastore=TRACE log4j.category.org.springframework.persistence=TRACE