use of com.google.cloud.datastore.Entity.Builder in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method applyQueryOptions.
public static void applyQueryOptions(StructuredQuery.Builder builder, DatastoreQueryOptions queryOptions, DatastorePersistentEntity<?> persistentEntity) {
if (persistentEntity.getDiscriminationFieldName() != null && persistentEntity.getDiscriminatorValue() != null) {
StructuredQuery.Filter discriminationFilter = PropertyFilter.eq(persistentEntity.getDiscriminationFieldName(), persistentEntity.getDiscriminatorValue());
StructuredQuery.Filter filter = builder.build().getFilter();
if (filter != null) {
discriminationFilter = StructuredQuery.CompositeFilter.and(filter, discriminationFilter);
}
builder.setFilter(discriminationFilter);
}
if (queryOptions == null) {
return;
}
if (queryOptions.getLimit() != null) {
builder.setLimit(queryOptions.getLimit());
}
if (queryOptions.getCursor() == null && queryOptions.getOffset() != null) {
builder.setOffset(queryOptions.getOffset());
}
if (queryOptions.getCursor() != null) {
builder.setStartCursor(queryOptions.getCursor());
}
if (queryOptions.getSort() != null && persistentEntity != null) {
queryOptions.getSort().stream().map((order) -> createOrderBy(persistentEntity, order)).forEachOrdered((orderBy) -> builder.addOrderBy(orderBy));
}
}
use of com.google.cloud.datastore.Entity.Builder in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method convertToEntityForSave.
private List<Entity> convertToEntityForSave(Object entity, Set<Key> persistedEntities, Key... ancestors) {
if (ancestors != null) {
for (Key ancestor : ancestors) {
validateKey(entity, keyToPathElement(ancestor));
}
}
Key key = getKey(entity, true, ancestors);
Builder builder = Entity.newBuilder(key);
List<Entity> entitiesToSave = new ArrayList<>();
this.datastoreEntityConverter.write(entity, builder);
entitiesToSave.addAll(getDescendantEntitiesForSave(entity, key, persistedEntities));
entitiesToSave.addAll(getReferenceEntitiesForSave(entity, builder, persistedEntities));
entitiesToSave.add(builder.build());
return entitiesToSave;
}
use of com.google.cloud.datastore.Entity.Builder in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method exampleToQuery.
private <T> StructuredQuery exampleToQuery(Example<T> example, DatastoreQueryOptions queryOptions, boolean keyQuery) {
validateExample(example);
T probe = example.getProbe();
FullEntity.Builder<IncompleteKey> probeEntityBuilder = Entity.newBuilder();
this.datastoreEntityConverter.write(probe, probeEntityBuilder);
FullEntity<IncompleteKey> probeEntity = probeEntityBuilder.build();
DatastorePersistentEntity<?> persistentEntity = this.datastoreMappingContext.getPersistentEntity(example.getProbeType());
LinkedList<StructuredQuery.Filter> filters = new LinkedList<>();
NullHandler nullHandler = example.getMatcher().getNullHandler();
persistentEntity.doWithColumnBackedProperties((persistentProperty) -> {
if (!ignoredProperty(example, persistentProperty)) {
Value<?> value = getValue(example, probeEntity, persistentEntity, persistentProperty);
addFilter(nullHandler, filters, persistentProperty.getFieldName(), value);
}
});
persistentEntity.doWithAssociations((AssociationHandler<DatastorePersistentProperty>) association -> {
PersistentPropertyAccessor<?> accessor = persistentEntity.getPropertyAccessor(example.getProbe());
DatastorePersistentProperty property = association.getInverse();
Object value = accessor.getProperty(property);
Value<?> key = value == null ? NullValue.of() : KeyValue.of(objectToKeyFactory.getKeyFromObject(value, this.datastoreMappingContext.getPersistentEntity(value.getClass())));
addFilter(nullHandler, filters, property.getFieldName(), key);
});
StructuredQuery.Builder<?> builder = keyQuery ? Query.newKeyQueryBuilder() : Query.newEntityQueryBuilder();
builder.setKind(persistentEntity.kindName());
if (!filters.isEmpty()) {
builder.setFilter(StructuredQuery.CompositeFilter.and(filters.pop(), filters.toArray(new StructuredQuery.Filter[0])));
}
applyQueryOptions(builder, queryOptions, persistentEntity);
return builder.build();
}
use of com.google.cloud.datastore.Entity.Builder in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method writeMap.
@Override
public <V> void writeMap(Key datastoreKey, Map<String, V> map) {
Assert.notNull(datastoreKey, "A non-null Key is required.");
Assert.notNull(map, "A non-null map is required.");
Builder builder = Entity.newBuilder(datastoreKey);
map.forEach((key, value) -> builder.set(key, this.datastoreEntityConverter.getConversions().convertOnWriteSingle(value)));
Entity entity = builder.build();
getDatastoreReadWriter().put(entity);
}
use of com.google.cloud.datastore.Entity.Builder 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;
}
Aggregations