Permalink
Browse files

cyclic dependency cleanup, now passing in neo4j-template to some of t…

…he inner classes on method call
  • Loading branch information...
1 parent 1767e52 commit 2cc97c6ec589b5a05135f322b08e5622c2538225 @jexp jexp committed Apr 23, 2012
Showing with 197 additions and 180 deletions.
  1. +2 −2 ...o4j-aspects/src/main/java/org/springframework/data/neo4j/aspects/support/node/Neo4jNodeBacking.aj
  2. +2 −2 ...main/java/org/springframework/data/neo4j/aspects/support/relationship/Neo4jRelationshipBacking.aj
  3. +1 −1 ...o4j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/path/EntityPathTest.java
  4. +3 −3 ...j-aspects/src/test/java/org/springframework/data/neo4j/aspects/support/query/QueryEngineTest.java
  5. +2 −2 ...st/resources/org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTest-context.xml
  6. +9 −0 ...src/main/java/org/springframework/data/neo4j/cross_store/config/CrossStoreNeo4jConfiguration.java
  7. +1 −1 .../main/java/org/springframework/data/neo4j/cross_store/support/node/CrossStoreNodeEntityState.java
  8. +3 −11 ...ava/org/springframework/data/neo4j/cross_store/support/node/CrossStoreNodeEntityStateFactory.java
  9. +1 −3 ...rc/test/resources/org/springframework/data/neo4j/partial/Neo4jGraphRecommendationTest-context.xml
  10. +1 −1 spring-data-neo4j-examples/cineasts-aspects/pom.xml
  11. +1 −0 ...eo4j-examples/cineasts-aspects/src/main/java/org/neo4j/cineasts/movieimport/MovieDbApiClient.java
  12. +1 −1 spring-data-neo4j-examples/cineasts-rest/pom.xml
  13. +1 −1 spring-data-neo4j-examples/cineasts/pom.xml
  14. +1 −5 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/config/Neo4jConfiguration.java
  15. +3 −3 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/conversion/DefaultConverter.java
  16. +6 −5 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/EntityPersister.java
  17. +4 −3 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jEntityConverter.java
  18. +1 −1 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/MappingInfrastructure.java
  19. +18 −14 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/Neo4jTemplate.java
  20. +26 −0 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/Neo4jTemplateAware.java
  21. +20 −9 ...-neo4j/src/main/java/org/springframework/data/neo4j/support/conversion/EntityResultConverter.java
  22. +5 −2 .../src/main/java/org/springframework/data/neo4j/support/mapping/EntityCreatingClosableIterable.java
  23. +10 −9 ...-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jEntityConverterImpl.java
  24. +7 −6 ...a-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jEntityFetchHandler.java
  25. +31 −30 ...data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jEntityPersister.java
  26. +5 −9 ...ta-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/SourceStateTransmitter.java
  27. +1 −3 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/node/EntityStateFactory.java
  28. +4 −10 ...-data-neo4j/src/main/java/org/springframework/data/neo4j/support/node/NodeEntityStateFactory.java
  29. +11 −9 ...ng-data-neo4j/src/main/java/org/springframework/data/neo4j/support/path/ConvertingEntityPath.java
  30. +6 −4 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/path/EntityEvaluator.java
  31. +1 −1 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/path/EntityMapper.java
  32. +1 −1 ...eo4j/src/main/java/org/springframework/data/neo4j/support/path/EntityPathPathIterableWrapper.java
  33. +1 −1 ...main/java/org/springframework/data/neo4j/support/query/ConversionServiceQueryResultConverter.java
  34. +1 −12 ...main/java/org/springframework/data/neo4j/support/relationship/RelationshipEntityStateFactory.java
  35. +3 −10 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/mapping/Neo4jEntityPersisterTest.java
  36. +3 −5 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/mapping/Neo4jPersistentTestBase.java
View
4 ...cts/src/main/java/org/springframework/data/neo4j/aspects/support/node/Neo4jNodeBacking.aj
@@ -120,7 +120,7 @@ public privileged aspect Neo4jNodeBacking { // extends AbstractTypeAnnotatingMix
log.error("entityStateFactory not set, not creating accessors for " + entity.getClass());
} else {
if (entity.entityState != null) return;
- entity.entityState = entityStateFactory.getEntityState(entity, true);
+ entity.entityState = entityStateFactory.getEntityState(entity, true, template);
}
}
@@ -135,7 +135,7 @@ public privileged aspect Neo4jNodeBacking { // extends AbstractTypeAnnotatingMix
public void NodeBacked.setPersistentState(Node n) {
if (this.entityState == null) {
- this.entityState = Neo4jNodeBacking.aspectOf().entityStateFactory.getEntityState(this, false);
+ this.entityState = Neo4jNodeBacking.aspectOf().entityStateFactory.getEntityState(this, false, getTemplate());
}
this.entityState.setPersistentState(n);
}
View
4 ...a/org/springframework/data/neo4j/aspects/support/relationship/Neo4jRelationshipBacking.aj
@@ -66,7 +66,7 @@ public aspect Neo4jRelationshipBacking {
log.error("entityStateFactory not set, not creating accessors for " + entity.getClass());
} else {
if (entity.entityState != null) return;
- entity.entityState = entityStateFactory.getEntityState(entity, true);
+ entity.entityState = entityStateFactory.getEntityState(entity, true, template);
}
}
@@ -107,7 +107,7 @@ public aspect Neo4jRelationshipBacking {
public void RelationshipBacked.setPersistentState(Relationship r) {
if (this.entityState == null) {
- this.entityState = Neo4jRelationshipBacking.aspectOf().entityStateFactory.getEntityState(this, true);
+ this.entityState = Neo4jRelationshipBacking.aspectOf().entityStateFactory.getEntityState(this, true, getTemplate());
}
this.entityState.setPersistentState(r);
}
View
2 ...cts/src/test/java/org/springframework/data/neo4j/aspects/support/path/EntityPathTest.java
@@ -43,7 +43,7 @@ public void shouldConvertNodePathToEntityPath() throws Exception {
Person michael = persist(new Person("Michael", 36));
Node node = getNodeState(michael);
NodePath path = new NodePath(node);
- EntityPath<Person, Person> entityPath = new ConvertingEntityPath<Person, Person>(neo4jTemplate, path);
+ EntityPath<Person, Person> entityPath = new ConvertingEntityPath<Person, Person>(path, neo4jTemplate);
Assert.assertEquals("start entity",michael, entityPath.startEntity());
Assert.assertEquals("start node",node, path.startNode());
View
6 ...s/src/test/java/org/springframework/data/neo4j/aspects/support/query/QueryEngineTest.java
@@ -71,7 +71,7 @@
public void setUp() throws Exception {
GraphDatabase graphDatabase = createGraphDatabase();
graphDatabase.setConversionService(conversionService);
- entityResultConverter = new EntityResultConverter(conversionService, neo4jTemplate);
+ entityResultConverter = new EntityResultConverter(conversionService);
testTeam.createSDGTeam();
queryEngine = graphDatabase.queryEngineFor(QueryType.Cypher);
michael = testTeam.michael;
@@ -104,7 +104,7 @@ public void testQueryListOfTypeNode() throws Exception {
@Test
public void testQueryListOfTypePerson() throws Exception {
final String queryString = "start person=node:`name-index`(name={name}) match (person) <-[:boss]- (boss) return boss";
- final Collection<Person> result = IteratorUtil.asCollection(queryEngine.query(queryString, michaelsName()).to(Person.class, new EntityResultConverter(conversionService, template)));
+ final Collection<Person> result = IteratorUtil.asCollection(queryEngine.query(queryString, michaelsName()).to(Person.class, new EntityResultConverter(conversionService)));
assertEquals(asList(testTeam.emil),result);
}
@@ -116,7 +116,7 @@ public void testQueryListOfTypePerson() throws Exception {
@Test
public void testQuerySingleOfTypePerson() throws Exception {
final String queryString = "start person=node:`name-index`(name={name}) match (person) <-[:boss]- (boss) return boss";
- final Person result = queryEngine.query(queryString, michaelsName()).to(Person.class, new EntityResultConverter<Map<String,Object>,Person>(conversionService, template)).single();
+ final Person result = queryEngine.query(queryString, michaelsName()).to(Person.class, new EntityResultConverter<Map<String,Object>,Person>(conversionService)).single();
assertEquals(testTeam.emil,result);
}
View
4 ...rces/org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTest-context.xml
@@ -83,7 +83,7 @@
<constructor-arg ref="template"/>
</bean>
</property>
- <property name="template" ref="template"/>
+ <!--property name="template" ref="template"/-->
<property name="mappingContext" ref="mappingContext"/>
</bean>
@@ -95,7 +95,7 @@
<constructor-arg ref="template"/>
</bean>
</property>
- <property name="template" ref="template"/>
+ <!--property name="template" ref="template"/-->
<property name="mappingContext" ref="mappingContext"/>
</bean>
View
9 .../java/org/springframework/data/neo4j/cross_store/config/CrossStoreNeo4jConfiguration.java
@@ -22,7 +22,9 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.aspects.config.Neo4jAspectConfiguration;
import org.springframework.data.neo4j.cross_store.support.node.CrossStoreNodeEntityInstantiator;
+import org.springframework.data.neo4j.cross_store.support.node.CrossStoreNodeEntityState;
import org.springframework.data.neo4j.cross_store.support.node.CrossStoreNodeEntityStateFactory;
+import org.springframework.data.neo4j.fieldaccess.DelegatingFieldAccessorFactory;
import org.springframework.data.neo4j.mapping.EntityInstantiator;
import org.springframework.data.neo4j.support.node.NodeEntityInstantiator;
import org.springframework.data.neo4j.support.node.NodeEntityStateFactory;
@@ -77,6 +79,13 @@ public PlatformTransactionManager neo4jTransactionManager() {
}
}
+ @Override
+ public DelegatingFieldAccessorFactory nodeDelegatingFieldAccessorFactory() throws Exception {
+ final CrossStoreNodeEntityState.CrossStoreNodeDelegatingFieldAccessorFactory nodeDelegatingFieldAccessorFactory = new CrossStoreNodeEntityState.CrossStoreNodeDelegatingFieldAccessorFactory(neo4jTemplate());
+ nodeEntityStateFactory().setNodeDelegatingFieldAccessorFactory(nodeDelegatingFieldAccessorFactory);
+ return nodeDelegatingFieldAccessorFactory;
+ }
+
@Bean
public NodeEntityStateFactory nodeEntityStateFactory() throws Exception {
final CrossStoreNodeEntityStateFactory nodeEntityStateFactory = new CrossStoreNodeEntityStateFactory();
View
2 ...va/org/springframework/data/neo4j/cross_store/support/node/CrossStoreNodeEntityState.java
@@ -47,7 +47,7 @@
private final Neo4jTemplate template;
private PersistenceUnitUtil persistenceUnitUtil;
- public CrossStoreNodeEntityState(final Node underlyingState, final ENTITY entity, final Class<? extends ENTITY> type, final Neo4jTemplate template, PersistenceUnitUtil persistenceUnitUtil, final CrossStoreNodeDelegatingFieldAccessorFactory delegatingFieldAccessorFactory, final Neo4jPersistentEntity persistentEntity) {
+ public CrossStoreNodeEntityState(final Node underlyingState, final NodeBacked entity, final Class<? extends NodeBacked> type, final Neo4jTemplate template, PersistenceUnitUtil persistenceUnitUtil, final DelegatingFieldAccessorFactory delegatingFieldAccessorFactory, final Neo4jPersistentEntity persistentEntity) {
super(underlyingState, entity, type, delegatingFieldAccessorFactory, persistentEntity);
this.template = template;
this.persistenceUnitUtil = persistenceUnitUtil;
View
14 ...springframework/data/neo4j/cross_store/support/node/CrossStoreNodeEntityStateFactory.java
@@ -32,16 +32,15 @@
* @since 30.09.11
*/
public class CrossStoreNodeEntityStateFactory extends NodeEntityStateFactory {
- private CrossStoreNodeEntityState.CrossStoreNodeDelegatingFieldAccessorFactory delegatingFieldAccessorFactory;
private EntityManagerFactory entityManagerFactory;
- public EntityState<Node> getEntityState(final Object entity, boolean detachable) {
+ public EntityState<Node> getEntityState(final Object entity, boolean detachable, Neo4jTemplate template) {
final Class<?> entityType = entity.getClass();
if (isPartial(entityType)) {
final Neo4jPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(entityType);
@SuppressWarnings("unchecked") final CrossStoreNodeEntityState<NodeBacked> partialNodeEntityState =
new CrossStoreNodeEntityState<NodeBacked>(null, (NodeBacked)entity, (Class<? extends NodeBacked>) entityType,
- template, getPersistenceUnitUtils(), delegatingFieldAccessorFactory,
+ template, getPersistenceUnitUtils(), nodeDelegatingFieldAccessorFactory,
persistentEntity);
if (!detachable) return partialNodeEntityState;
return new DetachedEntityState<Node>(partialNodeEntityState, template) {
@@ -51,7 +50,7 @@ protected boolean isDetached() {
}
};
} else {
- return super.getEntityState(entity,detachable);
+ return super.getEntityState(entity,detachable, template);
}
}
@@ -68,11 +67,4 @@ private PersistenceUnitUtil getPersistenceUnitUtils() {
public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}
-
- @Override
- public void setTemplate(Neo4jTemplate template) {
- super.setTemplate(template);
- this.delegatingFieldAccessorFactory = new CrossStoreNodeEntityState.CrossStoreNodeDelegatingFieldAccessorFactory(template);
- }
-
}
View
4 ...resources/org/springframework/data/neo4j/partial/Neo4jGraphRecommendationTest-context.xml
@@ -103,11 +103,10 @@
<bean id="nodeEntityStateFactory" class="org.springframework.data.neo4j.cross_store.support.node.CrossStoreNodeEntityStateFactory">
<property name="nodeDelegatingFieldAccessorFactory">
- <bean class="org.springframework.data.neo4j.fieldaccess.NodeDelegatingFieldAccessorFactory">
+ <bean class="org.springframework.data.neo4j.cross_store.support.node.CrossStoreNodeEntityState$CrossStoreNodeDelegatingFieldAccessorFactory">
<constructor-arg ref="neo4jTemplate"/>
</bean>
</property>
- <property name="template" ref="neo4jTemplate"/>
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="mappingContext" ref="mappingContext"/>
</bean>
@@ -121,7 +120,6 @@
<constructor-arg ref="neo4jTemplate"/>
</bean>
</property>
- <property name="template" ref="neo4jTemplate"/>
<property name="mappingContext" ref="mappingContext"/>
</bean>
View
2 spring-data-neo4j-examples/cineasts-aspects/pom.xml
@@ -218,7 +218,7 @@
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
- <version>1.7.2</version>
+ <version>1.8.3</version>
</dependency>
<dependency>
View
1 ...mples/cineasts-aspects/src/main/java/org/neo4j/cineasts/movieimport/MovieDbApiClient.java
@@ -1,5 +1,6 @@
package org.neo4j.cineasts.movieimport;
+
import org.codehaus.jackson.map.ObjectMapper;
import java.net.URL;
View
2 spring-data-neo4j-examples/cineasts-rest/pom.xml
@@ -237,7 +237,7 @@
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
- <version>1.7.2</version>
+ <version>1.8.3</version>
</dependency>
<dependency>
View
2 spring-data-neo4j-examples/cineasts/pom.xml
@@ -218,7 +218,7 @@
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
- <version>1.7.2</version>
+ <version>1.8.3</version>
</dependency>
<dependency>
View
6 ...ng-data-neo4j/src/main/java/org/springframework/data/neo4j/config/Neo4jConfiguration.java
@@ -126,11 +126,7 @@ public MappingInfrastructure mappingInfrastructure() throws Exception {
@Bean(initMethod="postConstruct")
public Neo4jTemplate neo4jTemplate() throws Exception {
- final Neo4jTemplate neo4jTemplate = new Neo4jTemplate();
- neo4jTemplate.setInfrastructure(mappingInfrastructure());
- nodeEntityStateFactory().setTemplate(neo4jTemplate);
- relationshipEntityStateFactory().setTemplate(neo4jTemplate);
- return neo4jTemplate;
+ return new Neo4jTemplate(mappingInfrastructure());
}
@Bean
View
6 ...-data-neo4j/src/main/java/org/springframework/data/neo4j/conversion/DefaultConverter.java
@@ -78,21 +78,21 @@ protected Object doConvert(Object value, Class<?> sourceType, Class type, Mappin
return null;
}
- protected Path toPath(Object value, Class<?> sourceType) {
+ public Path toPath(Object value, Class<?> sourceType) {
if (Node.class.isAssignableFrom(sourceType)) return new NodePath((Node) value);
if (Relationship.class.isAssignableFrom(sourceType)) return new RelationshipPath((Relationship) value);
if (Path.class.isAssignableFrom(sourceType)) return (Path) value;
return null;
}
- protected Relationship toRelationship(Object value, Class<?> sourceType) {
+ public Relationship toRelationship(Object value, Class<?> sourceType) {
if (Relationship.class.isAssignableFrom(sourceType)) return ((Relationship) value);
if (Path.class.isAssignableFrom(sourceType)) return ((Path) value).lastRelationship();
if (Node.class.isAssignableFrom(sourceType)) return ((Node) value).getRelationships().iterator().next();
return null;
}
- protected Node toNode(Object value, Class<?> sourceType) {
+ public Node toNode(Object value, Class<?> sourceType) {
if (Node.class.isAssignableFrom(sourceType)) return (Node)value;
if (Path.class.isAssignableFrom(sourceType)) return ((Path) value).endNode();
if (Relationship.class.isAssignableFrom(sourceType)) return ((Relationship) value).getEndNode();
View
11 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/EntityPersister.java
@@ -16,18 +16,19 @@
package org.springframework.data.neo4j.mapping;
import org.neo4j.graphdb.PropertyContainer;
+import org.springframework.data.neo4j.support.Neo4jTemplate;
/**
* @author mh
* @since 17.10.11
*/
public interface EntityPersister {
- <T> T projectTo(Object entity, Class<T> targetType);
- <T> T projectTo(Object entity, Class<T> targetType, MappingPolicy mappingPolicy);
- <S extends PropertyContainer, T> T createEntityFromState(S state, Class<T> type, MappingPolicy mappingPolicy);
- <S extends PropertyContainer, T> T createEntityFromStoredType(S state, MappingPolicy mappingPolicy);
- <S extends PropertyContainer, T> T createEntityFromStoredType(S state);
+ <T> T projectTo(Object entity, Class<T> targetType, final Neo4jTemplate template);
+ <T> T projectTo(Object entity, Class<T> targetType, MappingPolicy mappingPolicy, final Neo4jTemplate template);
+ <S extends PropertyContainer, T> T createEntityFromState(S state, Class<T> type, MappingPolicy mappingPolicy, final Neo4jTemplate template);
+ <S extends PropertyContainer, T> T createEntityFromStoredType(S state, MappingPolicy mappingPolicy, final Neo4jTemplate template);
+ <S extends PropertyContainer, T> T createEntityFromStoredType(S state, final Neo4jTemplate template);
boolean isNodeEntity(Class<?> targetType);
boolean isRelationshipEntity(Class<?> targetType);
MappingPolicy getMappingPolicy(Class<?> targetType);
View
7 ...data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jEntityConverter.java
@@ -18,16 +18,17 @@
import org.neo4j.graphdb.PropertyContainer;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.mapping.context.MappingContext;
+import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.data.neo4j.support.mapping.Neo4jPersistentEntityImpl;
/**
* @author mh
* @since 27.09.11
*/
public interface Neo4jEntityConverter<T, S extends PropertyContainer> {
- <R extends T> R read(Class<R> type, S source, MappingPolicy mappingPolicy);
- void write(T source, S sink, MappingPolicy mappingPolicy);
+ <R extends T> R read(Class<R> type, S source, MappingPolicy mappingPolicy, final Neo4jTemplate template);
+ void write(T source, S sink, MappingPolicy mappingPolicy, final Neo4jTemplate template);
MappingContext<? extends Neo4jPersistentEntity<?>, Neo4jPersistentProperty> getMappingContext();
ConversionService getConversionService();
- <R extends T> R loadEntity(R entity, S source, MappingPolicy mappingPolicy, Neo4jPersistentEntityImpl<R> persistentEntity);
+ <R extends T> R loadEntity(R entity, S source, MappingPolicy mappingPolicy, Neo4jPersistentEntityImpl<R> persistentEntity, final Neo4jTemplate template);
}
View
2 ...ata-neo4j/src/main/java/org/springframework/data/neo4j/support/MappingInfrastructure.java
@@ -108,7 +108,7 @@ public void postConstruct() {
this.entityPersister = new Neo4jEntityPersister(conversionService, nodeEntityTools, relationshipEntityTools, mappingContext, entityStateHandler);
this.entityRemover = new EntityRemover(this.entityStateHandler, nodeTypeRepresentationStrategy, relationshipTypeRepresentationStrategy, graphDatabase);
if (this.resultConverter==null) {
- this.resultConverter = new EntityResultConverter<Object, Object>(conversionService,entityPersister);
+ this.resultConverter = new EntityResultConverter<Object, Object>(conversionService);
}
this.graphDatabase.setResultConverter(resultConverter);
this.cypherQueryExecutor = new CypherQueryExecutor(graphDatabase.queryEngineFor(QueryType.Cypher, resultConverter));
View
32 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/Neo4jTemplate.java
@@ -36,7 +36,7 @@
import org.springframework.data.neo4j.core.TypeRepresentationStrategy;
import org.springframework.data.neo4j.core.UncategorizedGraphStoreException;
import org.springframework.data.neo4j.fieldaccess.GraphBackedEntityIterableWrapper;
-import org.springframework.data.neo4j.mapping.EntityPersister;
+import org.springframework.data.neo4j.mapping.EntityPersister2;
import org.springframework.data.neo4j.mapping.IndexInfo;
import org.springframework.data.neo4j.mapping.MappingPolicy;
import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty;
@@ -80,7 +80,7 @@
/*
TODO This is a merge of GraphDatabaseContext and the previous Neo4jTemplate, so it still contains inconsistencies, if you spot them, please mark them with a TODO
*/
-public class Neo4jTemplate implements Neo4jOperations, EntityPersister {
+public class Neo4jTemplate implements Neo4jOperations , EntityPersister2 {
private static final Logger log = LoggerFactory.getLogger(Neo4jTemplate.class);
private MappingInfrastructure infrastructure = new MappingInfrastructure();
@@ -148,12 +148,12 @@ public boolean transactionIsRunning() {
if (persistentEntity.isNodeEntity()) {
final Node node = getNode(id);
if (node==null) return null;
- return infrastructure.getEntityPersister().createEntityFromState(node, entityClass, persistentEntity.getMappingPolicy());
+ return infrastructure.getEntityPersister().createEntityFromState(node, entityClass, persistentEntity.getMappingPolicy(), this);
}
if (persistentEntity.isRelationshipEntity()) {
final Relationship relationship = getRelationship(id);
if (relationship==null) return null;
- return infrastructure.getEntityPersister().createEntityFromState(relationship, entityClass, persistentEntity.getMappingPolicy());
+ return infrastructure.getEntityPersister().createEntityFromState(relationship, entityClass, persistentEntity.getMappingPolicy(), this);
}
throw new IllegalArgumentException("provided entity type is not annotated with @NodeEntiy nor @RelationshipEntity");
}
@@ -174,34 +174,34 @@ public boolean transactionIsRunning() {
@Override
public <S extends PropertyContainer, T> T createEntityFromStoredType(S state) {
notNull(state,"node or relationship");
- return infrastructure.getEntityPersister().createEntityFromStoredType(state);
+ return infrastructure.getEntityPersister().createEntityFromStoredType(state, this);
}
@Override
public <S extends PropertyContainer, T> T createEntityFromStoredType(S state, MappingPolicy mappingPolicy) {
notNull(state,"node or relationship");
- return infrastructure.getEntityPersister().createEntityFromStoredType(state, mappingPolicy);
+ return infrastructure.getEntityPersister().createEntityFromStoredType(state, mappingPolicy, this);
}
@Override
public <S extends PropertyContainer, T> T createEntityFromState(S state, Class<T> type, MappingPolicy mappingPolicy) {
notNull(state,"node or relationship",type,"entity class");
- return infrastructure.getEntityPersister().createEntityFromState(state, type, mappingPolicy);
+ return infrastructure.getEntityPersister().createEntityFromState(state, type, mappingPolicy, this);
}
@Override
public <S extends PropertyContainer, T> T load(S state, Class<T> type) {
notNull(state,"node or relationship",type,"entity class");
- return infrastructure.getEntityPersister().createEntityFromState(state, type, getMappingPolicy(type));
+ return infrastructure.getEntityPersister().createEntityFromState(state, type, getMappingPolicy(type), this);
}
@Override
public <T> T projectTo(Object entity, Class<T> targetType) {
notNull(entity,"entity",targetType,"new entity class");
- return infrastructure.getEntityPersister().projectTo(entity, targetType);
+ return infrastructure.getEntityPersister().projectTo(entity, targetType, this);
}
@Override
public <T> T projectTo(Object entity, Class<T> targetType, MappingPolicy mappingPolicy) {
notNull(entity,"entity",targetType,"new entity class");
- return infrastructure.getEntityPersister().projectTo(entity, targetType, mappingPolicy);
+ return infrastructure.getEntityPersister().projectTo(entity, targetType, mappingPolicy, this);
}
/**
@@ -308,7 +308,7 @@ public boolean isRelationshipEntity(Class<?> targetType) {
@Override
@SuppressWarnings("unchecked")
public <T> T save(T entity) {
- return (T) infrastructure.getEntityPersister().persist(entity, getMappingPolicy(entity));
+ return (T) infrastructure.getEntityPersister().persist(entity, getMappingPolicy(entity), this);
}
public boolean isManaged(Object entity) {
@@ -340,7 +340,7 @@ public Object query(String statement, Map<String, Object> params, final TypeInfo
if (relationship == null) return null;
if (Relationship.class.isAssignableFrom(relationshipEntityClass)) return (R)relationship;
final Neo4jPersistentEntityImpl<?> persistentEntity = getPersistentEntity(relationshipEntityClass);
- return infrastructure.getEntityPersister().createEntityFromState(relationship, relationshipEntityClass, persistentEntity.getMappingPolicy());
+ return infrastructure.getEntityPersister().createEntityFromState(relationship, relationshipEntityClass, persistentEntity.getMappingPolicy(), this);
}
@Override
@@ -487,7 +487,7 @@ public void doWithGraphWithoutResult(GraphDatabase graph) throws Exception {
} else {
final PropertyContainer state = getPersistentState(value);
if (state != null) {
- return entityPersister.loadEntity(value, (Node) state, MappingPolicy.LOAD_POLICY, (Neo4jPersistentEntityImpl<T>) getPersistentEntity(targetType));
+ return entityPersister.loadEntity(value, (Node) state, MappingPolicy.LOAD_POLICY, (Neo4jPersistentEntityImpl<T>) getPersistentEntity(targetType), this);
} else {
// todo do nothing?
throw new MappingException("No state information available in " + value);
@@ -514,7 +514,11 @@ public MappingPolicy getMappingPolicy(Class<?> targetType) {
@Override
public ResultConverter getDefaultConverter() {
- return infrastructure.getResultConverter();
+ final ResultConverter resultConverter = infrastructure.getResultConverter();
+ if (resultConverter instanceof Neo4jTemplateAware) {
+ return ((Neo4jTemplateAware)resultConverter).with(this);
+ }
+ return resultConverter;
}
@Override
View
26 ...g-data-neo4j/src/main/java/org/springframework/data/neo4j/support/Neo4jTemplateAware.java
@@ -0,0 +1,26 @@
+/**
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.data.neo4j.support;
+
+import org.springframework.data.neo4j.conversion.ResultConverter;
+
+/**
+* @author mh
+* @since 19.04.12
+*/
+public interface Neo4jTemplateAware<T,R> {
+ ResultConverter<T,R> with(Neo4jTemplate template);
+}
View
29 ...rc/main/java/org/springframework/data/neo4j/support/conversion/EntityResultConverter.java
@@ -19,9 +19,12 @@
import org.springframework.core.convert.ConversionService;
import org.springframework.data.neo4j.annotation.MapResult;
import org.springframework.data.neo4j.conversion.DefaultConverter;
+import org.springframework.data.neo4j.conversion.ResultConverter;
import org.springframework.data.neo4j.core.EntityPath;
import org.springframework.data.neo4j.mapping.EntityPersister;
import org.springframework.data.neo4j.mapping.MappingPolicy;
+import org.springframework.data.neo4j.support.Neo4jTemplate;
+import org.springframework.data.neo4j.support.Neo4jTemplateAware;
import org.springframework.data.neo4j.support.path.ConvertingEntityPath;
import java.lang.reflect.InvocationHandler;
@@ -32,26 +35,34 @@
* @author mh
* @since 28.06.11
*/
-public class EntityResultConverter<T, R> extends DefaultConverter<T, R> {
+public class EntityResultConverter<T, R> extends DefaultConverter<T, R> implements Neo4jTemplateAware<T,R> {
private final ConversionService conversionService;
- private final EntityPersister entityPersister;
- public EntityResultConverter(ConversionService conversionService, EntityPersister entityPersister) {
+ public EntityResultConverter(ConversionService conversionService) {
this.conversionService = conversionService;
- this.entityPersister = entityPersister;
}
+ private static ThreadLocal<Neo4jTemplate> holder = new ThreadLocal<Neo4jTemplate>();
+
+ @Override
+ public ResultConverter<T,R> with(Neo4jTemplate template) {
+ holder.set(template);
+ return this;
+ }
+
+
@SuppressWarnings("unchecked")
@Override
protected Object doConvert(Object value, Class<?> sourceType, Class targetType, MappingPolicy mappingPolicy) {
+ Neo4jTemplate template = holder.get();
if (EntityPath.class.isAssignableFrom(targetType)) {
- return new ConvertingEntityPath(entityPersister, toPath(value, sourceType));
+ return new ConvertingEntityPath(toPath(value, sourceType),template);
}
- if (entityPersister.isNodeEntity(targetType)) {
- return entityPersister.projectTo(toNode(value, sourceType), targetType, mappingPolicy);
+ if (template.isNodeEntity(targetType)) {
+ return template.projectTo(toNode(value, sourceType), targetType, mappingPolicy);
}
- if (entityPersister.isRelationshipEntity(targetType)) {
- return entityPersister.projectTo(toRelationship(value, sourceType), targetType, mappingPolicy);
+ if (template.isRelationshipEntity(targetType)) {
+ return template.projectTo(toRelationship(value, sourceType), targetType, mappingPolicy);
}
final Object result = super.doConvert(value, sourceType, targetType, mappingPolicy);
View
7 ...n/java/org/springframework/data/neo4j/support/mapping/EntityCreatingClosableIterable.java
@@ -19,6 +19,7 @@
import org.neo4j.helpers.collection.ClosableIterable;
import org.neo4j.helpers.collection.IterableWrapper;
import org.springframework.data.neo4j.mapping.MappingPolicy;
+import org.springframework.data.neo4j.support.Neo4jTemplate;
/**
* @author mh
@@ -29,18 +30,20 @@
private final Class<T> entityClass;
private final Neo4jEntityPersister entityPersister;
private final MappingPolicy mappingPolicy;
+ private final Neo4jTemplate template;
- public EntityCreatingClosableIterable(ClosableIterable<PropertyContainer> iterable, Class<T> entityClass, Neo4jEntityPersister entityPersister) {
+ public EntityCreatingClosableIterable(ClosableIterable<PropertyContainer> iterable, Class<T> entityClass, Neo4jEntityPersister entityPersister, Neo4jTemplate template) {
super(iterable);
this.iterable = iterable;
this.entityClass = entityClass;
this.entityPersister = entityPersister;
+ this.template = template;
mappingPolicy = this.entityPersister.getMappingPolicy(this.entityClass);
}
@Override
protected T underlyingObjectToObject(PropertyContainer state) {
- return entityPersister.createEntityFromState(state, entityClass, mappingPolicy);
+ return entityPersister.createEntityFromState(state, entityClass, mappingPolicy, template);
}
@Override
View
19 ...rc/main/java/org/springframework/data/neo4j/support/mapping/Neo4jEntityConverterImpl.java
@@ -25,6 +25,7 @@
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.neo4j.mapping.*;
import org.springframework.data.neo4j.mapping.ManagedEntity;
+import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
@@ -66,7 +67,7 @@ public ConversionService getConversionService() {
}
@Override
- public <R extends T> R read(Class<R> requestedType, S source, MappingPolicy mappingPolicy) {
+ public <R extends T> R read(Class<R> requestedType, S source, MappingPolicy mappingPolicy, final Neo4jTemplate template) {
// 1) source -> type alias
// 2) type alias -> type
// 3) check for subtype matching / enforcement
@@ -87,22 +88,22 @@ public ConversionService getConversionService() {
entityStateHandler.setPersistentState(createdEntity,source);
if (persistentEntity.isManaged()) return createdEntity;
- loadEntity(createdEntity, source, mappingPolicy, persistentEntity);
+ loadEntity(createdEntity, source, mappingPolicy, persistentEntity, template);
return createdEntity;
}
@Override
- public <R extends T> R loadEntity(R entity, S source, MappingPolicy mappingPolicy, Neo4jPersistentEntityImpl<R> persistentEntity) {
+ public <R extends T> R loadEntity(R entity, S source, MappingPolicy mappingPolicy, Neo4jPersistentEntityImpl<R> persistentEntity, final Neo4jTemplate template) {
if (mappingPolicy.shouldLoad()) {
final BeanWrapper<Neo4jPersistentEntity<R>, R> wrapper = BeanWrapper.create(entity, conversionService);
- sourceStateTransmitter.copyPropertiesFrom(wrapper, source, persistentEntity,mappingPolicy);
+ sourceStateTransmitter.copyPropertiesFrom(wrapper, source, persistentEntity,mappingPolicy, template);
// 6) handle cascading fetches
- cascadeFetch(persistentEntity, wrapper, mappingPolicy);
+ cascadeFetch(persistentEntity, wrapper, mappingPolicy, template);
}
return entity;
}
- private <R extends T> void cascadeFetch(Neo4jPersistentEntityImpl<R> persistentEntity, final BeanWrapper<Neo4jPersistentEntity<R>, R> wrapper, final MappingPolicy policy) {
+ private <R extends T> void cascadeFetch(Neo4jPersistentEntityImpl<R> persistentEntity, final BeanWrapper<Neo4jPersistentEntity<R>, R> wrapper, final MappingPolicy policy, final Neo4jTemplate template) {
persistentEntity.doWithAssociations(new AssociationHandler<Neo4jPersistentProperty>() {
@Override
public void doWithAssociation(Association<Neo4jPersistentProperty> association) {
@@ -113,7 +114,7 @@ public void doWithAssociation(Association<Neo4jPersistentProperty> association)
final Object value = getProperty(wrapper, property);
@SuppressWarnings("unchecked") final Neo4jPersistentEntityImpl<Object> persistentEntity =
(Neo4jPersistentEntityImpl<Object>) mappingContext.getPersistentEntity(property.getTypeInformation().getActualType());
- final Object fetchedValue = entityFetchHandler.fetch(value, persistentEntity, property, mappingPolicy);
+ final Object fetchedValue = entityFetchHandler.fetch(value, persistentEntity, property, mappingPolicy, template);
// replace fetched one-time iterables and similiar managed values
sourceStateTransmitter.setProperty(wrapper, property, fetchedValue);
}
@@ -132,7 +133,7 @@ public void doWithAssociation(Association<Neo4jPersistentProperty> association)
}
@Override
- public void write(T source, S sink, MappingPolicy mappingPolicy) {
+ public void write(T source, S sink, MappingPolicy mappingPolicy, final Neo4jTemplate template) {
final Class<?> sourceType = source.getClass();
@SuppressWarnings("unchecked") final Neo4jPersistentEntityImpl<T> persistentEntity = (Neo4jPersistentEntityImpl<T>) mappingContext.getPersistentEntity(sourceType);
if (persistentEntity.isManaged()) { // todo check if typerepreentationstragegy is called ??
@@ -146,7 +147,7 @@ public void write(T source, S sink, MappingPolicy mappingPolicy) {
entityStateHandler.setPersistentState(source, sink);
typeMapper.writeType(sourceType, sink);
}
- sourceStateTransmitter.copyPropertiesTo(wrapper, sink, persistentEntity,mappingPolicy);
+ sourceStateTransmitter.copyPropertiesTo(wrapper, sink, persistentEntity,mappingPolicy, template);
}
/*
private Node useGetOrCreateNode(S node, Neo4jPersistentEntity<?> persistentEntity, BeanWrapper<Neo4jPersistentEntity<Object>, Object> wrapper) {
View
13 ...src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jEntityFetchHandler.java
@@ -23,6 +23,7 @@
import org.springframework.data.neo4j.mapping.MappingPolicy;
import org.springframework.data.neo4j.mapping.Neo4jPersistentEntity;
import org.springframework.data.neo4j.mapping.Neo4jPersistentProperty;
+import org.springframework.data.neo4j.support.Neo4jTemplate;
import java.util.ArrayList;
import java.util.List;
@@ -46,7 +47,7 @@ public Neo4jEntityFetchHandler(EntityStateHandler entityStateHandler, Conversion
// todo actually cascade !!
- public Object fetch(final Object value, Neo4jPersistentEntity<Object> persistentEntity, Neo4jPersistentProperty property, final MappingPolicy policy) {
+ public Object fetch(final Object value, Neo4jPersistentEntity<Object> persistentEntity, Neo4jPersistentProperty property, final MappingPolicy policy, final Neo4jTemplate template) {
if (value == null) return value;
//MappingPolicy mappingPolicy = mappingPolicy.combineWith(property.getMappingPolicy());
final MappingPolicy mappingPolicy = property.getMappingPolicy();
@@ -56,25 +57,25 @@ public Object fetch(final Object value, Neo4jPersistentEntity<Object> persistent
for (Object inner : ((Iterable) value)) {
final BeanWrapper<Neo4jPersistentEntity<Object>, Object> innerWrapper = BeanWrapper.create(inner, conversionService);
final PropertyContainer state = entityStateHandler.getPersistentState(inner);
- fetchValue(innerWrapper, state, persistentEntity, mappingPolicy);
+ fetchValue(innerWrapper, state, persistentEntity, mappingPolicy, template);
replacement.add(inner);
//sourceStateTransmitter.copyPropertiesFrom(innerWrapper, entityStateHandler.<S>getPersistentState(inner), persistentEntity);
}
return replacement;
} else {
final BeanWrapper<Neo4jPersistentEntity<Object>, Object> innerWrapper = BeanWrapper.create(value, conversionService);
final PropertyContainer state = entityStateHandler.getPersistentState(value);
- fetchValue(innerWrapper, state, persistentEntity, mappingPolicy);
+ fetchValue(innerWrapper, state, persistentEntity, mappingPolicy, template);
// sourceStateTransmitter.copyPropertiesFrom(innerWrapper, entityStateHandler.<S>getPersistentState(value), persistentEntity);
}
return value;
}
- public void fetchValue(final BeanWrapper<Neo4jPersistentEntity<Object>, Object> wrapper, PropertyContainer source, Neo4jPersistentEntity<Object> persistentEntity, final MappingPolicy mappingPolicy) {
+ public void fetchValue(final BeanWrapper<Neo4jPersistentEntity<Object>, Object> wrapper, PropertyContainer source, Neo4jPersistentEntity<Object> persistentEntity, final MappingPolicy mappingPolicy, final Neo4jTemplate template) {
if (persistentEntity.isNodeEntity()) {
- nodeStateTransmitter.copyPropertiesFrom(wrapper, (Node) source,persistentEntity, mappingPolicy);
+ nodeStateTransmitter.copyPropertiesFrom(wrapper, (Node) source,persistentEntity, mappingPolicy, template);
}
if (persistentEntity.isRelationshipEntity()) {
- relationshipStateTransmitter.copyPropertiesFrom(wrapper, (Relationship) source, persistentEntity, mappingPolicy);
+ relationshipStateTransmitter.copyPropertiesFrom(wrapper, (Relationship) source, persistentEntity, mappingPolicy, template);
}
}
}
View
61 ...4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4jEntityPersister.java
@@ -21,6 +21,7 @@
import org.springframework.core.convert.ConversionService;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.neo4j.mapping.*;
+import org.springframework.data.neo4j.support.Neo4jTemplate;
import java.util.HashMap;
import java.util.Map;
@@ -47,12 +48,12 @@ public Neo4jEntityPersister(ConversionService conversionService, EntityTools<Nod
}
- public <S extends PropertyContainer, T> T createEntityFromStoredType(S state, MappingPolicy mappingPolicy) {
- return createEntityFromState(state,null, mappingPolicy);
+ public <S extends PropertyContainer, T> T createEntityFromStoredType(S state, MappingPolicy mappingPolicy, final Neo4jTemplate template) {
+ return createEntityFromState(state,null, mappingPolicy, template);
}
- public <S extends PropertyContainer, T> T createEntityFromStoredType(S state) {
- return createEntityFromState(state,null, null);
+ public <S extends PropertyContainer, T> T createEntityFromStoredType(S state, final Neo4jTemplate template) {
+ return createEntityFromState(state,null, null, template);
}
@@ -155,38 +156,38 @@ public ConversionService getConversionService() {
}
@Override
- public <R> R loadEntity(R entity, S source, MappingPolicy mappingPolicy, Neo4jPersistentEntityImpl<R> persistentEntity) {
- return delegate.loadEntity(entity,source,mappingPolicy,persistentEntity);
+ public <R> R loadEntity(R entity, S source, MappingPolicy mappingPolicy, Neo4jPersistentEntityImpl<R> persistentEntity, final Neo4jTemplate template) {
+ return delegate.loadEntity(entity,source,mappingPolicy,persistentEntity, template);
}
@Override
- public <R> R read(Class<R> type, S state, MappingPolicy mappingPolicy) {
+ public <R> R read(Class<R> type, S state, MappingPolicy mappingPolicy, final Neo4jTemplate template) {
try {
if (state==null) throw new IllegalArgumentException("State must not be null");
StackedEntityCache.push();
if (StackedEntityCache.contains(state, mappingPolicy)) return StackedEntityCache.get(state,mappingPolicy);
- return StackedEntityCache.add(state, delegate.read(type, state,mappingPolicy),mappingPolicy);
+ return StackedEntityCache.add(state, delegate.read(type, state,mappingPolicy, template),mappingPolicy);
} finally {
StackedEntityCache.pop();
}
}
@Override
- public void write(Object source, S sink,MappingPolicy mappingPolicy) {
- delegate.write(source,sink,mappingPolicy);
+ public void write(Object source, S sink, MappingPolicy mappingPolicy, final Neo4jTemplate template) {
+ delegate.write(source,sink,mappingPolicy, template);
}
}
@SuppressWarnings("unchecked")
- public <S extends PropertyContainer, T> T createEntityFromState(S state, Class<T> type, MappingPolicy mappingPolicy) {
+ public <S extends PropertyContainer, T> T createEntityFromState(S state, Class<T> type, MappingPolicy mappingPolicy, final Neo4jTemplate template) {
if (state == null) {
throw new IllegalArgumentException("state has to be either a Node or Relationship, but is null");
}
if (isNode(state)) {
- return nodeConverter.read(type, (Node) state,mappingPolicy);
+ return nodeConverter.read(type, (Node) state,mappingPolicy, template);
}
if (isRelationship(state)) {
- return relationshipConverter.read(type, (Relationship) state,mappingPolicy);
+ return relationshipConverter.read(type, (Relationship) state,mappingPolicy, template);
}
throw new IllegalArgumentException("state has to be either a Node or Relationship");
}
@@ -199,18 +200,18 @@ private boolean isNode(PropertyContainer state) {
return state instanceof Node;
}
- public <T> T projectTo(Object entity, Class<T> targetType) {
- return projectTo(entity,targetType,getMappingPolicy(targetType));
+ public <T> T projectTo(Object entity, Class<T> targetType, final Neo4jTemplate template) {
+ return projectTo(entity,targetType,getMappingPolicy(targetType), template);
}
@SuppressWarnings("unchecked")
- public <T> T projectTo(Object entity, Class<T> targetType, MappingPolicy mappingPolicy) {
+ public <T> T projectTo(Object entity, Class<T> targetType, MappingPolicy mappingPolicy, final Neo4jTemplate template) {
if (targetType.isInstance(entity)) {
return (T)entity;
}
PropertyContainer state = getPersistentState(entity);
final MappingPolicy newPolicy = mappingPolicy == null ? getMappingPolicy(targetType) : mappingPolicy;
- return createEntityFromState(state, targetType, newPolicy);
+ return createEntityFromState(state, targetType, newPolicy, template);
}
@SuppressWarnings("unchecked")
@@ -219,30 +220,30 @@ private boolean isNode(PropertyContainer state) {
}
- public Object persist(Object entity, final MappingPolicy mappingPolicy) {
+ public Object persist(Object entity, final MappingPolicy mappingPolicy, final Neo4jTemplate template) {
final Class<?> type = entity.getClass();
if (isManaged(entity)) {
return ((ManagedEntity)entity).persist();
} else {
- return persist(entity, type, mappingPolicy);
+ return persist(entity, type, mappingPolicy, template);
}
}
public boolean isManaged(Object entity) {
return entityStateHandler.isManaged(entity);
}
- private Object persist(Object entity, Class<?> type,MappingPolicy mappingPolicy) {
+ private Object persist(Object entity, Class<?> type, MappingPolicy mappingPolicy, final Neo4jTemplate template) {
if (isNodeEntity(type)) {
final Node node = this.<Node>getPersistentState(entity);
- this.nodeConverter.write(entity, node,mappingPolicy);
- return createEntityFromState(getPersistentState(entity),type, getMappingPolicy(type));
+ this.nodeConverter.write(entity, node,mappingPolicy, template);
+ return createEntityFromState(getPersistentState(entity),type, getMappingPolicy(type), template);
//return entity; // TODO ?
}
if (isRelationshipEntity(type)) {
final Relationship relationship = this.<Relationship>getPersistentState(entity);
- this.relationshipConverter.write(entity, relationship,mappingPolicy);
- return createEntityFromState(getPersistentState(entity),type, getMappingPolicy(type));
+ this.relationshipConverter.write(entity, relationship,mappingPolicy, template);
+ return createEntityFromState(getPersistentState(entity),type, getMappingPolicy(type), template);
// return entity; // TODO ?
}
throw new IllegalArgumentException("@NodeEntity or @RelationshipEntity annotation required on domain class"+type);
@@ -280,18 +281,18 @@ public ConversionService getConversionService() {
}
@Override
- public <R> R read(Class<R> type, Node source, MappingPolicy mappingPolicy) {
- return createEntityFromState(source, type, mappingPolicy);
+ public <R> R read(Class<R> type, Node source, MappingPolicy mappingPolicy, final Neo4jTemplate template) {
+ return createEntityFromState(source, type, mappingPolicy, template);
}
@Override
- public <R> R loadEntity(R entity, Node source, MappingPolicy mappingPolicy, Neo4jPersistentEntityImpl<R> persistentEntity) {
- return nodeConverter.loadEntity(entity,source,mappingPolicy,persistentEntity);
+ public <R> R loadEntity(R entity, Node source, MappingPolicy mappingPolicy, Neo4jPersistentEntityImpl<R> persistentEntity, final Neo4jTemplate template) {
+ return nodeConverter.loadEntity(entity,source,mappingPolicy,persistentEntity, template);
}
@Override
- public void write(Object source, Node sink, MappingPolicy mappingPolicy) {
- nodeConverter.write(source,sink,mappingPolicy);
+ public void write(Object source, Node sink, MappingPolicy mappingPolicy, final Neo4jTemplate template) {
+ nodeConverter.write(source,sink,mappingPolicy, template);
}
}
View
14 .../src/main/java/org/springframework/data/neo4j/support/mapping/SourceStateTransmitter.java
@@ -43,12 +43,12 @@ public SourceStateTransmitter(EntityStateFactory<S> entityStateFactory) {
this.entityStateFactory = entityStateFactory;
}
- public <R> R copyPropertiesFrom(final BeanWrapper<Neo4jPersistentEntity<R>, R> wrapper, S source, Neo4jPersistentEntity<R> persistentEntity, final MappingPolicy mappingPolicy) {
+ public <R> R copyPropertiesFrom(final BeanWrapper<Neo4jPersistentEntity<R>, R> wrapper, S source, Neo4jPersistentEntity<R> persistentEntity, final MappingPolicy mappingPolicy, final Neo4jTemplate template) {
final R entity = wrapper.getBean();
/* final Transaction tx = getTemplate().beginTx();
try {
*/
- final EntityState<S> entityState = entityStateFactory.getEntityState(entity, false);
+ final EntityState<S> entityState = entityStateFactory.getEntityState(entity, false, template);
entityState.setPersistentState(source);
// entityState.persist();
persistentEntity.doWithProperties(new PropertyHandler<Neo4jPersistentProperty>() {
@@ -78,10 +78,6 @@ public void doWithAssociation(Association<Neo4jPersistentProperty> association)
entityState.setValue(property, value, mappingPolicy);
}
- private Neo4jTemplate getTemplate() {
- return entityStateFactory.getTemplate();
- }
-
private <T> T getProperty(BeanWrapper<Neo4jPersistentEntity<Object>, Object> wrapper, Neo4jPersistentProperty property, Class<T> type, boolean fieldAccessOnly) {
return wrapper.getProperty(property, type, fieldAccessOnly);
}
@@ -112,11 +108,11 @@ private Neo4jTemplate getTemplate() {
return value;
}
- public <R> void copyPropertiesTo(final BeanWrapper<Neo4jPersistentEntity<R>, R> wrapper, S target, Neo4jPersistentEntity<R> persistentEntity, MappingPolicy mappingPolicy) {
- final Transaction tx = getTemplate().beginTx();
+ public <R> void copyPropertiesTo(final BeanWrapper<Neo4jPersistentEntity<R>, R> wrapper, S target, Neo4jPersistentEntity<R> persistentEntity, MappingPolicy mappingPolicy, final Neo4jTemplate template) {
+ final Transaction tx = template.beginTx();
try {
//final Node targetNode = useGetOrCreateNode(node, persistentEntity, wrapper);
- final EntityState<S> entityState = entityStateFactory.getEntityState(wrapper.getBean(), false);
+ final EntityState<S> entityState = entityStateFactory.getEntityState(wrapper.getBean(), false, template);
entityState.setPersistentState(target);
entityState.persist();
// todo take mapping policies for attributes into account
View
4 ...a-neo4j/src/main/java/org/springframework/data/neo4j/support/node/EntityStateFactory.java
@@ -24,7 +24,5 @@
* @since 07.10.11
*/
public interface EntityStateFactory<S extends PropertyContainer> {
- EntityState<S> getEntityState(final Object entity, boolean detachable);
-
- Neo4jTemplate getTemplate();
+ EntityState<S> getEntityState(final Object entity, boolean detachable, Neo4jTemplate template);
}
View
14 ...o4j/src/main/java/org/springframework/data/neo4j/support/node/NodeEntityStateFactory.java
@@ -27,13 +27,11 @@
public class NodeEntityStateFactory implements EntityStateFactory<Node> {
- protected Neo4jTemplate template;
-
protected DelegatingFieldAccessorFactory nodeDelegatingFieldAccessorFactory;
protected Neo4jMappingContext mappingContext;
- public EntityState<Node> getEntityState(final Object entity, boolean detachable) {
+ public EntityState<Node> getEntityState(final Object entity, boolean detachable, Neo4jTemplate template) {
final Class<?> entityType = entity.getClass();
@SuppressWarnings("unchecked") final Neo4jPersistentEntity<Object> persistentEntity =
(Neo4jPersistentEntity<Object>) mappingContext.getPersistentEntity(entityType);
@@ -50,21 +48,17 @@ public void setNodeDelegatingFieldAccessorFactory(
DelegatingFieldAccessorFactory nodeDelegatingFieldAccessorFactory) {
this.nodeDelegatingFieldAccessorFactory = nodeDelegatingFieldAccessorFactory;
}
-
+
+ /*
public void setTemplate(Neo4jTemplate template) {
this.template = template;
}
-
+ */
public Neo4jMappingContext getMappingContext() {
return mappingContext;
}
public void setMappingContext(Neo4jMappingContext mappingContext) {
this.mappingContext = mappingContext;
}
-
- public Neo4jTemplate getTemplate() {
- return template;
- }
-
}
View
20 ...neo4j/src/main/java/org/springframework/data/neo4j/support/path/ConvertingEntityPath.java
@@ -24,6 +24,7 @@
import org.springframework.data.neo4j.core.EntityPath;
import org.springframework.data.neo4j.mapping.EntityPersister;
import org.springframework.data.neo4j.mapping.MappingPolicy;
+import org.springframework.data.neo4j.support.Neo4jTemplate;
import java.util.Iterator;
@@ -34,15 +35,17 @@
@SuppressWarnings("unchecked") // TODO DefaultNode/RelationshipBacked
public class ConvertingEntityPath<S,E> implements EntityPath<S,E> {
+ private final Neo4jTemplate template;
+
@Override
public <T> T startEntity(Class<T>... types) {
return projectEntityToFirstParameterOrCreateFromStoredType(startNode(), types);
}
private <T> T projectEntityToFirstParameterOrCreateFromStoredType(Node node, Class<T>... types) {
if (node==null) return null;
- if (types==null || types.length==0) return persister.createEntityFromStoredType(node, MappingPolicy.LOAD_POLICY);
- return persister.projectTo(node, types[0]);
+ if (types==null || types.length==0) return template.createEntityFromStoredType(node, MappingPolicy.LOAD_POLICY);
+ return template.projectTo(node, types[0]);
}
@Override
@@ -53,7 +56,7 @@
public <T> T lastRelationshipEntity(Class<T>... types) {
Relationship relationship = lastRelationship();
if (relationship==null) return null;
- return persister.projectTo(relationship, getFirstOrDefault((Class<T>) DefaultRelationshipBacked.class, types));
+ return template.projectTo(relationship, getFirstOrDefault((Class<T>) DefaultRelationshipBacked.class, types));
}
private static <T> T getFirstOrDefault(final T defaultValue, T... values) {
@@ -66,7 +69,7 @@
return new IterableWrapper<T,Node>(nodes()) {
@Override
protected T underlyingObjectToObject(Node node) {
- return persister.createEntityFromStoredType(node, null);
+ return template.createEntityFromStoredType(node, null);
}
};
}
@@ -76,7 +79,7 @@ protected T underlyingObjectToObject(Node node) {
return new IterableWrapper<T,Relationship>(relationships()) {
@Override
protected T underlyingObjectToObject(Relationship relationship) {
- return persister.projectTo(relationship, getFirstOrDefault((Class<T>) DefaultRelationshipBacked.class, relationships));
+ return template.projectTo(relationship, getFirstOrDefault((Class<T>) DefaultRelationshipBacked.class, relationships));
}
};
}
@@ -86,18 +89,17 @@ protected T underlyingObjectToObject(Relationship relationship) {
return new IterableWrapper<T,PropertyContainer>(delegate) {
@Override
protected T underlyingObjectToObject(PropertyContainer element) {
- return persister.projectTo(element, getFirstOrDefault((Class<T>) DefaultRelationshipBacked.class, relationships));
+ return template.projectTo(element, getFirstOrDefault((Class<T>) DefaultRelationshipBacked.class, relationships));
}
};
}
- public ConvertingEntityPath(EntityPersister persister, Path delegate) {
- this.persister = persister;
+ public ConvertingEntityPath(Path delegate, Neo4jTemplate template) {
this.delegate = delegate;
+ this.template = template;
}
- private final EntityPersister persister;
private final Path delegate;
@Override
View
10 ...data-neo4j/src/main/java/org/springframework/data/neo4j/support/path/EntityEvaluator.java
@@ -21,21 +21,23 @@
import org.neo4j.graphdb.traversal.Evaluator;
import org.springframework.data.neo4j.core.EntityPath;
import org.springframework.data.neo4j.mapping.EntityPersister;
+import org.springframework.data.neo4j.support.Neo4jTemplate;
/**
* @author mh
* @since 26.02.11
*/
public abstract class EntityEvaluator<S, E> implements Evaluator {
- private EntityPersister persister;
- protected EntityEvaluator(EntityPersister persister) {
- this.persister = persister;
+ private final Neo4jTemplate template;
+
+ protected EntityEvaluator(Neo4jTemplate template) {
+ this.template = template;
}
@Override
public Evaluation evaluate(Path path) {
- return evaluate(new ConvertingEntityPath<S,E>(persister, path));
+ return evaluate(new ConvertingEntityPath<S,E>(path, template));
}
public abstract Evaluation evaluate(EntityPath<S,E> path);
View
2 ...ng-data-neo4j/src/main/java/org/springframework/data/neo4j/support/path/EntityMapper.java
@@ -36,7 +36,7 @@ protected EntityMapper(Neo4jTemplate template) {
@Override
public T mapPath(Path path) {
- return mapPath(new ConvertingEntityPath<S,E>(template, path));
+ return mapPath(new ConvertingEntityPath<S,E>(path, template));
}
public abstract static class WithoutResult<S,E> extends EntityMapper<S,E,Void> {
View
2 .../main/java/org/springframework/data/neo4j/support/path/EntityPathPathIterableWrapper.java
@@ -35,6 +35,6 @@ public EntityPathPathIterableWrapper(Iterable<Path> paths, Neo4jTemplate templat
}
protected EntityPath<S, E> underlyingObjectToObject(Path path) {
- return new ConvertingEntityPath<S,E>(template,path);
+ return new ConvertingEntityPath<S,E>(path, template);
}
}
View
2 ...a/org/springframework/data/neo4j/support/query/ConversionServiceQueryResultConverter.java
@@ -33,7 +33,7 @@ public ConversionServiceQueryResultConverter(ConversionService conversionService
@SuppressWarnings("unchecked")
@Override
- protected Object doConvert(Object value, Class<?> sourceType, Class type, MappingPolicy mappingPolicy) {
+ public Object doConvert(Object value, Class<?> sourceType, Class type, MappingPolicy mappingPolicy) {
if (conversionService.canConvert(sourceType, type)) {
return conversionService.convert(value, type);
}
View
13 ...a/org/springframework/data/neo4j/support/relationship/RelationshipEntityStateFactory.java
@@ -27,13 +27,11 @@
public class RelationshipEntityStateFactory implements EntityStateFactory<Relationship> {
- private Neo4jTemplate template;
-
private DelegatingFieldAccessorFactory relationshipDelegatingFieldAccessorFactory;
private Neo4jMappingContext mappingContext;
@SuppressWarnings("unchecked")
- public EntityState<Relationship> getEntityState(final Object entity, boolean detachable) {
+ public EntityState<Relationship> getEntityState(final Object entity, boolean detachable, Neo4jTemplate template) {
final Class<?> entityType = entity.getClass();
final Neo4jPersistentEntity persistentEntity = (Neo4jPersistentEntity) mappingContext.getPersistentEntity(entityType);
final RelationshipEntityState relationshipEntityState = new RelationshipEntityState(null, entity, entityType, template, relationshipDelegatingFieldAccessorFactory, persistentEntity);
@@ -44,10 +42,6 @@
}
- public void setTemplate(Neo4jTemplate template) {
- this.template = template;
- }
-
public void setRelationshipDelegatingFieldAccessorFactory(
DelegatingFieldAccessorFactory delegatingFieldAccessorFactory) {
this.relationshipDelegatingFieldAccessorFactory = delegatingFieldAccessorFactory;
@@ -56,9 +50,4 @@ public void setRelationshipDelegatingFieldAccessorFactory(
public void setMappingContext(Neo4jMappingContext mappingContext) {
this.mappingContext = mappingContext;
}
-
- @Override
- public Neo4jTemplate getTemplate() {
- return template;
- }
}
View
13 ...-neo4j/src/test/java/org/springframework/data/neo4j/mapping/Neo4jEntityPersisterTest.java
@@ -17,18 +17,11 @@
import org.junit.Test;
import org.mockito.Mockito;
-import org.neo4j.graphdb.Direction;
-import org.neo4j.graphdb.DynamicRelationshipType;
import org.neo4j.graphdb.Node;
-import org.neo4j.graphdb.PropertyContainer;
-import org.neo4j.graphdb.Relationship;
-import org.springframework.data.neo4j.model.BestFriend;
import org.springframework.data.neo4j.model.Friendship;
import org.springframework.data.neo4j.model.Person;
import org.springframework.transaction.annotation.Transactional;
-import java.util.Collections;
-
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -44,7 +37,7 @@
public void testCreateEntityFromStoredType() throws Exception {
final Node personNode = template.createNode();
personNode.setProperty("name","Michael");
- final Person person = entityPersister.createEntityFromState(personNode, Person.class, template.getMappingPolicy(Person.class));
+ final Person person = entityPersister.createEntityFromState(personNode, Person.class, template.getMappingPolicy(Person.class), template);
assertEquals("Michael",person.getName());
}
@@ -56,7 +49,7 @@ public void testCreateEntityFromState() throws Exception {
@Test
public void testProjectTo() throws Exception {
storeInGraph(michael);
- final Developer developer = entityPersister.projectTo(michael, Developer.class);
+ final Developer developer = entityPersister.projectTo(michael, Developer.class, template);
assertEquals(michael.getId(), developer.id);
assertEquals(michael.getName(), developer.name);
}
@@ -69,7 +62,7 @@ public void testGetPersistentState() throws Exception {
@Test
public void testPersist() throws Exception {
- entityPersister.persist(michael, template.getMappingPolicy(michael));
+ entityPersister.persist(michael, template.getMappingPolicy(michael), template);
assertEquals((Long) michaelNode().getId(), michael.getId());
assertEquals(michaelNode(), entityPersister.getPersistentState(michael));
assertEquals(michaelNode().getProperty("name"), michael.getName());
View
8 ...a-neo4j/src/test/java/org/springframework/data/neo4j/mapping/Neo4jPersistentTestBase.java
@@ -123,15 +123,13 @@ public void setUp() throws Exception {
private NodeEntityStateFactory createNodeEntityStateFactory(Neo4jMappingContext mappingContext) {
final NodeEntityStateFactory nodeEntityStateFactory = new NodeEntityStateFactory();
nodeEntityStateFactory.setMappingContext(mappingContext);
- nodeEntityStateFactory.setTemplate(template);
nodeEntityStateFactory.setNodeDelegatingFieldAccessorFactory(new NodeDelegatingFieldAccessorFactory(template));
return nodeEntityStateFactory;
}
private RelationshipEntityStateFactory createRelationshipEntityStateFactory(Neo4jMappingContext mappingContext) {
final RelationshipEntityStateFactory relationshipEntityStateFactory = new RelationshipEntityStateFactory();
relationshipEntityStateFactory.setMappingContext(mappingContext);
- relationshipEntityStateFactory.setTemplate(template);
relationshipEntityStateFactory.setRelationshipDelegatingFieldAccessorFactory(new RelationshipDelegatingFieldAccessorFactory(template));
return relationshipEntityStateFactory;
}
@@ -195,7 +193,7 @@ protected Person storeInGraph(Person p) {
}
protected Object write(Object entity, Node node) {
- entityPersister.write(entity, node, template.getMappingPolicy(entity));
+ entityPersister.write(entity, node, template.getMappingPolicy(entity), template);
return entity;
}
@@ -225,7 +223,7 @@ protected Node emilNode() {
}
public Person readPerson(Node node) {
- return entityPersister.read(Person.class, node, template.getMappingPolicy(Person.class));
+ return entityPersister.read(Person.class, node, template.getMappingPolicy(Person.class), template);
}
protected Relationship makeFriends(Node from, Node to, int years) {
@@ -235,7 +233,7 @@ protected Relationship makeFriends(Node from, Node to, int years) {
}
public Group readGroup(Node node) {
- return entityPersister.read(Group.class, node,template.getMappingPolicy(Group.class));
+ return entityPersister.read(Group.class, node,template.getMappingPolicy(Group.class), template);
}
protected List<Node> getRelatedNodes(Node startNode, String type, Direction direction) {

0 comments on commit 2cc97c6

Please sign in to comment.