use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.
the class DatastoreTemplate method getReferenceEntitiesForSave.
private List<Entity> getReferenceEntitiesForSave(Object entity, Builder builder, Set<Key> persistedEntities) {
DatastorePersistentEntity<?> datastorePersistentEntity = 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;
}
use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty 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.DatastorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.
the class DatastoreTemplate method getDescendantEntitiesForSave.
private List<Entity> getDescendantEntitiesForSave(Object entity, Key key, Set<Key> persistedEntities) {
DatastorePersistentEntity<?> datastorePersistentEntity = getPersistentEntity(entity.getClass());
List<Entity> entitiesToSave = new ArrayList<>();
datastorePersistentEntity.doWithDescendantProperties((DatastorePersistentProperty persistentProperty) -> {
// Convert and write descendants, applying ancestor from parent entry
PersistentPropertyAccessor accessor = datastorePersistentEntity.getPropertyAccessor(entity);
Object val = accessor.getProperty(persistentProperty);
if (val != null) {
// we can be sure that the property is an array or an iterable,
// because we check it in isDescendant
entitiesToSave.addAll(getEntitiesForSave((Iterable<?>) ValueUtil.toListIfArray(val), persistedEntities, key));
}
});
return entitiesToSave;
}
use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.
the class EntityPropertyValueProviderTests method testException.
@Test
void testException() {
Entity entity = Entity.newBuilder(this.datastore.newKeyFactory().setKind("aKind").newKey("1")).set("boolField", 123L).build();
EntityPropertyValueProvider provider = new EntityPropertyValueProvider(entity, this.twoStepsConversion);
DatastorePersistentProperty testDpe = this.persistentEntity.getPersistentProperty("boolField");
assertThatThrownBy(() -> provider.getPropertyValue(testDpe)).isInstanceOf(DatastoreDataException.class).hasMessage("Unable to read property boolField; nested exception is " + "com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException: " + "Unable to convert class java.lang.Long to class java.lang.Boolean");
}
use of com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.
the class PartTreeDatastoreQuery method mapToFieldName.
private String mapToFieldName(PropertyDescriptor propertyDescriptor) {
String name = propertyDescriptor.getName();
DatastorePersistentProperty persistentProperty = (DatastorePersistentProperty) this.datastorePersistentEntity.getPersistentProperty(name);
Assert.notNull(persistentProperty, "Property '" + name + "' must exist in entity '" + this.datastorePersistentEntity.getName() + "'.");
return persistentProperty.getFieldName();
}
Aggregations