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;
}
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;
}
Aggregations