37
37
import org .springframework .core .convert .ConversionService ;
38
38
import org .springframework .core .convert .support .DefaultConversionService ;
39
39
import org .springframework .dao .InvalidDataAccessApiUsageException ;
40
- import org .springframework .data .cassandra .core .mapping .*;
40
+ import org .springframework .data .cassandra .core .mapping .BasicCassandraPersistentEntity ;
41
+ import org .springframework .data .cassandra .core .mapping .BasicMapId ;
42
+ import org .springframework .data .cassandra .core .mapping .CassandraMappingContext ;
43
+ import org .springframework .data .cassandra .core .mapping .CassandraPersistentEntity ;
44
+ import org .springframework .data .cassandra .core .mapping .CassandraPersistentProperty ;
45
+ import org .springframework .data .cassandra .core .mapping .Column ;
46
+ import org .springframework .data .cassandra .core .mapping .Element ;
47
+ import org .springframework .data .cassandra .core .mapping .Embedded ;
41
48
import org .springframework .data .cassandra .core .mapping .Embedded .OnEmpty ;
49
+ import org .springframework .data .cassandra .core .mapping .EmbeddedEntityOperations ;
50
+ import org .springframework .data .cassandra .core .mapping .MapId ;
51
+ import org .springframework .data .cassandra .core .mapping .MapIdentifiable ;
52
+ import org .springframework .data .cassandra .core .mapping .UserTypeResolver ;
42
53
import org .springframework .data .convert .CustomConversions ;
43
54
import org .springframework .data .mapping .AccessOptions ;
44
55
import org .springframework .data .mapping .MappingException ;
@@ -113,19 +124,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
113
124
* Create a new {@link MappingCassandraConverter} with a {@link CassandraMappingContext}.
114
125
*/
115
126
public MappingCassandraConverter () {
116
-
117
- super (newConversionService ());
118
-
119
- CassandraCustomConversions conversions = new CassandraCustomConversions (Collections .emptyList ());
120
-
121
- this .mappingContext = newDefaultMappingContext (conversions );
122
- this .codecRegistry = mappingContext .getCodecRegistry ();
123
- this .spELContext = new SpELContext (RowReaderPropertyAccessor .INSTANCE );
124
- this .cassandraTypeResolver = new DefaultColumnTypeResolver (mappingContext ,
125
- userTypeName -> getUserTypeResolver ().resolveType (userTypeName ), this ::getCodecRegistry ,
126
- this ::getCustomConversions );
127
- this .setCustomConversions (conversions );
128
- this .embeddedEntityOperations = new EmbeddedEntityOperations (mappingContext );
127
+ this (newDefaultMappingContext (new CassandraCustomConversions (Collections .emptyList ())));
129
128
}
130
129
131
130
/**
@@ -139,31 +138,42 @@ public MappingCassandraConverter(CassandraMappingContext mappingContext) {
139
138
140
139
Assert .notNull (mappingContext , "CassandraMappingContext must not be null" );
141
140
141
+ UserTypeResolver userTypeResolver = userTypeName -> getUserTypeResolver ().resolveType (userTypeName );
142
+
142
143
this .mappingContext = mappingContext ;
143
- this .codecRegistry = mappingContext .getCodecRegistry ();
144
- this .spELContext = new SpELContext (RowReaderPropertyAccessor .INSTANCE );
145
- this .cassandraTypeResolver = new DefaultColumnTypeResolver (mappingContext ,
146
- userTypeName -> getUserTypeResolver ().resolveType (userTypeName ), this ::getCodecRegistry ,
147
- this ::getCustomConversions );
144
+ this .setCodecRegistry (mappingContext .getCodecRegistry ());
148
145
this .setCustomConversions (mappingContext .getCustomConversions ());
146
+ this .cassandraTypeResolver = new DefaultColumnTypeResolver (mappingContext , userTypeResolver ,
147
+ this ::getCodecRegistry , this ::getCustomConversions );
149
148
this .embeddedEntityOperations = new EmbeddedEntityOperations (mappingContext );
149
+ this .spELContext = new SpELContext (RowReaderPropertyAccessor .INSTANCE );
150
150
}
151
151
152
152
/**
153
- * Creates a new {@link ConversionContext}.
153
+ * Constructs a new instance of {@link ConversionContext} with various converters to convert different Cassandra
154
+ * value types.
154
155
*
155
156
* @return the {@link ConversionContext}.
157
+ * @see ConversionContext
156
158
*/
157
159
protected ConversionContext getConversionContext () {
158
160
159
- return new ConversionContext (getCustomConversions (), this ::doReadRow , this ::doReadTupleValue , this ::doReadUdtValue ,
161
+ return new ConversionContext (this ::doReadRow , this ::doReadTupleValue , this ::doReadUdtValue ,
160
162
this ::readCollectionOrArray , this ::readMap , this ::getPotentiallyConvertedSimpleRead );
161
163
}
162
164
163
165
private static ConversionService newConversionService () {
164
166
return new DefaultConversionService ();
165
167
}
166
168
169
+ /**
170
+ * Constructs a new instance of a {@link MappingContext} for Cassandra.
171
+ *
172
+ * @param conversions {@link CassandraCustomConversions} object encapsulating complex type conversion logic.
173
+ * @return a new {@link CassandraMappingContext}.
174
+ * @see org.springframework.data.cassandra.core.mapping.CassandraMappingContext
175
+ * @see org.springframework.data.mapping.context.MappingContext
176
+ */
167
177
private static CassandraMappingContext newDefaultMappingContext (CassandraCustomConversions conversions ) {
168
178
169
179
CassandraMappingContext mappingContext = new CassandraMappingContext ();
@@ -174,6 +184,7 @@ private static CassandraMappingContext newDefaultMappingContext(CassandraCustomC
174
184
return mappingContext ;
175
185
}
176
186
187
+
177
188
/* (non-Javadoc)
178
189
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
179
190
*/
@@ -222,12 +233,7 @@ public void setCodecRegistry(CodecRegistry codecRegistry) {
222
233
*/
223
234
@ Override
224
235
public CodecRegistry getCodecRegistry () {
225
-
226
- if (this .codecRegistry == null ) {
227
- return mappingContext .getCodecRegistry ();
228
- }
229
-
230
- return this .codecRegistry ;
236
+ return this .codecRegistry != null ? this .codecRegistry : getMappingContext ().getCodecRegistry ();
231
237
}
232
238
233
239
/**
@@ -249,12 +255,7 @@ public void setUserTypeResolver(UserTypeResolver userTypeResolver) {
249
255
* @since 3.0
250
256
*/
251
257
public UserTypeResolver getUserTypeResolver () {
252
-
253
- if (this .userTypeResolver == null ) {
254
- return this .mappingContext .getUserTypeResolver ();
255
- }
256
-
257
- return userTypeResolver ;
258
+ return this .userTypeResolver != null ? this .userTypeResolver : getMappingContext ().getUserTypeResolver ();
258
259
}
259
260
260
261
/* (non-Javadoc)
@@ -500,8 +501,8 @@ protected <S> S doReadEntity(ConversionContext context, CassandraValueProvider v
500
501
return (S ) getConversionService ().convert (valueProvider .getSource (), rawType );
501
502
}
502
503
503
- CassandraPersistentEntity <S > entity = ( CassandraPersistentEntity < S >) getMappingContext ()
504
- .getPersistentEntity (typeHint );
504
+ CassandraPersistentEntity <S > entity =
505
+ ( CassandraPersistentEntity < S >) getMappingContext () .getPersistentEntity (typeHint );
505
506
506
507
if (entity == null ) {
507
508
throw new MappingException (
@@ -526,8 +527,8 @@ private static Class<?> getRawSourceType(CassandraValueProvider valueProvider) {
526
527
return UdtValue .class ;
527
528
}
528
529
529
- throw new InvalidDataAccessApiUsageException (
530
- "Unsupported source type: " + ClassUtils .getDescriptiveType (valueProvider .getSource ()));
530
+ throw new InvalidDataAccessApiUsageException (String . format ( "Unsupported source type: %s" ,
531
+ ClassUtils .getDescriptiveType (valueProvider .getSource () )));
531
532
}
532
533
533
534
private <S > S doReadEntity (ConversionContext context , CassandraValueProvider valueProvider ,
@@ -538,8 +539,8 @@ private <S> S doReadEntity(ConversionContext context, CassandraValueProvider val
538
539
539
540
if (persistenceConstructor != null && persistenceConstructor .hasParameters ()) {
540
541
SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator (valueProvider .getSource (), spELContext );
541
- ParameterValueProvider <CassandraPersistentProperty > parameterValueProvider = newParameterValueProvider ( context ,
542
- entity , valueProvider );
542
+ ParameterValueProvider <CassandraPersistentProperty > parameterValueProvider =
543
+ newParameterValueProvider ( context , entity , valueProvider );
543
544
provider = new ConverterAwareSpELExpressionParameterValueProvider (evaluator , getConversionService (),
544
545
parameterValueProvider , context );
545
546
} else {
@@ -881,10 +882,7 @@ public Object getId(Object object, CassandraPersistentEntity<?> entity) {
881
882
882
883
/**
883
884
* Check custom conversions for type override or fall back to
884
- * {@link #determineTargetType(CassandraPersistentProperty)}
885
- *
886
- * @param property
887
- * @return
885
+ * {@link ColumnTypeResolver#resolve(CassandraPersistentProperty)}.
888
886
*/
889
887
private Class <?> getTargetType (CassandraPersistentProperty property ) {
890
888
return getCustomConversions ().getCustomWriteTarget (property .getType ())
@@ -929,17 +927,20 @@ private Object getWriteValue(@Nullable Object value, ColumnType columnType) {
929
927
930
928
if (getCustomConversions ().hasCustomWriteTarget (value .getClass (), requestedTargetType )) {
931
929
932
- Class <?> resolvedTargetType = getCustomConversions ().getCustomWriteTarget (value .getClass (), requestedTargetType )
930
+ Class <?> resolvedTargetType = getCustomConversions ()
931
+ .getCustomWriteTarget (value .getClass (), requestedTargetType )
933
932
.orElse (requestedTargetType );
934
933
935
934
return getConversionService ().convert (value , resolvedTargetType );
936
935
}
937
936
938
937
if (getCustomConversions ().hasCustomWriteTarget (value .getClass ())) {
939
938
940
- Class <?> resolvedTargetType = getCustomConversions ().getCustomWriteTarget (value .getClass ())
941
- .orElseThrow (() -> new IllegalStateException (String
942
- .format ("Unable to determined custom write target for value type [%s]" , value .getClass ().getName ())));
939
+ Class <?> resolvedTargetType = getCustomConversions ()
940
+ .getCustomWriteTarget (value .getClass ())
941
+ .orElseThrow (() -> new IllegalStateException (
942
+ String .format ("Unable to determined custom write target for value type [%s]" ,
943
+ value .getClass ().getName ())));
943
944
944
945
return getConversionService ().convert (value , resolvedTargetType );
945
946
}
@@ -1123,6 +1124,7 @@ private Object getReadValue(ConversionContext context, CassandraValueProvider va
1123
1124
}
1124
1125
1125
1126
Object value = valueProvider .getPropertyValue (property );
1127
+
1126
1128
return value == null ? null : context .convert (value , property .getTypeInformation ());
1127
1129
}
1128
1130
0 commit comments