Search in sources :

Example 1 with DatastorePersistentProperty

use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.

the class DatastoreTemplate method getReferenceEntitiesForSave.

private List<Entity> getReferenceEntitiesForSave(Object entity, Builder builder, Set<Key> persistedEntities) {
    DatastorePersistentEntity<?> datastorePersistentEntity = getPersistentEntity(entity.getClass());
    List<Entity> entitiesToSave = new ArrayList<>();
    datastorePersistentEntity.doWithAssociations((AssociationHandler) association -> {
        PersistentProperty persistentProperty = association.getInverse();
        PersistentPropertyAccessor accessor = datastorePersistentEntity.getPropertyAccessor(entity);
        Object val = accessor.getProperty(persistentProperty);
        if (val == null) {
            return;
        }
        Value<?> value;
        if (LazyUtil.isLazyAndNotLoaded(val)) {
            value = LazyUtil.getKeys(val);
        } else if (persistentProperty.isCollectionLike()) {
            Iterable<?> iterableVal = (Iterable<?>) ValueUtil.toListIfArray(val);
            entitiesToSave.addAll(getEntitiesForSave(iterableVal, persistedEntities));
            List<KeyValue> keyValues = StreamSupport.stream((iterableVal).spliterator(), false).map(o -> KeyValue.of(this.getKey(o, false))).collect(Collectors.toList());
            value = ListValue.of(keyValues);
        } else {
            entitiesToSave.addAll(getEntitiesForSave(Collections.singletonList(val), persistedEntities));
            Key key = getKey(val, false);
            value = KeyValue.of(key);
        }
        builder.set(((DatastorePersistentProperty) persistentProperty).getFieldName(), value);
    });
    return entitiesToSave;
}
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) FullEntity(com.google.cloud.datastore.FullEntity) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) Entity(com.google.cloud.datastore.Entity) KeyValue(com.google.cloud.datastore.KeyValue) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ArrayList(java.util.ArrayList) PersistentProperty(org.springframework.data.mapping.PersistentProperty) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) 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) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Key(com.google.cloud.datastore.Key) BaseKey(com.google.cloud.datastore.BaseKey)

Example 2 with DatastorePersistentProperty

use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.

the class DatastoreTemplate method createOrderBy.

private static StructuredQuery.OrderBy createOrderBy(DatastorePersistentEntity<?> persistentEntity, Sort.Order order) {
    if (order.isIgnoreCase()) {
        throw new DatastoreDataException("Datastore doesn't support sorting ignoring case");
    }
    if (!order.getNullHandling().equals(Sort.NullHandling.NATIVE)) {
        throw new DatastoreDataException("Datastore supports only NullHandling.NATIVE null handling");
    }
    DatastorePersistentProperty persistentProperty = persistentEntity.getPersistentProperty(order.getProperty());
    Assert.notNull(persistentProperty, "Sort property '" + order.getProperty() + "' must exist in entity '" + persistentEntity.getName() + "'.");
    return new StructuredQuery.OrderBy(persistentProperty.getFieldName(), (order.getDirection() == Sort.Direction.DESC) ? StructuredQuery.OrderBy.Direction.DESCENDING : StructuredQuery.OrderBy.Direction.ASCENDING);
}
Also used : DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)

Example 3 with DatastorePersistentProperty

use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.

the class DatastoreTemplate method getDescendantEntitiesForSave.

private List<Entity> getDescendantEntitiesForSave(Object entity, Key key, Set<Key> persistedEntities) {
    DatastorePersistentEntity<?> datastorePersistentEntity = getPersistentEntity(entity.getClass());
    List<Entity> entitiesToSave = new ArrayList<>();
    datastorePersistentEntity.doWithDescendantProperties((DatastorePersistentProperty persistentProperty) -> {
        // Convert and write descendants, applying ancestor from parent entry
        PersistentPropertyAccessor accessor = datastorePersistentEntity.getPropertyAccessor(entity);
        Object val = accessor.getProperty(persistentProperty);
        if (val != null) {
            // we can be sure that the property is an array or an iterable,
            // because we check it in isDescendant
            entitiesToSave.addAll(getEntitiesForSave((Iterable<?>) ValueUtil.toListIfArray(val), persistedEntities, key));
        }
    });
    return entitiesToSave;
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) Entity(com.google.cloud.datastore.Entity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ArrayList(java.util.ArrayList) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)

Example 4 with DatastorePersistentProperty

use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.

the class EntityPropertyValueProviderTests method testException.

@Test
void testException() {
    Entity entity = Entity.newBuilder(this.datastore.newKeyFactory().setKind("aKind").newKey("1")).set("boolField", 123L).build();
    EntityPropertyValueProvider provider = new EntityPropertyValueProvider(entity, this.twoStepsConversion);
    DatastorePersistentProperty testDpe = this.persistentEntity.getPersistentProperty("boolField");
    assertThatThrownBy(() -> provider.getPropertyValue(testDpe)).isInstanceOf(DatastoreDataException.class).hasMessage("Unable to read property boolField; nested exception is " + "com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException: " + "Unable to convert class java.lang.Long to class java.lang.Boolean");
}
Also used : DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) Entity(com.google.cloud.datastore.Entity) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) Test(org.junit.jupiter.api.Test)

Example 5 with DatastorePersistentProperty

use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.

the class PartTreeDatastoreQuery method mapToFieldName.

private String mapToFieldName(PropertyDescriptor propertyDescriptor) {
    String name = propertyDescriptor.getName();
    DatastorePersistentProperty persistentProperty = (DatastorePersistentProperty) this.datastorePersistentEntity.getPersistentProperty(name);
    Assert.notNull(persistentProperty, "Property '" + name + "' must exist in entity '" + this.datastorePersistentEntity.getName() + "'.");
    return persistentProperty.getFieldName();
}
Also used : DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)

Aggregations

DatastorePersistentProperty (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)11 DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)8 DatastorePersistentEntity (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity)7 Entity (com.google.cloud.datastore.Entity)5 Value (com.google.cloud.datastore.Value)5 BaseEntity (com.google.cloud.datastore.BaseEntity)4 Cursor (com.google.cloud.datastore.Cursor)4 EntityQuery (com.google.cloud.datastore.EntityQuery)4 FullEntity (com.google.cloud.datastore.FullEntity)4 KeyQuery (com.google.cloud.datastore.KeyQuery)4 KeyValue (com.google.cloud.datastore.KeyValue)4 ListValue (com.google.cloud.datastore.ListValue)4 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)4 Query (com.google.cloud.datastore.Query)4 StructuredQuery (com.google.cloud.datastore.StructuredQuery)4 Filter (com.google.cloud.datastore.StructuredQuery.Filter)4 PropertyFilter (com.google.cloud.datastore.StructuredQuery.PropertyFilter)4 DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)4 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)4 BaseKey (com.google.cloud.datastore.BaseKey)3