use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.
the class DatastoreTemplate method computeReferencedField.
private <T> T computeReferencedField(BaseEntity entity, ReadContext context, DatastorePersistentProperty referenceProperty, String fieldName, Class<T> type) {
T referenced;
if (referenceProperty.isLazyLoaded()) {
DatastoreReaderWriter originalTx = getDatastoreReadWriter();
referenced = LazyUtil.wrapSimpleLazyProxy(() -> {
if (getDatastoreReadWriter() != originalTx) {
throw new DatastoreDataException("Lazy load should be invoked within the same transaction");
}
return (T) findReferenced(entity, referenceProperty, context);
}, type, entity.getValue(fieldName));
} else {
referenced = (T) findReferenced(entity, referenceProperty, context);
}
return referenced;
}
use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.
the class DatastoreTemplate method validateKey.
private void validateKey(Object entity, PathElement ancestorPathElement) {
DatastorePersistentEntity datastorePersistentEntity = getPersistentEntity(entity.getClass());
DatastorePersistentProperty idProp = datastorePersistentEntity.getIdPropertyOrFail();
if (!TypeUtils.isAssignable(BaseKey.class, idProp.getType())) {
throw new DatastoreDataException("Only Key types are allowed for descendants id");
}
Key key = getKey(entity, false);
if (key == null || key.getAncestors().stream().anyMatch(pe -> pe.equals(ancestorPathElement))) {
return;
}
throw new DatastoreDataException("Descendant object has a key without current ancestor");
}
use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.
the class DatastoreTemplate method validateExample.
private <T> void validateExample(Example<T> example) {
Assert.notNull(example, "A non-null example is expected");
ExampleMatcher matcher = example.getMatcher();
if (!matcher.isAllMatching()) {
throw new DatastoreDataException("Unsupported MatchMode. Only MatchMode.ALL is supported");
}
if (matcher.isIgnoreCaseEnabled()) {
throw new DatastoreDataException("Ignore case matching is not supported");
}
if (!(matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.EXACT || matcher.getDefaultStringMatcher() == ExampleMatcher.StringMatcher.DEFAULT)) {
throw new DatastoreDataException("Unsupported StringMatcher. Only EXACT and DEFAULT are supported");
}
Optional<String> path = example.getMatcher().getIgnoredPaths().stream().filter(s -> s.contains(".")).findFirst();
if (path.isPresent()) {
throw new DatastoreDataException("Ignored paths deeper than 1 are not supported");
}
if (matcher.getPropertySpecifiers().hasValues()) {
throw new DatastoreDataException("Property matchers are not supported");
}
}
use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.
the class PartTreeDatastoreQuery method applySelectWithFilter.
private void applySelectWithFilter(Object[] parameters, Builder builder) {
Iterator it = Arrays.asList(parameters).iterator();
Filter[] filters = this.filterParts.stream().map(part -> {
// build properties chain for nested properties
// if the property is not nested, the list would contain only one property
List<DatastorePersistentProperty> propertiesChain = getPropertiesChain(part);
String fieldName = propertiesChain.stream().map(DatastorePersistentProperty::getFieldName).collect(Collectors.joining("."));
if (part.getType() == Part.Type.IS_NULL) {
return PropertyFilter.isNull(fieldName);
}
BiFunction<String, Value, PropertyFilter> filterFactory = FILTER_FACTORIES.get(part.getType());
if (filterFactory == null) {
throw new DatastoreDataException("Unsupported predicate keyword: " + part.getType());
}
if (!it.hasNext()) {
throw new DatastoreDataException("Too few parameters are provided for query method: " + getQueryMethod().getName());
}
Value convertedValue = convertParam(propertiesChain.get(propertiesChain.size() - 1), it.next());
return filterFactory.apply(fieldName, convertedValue);
}).toArray(Filter[]::new);
builder.setFilter((filters.length > 1) ? CompositeFilter.and(filters[0], Arrays.copyOfRange(filters, 1, filters.length)) : filters[0]);
}
Aggregations