use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.
the class DefaultDatastoreEntityConverter method getDiscriminationPersistentEntity.
public <T> DatastorePersistentEntity<T> getDiscriminationPersistentEntity(Class<T> entityClass, BaseEntity<?> entity) {
DatastorePersistentEntity ostensiblePersistentEntity = this.mappingContext.getPersistentEntity(entityClass);
if (ostensiblePersistentEntity == null) {
throw new DatastoreDataException("Unable to convert Datastore Entity to " + entityClass);
}
EntityPropertyValueProvider propertyValueProvider = new EntityPropertyValueProvider(entity, this.conversions);
return getDiscriminationPersistentEntity(ostensiblePersistentEntity, propertyValueProvider);
}
use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.
the class DefaultDatastoreEntityConverter method write.
@Override
@SuppressWarnings("unchecked")
public void write(Object source, BaseEntity.Builder sink) {
DatastorePersistentEntity<?> persistentEntity = this.mappingContext.getDatastorePersistentEntity(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 com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.
the class GqlDatastoreQuery method setGqlResolvedEntityClassName.
private void setGqlResolvedEntityClassName() {
Matcher matcher = CLASS_NAME_PATTERN.matcher(GqlDatastoreQuery.this.originalGql);
String result = GqlDatastoreQuery.this.originalGql;
while (matcher.find()) {
String matched = matcher.group();
String className = matched.substring(1, matched.length() - 1);
try {
Class entityClass = Class.forName(className);
DatastorePersistentEntity datastorePersistentEntity = GqlDatastoreQuery.this.datastoreMappingContext.getPersistentEntity(entityClass);
if (datastorePersistentEntity == null) {
throw new DatastoreDataException("The class used in the GQL statement is not a Cloud Datastore persistent entity: " + className);
}
result = result.replace(matched, datastorePersistentEntity.kindName());
} catch (ClassNotFoundException ex) {
throw new DatastoreDataException("The class name does not refer to an available entity type: " + className);
}
}
this.gqlResolvedEntityClassName = result;
}
use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.
the class GqlDatastoreQuery method getNonEntityObjectFromRow.
private static Object getNonEntityObjectFromRow(Object x) {
Object mappedResult;
if (x instanceof Key) {
mappedResult = x;
} else {
BaseEntity entity = (BaseEntity) x;
Set<String> colNames = entity.getNames();
if (colNames.size() > 1) {
throw new DatastoreDataException("The query method returns non-entity types, but the query result has " + "more than one column. Use a Projection entity type instead.");
}
mappedResult = entity.getValue((String) colNames.toArray()[0]).get();
}
return mappedResult;
}
use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.
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);
}
Aggregations