Search in sources :

Example 1 with DatastoreDataException

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;
}
Also used : DatastorePersistentEntity(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) EntityInstantiator(org.springframework.data.convert.EntityInstantiator) PersistentEntityParameterValueProvider(org.springframework.data.mapping.model.PersistentEntityParameterValueProvider) DatastorePersistentProperty(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Example 2 with DatastoreDataException

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);
        }
    });
}
Also used : PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) DatastoreDataException(org.springframework.cloud.gcp.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(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)

Example 3 with DatastoreDataException

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;
}
Also used : DiscriminatorField(org.springframework.cloud.gcp.data.datastore.core.mapping.DiscriminatorField) DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) DatastoreResultsIterable(org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable) GqlQuery(com.google.cloud.datastore.GqlQuery)

Example 4 with DatastoreDataException

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

Example 5 with DatastoreDataException

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.");
    }
}
Also used : DatastoreDataException(org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Aggregations

DatastoreDataException (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException)16 Key (com.google.cloud.datastore.Key)5 Value (com.google.cloud.datastore.Value)5 DatastorePersistentEntity (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentEntity)5 DatastorePersistentProperty (org.springframework.cloud.gcp.data.datastore.core.mapping.DatastorePersistentProperty)5 IncompleteKey (com.google.cloud.datastore.IncompleteKey)4 KeyValue (com.google.cloud.datastore.KeyValue)4 Iterator (java.util.Iterator)4 List (java.util.List)4 Function (java.util.function.Function)4 Pageable (org.springframework.data.domain.Pageable)4 Sort (org.springframework.data.domain.Sort)4 BaseEntity (com.google.cloud.datastore.BaseEntity)3 Cursor (com.google.cloud.datastore.Cursor)3 DatastoreReaderWriter (com.google.cloud.datastore.DatastoreReaderWriter)3 EntityQuery (com.google.cloud.datastore.EntityQuery)3 KeyQuery (com.google.cloud.datastore.KeyQuery)3 ListValue (com.google.cloud.datastore.ListValue)3 NullValue (com.google.cloud.datastore.NullValue)3 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)3