added finder for traversals, renamed finder methods to findXXX

This commit is contained in:
Michael Hunger
2010-09-07 22:34:26 +02:00
parent 46e9c3c6ab
commit 04b1eccab8
2 changed files with 23 additions and 5 deletions

View File

@@ -54,7 +54,7 @@ public class Finder<T extends NodeBacked> {
return null;
}
}
public T getByIndex(String property, Object value) {
public T findByPropertyValue(String property, Object value) {
try {
final Node node = indexService.getSingleNode(property, value);
if (node==null) return null;
@@ -64,7 +64,7 @@ public class Finder<T extends NodeBacked> {
}
}
public Iterable<T> getAllByIndex(String property, Object value) {
public Iterable<T> findAllByPropertyValue(String property, Object value) {
try {
final IndexHits<Node> nodes = indexService.getNodes(property, value);
if (nodes==null) return Collections.emptyList();
@@ -77,7 +77,10 @@ public class Finder<T extends NodeBacked> {
} catch(NotFoundException e) {
return null;
}
}
public <N extends NodeBacked> Iterable<T> findAllByTraversal(N startNode,TraversalDescription traversalDescription) {
return (Iterable<T>) startNode.find(clazz, traversalDescription);
}
}

View File

@@ -7,7 +7,6 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.neo4j.graphdb.*;
import org.neo4j.graphdb.traversal.PruneEvaluator;
import org.neo4j.graphdb.traversal.TraversalDescription;
import org.neo4j.helpers.collection.IteratorUtil;
import org.neo4j.index.IndexService;
@@ -410,7 +409,7 @@ public class Neo4jGraphPersistenceTest {
Group group = new Group();
group.setName("test");
final Finder<Group> finder = finderFactory.getFinderForClass(Group.class);
final Group found = finder.getByIndex("name", "test");
final Group found = finder.findByPropertyValue("name", "test");
Assert.assertEquals(group,found);
}
@@ -422,7 +421,7 @@ public class Neo4jGraphPersistenceTest {
Group group2 = new Group();
group2.setName("test");
final Finder<Group> finder = finderFactory.getFinderForClass(Group.class);
final Iterable<Group> found = finder.getAllByIndex("name", "test");
final Iterable<Group> found = finder.findAllByPropertyValue("name", "test");
final Collection<Group> result = IteratorUtil.addToCollection(found.iterator(), new HashSet<Group>());
Assert.assertEquals(new HashSet<Group>(Arrays.asList(group,group2)), result);
}
@@ -441,4 +440,20 @@ public class Neo4jGraphPersistenceTest {
}
Assert.assertEquals(Collections.singleton(p),found);
}
@Test
@Transactional
public void testTraverseFromGroupToPeopleWithFinder() {
final Finder<Person> finder = finderFactory.getFinderForClass(Person.class);
Person p=new Person("Michael",35);
Group group = new Group();
group.setName("dev");
group.addPerson(p);
final TraversalDescription traversalDescription = new TraversalDescriptionImpl().relationships(DynamicRelationshipType.withName("persons")).filter(Traversal.returnAllButStartNode());
Iterable<Person> people=finder.findAllByTraversal(group, traversalDescription);
final HashSet<Person> found = new HashSet<Person>();
for (Person person : people) {
found.add(person);
}
Assert.assertEquals(Collections.singleton(p),found);
}
}