use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method getDescendantEntitiesForSave.
private List<Entity> getDescendantEntitiesForSave(Object entity, Key key, Set<Key> persistedEntities) {
DatastorePersistentEntity datastorePersistentEntity = this.datastoreMappingContext.getPersistentEntity(entity.getClass());
List<Entity> entitiesToSave = new ArrayList<>();
datastorePersistentEntity.doWithDescendantProperties((PersistentProperty 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 org.springframework.data.mapping.PersistentPropertyAccessor in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method getReferenceEntitiesForSave.
private List<Entity> getReferenceEntitiesForSave(Object entity, Builder builder, Set<Key> persistedEntities) {
DatastorePersistentEntity datastorePersistentEntity = this.datastoreMappingContext.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 org.springframework.data.mapping.PersistentPropertyAccessor in project spring-cloud-gcp by spring-cloud.
the class DatastorePersistentEntityImplTests method testIgnoredProperty.
@Test
public void testIgnoredProperty() {
TestEntity t = new TestEntity();
t.id = "a";
t.something = "a";
t.notMapped = "b";
DatastorePersistentEntity p = new DatastoreMappingContext().getPersistentEntity(TestEntity.class);
PersistentPropertyAccessor accessor = p.getPropertyAccessor(t);
p.doWithProperties((SimplePropertyHandler) (property) -> assertThat(accessor.getProperty(property)).isNotEqualTo("b"));
}
use of org.springframework.data.mapping.PersistentPropertyAccessor in project spring-cloud-gcp by spring-cloud.
the class ConverterAwareMappingSpannerEntityWriter method write.
/**
* Writes an object's properties to the sink.
* @param source the object to write
* @param sink the sink to which to write
* @param includeColumns the columns to write. If null, then all columns are written.
*/
public void write(Object source, MultipleValueBinder sink, Set<String> includeColumns) {
boolean writeAllColumns = includeColumns == null;
SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(source.getClass());
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(source);
persistentEntity.doWithColumnBackedProperties((spannerPersistentProperty) -> {
if (spannerPersistentProperty.isEmbedded()) {
Object embeddedObject = accessor.getProperty(spannerPersistentProperty);
if (embeddedObject != null) {
write(embeddedObject, sink, includeColumns);
}
} else if (writeAllColumns || includeColumns.contains(spannerPersistentProperty.getColumnName())) {
writeProperty(sink, accessor, spannerPersistentProperty);
}
});
}
Aggregations