35
35
import org .springframework .core .convert .ConversionService ;
36
36
import org .springframework .core .convert .support .DefaultConversionService ;
37
37
import org .springframework .dao .InvalidDataAccessApiUsageException ;
38
- import org .springframework .data .cassandra .core .mapping .*;
38
+ import org .springframework .data .cassandra .core .mapping .BasicCassandraPersistentEntity ;
39
+ import org .springframework .data .cassandra .core .mapping .BasicMapId ;
40
+ import org .springframework .data .cassandra .core .mapping .CassandraMappingContext ;
41
+ import org .springframework .data .cassandra .core .mapping .CassandraPersistentEntity ;
42
+ import org .springframework .data .cassandra .core .mapping .CassandraPersistentProperty ;
43
+ import org .springframework .data .cassandra .core .mapping .Column ;
44
+ import org .springframework .data .cassandra .core .mapping .Element ;
45
+ import org .springframework .data .cassandra .core .mapping .Embedded ;
39
46
import org .springframework .data .cassandra .core .mapping .Embedded .OnEmpty ;
47
+ import org .springframework .data .cassandra .core .mapping .EmbeddedEntityOperations ;
48
+ import org .springframework .data .cassandra .core .mapping .MapId ;
49
+ import org .springframework .data .cassandra .core .mapping .MapIdentifiable ;
50
+ import org .springframework .data .cassandra .core .mapping .UserTypeResolver ;
40
51
import org .springframework .data .mapping .MappingException ;
41
52
import org .springframework .data .mapping .PersistentPropertyAccessor ;
42
53
import org .springframework .data .mapping .PreferredConstructor ;
@@ -100,19 +111,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
100
111
* Create a new {@link MappingCassandraConverter} with a {@link CassandraMappingContext}.
101
112
*/
102
113
public MappingCassandraConverter () {
103
-
104
- super (newConversionService ());
105
-
106
- CassandraCustomConversions conversions = new CassandraCustomConversions (Collections .emptyList ());
107
-
108
- this .mappingContext = newDefaultMappingContext (conversions );
109
- this .codecRegistry = mappingContext .getCodecRegistry ();
110
- this .spELContext = new SpELContext (RowReaderPropertyAccessor .INSTANCE );
111
- this .cassandraTypeResolver = new DefaultColumnTypeResolver (mappingContext ,
112
- userTypeName -> getUserTypeResolver ().resolveType (userTypeName ), this ::getCodecRegistry ,
113
- this ::getCustomConversions );
114
- this .setCustomConversions (conversions );
115
- this .embeddedEntityOperations = new EmbeddedEntityOperations (mappingContext );
114
+ this (newDefaultMappingContext (new CassandraCustomConversions (Collections .emptyList ())));
116
115
}
117
116
118
117
/**
@@ -126,31 +125,42 @@ public MappingCassandraConverter(CassandraMappingContext mappingContext) {
126
125
127
126
Assert .notNull (mappingContext , "CassandraMappingContext must not be null" );
128
127
128
+ UserTypeResolver userTypeResolver = userTypeName -> getUserTypeResolver ().resolveType (userTypeName );
129
+
129
130
this .mappingContext = mappingContext ;
130
- this .codecRegistry = mappingContext .getCodecRegistry ();
131
- this .spELContext = new SpELContext (RowReaderPropertyAccessor .INSTANCE );
132
- this .cassandraTypeResolver = new DefaultColumnTypeResolver (mappingContext ,
133
- userTypeName -> getUserTypeResolver ().resolveType (userTypeName ), this ::getCodecRegistry ,
134
- this ::getCustomConversions );
131
+ this .setCodecRegistry (mappingContext .getCodecRegistry ());
135
132
this .setCustomConversions (mappingContext .getCustomConversions ());
133
+ this .cassandraTypeResolver = new DefaultColumnTypeResolver (mappingContext , userTypeResolver ,
134
+ this ::getCodecRegistry , this ::getCustomConversions );
136
135
this .embeddedEntityOperations = new EmbeddedEntityOperations (mappingContext );
136
+ this .spELContext = new SpELContext (RowReaderPropertyAccessor .INSTANCE );
137
137
}
138
138
139
139
/**
140
- * Creates a new {@link ConversionContext}.
140
+ * Constructs a new instance of {@link ConversionContext} with various converters to convert different Cassandra
141
+ * value types.
141
142
*
142
143
* @return the {@link ConversionContext}.
144
+ * @see ConversionContext
143
145
*/
144
146
protected ConversionContext getConversionContext () {
145
147
146
- return new ConversionContext (getCustomConversions (), this ::doReadRow , this ::doReadTupleValue , this ::doReadUdtValue ,
148
+ return new ConversionContext (this ::doReadRow , this ::doReadTupleValue , this ::doReadUdtValue ,
147
149
this ::readCollectionOrArray , this ::readMap , this ::getPotentiallyConvertedSimpleRead );
148
150
}
149
151
150
152
private static ConversionService newConversionService () {
151
153
return new DefaultConversionService ();
152
154
}
153
155
156
+ /**
157
+ * Constructs a new instance of a {@link MappingContext} for Cassandra.
158
+ *
159
+ * @param conversions {@link CassandraCustomConversions} object encapsulating complex type conversion logic.
160
+ * @return a new {@link CassandraMappingContext}.
161
+ * @see org.springframework.data.cassandra.core.mapping.CassandraMappingContext
162
+ * @see org.springframework.data.mapping.context.MappingContext
163
+ */
154
164
private static CassandraMappingContext newDefaultMappingContext (CassandraCustomConversions conversions ) {
155
165
156
166
CassandraMappingContext mappingContext = new CassandraMappingContext ();
@@ -161,6 +171,7 @@ private static CassandraMappingContext newDefaultMappingContext(CassandraCustomC
161
171
return mappingContext ;
162
172
}
163
173
174
+
164
175
/* (non-Javadoc)
165
176
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
166
177
*/
@@ -202,12 +213,7 @@ public void setCodecRegistry(CodecRegistry codecRegistry) {
202
213
*/
203
214
@ Override
204
215
public CodecRegistry getCodecRegistry () {
205
-
206
- if (this .codecRegistry == null ) {
207
- return mappingContext .getCodecRegistry ();
208
- }
209
-
210
- return this .codecRegistry ;
216
+ return this .codecRegistry != null ? this .codecRegistry : getMappingContext ().getCodecRegistry ();
211
217
}
212
218
213
219
/**
@@ -229,12 +235,7 @@ public void setUserTypeResolver(UserTypeResolver userTypeResolver) {
229
235
* @since 3.0
230
236
*/
231
237
public UserTypeResolver getUserTypeResolver () {
232
-
233
- if (this .userTypeResolver == null ) {
234
- return this .mappingContext .getUserTypeResolver ();
235
- }
236
-
237
- return userTypeResolver ;
238
+ return this .userTypeResolver != null ? this .userTypeResolver : getMappingContext ().getUserTypeResolver ();
238
239
}
239
240
240
241
/* (non-Javadoc)
@@ -367,8 +368,8 @@ protected <S> S doReadEntity(ConversionContext context, CassandraValueProvider v
367
368
return (S ) getConversionService ().convert (valueProvider .getSource (), rawType );
368
369
}
369
370
370
- CassandraPersistentEntity <S > entity = ( CassandraPersistentEntity < S >) getMappingContext ()
371
- .getPersistentEntity (typeHint );
371
+ CassandraPersistentEntity <S > entity =
372
+ ( CassandraPersistentEntity < S >) getMappingContext () .getPersistentEntity (typeHint );
372
373
373
374
if (entity == null ) {
374
375
throw new MappingException (
@@ -393,8 +394,8 @@ private static Class<?> getRawSourceType(CassandraValueProvider valueProvider) {
393
394
return UdtValue .class ;
394
395
}
395
396
396
- throw new InvalidDataAccessApiUsageException (
397
- "Unsupported source type: " + ClassUtils .getDescriptiveType (valueProvider .getSource ()));
397
+ throw new InvalidDataAccessApiUsageException (String . format ( "Unsupported source type: %s" ,
398
+ ClassUtils .getDescriptiveType (valueProvider .getSource () )));
398
399
}
399
400
400
401
private <S > S doReadEntity (ConversionContext context , CassandraValueProvider valueProvider ,
@@ -405,8 +406,8 @@ private <S> S doReadEntity(ConversionContext context, CassandraValueProvider val
405
406
406
407
if (persistenceConstructor != null && persistenceConstructor .hasParameters ()) {
407
408
SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator (valueProvider .getSource (), spELContext );
408
- ParameterValueProvider <CassandraPersistentProperty > parameterValueProvider = newParameterValueProvider ( context ,
409
- entity , valueProvider );
409
+ ParameterValueProvider <CassandraPersistentProperty > parameterValueProvider =
410
+ newParameterValueProvider ( context , entity , valueProvider );
410
411
provider = new ConverterAwareSpELExpressionParameterValueProvider (evaluator , getConversionService (),
411
412
parameterValueProvider , context );
412
413
} else {
@@ -746,10 +747,7 @@ public Object getId(Object object, CassandraPersistentEntity<?> entity) {
746
747
747
748
/**
748
749
* Check custom conversions for type override or fall back to
749
- * {@link #determineTargetType(CassandraPersistentProperty)}
750
- *
751
- * @param property
752
- * @return
750
+ * {@link ColumnTypeResolver#resolve(CassandraPersistentProperty)}.
753
751
*/
754
752
private Class <?> getTargetType (CassandraPersistentProperty property ) {
755
753
return getCustomConversions ().getCustomWriteTarget (property .getType ())
@@ -794,17 +792,20 @@ private Object getWriteValue(@Nullable Object value, ColumnType columnType) {
794
792
795
793
if (getCustomConversions ().hasCustomWriteTarget (value .getClass (), requestedTargetType )) {
796
794
797
- Class <?> resolvedTargetType = getCustomConversions ().getCustomWriteTarget (value .getClass (), requestedTargetType )
795
+ Class <?> resolvedTargetType = getCustomConversions ()
796
+ .getCustomWriteTarget (value .getClass (), requestedTargetType )
798
797
.orElse (requestedTargetType );
799
798
800
799
return getConversionService ().convert (value , resolvedTargetType );
801
800
}
802
801
803
802
if (getCustomConversions ().hasCustomWriteTarget (value .getClass ())) {
804
803
805
- Class <?> resolvedTargetType = getCustomConversions ().getCustomWriteTarget (value .getClass ())
806
- .orElseThrow (() -> new IllegalStateException (String
807
- .format ("Unable to determined custom write target for value type [%s]" , value .getClass ().getName ())));
804
+ Class <?> resolvedTargetType = getCustomConversions ()
805
+ .getCustomWriteTarget (value .getClass ())
806
+ .orElseThrow (() -> new IllegalStateException (
807
+ String .format ("Unable to determined custom write target for value type [%s]" ,
808
+ value .getClass ().getName ())));
808
809
809
810
return getConversionService ().convert (value , resolvedTargetType );
810
811
}
@@ -988,6 +989,7 @@ private Object getReadValue(ConversionContext context, CassandraValueProvider va
988
989
}
989
990
990
991
Object value = valueProvider .getPropertyValue (property );
992
+
991
993
return value == null ? null : context .convert (value , property .getTypeInformation ());
992
994
}
993
995
0 commit comments