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);
}
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);
}
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;
}
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;
}
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;
}
Aggregations