use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by spring-cloud.
the class DefaultDatastoreEntityConverter method read.
@Override
@SuppressWarnings("unchecked")
public <R> R read(Class<R> aClass, BaseEntity entity) {
if (entity == null) {
return null;
}
DatastorePersistentEntity<R> ostensiblePersistentEntity = (DatastorePersistentEntity<R>) this.mappingContext.getPersistentEntity(aClass);
if (ostensiblePersistentEntity == null) {
throw new DatastoreDataException("Unable to convert Datastore Entity to " + aClass);
}
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;
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by spring-cloud.
the class DefaultDatastoreEntityConverter method write.
@Override
@SuppressWarnings("unchecked")
public void write(Object source, BaseEntity.Builder sink) {
DatastorePersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(source.getClass());
String discriminationFieldName = persistentEntity.getDiscriminationFieldName();
List<String> discriminationValues = persistentEntity.getCompatibleDiscriminationValues();
if (!discriminationValues.isEmpty() || discriminationFieldName != null) {
sink.set(discriminationFieldName, discriminationValues.stream().map(StringValue::of).collect(Collectors.toList()));
}
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(source);
persistentEntity.doWithColumnBackedProperties((DatastorePersistentProperty persistentProperty) -> {
// Datastore doesn't store its Key as a regular field.
if (persistentProperty.isIdProperty()) {
return;
}
try {
Object val = accessor.getProperty(persistentProperty);
Value convertedVal = this.conversions.convertOnWrite(val, persistentProperty);
if (persistentProperty.isUnindexed()) {
convertedVal = setExcludeFromIndexes(convertedVal);
}
sink.set(persistentProperty.getFieldName(), convertedVal);
} catch (DatastoreDataException ex) {
throw new DatastoreDataException("Unable to write " + persistentEntity.kindName() + "." + persistentProperty.getFieldName(), ex);
}
});
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by spring-cloud.
the class GqlDatastoreQuery method execute.
@Override
public Object execute(Object[] parameters) {
if (getAnnotation(this.entityType.getSuperclass(), DiscriminatorField.class) != null) {
throw new DatastoreDataException("Can't append discrimination condition");
}
ParsedQueryWithTagsAndValues parsedQueryWithTagsAndValues = new ParsedQueryWithTagsAndValues(this.originalParamTags, parameters);
GqlQuery query = parsedQueryWithTagsAndValues.bindArgsToGqlQuery();
Class returnedItemType = this.queryMethod.getReturnedObjectType();
boolean isNonEntityReturnType = isNonEntityReturnedType(returnedItemType);
DatastoreResultsIterable found = isNonEntityReturnType ? this.datastoreOperations.queryIterable(query, GqlDatastoreQuery::getNonEntityObjectFromRow) : this.datastoreOperations.queryKeysOrEntities(query, this.entityType);
Object result;
if (isPageQuery() || isSliceQuery()) {
result = buildPageOrSlice(parameters, parsedQueryWithTagsAndValues, found);
} else if (this.queryMethod.isCollectionQuery()) {
result = convertCollectionResult(returnedItemType, found);
} else {
result = convertSingularResult(returnedItemType, isNonEntityReturnType, found);
}
return result;
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by spring-cloud.
the class GqlDatastoreQuery method convertSingularResult.
private Object convertSingularResult(Class returnedItemType, boolean isNonEntityReturnType, Iterable rawResult) {
if (rawResult == null) {
return null;
}
Iterator iterator = rawResult.iterator();
if (this.queryMethod.isExistsQuery()) {
return iterator.hasNext();
}
if (this.queryMethod.isCountQuery()) {
return StreamSupport.stream(rawResult.spliterator(), false).count();
}
if (!iterator.hasNext()) {
return null;
}
Object result = iterator.next();
if (iterator.hasNext()) {
throw new DatastoreDataException("The query method returns a singular object but " + "the query returned more than one result.");
}
return isNonEntityReturnType ? this.datastoreOperations.getDatastoreEntityConverter().getConversions().convertOnRead(result, null, returnedItemType) : this.queryMethod.getResultProcessor().processResult(result);
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method findReferenced.
// Extracts key(s) from a property, fetches and if necessary, converts values to the required type
private Object findReferenced(BaseEntity entity, DatastorePersistentProperty referencePersistentProperty, ReadContext context) {
String fieldName = referencePersistentProperty.getFieldName();
try {
Object referenced;
if (referencePersistentProperty.isCollectionLike()) {
referenced = fetchReferenced(referencePersistentProperty, context, valuesToKeys(entity.getList(fieldName)));
} else {
List referencedList = findAllById(Collections.singleton(entity.getKey(fieldName)), referencePersistentProperty.getType(), context);
referenced = referencedList.isEmpty() ? null : referencedList.get(0);
}
return referenced;
} catch (ClassCastException ex) {
throw new DatastoreDataException("Error loading reference property " + fieldName + "." + "Reference properties must be stored as Keys or lists of Keys" + " in Cloud Datastore for singular or multiple references, respectively.");
}
}
Aggregations