Search in sources :

Example 16 with DatastoreDataException

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

the class DatastoreTemplate method computeReferencedField.

private <T> T computeReferencedField(BaseEntity entity, ReadContext context, DatastorePersistentProperty referenceProperty, String fieldName, Class<T> type) {
    T referenced;
    if (referenceProperty.isLazyLoaded()) {
        DatastoreReaderWriter originalTx = getDatastoreReadWriter();
        referenced = LazyUtil.wrapSimpleLazyProxy(() -> {
            if (getDatastoreReadWriter() != originalTx) {
                throw new DatastoreDataException("Lazy load should be invoked within the same transaction");
            }
            return (T) findReferenced(entity, referenceProperty, context);
        }, type, entity.getValue(fieldName));
    } else {
        referenced = (T) findReferenced(entity, referenceProperty, context);
    }
    return referenced;
}
Also used : DatastoreReaderWriter(com.google.cloud.datastore.DatastoreReaderWriter) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)

Example 17 with DatastoreDataException

use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException 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 18 with DatastoreDataException

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

the class DatastoreTemplate method validateExample.

private <T> void validateExample(Example<T> example) {
    Assert.notNull(example, "A non-null example is expected");
    ExampleMatcher matcher = example.getMatcher();
    if (!matcher.isAllMatching()) {
        throw new DatastoreDataException("Unsupported MatchMode. Only MatchMode.ALL is supported");
    }
    if (matcher.isIgnoreCaseEnabled()) {
        throw new DatastoreDataException("Ignore case matching is not supported");
    }
    if (!(matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.EXACT || matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.DEFAULT)) {
        throw new DatastoreDataException("Unsupported StringMatcher. Only EXACT and DEFAULT are supported");
    }
    Optional<String> path = example.getMatcher().getIgnoredPaths().stream().filter(s -> s.contains(".")).findFirst();
    if (path.isPresent()) {
        throw new DatastoreDataException("Ignored paths deeper than 1 are not supported");
    }
    if (matcher.getPropertySpecifiers().hasValues()) {
        throw new DatastoreDataException("Property matchers are not supported");
    }
}
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) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) ExampleMatcher(org.springframework.data.domain.ExampleMatcher)

Example 19 with DatastoreDataException

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

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) MapBuilder(com.google.cloud.spring.core.util.MapBuilder) 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) DatastoreQueryOptions(com.google.cloud.spring.data.datastore.core.DatastoreQueryOptions) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Collector(java.util.stream.Collector) 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) KeyValue(com.google.cloud.datastore.KeyValue) ParameterAccessor(org.springframework.data.repository.query.ParameterAccessor) 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) GREATER_THAN(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) ProjectionFactory(org.springframework.data.projection.ProjectionFactory) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) 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) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) StreamSupport(java.util.stream.StreamSupport) StructuredQuery(com.google.cloud.datastore.StructuredQuery) DatastoreOperations(com.google.cloud.spring.data.datastore.core.DatastoreOperations) Iterator(java.util.Iterator) PartTree(org.springframework.data.repository.query.parser.PartTree) CompositeFilter(com.google.cloud.datastore.StructuredQuery.CompositeFilter) EntityQuery(com.google.cloud.datastore.EntityQuery) Value(com.google.cloud.datastore.Value) GREATER_THAN_EQUAL(org.springframework.data.repository.query.parser.Part.Type.GREATER_THAN_EQUAL) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) DatastoreTemplate(com.google.cloud.spring.data.datastore.core.DatastoreTemplate) 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(com.google.cloud.spring.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(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)

Aggregations

DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)19 DatastorePersistentProperty (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)7 Value (com.google.cloud.datastore.Value)6 DatastorePersistentEntity (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity)6 IncompleteKey (com.google.cloud.datastore.IncompleteKey)5 Key (com.google.cloud.datastore.Key)5 List (java.util.List)5 Function (java.util.function.Function)5 BaseEntity (com.google.cloud.datastore.BaseEntity)4 KeyValue (com.google.cloud.datastore.KeyValue)4 DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)4 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 StreamSupport (java.util.stream.StreamSupport)4 Pageable (org.springframework.data.domain.Pageable)4 Sort (org.springframework.data.domain.Sort)4 Cursor (com.google.cloud.datastore.Cursor)3