Search in sources :

Example 6 with DatastoreDataException

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

the class DatastoreServiceObjectToKeyFactory method allocateKeyForObject.

@Override
public Key allocateKeyForObject(Object entity, DatastorePersistentEntity datastorePersistentEntity, Key... ancestors) {
    Assert.notNull(entity, "Cannot get key for null entity object.");
    Assert.notNull(datastorePersistentEntity, "Persistent entity must not be null.");
    PersistentProperty idProp = datastorePersistentEntity.getIdPropertyOrFail();
    Class idPropType = idProp.getType();
    if (!idPropType.equals(Key.class) && !idPropType.equals(Long.class)) {
        throw new DatastoreDataException("Cloud Datastore can only allocate IDs for Long and Key properties. " + "Cannot allocate for type: " + idPropType);
    }
    KeyFactory keyFactory = getKeyFactory().setKind(datastorePersistentEntity.kindName());
    if (ancestors != null && ancestors.length > 0) {
        if (!idPropType.equals(Key.class)) {
            throw new DatastoreDataException("Only Key types are allowed for descendants id");
        }
        for (Key ancestor : ancestors) {
            keyFactory.addAncestor(DatastoreTemplate.keyToPathElement(ancestor));
        }
    }
    Key allocatedKey = this.datastore.get().allocateId(keyFactory.newKey());
    Object value;
    if (idPropType.equals(Key.class)) {
        value = allocatedKey;
    } else if (idPropType.equals(Long.class)) {
        value = allocatedKey.getId();
    } else {
        value = allocatedKey.getId().toString();
    }
    datastorePersistentEntity.getPropertyAccessor(entity).setProperty(idProp, value);
    return allocatedKey;
}
Also used : DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) PersistentProperty(org.springframework.data.mapping.PersistentProperty) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 7 with DatastoreDataException

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

the class DatastoreNativeTypes method wrapValue.

/**
 * Wraps Datastore native type to Datastore value type.
 * @param propertyVal the property value to wrap
 * @return the wrapped value
 */
@SuppressWarnings("unchecked")
public static Value wrapValue(Object propertyVal) {
    if (propertyVal == null) {
        return new NullValue();
    }
    Class propertyClass = getTypeForWrappingInDatastoreValue(propertyVal);
    Function wrapper = DatastoreNativeTypes.DATASTORE_TYPE_WRAPPERS.get(propertyClass);
    if (wrapper != null) {
        return (Value) wrapper.apply(propertyVal);
    }
    throw new DatastoreDataException("Unable to convert " + propertyClass + " to Datastore supported type.");
}
Also used : BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) NullValue(com.google.cloud.datastore.NullValue) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) LongValue(com.google.cloud.datastore.LongValue) DoubleValue(com.google.cloud.datastore.DoubleValue) TimestampValue(com.google.cloud.datastore.TimestampValue) BooleanValue(com.google.cloud.datastore.BooleanValue) NullValue(com.google.cloud.datastore.NullValue) KeyValue(com.google.cloud.datastore.KeyValue) EntityValue(com.google.cloud.datastore.EntityValue) BlobValue(com.google.cloud.datastore.BlobValue) LatLngValue(com.google.cloud.datastore.LatLngValue) Value(com.google.cloud.datastore.Value) StringValue(com.google.cloud.datastore.StringValue)

Example 8 with DatastoreDataException

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

the class GqlDatastoreQuery method setGqlResolvedEntityClassName.

private void setGqlResolvedEntityClassName() {
    Matcher matcher = CLASS_NAME_PATTERN.matcher(GqlDatastoreQuery.this.originalGql);
    String result = GqlDatastoreQuery.this.originalGql;
    while (matcher.find()) {
        String matched = matcher.group();
        String className = matched.substring(1, matched.length() - 1);
        try {
            Class entityClass = Class.forName(className);
            DatastorePersistentEntity datastorePersistentEntity = GqlDatastoreQuery.this.datastoreMappingContext.getPersistentEntity(entityClass);
            if (datastorePersistentEntity == null) {
                throw new DatastoreDataException("The class used in the GQL statement is not a Cloud Datastore persistent entity: " + className);
            }
            result = result.replace(matched, datastorePersistentEntity.kindName());
        } catch (ClassNotFoundException ex) {
            throw new DatastoreDataException("The class name does not refer to an available entity type: " + className);
        }
    }
    this.gqlResolvedEntityClassName = result;
}
Also used : Matcher(java.util.regex.Matcher) DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException)

Example 9 with DatastoreDataException

use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException 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)

Example 10 with DatastoreDataException

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

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(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException)

Aggregations

DatastoreDataException (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException)16 Key (com.google.cloud.datastore.Key)5 Value (com.google.cloud.datastore.Value)5 DatastorePersistentEntity (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity)5 DatastorePersistentProperty (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)5 IncompleteKey (com.google.cloud.datastore.IncompleteKey)4 KeyValue (com.google.cloud.datastore.KeyValue)4 Iterator (java.util.Iterator)4 List (java.util.List)4 Function (java.util.function.Function)4 Pageable (org.springframework.data.domain.Pageable)4 Sort (org.springframework.data.domain.Sort)4 BaseEntity (com.google.cloud.datastore.BaseEntity)3 Cursor (com.google.cloud.datastore.Cursor)3 DatastoreReaderWriter (com.google.cloud.datastore.DatastoreReaderWriter)3 EntityQuery (com.google.cloud.datastore.EntityQuery)3 KeyQuery (com.google.cloud.datastore.KeyQuery)3 ListValue (com.google.cloud.datastore.ListValue)3 NullValue (com.google.cloud.datastore.NullValue)3 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)3