From 5013fc76a1ba5eeb00b3f2fa2e64a0acd6aa78cf Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Mon, 30 Jun 2014 12:13:40 +0200 Subject: [PATCH] DATAGRAPH-487 - Handle unique fields that have to be converted correctly --- .../Neo4jGraphPersistenceTests-context.xml | 1 + .../Neo4jGraphRecommendationTests-context.xml | 2 ++ ...enericNodePropertyFieldAccessorFactory.java | 9 +++------ .../neo4j/fieldaccess/PropertyConverter.java | 18 +++++++++++------- .../neo4j/mapping/Neo4jPersistentProperty.java | 2 ++ .../query/IndexBasedStartClause.java | 2 +- .../neo4j/repository/query/WhereClause.java | 4 ++-- .../mapping/Neo4JPersistentPropertyImpl.java | 18 ++++++++++++++---- 8 files changed, 36 insertions(+), 20 deletions(-) diff --git a/spring-data-neo4j-aspects/src/test/resources/org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml b/spring-data-neo4j-aspects/src/test/resources/org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml index 9d3e58113..df28c8ac4 100644 --- a/spring-data-neo4j-aspects/src/test/resources/org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml +++ b/spring-data-neo4j-aspects/src/test/resources/org/springframework/data/neo4j/aspects/support/Neo4jGraphPersistenceTests-context.xml @@ -82,6 +82,7 @@ + diff --git a/spring-data-neo4j-cross-store/src/test/resources/org/springframework/data/neo4j/partial/Neo4jGraphRecommendationTests-context.xml b/spring-data-neo4j-cross-store/src/test/resources/org/springframework/data/neo4j/partial/Neo4jGraphRecommendationTests-context.xml index 65a9e7d6c..1a9de956d 100644 --- a/spring-data-neo4j-cross-store/src/test/resources/org/springframework/data/neo4j/partial/Neo4jGraphRecommendationTests-context.xml +++ b/spring-data-neo4j-cross-store/src/test/resources/org/springframework/data/neo4j/partial/Neo4jGraphRecommendationTests-context.xml @@ -54,7 +54,9 @@ + + diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/GenericNodePropertyFieldAccessorFactory.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/GenericNodePropertyFieldAccessorFactory.java index 8a2f6c2c6..aea76b049 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/GenericNodePropertyFieldAccessorFactory.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/GenericNodePropertyFieldAccessorFactory.java @@ -60,18 +60,15 @@ public class GenericNodePropertyFieldAccessorFactory implements FieldAccessorFac @Override public Object setValue(final Object entity, final Object newVal, MappingPolicy mappingPolicy) { - Object value = propertyConverter.isObjectOrSupportedType(newVal) ? newVal : propertyConverter.serializePropertyValue(newVal); + Object value = propertyConverter.serializeIfNotBuiltIn(newVal); super.setValue(entity, value, mappingPolicy); return newVal; } @Override public Object doGetValue(final Object entity) { - Object ret = super.doGetValue(entity); - if (propertyConverter.isObjectOrSupportedType(ret)) { - return ret; - } - return propertyConverter.deserializePropertyValue(ret); + Object value = super.doGetValue(entity); + return propertyConverter.deserializeIfNotBuiltIn(value); } @Override diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/PropertyConverter.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/PropertyConverter.java index 578b6143d..e6e19c349 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/PropertyConverter.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/PropertyConverter.java @@ -32,22 +32,25 @@ public class PropertyConverter { private final ConversionService conversionService; private final Neo4jPersistentProperty property; private final TypeInformation typeInformation; - private final Class targetType; + private final Class propertyType; + private final boolean userDefinedPropertyType; public PropertyConverter(ConversionService conversionService, Neo4jPersistentProperty property) { this.conversionService = conversionService; this.property = property; this.typeInformation = property.getTypeInformation(); - targetType = property.getPropertyType(); + Class configuredPropertyType = property.getPropertyType(); + userDefinedPropertyType = configuredPropertyType != null; + propertyType = userDefinedPropertyType ? configuredPropertyType : Neo4jPersistentProperty.DEFAULT_NEO4J_PROPERTY_TYPE; } public Object serializePropertyValue(final Object newVal) { if (newVal == null) return null; final TypeInformation typeInformation = property.getTypeInformation(); if (typeInformation.isCollectionLike()) { - return serializeCollection(newVal, conversionService, typeInformation, targetType); + return serializeCollection(newVal, conversionService, typeInformation, propertyType); } - return conversionService.convert(newVal, targetType); + return conversionService.convert(newVal, propertyType); } public Object deserializePropertyValue(final Object newVal) { @@ -95,15 +98,16 @@ public class PropertyConverter { } } - boolean isObjectOrSupportedType(final Object value) { + private boolean isObjectOrSupportedType(final Object value) { return property.getType().equals(Object.class) && property.isNeo4jPropertyValue(value); } public Object deserializeIfNotBuiltIn(Object ret) { - if (isObjectOrSupportedType(ret)) { + if (!userDefinedPropertyType && isObjectOrSupportedType(ret)) { return ret; + } else { + return deserializePropertyValue(ret); } - return deserializePropertyValue(ret); } public Object serializeIfNotBuiltIn(Object newVal) { diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java index b451ebc42..7d96d8ab5 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/mapping/Neo4jPersistentProperty.java @@ -27,6 +27,8 @@ import org.springframework.data.mapping.PersistentProperty; */ public interface Neo4jPersistentProperty extends PersistentProperty { + Class DEFAULT_NEO4J_PROPERTY_TYPE = String.class; + /** * Returns whether the property represents a relationship. If this returns {@literal true}, clients can expect * {@link #getRelationshipInfo()} to return a non-{@literal null} value. diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/IndexBasedStartClause.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/IndexBasedStartClause.java index c4f18607e..42d48bedb 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/IndexBasedStartClause.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/IndexBasedStartClause.java @@ -54,7 +54,7 @@ abstract class IndexBasedStartClause extends StartClause { if (property.isNeo4jPropertyType() && property.isNeo4jPropertyValue(value)) return value; PropertyConverter converter = new PropertyConverter(template.getConversionService(), property); - return converter.serializePropertyValue(value); + return converter.serializeIfNotBuiltIn(value); } protected Map findMyParameters(Set parameters) { diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/WhereClause.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/WhereClause.java index 8db3dee5d..2f92e4586 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/WhereClause.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/WhereClause.java @@ -117,8 +117,8 @@ class WhereClause { protected Object convertValue(PartInfo partInfo, Object value) { if (EnumSet.of(Type.CONTAINING, Type.STARTING_WITH, Type.ENDING_WITH).contains(type)) return QueryTemplates.formatExpression(this.partInfo, value); - else if (propertyConverter!=null) { - return propertyConverter.serializePropertyValue(value); + else if (propertyConverter!=null ) { + return propertyConverter.serializeIfNotBuiltIn(value); } return value; } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java index dad0e016f..afe66d3a5 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/support/mapping/Neo4JPersistentPropertyImpl.java @@ -78,8 +78,8 @@ class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty extractPropertyType() { final GraphProperty graphProperty = getAnnotation(GraphProperty.class); - if (graphProperty==null) return String.class; + if (graphProperty==null) return null; return graphProperty.propertyType(); } @@ -196,9 +196,14 @@ class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty type = getType(); if (getTypeInformation().isCollectionLike()) { return isConvertible(conversionService, getComponentType()); @@ -207,7 +212,12 @@ class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty type) { - return conversionService.canConvert(type, propertyType) && conversionService.canConvert(propertyType, type); + Class targetType = getPropertyTypeOrDefault(); + return conversionService.canConvert(type, targetType) && conversionService.canConvert(targetType, type); + } + + private Class getPropertyTypeOrDefault() { + return propertyType == null ? DEFAULT_NEO4J_PROPERTY_TYPE : propertyType; } @Override @@ -238,7 +248,7 @@ class Neo4jPersistentPropertyImpl extends AnnotationBasedPersistentProperty