use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity 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.DatastorePersistentEntity in project spring-cloud-gcp by spring-cloud.
the class GqlDatastoreQuery method setGqlResolvedEntityClassName.
private void setGqlResolvedEntityClassName() {
Matcher matcher = CLASS_NAME_PATTERN.matcher(GqlDatastoreQuery.this.originalGql);
String result = GqlDatastoreQuery.this.originalGql;
while (matcher.find()) {
String matched = matcher.group();
String className = matched.substring(1, matched.length() - 1);
try {
Class entityClass = Class.forName(className);
DatastorePersistentEntity datastorePersistentEntity = GqlDatastoreQuery.this.datastoreMappingContext.getPersistentEntity(entityClass);
if (datastorePersistentEntity == null) {
throw new DatastoreDataException("The class used in the GQL statement is not a Cloud Datastore persistent entity: " + className);
}
result = result.replace(matched, datastorePersistentEntity.kindName());
} catch (ClassNotFoundException ex) {
throw new DatastoreDataException("The class name does not refer to an available entity type: " + className);
}
}
this.gqlResolvedEntityClassName = result;
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method applyQueryOptions.
public static void applyQueryOptions(StructuredQuery.Builder builder, DatastoreQueryOptions queryOptions, DatastorePersistentEntity<?> persistentEntity) {
if (persistentEntity.getDiscriminationFieldName() != null && persistentEntity.getDiscriminatorValue() != null) {
StructuredQuery.Filter discriminationFilter = PropertyFilter.eq(persistentEntity.getDiscriminationFieldName(), persistentEntity.getDiscriminatorValue());
StructuredQuery.Filter filter = builder.build().getFilter();
if (filter != null) {
discriminationFilter = StructuredQuery.CompositeFilter.and(filter, discriminationFilter);
}
builder.setFilter(discriminationFilter);
}
if (queryOptions == null) {
return;
}
if (queryOptions.getLimit() != null) {
builder.setLimit(queryOptions.getLimit());
}
if (queryOptions.getCursor() == null && queryOptions.getOffset() != null) {
builder.setOffset(queryOptions.getOffset());
}
if (queryOptions.getCursor() != null) {
builder.setStartCursor(queryOptions.getCursor());
}
if (queryOptions.getSort() != null && persistentEntity != null) {
queryOptions.getSort().stream().map((order) -> createOrderBy(persistentEntity, order)).forEachOrdered((orderBy) -> builder.addOrderBy(orderBy));
}
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity 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.DatastorePersistentEntity 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);
}
Aggregations