DATAGRAPH-436 Use case for @Labels (first stab at this)

This commit is contained in:
Michael Hunger
2014-03-13 11:23:45 +01:00
parent 76dafd608a
commit 927dcd4318
3 changed files with 47 additions and 4 deletions

View File

@@ -25,9 +25,7 @@ import org.springframework.data.neo4j.support.index.IndexType;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.*;
@NodeEntity
@@ -93,6 +91,9 @@ public class Person {
@Query(value = "start person=node({self}) match (person)<-[:persons]-(team)-[:persons]->(member) return member.name, member.age")
private Iterable<Map<String,Object>> otherTeamMemberData;
@Labels
private Collection<String> labels;
public Person(Node n) {
setPersistentState(n);
}
@@ -262,4 +263,18 @@ public class Person {
public String getDefaultedName() {
return defaultedName;
}
public Collection<String> getLabels() {
return labels;
}
public void addLabel(String label) {
HashSet<String> newLabels = new HashSet<>(this.labels);
if (newLabels.add(label)) this.labels = newLabels;
}
public void removeLabel(String label) {
HashSet<String> newLabels = new HashSet<>(this.labels);
if (newLabels.remove(label)) this.labels = newLabels;
}
}

View File

@@ -20,7 +20,9 @@ import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Transaction;
import org.neo4j.helpers.collection.IteratorUtil;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.data.neo4j.aspects.Attribute;
import org.springframework.data.neo4j.aspects.Group;
@@ -40,6 +42,8 @@ import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.junit.Assert.*;
import static org.springframework.data.neo4j.aspects.Person.persistedPerson;
@@ -99,6 +103,30 @@ public class NodeEntityTests extends EntityTestBase {
assertArrayEquals(roleNames, g.getRoleNames());
}
@Test
@Transactional
public void testLabels() {
String[] labelNames = {"Person", "Developer", "Father","_Person"};
Person p = new Person("Michael",39).persist();
assertThat(p.getLabels(), hasItems(labelNames[0],labelNames[3]));
p.addLabel(labelNames[1]);
p.addLabel(labelNames[2]);
neo4jTemplate.save(p);
System.out.println("p.getLabels() = " + p.getLabels());
assertEquals(4, IteratorUtil.count(getNodeState(p).getLabels()));
for (Label l : getNodeState(p).getLabels()) {
assertEquals("Wrong label "+l.name(),true, asList(labelNames).contains(l.name()));
}
assertThat(p.getLabels(), hasItems(labelNames));
Person loaded = neo4jTemplate.findOne(p.getId(), Person.class);
assertThat(loaded.getLabels(), hasItems(labelNames));
loaded.removeLabel(labelNames[2]);
assertThat(p.getLabels(), hasItems(labelNames[0], labelNames[1]));
assertThat(loaded.getLabels(), hasItems(labelNames[0], labelNames[1]));
loaded = neo4jTemplate.findOne(p.getId(), Person.class);
assertThat(loaded.getLabels(), hasItems(labelNames[0],labelNames[1]));
}
@Test
@Transactional
public void testConvertedArrayProperties() {

View File

@@ -48,7 +48,7 @@ public class NodeDelegatingFieldAccessorFactory extends DelegatingFieldAccessorF
return Arrays.<FieldAccessorFactory>asList(
new IdFieldAccessorFactory(template),
new TransientFieldAccessorFactory(),
//TODO Labels new LabelFieldAccessorFactory(template),
new LabelFieldAccessorFactory(template),
new TraversalFieldAccessorFactory(template),
new QueryFieldAccessorFactory(template),
new PropertyFieldAccessorFactory(template),