Search in sources :

Example 6 with DatastorePersistentProperty

use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQuery method mapToFieldName.

private String mapToFieldName(PropertyDescriptor propertyDescriptor) {
    String name = propertyDescriptor.getName();
    DatastorePersistentProperty persistentProperty = (DatastorePersistentProperty) this.datastorePersistentEntity.getPersistentProperty(name);
    return persistentProperty.getFieldName();
}
Also used : DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Example 7 with DatastorePersistentProperty

use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by spring-cloud.

the class PartTreeDatastoreQuery method applySelectWithFilter.

private void applySelectWithFilter(Object[] parameters, Builder builder) {
    Iterator it = Arrays.asList(parameters).iterator();
    Filter[] filters = this.filterParts.stream().map((part) -> {
        // build properties chain for nested properties
        // if the property is not nested, the list would contain only one property
        List<DatastorePersistentProperty> propertiesChain = getPropertiesChain(part);
        String fieldName = propertiesChain.stream().map(DatastorePersistentProperty::getFieldName).collect(Collectors.joining("."));
        if (part.getType() == Part.Type.IS_NULL) {
            return PropertyFilter.isNull(fieldName);
        }
        BiFunction<String, Value, PropertyFilter> filterFactory = FILTER_FACTORIES.get(part.getType());
        if (filterFactory == null) {
            throw new DatastoreDataException("Unsupported predicate keyword: " + part.getType());
        }
        if (!it.hasNext()) {
            throw new DatastoreDataException("Too few parameters are provided for query method: " + getQueryMethod().getName());
        }
        Value convertedValue = convertParam(propertiesChain.get(propertiesChain.size() - 1), it.next());
        return filterFactory.apply(fieldName, convertedValue);
    }).toArray(Filter[]::new);
    builder.setFilter((filters.length > 1) ? CompositeFilter.and(filters[0], Arrays.copyOfRange(filters, 1, filters.length)) : filters[0]);
}
Also used : Query(com.google.cloud.datastore.Query) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) LESS_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN_EQUAL) Filter(com.google.cloud.datastore.StructuredQuery.Filter) KeyQuery(com.google.cloud.datastore.KeyQuery) Map(java.util.Map) MapBuilder(org.springframework.cloud.gcp.core.util.MapBuilder) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Collector(java.util.stream.Collector) DatastoreTemplate(org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate) LESS_THAN(org.springframework.data.repository.query.parser.Part.Type.LESS_THAN) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) Builder(com.google.cloud.datastore.StructuredQuery.Builder) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) KeyValue(com.google.cloud.datastore.KeyValue) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) SIMPLE_PROPERTY(org.springframework.data.repository.query.parser.Part.Type.SIMPLE_PROPERTY) Collectors(java.util.stream.Collectors) Slice(org.springframework.data.domain.Slice) List(java.util.List) PropertyDescriptor(java.beans.PropertyDescriptor) OrPart(org.springframework.data.repository.query.parser.PartTree.OrPart) Optional(java.util.Optional) PropertyPath(org.springframework.data.mapping.PropertyPath) PageImpl(org.springframework.data.domain.PageImpl) DatastoreMappingContext(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreMappingContext) GREATER_THAN(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN) ProjectionFactory(org.springframework.data.projection.ProjectionFactory) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) Function(java.util.function.Function) Part(org.springframework.data.repository.query.parser.Part) Cursor(com.google.cloud.datastore.Cursor) ProjectionInformation(org.springframework.data.projection.ProjectionInformation) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) StreamSupport(java.util.stream.StreamSupport) StructuredQuery(com.google.cloud.datastore.StructuredQuery) Iterator(java.util.Iterator) PartTree(org.springframework.data.repository.query.parser.PartTree) CompositeFilter(com.google.cloud.datastore.StructuredQuery.CompositeFilter) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) EntityQuery(com.google.cloud.datastore.EntityQuery) DatastoreOperations(org.springframework.cloud.gcp.data.datastore.core.DatastoreOperations) Value(com.google.cloud.datastore.Value) DatastoreQueryOptions(org.springframework.cloud.gcp.data.datastore.core.DatastoreQueryOptions) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty) GREATER_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN_EQUAL) Collections(java.util.Collections) Assert(org.springframework.util.Assert) Filter(com.google.cloud.datastore.StructuredQuery.Filter) PropertyFilter(com.google.cloud.datastore.StructuredQuery.PropertyFilter) CompositeFilter(com.google.cloud.datastore.StructuredQuery.CompositeFilter) BiFunction(java.util.function.BiFunction) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) Iterator(java.util.Iterator) KeyValue(com.google.cloud.datastore.KeyValue) Value(com.google.cloud.datastore.Value) List(java.util.List) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Example 8 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 getReferenceEntitiesForSave.

private List<Entity> getReferenceEntitiesForSave(Object entity, Builder builder, Set<Key> persistedEntities) {
    DatastorePersistentEntity datastorePersistentEntity = this.datastoreMappingContext.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) 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) FullEntity(com.google.cloud.datastore.FullEntity) BaseEntity(com.google.cloud.datastore.BaseEntity) DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) Entity(com.google.cloud.datastore.Entity) KeyValue(com.google.cloud.datastore.KeyValue) DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) ArrayList(java.util.ArrayList) PersistentProperty(org.springframework.data.mapping.PersistentProperty) DatastorePersistentProperty(org.springframework.cloud.gcp.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(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