DATACOUCH-134 - allow changing the name of field storing type.

This commit is contained in:
Simon Baslé
2015-07-07 11:38:24 +02:00
parent c29727595c
commit 2bf2d3e544
8 changed files with 126 additions and 6 deletions

View File

@@ -137,6 +137,18 @@ public abstract class AbstractCouchbaseConfiguration {
return new CouchbaseTemplate(couchbaseClient(), mappingCouchbaseConverter(), translationService());
}
/**
* Determines the name of the field that will store the type information for complex types when
* using the {@link #mappingCouchbaseConverter()}.
* Defaults to {@value MappingCouchbaseConverter#TYPEKEY_DEFAULT}.
*
* @see MappingCouchbaseConverter#TYPEKEY_DEFAULT
* @see MappingCouchbaseConverter#TYPEKEY_SYNCGATEWAY_COMPATIBLE
*/
public String typeKey() {
return MappingCouchbaseConverter.TYPEKEY_DEFAULT;
}
/**
* Creates a {@link MappingCouchbaseConverter} using the configured {@link #couchbaseMappingContext}.
*
@@ -144,7 +156,7 @@ public abstract class AbstractCouchbaseConfiguration {
*/
@Bean
public MappingCouchbaseConverter mappingCouchbaseConverter() throws Exception {
MappingCouchbaseConverter converter = new MappingCouchbaseConverter(couchbaseMappingContext());
MappingCouchbaseConverter converter = new MappingCouchbaseConverter(couchbaseMappingContext(), typeKey());
converter.setCustomConversions(customConversions());
return converter;
}

View File

@@ -26,4 +26,6 @@ import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
*/
public interface CouchbaseTypeMapper extends TypeMapper<CouchbaseDocument> {
String getTypeKey();
}

View File

@@ -32,6 +32,8 @@ public class DefaultCouchbaseTypeMapper extends DefaultTypeMapper<CouchbaseDocum
*/
public static final String DEFAULT_TYPE_KEY = "_class";
private final String typeKey;
/**
* Create a new type mapper with the type key.
*
@@ -39,6 +41,12 @@ public class DefaultCouchbaseTypeMapper extends DefaultTypeMapper<CouchbaseDocum
*/
public DefaultCouchbaseTypeMapper(final String typeKey) {
super(new CouchbaseDocumentTypeAliasAccessor(typeKey));
this.typeKey = typeKey;
}
@Override
public String getTypeKey() {
return this.typeKey;
}
public static final class CouchbaseDocumentTypeAliasAccessor implements TypeAliasAccessor<CouchbaseDocument> {

View File

@@ -64,6 +64,19 @@ import org.springframework.util.CollectionUtils;
public class MappingCouchbaseConverter extends AbstractCouchbaseConverter
implements ApplicationContextAware {
/**
* The default "type key", the name of the field that will hold type information.
*
* @see #TYPEKEY_SYNCGATEWAY_COMPATIBLE
*/
public static final String TYPEKEY_DEFAULT = DefaultCouchbaseTypeMapper.DEFAULT_TYPE_KEY;
/**
* A "type key" (the name of the field that will hold type information) that is
* compatible with Sync Gateway (which doesn't allows underscores).
*/
public static final String TYPEKEY_SYNCGATEWAY_COMPATIBLE = "javaClass";
/**
* The overall application context.
*/
@@ -90,13 +103,24 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter
*
* @param mappingContext the mapping context to use.
*/
@SuppressWarnings("deprecation")
public MappingCouchbaseConverter(final MappingContext<? extends CouchbasePersistentEntity<?>,
CouchbasePersistentProperty> mappingContext) {
CouchbasePersistentProperty> mappingContext) {
this(mappingContext, TYPEKEY_DEFAULT);
}
/**
* Create a new {@link MappingCouchbaseConverter} that will store class name for
* complex types in the <i>typeKey</i> attribute.
*
* @param mappingContext the mapping context to use.
* @param typeKey the attribute name to use to store complex types class name.
*/
public MappingCouchbaseConverter(final MappingContext<? extends CouchbasePersistentEntity<?>,
CouchbasePersistentProperty> mappingContext, final String typeKey) {
super(new DefaultConversionService());
this.mappingContext = mappingContext;
typeMapper = new DefaultCouchbaseTypeMapper(DefaultCouchbaseTypeMapper.DEFAULT_TYPE_KEY);
typeMapper = new DefaultCouchbaseTypeMapper(typeKey != null ? typeKey : TYPEKEY_DEFAULT);
spELContext = new SpELContext(CouchbaseDocumentPropertyAccessor.INSTANCE);
}
@@ -105,6 +129,13 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter
return mappingContext;
}
/**
* @return the name of the field that will hold type information.
*/
public String getTypeKey() {
return typeMapper.getTypeKey();
}
@Override
public <R> R read(final Class<R> clazz, final CouchbaseDocument source) {
return read(ClassTypeInformation.from(clazz), source, null);