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,32 +125,35 @@ 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} given {@link ObjectPath }.
140
+ * Construct a new instance of the {@literal default} Spring {@link ConversionService }.
141
141
*
142
- * @param path the current {@link ObjectPath}, must not be {@literal null }.
143
- * @return the {@link ConversionContext}.
142
+ * @return a new instance of the {@literal default} Spring {@link ConversionService }.
143
+ * @see org.springframework.core.convert.ConversionService
144
144
*/
145
- protected ConversionContext getConversionContext () {
146
-
147
- return new ConversionContext (this ::doReadRow , this ::doReadTupleValue , this ::doReadUdtValue ,
148
- this ::readCollectionOrArray , this ::readMap , this ::getPotentiallyConvertedSimpleRead );
149
- }
150
-
151
145
private static ConversionService newConversionService () {
152
146
return new DefaultConversionService ();
153
147
}
154
148
149
+ /**
150
+ * Constructs a new instance of a {@link MappingContext} for Cassandra.
151
+ *
152
+ * @param conversions {@link CassandraCustomConversions} object encapsulating complex type conversion logic.
153
+ * @return a new {@link CassandraMappingContext}.
154
+ * @see org.springframework.data.cassandra.core.mapping.CassandraMappingContext
155
+ * @see org.springframework.data.mapping.context.MappingContext
156
+ */
155
157
private static CassandraMappingContext newDefaultMappingContext (CassandraCustomConversions conversions ) {
156
158
157
159
CassandraMappingContext mappingContext = new CassandraMappingContext ();
@@ -162,6 +164,19 @@ private static CassandraMappingContext newDefaultMappingContext(CassandraCustomC
162
164
return mappingContext ;
163
165
}
164
166
167
+ /**
168
+ * Constructs a new instance of {@link ConversionContext} with various converters to convert different Cassandra
169
+ * value types.
170
+ *
171
+ * @return the {@link ConversionContext}.
172
+ * @see ConversionContext
173
+ */
174
+ protected ConversionContext getConversionContext () {
175
+
176
+ return new ConversionContext (this ::doReadRow , this ::doReadTupleValue , this ::doReadUdtValue ,
177
+ this ::readCollectionOrArray , this ::readMap , this ::getPotentiallyConvertedSimpleRead );
178
+ }
179
+
165
180
/* (non-Javadoc)
166
181
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
167
182
*/
@@ -203,12 +218,7 @@ public void setCodecRegistry(CodecRegistry codecRegistry) {
203
218
*/
204
219
@ Override
205
220
public CodecRegistry getCodecRegistry () {
206
-
207
- if (this .codecRegistry == null ) {
208
- return mappingContext .getCodecRegistry ();
209
- }
210
-
211
- return this .codecRegistry ;
221
+ return this .codecRegistry != null ? this .codecRegistry : getMappingContext ().getCodecRegistry ();
212
222
}
213
223
214
224
/**
@@ -230,12 +240,7 @@ public void setUserTypeResolver(UserTypeResolver userTypeResolver) {
230
240
* @since 3.0
231
241
*/
232
242
public UserTypeResolver getUserTypeResolver () {
233
-
234
- if (this .userTypeResolver == null ) {
235
- return this .mappingContext .getUserTypeResolver ();
236
- }
237
-
238
- return userTypeResolver ;
243
+ return this .userTypeResolver != null ? this .userTypeResolver : getMappingContext ().getUserTypeResolver ();
239
244
}
240
245
241
246
/* (non-Javadoc)
@@ -368,8 +373,8 @@ protected <S> S doReadEntity(ConversionContext context, CassandraValueProvider v
368
373
return (S ) getConversionService ().convert (valueProvider .getSource (), rawType );
369
374
}
370
375
371
- CassandraPersistentEntity <S > entity = ( CassandraPersistentEntity < S >) getMappingContext ()
372
- .getPersistentEntity (typeHint );
376
+ CassandraPersistentEntity <S > entity =
377
+ ( CassandraPersistentEntity < S >) getMappingContext () .getPersistentEntity (typeHint );
373
378
374
379
if (entity == null ) {
375
380
throw new MappingException (
@@ -394,8 +399,8 @@ private static Class<?> getRawSourceType(CassandraValueProvider valueProvider) {
394
399
return UdtValue .class ;
395
400
}
396
401
397
- throw new InvalidDataAccessApiUsageException (
398
- "Unsupported source type: " + ClassUtils .getDescriptiveType (valueProvider .getSource ()));
402
+ throw new InvalidDataAccessApiUsageException (String . format ( "Unsupported source type: %s" ,
403
+ ClassUtils .getDescriptiveType (valueProvider .getSource () )));
399
404
}
400
405
401
406
private <S > S doReadEntity (ConversionContext context , CassandraValueProvider valueProvider ,
@@ -406,8 +411,8 @@ private <S> S doReadEntity(ConversionContext context, CassandraValueProvider val
406
411
407
412
if (persistenceConstructor != null && persistenceConstructor .hasParameters ()) {
408
413
SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator (valueProvider .getSource (), spELContext );
409
- ParameterValueProvider <CassandraPersistentProperty > parameterValueProvider = newParameterValueProvider ( context ,
410
- entity , valueProvider );
414
+ ParameterValueProvider <CassandraPersistentProperty > parameterValueProvider =
415
+ newParameterValueProvider ( context , entity , valueProvider );
411
416
provider = new ConverterAwareSpELExpressionParameterValueProvider (evaluator , getConversionService (),
412
417
parameterValueProvider , context );
413
418
} else {
@@ -745,10 +750,7 @@ public Object getId(Object object, CassandraPersistentEntity<?> entity) {
745
750
746
751
/**
747
752
* Check custom conversions for type override or fall back to
748
- * {@link #determineTargetType(CassandraPersistentProperty)}
749
- *
750
- * @param property
751
- * @return
753
+ * {@link ColumnTypeResolver#resolve(CassandraPersistentProperty)}.
752
754
*/
753
755
private Class <?> getTargetType (CassandraPersistentProperty property ) {
754
756
return getCustomConversions ().getCustomWriteTarget (property .getType ())
@@ -793,17 +795,20 @@ private Object getWriteValue(@Nullable Object value, ColumnType columnType) {
793
795
794
796
if (getCustomConversions ().hasCustomWriteTarget (value .getClass (), requestedTargetType )) {
795
797
796
- Class <?> resolvedTargetType = getCustomConversions ().getCustomWriteTarget (value .getClass (), requestedTargetType )
798
+ Class <?> resolvedTargetType = getCustomConversions ()
799
+ .getCustomWriteTarget (value .getClass (), requestedTargetType )
797
800
.orElse (requestedTargetType );
798
801
799
802
return getConversionService ().convert (value , resolvedTargetType );
800
803
}
801
804
802
805
if (getCustomConversions ().hasCustomWriteTarget (value .getClass ())) {
803
806
804
- Class <?> resolvedTargetType = getCustomConversions ().getCustomWriteTarget (value .getClass ())
805
- .orElseThrow (() -> new IllegalStateException (String
806
- .format ("Unable to determined custom write target for value type [%s]" , value .getClass ().getName ())));
807
+ Class <?> resolvedTargetType = getCustomConversions ()
808
+ .getCustomWriteTarget (value .getClass ())
809
+ .orElseThrow (() -> new IllegalStateException (
810
+ String .format ("Unable to determined custom write target for value type [%s]" ,
811
+ value .getClass ().getName ())));
807
812
808
813
return getConversionService ().convert (value , resolvedTargetType );
809
814
}
@@ -987,6 +992,7 @@ private Object getReadValue(ConversionContext context, CassandraValueProvider va
987
992
}
988
993
989
994
Object value = valueProvider .getPropertyValue (property );
995
+
990
996
return value == null ? null : context .convert (value , property .getTypeInformation ());
991
997
}
992
998
0 commit comments