Search in sources :

Example 1 with PreferredConstructor

use of org.springframework.data.mapping.PreferredConstructor in project spring-cloud-gcp by spring-cloud.

the class ConverterAwareMappingSpannerEntityReader method read.

/**
 * Reads a single POJO from a Cloud Spanner row.
 * @param type the type of POJO
 * @param source the Cloud Spanner row
 * @param includeColumns the columns to read. If null then all columns will be read.
 * @param allowMissingColumns if true, then properties with no corresponding column are
 * not mapped. If false, then an exception is thrown.
 * @param <R> the type of the POJO.
 * @return the POJO
 */
@SuppressWarnings("unchecked")
public <R> R read(Class<R> type, Struct source, Set<String> includeColumns, boolean allowMissingColumns) {
    boolean readAllColumns = includeColumns == null;
    SpannerPersistentEntity<R> persistentEntity = (SpannerPersistentEntity<R>) this.spannerMappingContext.getPersistentEntity(type);
    StructAccessor structAccessor = new StructAccessor(source);
    StructPropertyValueProvider propertyValueProvider = new StructPropertyValueProvider(structAccessor, this.converter, this, allowMissingColumns);
    PreferredConstructor<?, SpannerPersistentProperty> persistenceConstructor = persistentEntity.getPersistenceConstructor();
    // @formatter:off
    ParameterValueProvider<SpannerPersistentProperty> parameterValueProvider = new PersistentEntityParameterValueProvider<>(persistentEntity, propertyValueProvider, null);
    // @formatter:on
    EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(persistentEntity);
    R instance = instantiator.createInstance(persistentEntity, parameterValueProvider);
    PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(instance);
    persistentEntity.doWithProperties((PropertyHandler<SpannerPersistentProperty>) (spannerPersistentProperty) -> {
        if (spannerPersistentProperty.isEmbedded()) {
            accessor.setProperty(spannerPersistentProperty, read(spannerPersistentProperty.getType(), source, includeColumns, allowMissingColumns));
        } else {
            if (!shouldSkipProperty(structAccessor, spannerPersistentProperty, includeColumns, readAllColumns, allowMissingColumns, persistenceConstructor)) {
                Object value = propertyValueProvider.getPropertyValue(spannerPersistentProperty);
                accessor.setProperty(spannerPersistentProperty, value);
            }
        }
    });
    return instance;
}
Also used : SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) ParameterValueProvider(org.springframework.data.mapping.model.ParameterValueProvider) Set(java.util.Set) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PropertyHandler(org.springframework.data.mapping.PropertyHandler) EntityInstantiator(org.springframework.data.convert.EntityInstantiator) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty) Struct(com.google.cloud.spanner.Struct) EntityInstantiators(org.springframework.data.convert.EntityInstantiators) PersistentEntityParameterValueProvider(org.springframework.data.mapping.model.PersistentEntityParameterValueProvider) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) PreferredConstructor(org.springframework.data.mapping.PreferredConstructor) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) EntityInstantiator(org.springframework.data.convert.EntityInstantiator) PersistentEntityParameterValueProvider(org.springframework.data.mapping.model.PersistentEntityParameterValueProvider) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity)

Aggregations

Struct (com.google.cloud.spanner.Struct)1 Set (java.util.Set)1 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)1 SpannerMappingContext (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext)1 SpannerPersistentEntity (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity)1 SpannerPersistentProperty (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)1 EntityInstantiator (org.springframework.data.convert.EntityInstantiator)1 EntityInstantiators (org.springframework.data.convert.EntityInstantiators)1 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)1 PreferredConstructor (org.springframework.data.mapping.PreferredConstructor)1 PropertyHandler (org.springframework.data.mapping.PropertyHandler)1 ParameterValueProvider (org.springframework.data.mapping.model.ParameterValueProvider)1 PersistentEntityParameterValueProvider (org.springframework.data.mapping.model.PersistentEntityParameterValueProvider)1