Search in sources :

Example 1 with DatastorePersistentProperty

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;
}
Also used : DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) EntityInstantiator(org.springframework.data.convert.EntityInstantiator) PersistentEntityParameterValueProvider(org.springframework.data.mapping.model.PersistentEntityParameterValueProvider) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Example 2 with DatastorePersistentProperty

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);
        }
    });
}
Also used : PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) EntityValue(com.google.cloud.datastore.EntityValue) Value(com.google.cloud.datastore.Value) StringValue(com.google.cloud.datastore.StringValue) ListValue(com.google.cloud.datastore.ListValue) StringValue(com.google.cloud.datastore.StringValue) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Example 3 with DatastorePersistentProperty

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();
}
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) SliceUtil(org.springframework.cloud.gcp.data.datastore.core.util.SliceUtil) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) TransactionSynchronizationManager(org.springframework.transaction.support.TransactionSynchronizationManager) Filter(com.google.cloud.datastore.StructuredQuery.Filter) AfterQueryEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterQueryEvent) 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) PersistentProperty(org.springframework.data.mapping.PersistentProperty) AfterFindByKeyEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterFindByKeyEvent) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) Collection(java.util.Collection) KeyValue(com.google.cloud.datastore.KeyValue) AfterSaveEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterSaveEvent) Set(java.util.Set) DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) Example(org.springframework.data.domain.Example) Collectors(java.util.stream.Collectors) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Slice(org.springframework.data.domain.Slice) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) KeyUtil(org.springframework.cloud.gcp.data.datastore.core.util.KeyUtil) ListValue(com.google.cloud.datastore.ListValue) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) BeforeDeleteEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.BeforeDeleteEvent) Key(com.google.cloud.datastore.Key) BeforeSaveEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.BeforeSaveEvent) DatastoreEntityConverter(org.springframework.cloud.gcp.data.datastore.core.convert.DatastoreEntityConverter) SliceImpl(org.springframework.data.domain.SliceImpl) HashMap(java.util.HashMap) Datastore(com.google.cloud.datastore.Datastore) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Cursor(com.google.cloud.datastore.Cursor) HashSet(java.util.HashSet) NullHandler(org.springframework.data.domain.ExampleMatcher.NullHandler) ValueUtil(org.springframework.cloud.gcp.data.datastore.core.util.ValueUtil) Entity(com.google.cloud.datastore.Entity) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) NullValue(com.google.cloud.datastore.NullValue) 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) TypeUtils(org.springframework.util.TypeUtils) ObjectToKeyFactory(org.springframework.cloud.gcp.data.datastore.core.convert.ObjectToKeyFactory) ExampleMatcher(org.springframework.data.domain.ExampleMatcher) DatastoreReaderWriter(com.google.cloud.datastore.DatastoreReaderWriter) EntityQuery(com.google.cloud.datastore.EntityQuery) AfterDeleteEvent(org.springframework.cloud.gcp.data.datastore.core.mapping.event.AfterDeleteEvent) ApplicationEvent(org.springframework.context.ApplicationEvent) AssociationHandler(org.springframework.data.mapping.AssociationHandler) BaseKey(com.google.cloud.datastore.BaseKey) Value(com.google.cloud.datastore.Value) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty) Collections(java.util.Collections) DatastorePageable(org.springframework.cloud.gcp.data.datastore.repository.query.DatastorePageable) 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(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Example 4 with DatastorePersistentProperty

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);
}
Also used : DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Example 5 with DatastorePersistentProperty

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

Aggregations

DatastorePersistentProperty (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)8 DatastoreDataException (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException)6 DatastorePersistentEntity (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity)6 Value (com.google.cloud.datastore.Value)5 Cursor (com.google.cloud.datastore.Cursor)4 EntityQuery (com.google.cloud.datastore.EntityQuery)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 Collections (java.util.Collections)4 Iterator (java.util.Iterator)4 List (java.util.List)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Function (java.util.function.Function)4