Search in sources :

Example 1 with PersistentEntityParameterValueProvider

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

the class DefaultDatastoreEntityConverter method read.

@Override
@SuppressWarnings("unchecked")
public <R> R read(Class<R> aClass, BaseEntity entity) {
    if (entity == null) {
        return null;
    }
    DatastorePersistentEntity<R> ostensiblePersistentEntity = (DatastorePersistentEntity<R>) this.mappingContext.getPersistentEntity(aClass);
    if (ostensiblePersistentEntity == null) {
        throw new DatastoreDataException("Unable to convert Datastore Entity to " + aClass);
    }
    EntityPropertyValueProvider propertyValueProvider = new EntityPropertyValueProvider(entity, this.conversions);
    DatastorePersistentEntity<?> persistentEntity = getDiscriminationPersistentEntity(ostensiblePersistentEntity, propertyValueProvider);
    ParameterValueProvider<DatastorePersistentProperty> parameterValueProvider = new PersistentEntityParameterValueProvider<>(persistentEntity, propertyValueProvider, null);
    EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(persistentEntity);
    Object instance;
    try {
        instance = instantiator.createInstance(persistentEntity, parameterValueProvider);
        PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(instance);
        persistentEntity.doWithColumnBackedProperties((datastorePersistentProperty) -> {
            // if a property is a constructor argument, it was already computed on instantiation
            if (!persistentEntity.isConstructorArgument(datastorePersistentProperty)) {
                Object value = propertyValueProvider.getPropertyValue(datastorePersistentProperty);
                if (value != null) {
                    accessor.setProperty(datastorePersistentProperty, value);
                }
            }
        });
    } catch (DatastoreDataException ex) {
        throw new DatastoreDataException("Unable to read " + persistentEntity.getName() + " entity", ex);
    }
    return (R) instance;
}
Also used : DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) EntityInstantiator(org.springframework.data.convert.EntityInstantiator) PersistentEntityParameterValueProvider(org.springframework.data.mapping.model.PersistentEntityParameterValueProvider) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Example 2 with PersistentEntityParameterValueProvider

use of org.springframework.data.mapping.model.PersistentEntityParameterValueProvider 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

EntityInstantiator (org.springframework.data.convert.EntityInstantiator)2 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)2 PersistentEntityParameterValueProvider (org.springframework.data.mapping.model.PersistentEntityParameterValueProvider)2 Struct (com.google.cloud.spanner.Struct)1 Set (java.util.Set)1 DatastoreDataException (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException)1 DatastorePersistentEntity (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity)1 DatastorePersistentProperty (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)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 EntityInstantiators (org.springframework.data.convert.EntityInstantiators)1 PreferredConstructor (org.springframework.data.mapping.PreferredConstructor)1 PropertyHandler (org.springframework.data.mapping.PropertyHandler)1 ParameterValueProvider (org.springframework.data.mapping.model.ParameterValueProvider)1