Search in sources :

Example 11 with DatastorePersistentEntity

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");
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Query(com.google.cloud.datastore.Query) Builder(com.google.cloud.datastore.Entity.Builder) PathElement(com.google.cloud.datastore.PathElement) ObjectToKeyFactory(com.google.cloud.spring.data.datastore.core.convert.ObjectToKeyFactory) ValueUtil(com.google.cloud.spring.data.datastore.core.util.ValueUtil) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) Filter(com.google.cloud.datastore.StructuredQuery.Filter) KeyQuery(com.google.cloud.datastore.KeyQuery) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) QueryResults(com.google.cloud.datastore.QueryResults) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) AfterDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent) PersistentProperty(org.springframework.data.mapping.PersistentProperty) BaseEntity(com.google.cloud.datastore.BaseEntity) KeyValue(com.google.cloud.datastore.KeyValue) DatastorePageable(com.google.cloud.spring.data.datastore.repository.query.DatastorePageable) Collection(java.util.Collection) Set(java.util.Set) AfterQueryEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterQueryEvent) Example(org.springframework.data.domain.Example) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Collectors(java.util.stream.Collectors) Slice(org.springframework.data.domain.Slice) Objects(java.util.Objects) BeforeSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeSaveEvent) List(java.util.List) SliceUtil(com.google.cloud.spring.data.datastore.core.util.SliceUtil) Optional(java.util.Optional) NonNull(org.springframework.lang.NonNull) ListValue(com.google.cloud.datastore.ListValue) Key(com.google.cloud.datastore.Key) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) SliceImpl(org.springframework.data.domain.SliceImpl) AfterFindByKeyEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent) HashMap(java.util.HashMap) Datastore(com.google.cloud.datastore.Datastore) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Cursor(com.google.cloud.datastore.Cursor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) Entity(com.google.cloud.datastore.Entity) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) BeforeDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeDeleteEvent) NullValue(com.google.cloud.datastore.NullValue) DatastoreEntityConverter(com.google.cloud.spring.data.datastore.core.convert.DatastoreEntityConverter) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) StreamSupport(java.util.stream.StreamSupport) Nullable(org.springframework.lang.Nullable) LinkedList(java.util.LinkedList) StructuredQuery(com.google.cloud.datastore.StructuredQuery) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Iterator(java.util.Iterator) KeyUtil(com.google.cloud.spring.data.datastore.core.util.KeyUtil) TypeUtils(org.springframework.util.TypeUtils) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) DatastoreReaderWriter(com.google.cloud.datastore.DatastoreReaderWriter) EntityQuery(com.google.cloud.datastore.EntityQuery) ApplicationEvent(org.springframework.context.ApplicationEvent) AssociationHandler(org.springframework.data.mapping.AssociationHandler) BaseKey(com.google.cloud.datastore.BaseKey) Value(com.google.cloud.datastore.Value) AfterSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterSaveEvent) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Collections(java.util.Collections) Assert(org.springframework.util.Assert) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) BaseKey(com.google.cloud.datastore.BaseKey) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Key(com.google.cloud.datastore.Key) BaseKey(com.google.cloud.datastore.BaseKey)

Example 12 with DatastorePersistentEntity

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);
    }
}
Also used : StructuredQuery(com.google.cloud.datastore.StructuredQuery) FullEntity(com.google.cloud.datastore.FullEntity) Query(com.google.cloud.datastore.Query) Builder(com.google.cloud.datastore.Entity.Builder) PathElement(com.google.cloud.datastore.PathElement) ObjectToKeyFactory(com.google.cloud.spring.data.datastore.core.convert.ObjectToKeyFactory) ValueUtil(com.google.cloud.spring.data.datastore.core.util.ValueUtil) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) Filter(com.google.cloud.datastore.StructuredQuery.Filter) KeyQuery(com.google.cloud.datastore.KeyQuery) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) QueryResults(com.google.cloud.datastore.QueryResults) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) AfterDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent) PersistentProperty(org.springframework.data.mapping.PersistentProperty) BaseEntity(com.google.cloud.datastore.BaseEntity) KeyValue(com.google.cloud.datastore.KeyValue) DatastorePageable(com.google.cloud.spring.data.datastore.repository.query.DatastorePageable) Collection(java.util.Collection) Set(java.util.Set) AfterQueryEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterQueryEvent) Example(org.springframework.data.domain.Example) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Collectors(java.util.stream.Collectors) Slice(org.springframework.data.domain.Slice) Objects(java.util.Objects) BeforeSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeSaveEvent) List(java.util.List) SliceUtil(com.google.cloud.spring.data.datastore.core.util.SliceUtil) Optional(java.util.Optional) NonNull(org.springframework.lang.NonNull) ListValue(com.google.cloud.datastore.ListValue) Key(com.google.cloud.datastore.Key) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) SliceImpl(org.springframework.data.domain.SliceImpl) AfterFindByKeyEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent) HashMap(java.util.HashMap) Datastore(com.google.cloud.datastore.Datastore) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Cursor(com.google.cloud.datastore.Cursor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) Entity(com.google.cloud.datastore.Entity) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) BeforeDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeDeleteEvent) NullValue(com.google.cloud.datastore.NullValue) DatastoreEntityConverter(com.google.cloud.spring.data.datastore.core.convert.DatastoreEntityConverter) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) StreamSupport(java.util.stream.StreamSupport) Nullable(org.springframework.lang.Nullable) LinkedList(java.util.LinkedList) StructuredQuery(com.google.cloud.datastore.StructuredQuery) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Iterator(java.util.Iterator) KeyUtil(com.google.cloud.spring.data.datastore.core.util.KeyUtil) TypeUtils(org.springframework.util.TypeUtils) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) DatastoreReaderWriter(com.google.cloud.datastore.DatastoreReaderWriter) EntityQuery(com.google.cloud.datastore.EntityQuery) ApplicationEvent(org.springframework.context.ApplicationEvent) AssociationHandler(org.springframework.data.mapping.AssociationHandler) BaseKey(com.google.cloud.datastore.BaseKey) Value(com.google.cloud.datastore.Value) AfterSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterSaveEvent) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Collections(java.util.Collections) Assert(org.springframework.util.Assert) Filter(com.google.cloud.datastore.StructuredQuery.Filter)

Example 13 with DatastorePersistentEntity

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();
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) Query(com.google.cloud.datastore.Query) Builder(com.google.cloud.datastore.Entity.Builder) PathElement(com.google.cloud.datastore.PathElement) ObjectToKeyFactory(com.google.cloud.spring.data.datastore.core.convert.ObjectToKeyFactory) ValueUtil(com.google.cloud.spring.data.datastore.core.util.ValueUtil) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) Filter(com.google.cloud.datastore.StructuredQuery.Filter) KeyQuery(com.google.cloud.datastore.KeyQuery) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) QueryResults(com.google.cloud.datastore.QueryResults) Map(java.util.Map) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) AfterDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterDeleteEvent) PersistentProperty(org.springframework.data.mapping.PersistentProperty) BaseEntity(com.google.cloud.datastore.BaseEntity) KeyValue(com.google.cloud.datastore.KeyValue) DatastorePageable(com.google.cloud.spring.data.datastore.repository.query.DatastorePageable) Collection(java.util.Collection) Set(java.util.Set) AfterQueryEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterQueryEvent) Example(org.springframework.data.domain.Example) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Collectors(java.util.stream.Collectors) Slice(org.springframework.data.domain.Slice) Objects(java.util.Objects) BeforeSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeSaveEvent) List(java.util.List) SliceUtil(com.google.cloud.spring.data.datastore.core.util.SliceUtil) Optional(java.util.Optional) NonNull(org.springframework.lang.NonNull) ListValue(com.google.cloud.datastore.ListValue) Key(com.google.cloud.datastore.Key) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) SliceImpl(org.springframework.data.domain.SliceImpl) AfterFindByKeyEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent) HashMap(java.util.HashMap) Datastore(com.google.cloud.datastore.Datastore) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Cursor(com.google.cloud.datastore.Cursor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) Entity(com.google.cloud.datastore.Entity) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) BeforeDeleteEvent(com.google.cloud.spring.data.datastore.core.mapping.event.BeforeDeleteEvent) NullValue(com.google.cloud.datastore.NullValue) DatastoreEntityConverter(com.google.cloud.spring.data.datastore.core.convert.DatastoreEntityConverter) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) StreamSupport(java.util.stream.StreamSupport) Nullable(org.springframework.lang.Nullable) LinkedList(java.util.LinkedList) StructuredQuery(com.google.cloud.datastore.StructuredQuery) ApplicationEventPublisherAware(org.springframework.context.ApplicationEventPublisherAware) Iterator(java.util.Iterator) KeyUtil(com.google.cloud.spring.data.datastore.core.util.KeyUtil) TypeUtils(org.springframework.util.TypeUtils) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) DatastoreReaderWriter(com.google.cloud.datastore.DatastoreReaderWriter) EntityQuery(com.google.cloud.datastore.EntityQuery) ApplicationEvent(org.springframework.context.ApplicationEvent) AssociationHandler(org.springframework.data.mapping.AssociationHandler) BaseKey(com.google.cloud.datastore.BaseKey) Value(com.google.cloud.datastore.Value) AfterSaveEvent(com.google.cloud.spring.data.datastore.core.mapping.event.AfterSaveEvent) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StructuredQuery(com.google.cloud.datastore.StructuredQuery) NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) FullEntity(com.google.cloud.datastore.FullEntity) LinkedList(java.util.LinkedList) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Filter(com.google.cloud.datastore.StructuredQuery.Filter) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) KeyValue(com.google.cloud.datastore.KeyValue) ListValue(com.google.cloud.datastore.ListValue) NullValue(com.google.cloud.datastore.NullValue) Value(com.google.cloud.datastore.Value) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)

Example 14 with DatastorePersistentEntity

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");
}
Also used : DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Test(org.junit.jupiter.api.Test)

Aggregations

DatastorePersistentEntity (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity)14 DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)10 Key (com.google.cloud.datastore.Key)8 Entity (com.google.cloud.datastore.Entity)7 FullEntity (com.google.cloud.datastore.FullEntity)7 IncompleteKey (com.google.cloud.datastore.IncompleteKey)7 ArrayList (java.util.ArrayList)7 DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)6 DatastorePersistentProperty (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)6 BaseEntity (com.google.cloud.datastore.BaseEntity)5 BaseKey (com.google.cloud.datastore.BaseKey)5 EntityQuery (com.google.cloud.datastore.EntityQuery)5 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)5 Filter (com.google.cloud.datastore.StructuredQuery.Filter)5 PropertyFilter (com.google.cloud.datastore.StructuredQuery.PropertyFilter)5 AfterFindByKeyEvent (com.google.cloud.spring.data.datastore.core.mapping.event.AfterFindByKeyEvent)5 Cursor (com.google.cloud.datastore.Cursor)4 Datastore (com.google.cloud.datastore.Datastore)4 DatastoreReaderWriter (com.google.cloud.datastore.DatastoreReaderWriter)4 Builder (com.google.cloud.datastore.Entity.Builder)4