cleaned up DefaultEntityStateAccessors to be reusable w/o GraphBacked
This commit is contained in:
@@ -3,7 +3,6 @@ package org.springframework.datastore.graph.neo4j.fieldaccess;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.datastore.graph.api.GraphBacked;
|
||||
import org.springframework.datastore.graph.neo4j.support.GraphDatabaseContext;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
@@ -19,7 +18,6 @@ public abstract class DefaultEntityStateAccessors<ENTITY extends GraphBacked<STA
|
||||
private final STATE underlyingState;
|
||||
protected final ENTITY entity;
|
||||
private final Class<? extends ENTITY> type;
|
||||
protected final GraphDatabaseContext graphDatabaseContext;
|
||||
private final Map<Field,FieldAccessor<ENTITY,?>> fieldAccessors=new HashMap<Field, FieldAccessor<ENTITY,?>>();
|
||||
private final Map<Field,List<FieldAccessListener<ENTITY,?>>> fieldAccessorListeners=new HashMap<Field, List<FieldAccessListener<ENTITY,?>>>();
|
||||
private STATE state;
|
||||
@@ -27,13 +25,12 @@ public abstract class DefaultEntityStateAccessors<ENTITY extends GraphBacked<STA
|
||||
private DelegatingFieldAccessorFactory delegatingFieldAccessorFactory;
|
||||
|
||||
|
||||
public DefaultEntityStateAccessors(final STATE underlyingState, final ENTITY entity, final Class<? extends ENTITY> type, final GraphDatabaseContext graphDatabaseContext, final DelegatingFieldAccessorFactory delegatingFieldAccessorFactory) {
|
||||
public DefaultEntityStateAccessors(final STATE underlyingState, final ENTITY entity, final Class<? extends ENTITY> type, final DelegatingFieldAccessorFactory delegatingFieldAccessorFactory) {
|
||||
this.underlyingState = underlyingState;
|
||||
this.entity = entity;
|
||||
this.type = type;
|
||||
this.graphDatabaseContext = graphDatabaseContext;
|
||||
this.delegatingFieldAccessorFactory = delegatingFieldAccessorFactory;
|
||||
createAccessorsAndListeners(type, graphDatabaseContext);
|
||||
createAccessorsAndListeners(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,21 +41,16 @@ public abstract class DefaultEntityStateAccessors<ENTITY extends GraphBacked<STA
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GraphDatabaseContext getGraphDatabaseContext() {
|
||||
return graphDatabaseContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUnderlyingState(final STATE state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
private void createAccessorsAndListeners(final Class<? extends ENTITY> type, final GraphDatabaseContext graphDatabaseContext) {
|
||||
private void createAccessorsAndListeners(final Class<? extends ENTITY> type) {
|
||||
ReflectionUtils.doWithFields(type, new ReflectionUtils.FieldCallback() {
|
||||
public void doWith(final Field field) throws IllegalArgumentException, IllegalAccessException {
|
||||
fieldAccessors.put(field, delegatingFieldAccessorFactory.forField(field));
|
||||
fieldAccessorListeners.put(field, delegatingFieldAccessorFactory.listenersFor(field)); // TODO Bad code
|
||||
fieldAccessorListeners.put(field, delegatingFieldAccessorFactory.listenersFor(field));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -21,9 +21,11 @@ public class DetachableEntityStateAccessors<ENTITY extends GraphBacked<STATE>, S
|
||||
private final Map<Field, Object> dirty = new HashMap<Field, Object>();
|
||||
private final EntityStateAccessors<ENTITY,STATE> delegate;
|
||||
private final static Log log = LogFactory.getLog(DetachableEntityStateAccessors.class);
|
||||
private GraphDatabaseContext graphDatabaseContext;
|
||||
|
||||
public DetachableEntityStateAccessors(final EntityStateAccessors<ENTITY,STATE> delegate) {
|
||||
public DetachableEntityStateAccessors(final EntityStateAccessors<ENTITY,STATE> delegate, GraphDatabaseContext graphDatabaseContext) {
|
||||
this.delegate = delegate;
|
||||
this.graphDatabaseContext = graphDatabaseContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -36,11 +38,6 @@ public class DetachableEntityStateAccessors<ENTITY extends GraphBacked<STATE>, S
|
||||
return delegate.getEntity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GraphDatabaseContext getGraphDatabaseContext() {
|
||||
return delegate.getGraphDatabaseContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue(final Field field) {
|
||||
if (!transactionIsRunning()) {
|
||||
@@ -54,7 +51,7 @@ public class DetachableEntityStateAccessors<ENTITY extends GraphBacked<STATE>, S
|
||||
}
|
||||
|
||||
private boolean transactionIsRunning() {
|
||||
return delegate.getGraphDatabaseContext().transactionIsRunning();
|
||||
return getGraphDatabaseContext().transactionIsRunning();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -151,6 +148,9 @@ public class DetachableEntityStateAccessors<ENTITY extends GraphBacked<STATE>, S
|
||||
this.dirty.put(f, previousValue);
|
||||
}
|
||||
|
||||
public GraphDatabaseContext getGraphDatabaseContext() {
|
||||
return graphDatabaseContext;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -14,8 +14,6 @@ import java.lang.reflect.Field;
|
||||
public interface EntityStateAccessors<ENTITY extends GraphBacked<STATE>,STATE> {
|
||||
ENTITY getEntity();
|
||||
|
||||
GraphDatabaseContext getGraphDatabaseContext();
|
||||
|
||||
void setUnderlyingState(STATE state);
|
||||
|
||||
Object getValue(Field field);
|
||||
|
||||
@@ -15,8 +15,10 @@ import java.util.Collection;
|
||||
*/
|
||||
public class NodeEntityStateAccessors<ENTITY extends NodeBacked> extends DefaultEntityStateAccessors<ENTITY, Node> {
|
||||
|
||||
private final GraphDatabaseContext graphDatabaseContext;
|
||||
|
||||
public NodeEntityStateAccessors(final Node underlyingState, final ENTITY entity, final Class<? extends ENTITY> type, final GraphDatabaseContext graphDatabaseContext) {
|
||||
super(underlyingState, entity, type, graphDatabaseContext, new DelegatingFieldAccessorFactory(graphDatabaseContext) {
|
||||
super(underlyingState, entity, type, new DelegatingFieldAccessorFactory(graphDatabaseContext) {
|
||||
@Override
|
||||
protected Collection<FieldAccessorListenerFactory<?>> createListenerFactories() {
|
||||
return Arrays.<FieldAccessorListenerFactory<?>>asList(
|
||||
@@ -39,6 +41,7 @@ public class NodeEntityStateAccessors<ENTITY extends NodeBacked> extends Default
|
||||
);
|
||||
}
|
||||
});
|
||||
this.graphDatabaseContext = graphDatabaseContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -11,6 +11,6 @@ public class NodeEntityStateAccessorsFactory {
|
||||
|
||||
public EntityStateAccessors<NodeBacked,Node> getEntityStateAccessors(final NodeBacked entity) {
|
||||
return new DetachableEntityStateAccessors<NodeBacked, Node>(
|
||||
new NodeEntityStateAccessors<NodeBacked>(null,entity,entity.getClass(), graphDatabaseContext));
|
||||
new NodeEntityStateAccessors<NodeBacked>(null,entity,entity.getClass(), graphDatabaseContext),graphDatabaseContext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import java.util.Collections;
|
||||
public class RelationshipEntityStateAccessors<ENTITY extends RelationshipBacked> extends DefaultEntityStateAccessors<ENTITY, Relationship> {
|
||||
|
||||
public RelationshipEntityStateAccessors(final Relationship underlyingState, final ENTITY entity, final Class<? extends ENTITY> type, final GraphDatabaseContext graphDatabaseContext) {
|
||||
super(underlyingState, entity, type, graphDatabaseContext, new DelegatingFieldAccessorFactory(graphDatabaseContext) {
|
||||
super(underlyingState, entity, type, new DelegatingFieldAccessorFactory(graphDatabaseContext) {
|
||||
@Override
|
||||
protected Collection<FieldAccessorListenerFactory<?>> createListenerFactories() {
|
||||
return Collections.emptyList();
|
||||
|
||||
Reference in New Issue
Block a user