Search in sources :

Example 1 with DatastoreDataException

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

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

the class TwoStepsConversions method convertOnRead.

private <T> T convertOnRead(Object val, EmbeddedType embeddedType, Class targetCollectionType, TypeInformation targetComponentType) {
    if (val == null) {
        return null;
    }
    BiFunction<Object, TypeInformation<?>, ?> readConverter;
    switch(embeddedType) {
        case EMBEDDED_MAP:
            readConverter = (x, typeInformation) -> convertOnReadSingleEmbeddedMap(x, typeInformation.getComponentType().getType(), typeInformation.getMapValueType(), targetComponentType);
            break;
        case EMBEDDED_ENTITY:
            readConverter = this::convertOnReadSingleEmbedded;
            break;
        case NOT_EMBEDDED:
            readConverter = this::convertOnReadSingle;
            break;
        default:
            throw new DatastoreDataException("Unexpected property embedded type: " + embeddedType);
    }
    if (ValueUtil.isCollectionLike(val.getClass()) && targetCollectionType != null && targetComponentType != null) {
        // Convert collection.
        try {
            Assert.isInstanceOf(Iterable.class, val, "Value passed to convertOnRead expected to be Iterable");
            List elements = StreamSupport.stream(((Iterable<?>) val).spliterator(), false).map(v -> {
                Object o = (v instanceof Value) ? ((Value) v).get() : v;
                return readConverter.apply(o, targetComponentType);
            }).collect(Collectors.toList());
            return (T) convertCollection(elements, targetCollectionType);
        } catch (ConversionException | DatastoreDataException ex) {
            throw new DatastoreDataException("Unable process elements of a collection", ex);
        }
    }
    // Convert single value.
    return (T) readConverter.apply(val, targetComponentType);
}
Also used : FullEntity(com.google.cloud.datastore.FullEntity) CustomConversions(org.springframework.data.convert.CustomConversions) BiFunction(java.util.function.BiFunction) ValueUtil(com.google.cloud.spring.data.datastore.core.util.ValueUtil) Builder(com.google.cloud.datastore.FullEntity.Builder) TypeInformation(org.springframework.data.util.TypeInformation) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty) ValueUtil.boxIfNeeded(com.google.cloud.spring.data.datastore.core.util.ValueUtil.boxIfNeeded) EmbeddedType(com.google.cloud.spring.data.datastore.core.mapping.EmbeddedType) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) Map(java.util.Map) DatastoreMappingContext(com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext) StreamSupport(java.util.stream.StreamSupport) Converter(org.springframework.core.convert.converter.Converter) ClassUtils(org.springframework.util.ClassUtils) BaseEntity(com.google.cloud.datastore.BaseEntity) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EntityValue(com.google.cloud.datastore.EntityValue) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) ConversionException(org.springframework.core.convert.ConversionException) IncompleteKey(com.google.cloud.datastore.IncompleteKey) Collectors(java.util.stream.Collectors) PersistentEntity(org.springframework.data.mapping.PersistentEntity) Consumer(java.util.function.Consumer) List(java.util.List) Value(com.google.cloud.datastore.Value) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Optional(java.util.Optional) Blob(com.google.cloud.datastore.Blob) ListValue(com.google.cloud.datastore.ListValue) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Assert(org.springframework.util.Assert) ConversionException(org.springframework.core.convert.ConversionException) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) EntityValue(com.google.cloud.datastore.EntityValue) Value(com.google.cloud.datastore.Value) ListValue(com.google.cloud.datastore.ListValue) ArrayList(java.util.ArrayList) List(java.util.List) TypeInformation(org.springframework.data.util.TypeInformation) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation)

Example 3 with DatastoreDataException

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

the class DatastoreServiceObjectToKeyFactory method getKeyFromId.

@Override
public Key getKeyFromId(Object id, String kindName) {
    Assert.notNull(id, "Cannot get key for null ID value.");
    if (id instanceof Key) {
        return (Key) id;
    }
    KeyFactory keyFactory = getKeyFactory();
    keyFactory.setKind(kindName);
    Key key;
    if (id instanceof String) {
        key = keyFactory.newKey((String) id);
    } else if (id instanceof Long) {
        key = keyFactory.newKey((long) id);
    } else {
        // in the future.
        throw new DatastoreDataException("Keys can only be created using String or long values.");
    }
    return key;
}
Also used : DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey) KeyFactory(com.google.cloud.datastore.KeyFactory)

Example 4 with DatastoreDataException

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

the class DefaultDatastoreEntityConverter method readAsMap.

@Override
public <T, R> Map<T, R> readAsMap(BaseEntity entity, TypeInformation mapTypeInformation) {
    Assert.notNull(mapTypeInformation, "mapTypeInformation can't be null");
    if (entity == null) {
        return null;
    }
    Map<T, R> result;
    if (!mapTypeInformation.getType().isInterface()) {
        try {
            result = (Map<T, R>) ((Constructor<?>) mapTypeInformation.getType().getConstructor()).newInstance();
        } catch (Exception e) {
            throw new DatastoreDataException("Unable to create an instance of a custom map type: " + mapTypeInformation.getType() + " (make sure the class is public and has a public no-args constructor)", e);
        }
    } else {
        result = new HashMap<>();
    }
    EntityPropertyValueProvider propertyValueProvider = new EntityPropertyValueProvider(entity, this.conversions);
    Set<String> fieldNames = entity.getNames();
    for (String field : fieldNames) {
        result.put(this.conversions.convertOnRead(field, NOT_EMBEDDED, mapTypeInformation.getComponentType()), propertyValueProvider.getPropertyValue(field, EmbeddedType.of(mapTypeInformation.getMapValueType()), mapTypeInformation.getMapValueType()));
    }
    return result;
}
Also used : Constructor(java.lang.reflect.Constructor) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)

Example 5 with DatastoreDataException

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

the class DefaultDatastoreEntityConverter method read.

@Override
@SuppressWarnings("unchecked")
public <R> R read(Class<R> clazz, BaseEntity entity) {
    if (entity == null) {
        return null;
    }
    DatastorePersistentEntity<R> ostensiblePersistentEntity = (DatastorePersistentEntity<R>) this.mappingContext.getPersistentEntity(clazz);
    if (ostensiblePersistentEntity == null) {
        throw new DatastoreDataException("Unable to convert Datastore Entity to " + clazz);
    }
    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(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) EntityInstantiator(org.springframework.data.mapping.model.EntityInstantiator) PersistentEntityParameterValueProvider(org.springframework.data.mapping.model.PersistentEntityParameterValueProvider) 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