use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity in project spring-cloud-gcp by GoogleCloudPlatform.
the class DatastoreTemplate method validateKey.
private void validateKey(Object entity, PathElement ancestorPathElement) {
DatastorePersistentEntity datastorePersistentEntity = 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(ancestorPathElement))) {
return;
}
throw new DatastoreDataException("Descendant object has a key without current ancestor");
}
use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity in project spring-cloud-gcp by GoogleCloudPlatform.
the class DatastoreTemplate method applyQueryOptions.
public static void applyQueryOptions(StructuredQuery.Builder builder, DatastoreQueryOptions queryOptions, DatastorePersistentEntity<?> persistentEntity) {
Assert.notNull(persistentEntity, "A non-null persistentEntity is required.");
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) {
queryOptions.getSort().stream().map(order -> createOrderBy(persistentEntity, order)).forEachOrdered(builder::addOrderBy);
}
}
use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity in project spring-cloud-gcp by GoogleCloudPlatform.
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 = FullEntity.newBuilder();
this.datastoreEntityConverter.write(probe, probeEntityBuilder);
FullEntity<IncompleteKey> probeEntity = probeEntityBuilder.build();
DatastorePersistentEntity<?> persistentEntity = 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, 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 com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity in project spring-cloud-gcp by GoogleCloudPlatform.
the class DatastoreServiceObjectToKeyFactoryTests method getKeyNoIdTest.
@Test
void getKeyNoIdTest() {
DatastorePersistentEntity dpeTest = this.datastoreMappingContext.getPersistentEntity(TestEntityNoId.class);
Object entity = new TestEntityNoId();
assertThatThrownBy(() -> this.datastoreServiceObjectToKeyFactory.getKeyFromObject(entity, dpeTest)).isInstanceOf(DatastoreDataException.class).hasMessage("An ID property was required but does not exist for the type: " + "class com.google.cloud.spring.data.datastore.core.convert." + "DatastoreServiceObjectToKeyFactoryTests$TestEntityNoId");
}
Aggregations