Search in sources :

Example 6 with DatastoreDataException

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);
}
Also used : DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)

Example 7 with DatastoreDataException

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);
        }
    });
}
Also used : PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) EntityValue(com.google.cloud.datastore.EntityValue) Value(com.google.cloud.datastore.Value) StringValue(com.google.cloud.datastore.StringValue) ListValue(com.google.cloud.datastore.ListValue) StringValue(com.google.cloud.datastore.StringValue) DatastorePersistentProperty(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)

Example 8 with DatastoreDataException

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;
}
Also used : Matcher(java.util.regex.Matcher) DatastorePersistentEntity(com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)

Example 9 with DatastoreDataException

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;
}
Also used : DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) BaseEntity(com.google.cloud.datastore.BaseEntity) Key(com.google.cloud.datastore.Key)

Example 10 with DatastoreDataException

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);
}
Also used : DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Iterator(java.util.Iterator)

Aggregations

DatastoreDataException (com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException)19 DatastorePersistentProperty (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentProperty)7 Value (com.google.cloud.datastore.Value)6 DatastorePersistentEntity (com.google.cloud.spring.data.datastore.core.mapping.DatastorePersistentEntity)6 IncompleteKey (com.google.cloud.datastore.IncompleteKey)5 Key (com.google.cloud.datastore.Key)5 List (java.util.List)5 Function (java.util.function.Function)5 BaseEntity (com.google.cloud.datastore.BaseEntity)4 KeyValue (com.google.cloud.datastore.KeyValue)4 DatastoreMappingContext (com.google.cloud.spring.data.datastore.core.mapping.DatastoreMappingContext)4 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 StreamSupport (java.util.stream.StreamSupport)4 Pageable (org.springframework.data.domain.Pageable)4 Sort (org.springframework.data.domain.Sort)4 Cursor (com.google.cloud.datastore.Cursor)3