use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty 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.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by spring-cloud.
the class DefaultDatastoreEntityConverter method write.
@Override
@SuppressWarnings("unchecked")
public void write(Object source, BaseEntity.Builder sink) {
DatastorePersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(source.getClass());
String discriminationFieldName = persistentEntity.getDiscriminationFieldName();
List<String> discriminationValues = persistentEntity.getCompatibleDiscriminationValues();
if (!discriminationValues.isEmpty() || discriminationFieldName != null) {
sink.set(discriminationFieldName, discriminationValues.stream().map(StringValue::of).collect(Collectors.toList()));
}
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(source);
persistentEntity.doWithColumnBackedProperties((DatastorePersistentProperty persistentProperty) -> {
// Datastore doesn't store its Key as a regular field.
if (persistentProperty.isIdProperty()) {
return;
}
try {
Object val = accessor.getProperty(persistentProperty);
Value convertedVal = this.conversions.convertOnWrite(val, persistentProperty);
if (persistentProperty.isUnindexed()) {
convertedVal = setExcludeFromIndexes(convertedVal);
}
sink.set(persistentProperty.getFieldName(), convertedVal);
} catch (DatastoreDataException ex) {
throw new DatastoreDataException("Unable to write " + persistentEntity.kindName() + "." + persistentProperty.getFieldName(), ex);
}
});
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method exampleToQuery.
private <T> StructuredQuery exampleToQuery(Example<T> example, DatastoreQueryOptions queryOptions, boolean keyQuery) {
validateExample(example);
T probe = example.getProbe();
FullEntity.Builder<IncompleteKey> probeEntityBuilder = Entity.newBuilder();
this.datastoreEntityConverter.write(probe, probeEntityBuilder);
FullEntity<IncompleteKey> probeEntity = probeEntityBuilder.build();
DatastorePersistentEntity<?> persistentEntity = this.datastoreMappingContext.getPersistentEntity(example.getProbeType());
LinkedList<StructuredQuery.Filter> filters = new LinkedList<>();
NullHandler nullHandler = example.getMatcher().getNullHandler();
persistentEntity.doWithColumnBackedProperties((persistentProperty) -> {
if (!ignoredProperty(example, persistentProperty)) {
Value<?> value = getValue(example, probeEntity, persistentEntity, persistentProperty);
addFilter(nullHandler, filters, persistentProperty.getFieldName(), value);
}
});
persistentEntity.doWithAssociations((AssociationHandler<DatastorePersistentProperty>) association -> {
PersistentPropertyAccessor<?> accessor = persistentEntity.getPropertyAccessor(example.getProbe());
DatastorePersistentProperty property = association.getInverse();
Object value = accessor.getProperty(property);
Value<?> key = value == null ? NullValue.of() : KeyValue.of(objectToKeyFactory.getKeyFromObject(value, this.datastoreMappingContext.getPersistentEntity(value.getClass())));
addFilter(nullHandler, filters, property.getFieldName(), key);
});
StructuredQuery.Builder<?> builder = keyQuery ? Query.newKeyQueryBuilder() : Query.newEntityQueryBuilder();
builder.setKind(persistentEntity.kindName());
if (!filters.isEmpty()) {
builder.setFilter(StructuredQuery.CompositeFilter.and(filters.pop(), filters.toArray(new StructuredQuery.Filter[0])));
}
applyQueryOptions(builder, queryOptions, persistentEntity);
return builder.build();
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method getKey.
private Key getKey(Object entity, boolean allocateKey, Key... ancestors) {
DatastorePersistentEntity datastorePersistentEntity = this.datastoreMappingContext.getPersistentEntity(entity.getClass());
DatastorePersistentProperty idProp = datastorePersistentEntity.getIdPropertyOrFail();
if (datastorePersistentEntity.getPropertyAccessor(entity).getProperty(idProp) == null && allocateKey) {
return this.objectToKeyFactory.allocateKeyForObject(entity, datastorePersistentEntity, ancestors);
}
return this.objectToKeyFactory.getKeyFromObject(entity, datastorePersistentEntity);
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method validateKey.
private void validateKey(Object entity, PathElement ancestorPE) {
DatastorePersistentEntity datastorePersistentEntity = this.datastoreMappingContext.getPersistentEntity(entity.getClass());
DatastorePersistentProperty idProp = datastorePersistentEntity.getIdPropertyOrFail();
if (!TypeUtils.isAssignable(BaseKey.class, idProp.getType())) {
throw new DatastoreDataException("Only Key types are allowed for descendants id");
}
Key key = getKey(entity, false);
if (key == null || key.getAncestors().stream().anyMatch((pe) -> pe.equals(ancestorPE))) {
return;
}
throw new DatastoreDataException("Descendant object has a key without current ancestor");
}
Aggregations