Search in sources :

Example 11 with DatastoreDataException

use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.

the class DatastoreNativeTypes method wrapValue.

/**
 * Wraps Datastore native type to Datastore value type.
 *
 * @param propertyVal the property value to wrap
 * @return the wrapped value
 */
@SuppressWarnings("unchecked")
public static Value wrapValue(Object propertyVal) {
    if (propertyVal == null) {
        return new NullValue();
    }
    Class propertyClass = getTypeForWrappingInDatastoreValue(propertyVal);
    Function wrapper = DatastoreNativeTypes.DATASTORE_TYPE_WRAPPERS.get(propertyClass);
    if (wrapper != null) {
        return (Value) wrapper.apply(propertyVal);
    }
    throw new DatastoreDataException("Unable to convert " + propertyClass + " to Datastore supported type.");
}
Also used : BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) NullValue(com.google.cloud.datastore.NullValue) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) LongValue(com.google.cloud.datastore.LongValue) DoubleValue(com.google.cloud.datastore.DoubleValue) TimestampValue(com.google.cloud.datastore.TimestampValue) BooleanValue(com.google.cloud.datastore.BooleanValue) NullValue(com.google.cloud.datastore.NullValue) KeyValue(com.google.cloud.datastore.KeyValue) EntityValue(com.google.cloud.datastore.EntityValue) BlobValue(com.google.cloud.datastore.BlobValue) LatLngValue(com.google.cloud.datastore.LatLngValue) Value(com.google.cloud.datastore.Value) StringValue(com.google.cloud.datastore.StringValue)

Example 12 with DatastoreDataException

use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.

the class DatastoreServiceObjectToKeyFactory method allocateKeyForObject.

@Override
public Key allocateKeyForObject(Object entity, DatastorePersistentEntity datastorePersistentEntity, Key... ancestors) {
    Assert.notNull(entity, "Cannot get key for null entity object.");
    Assert.notNull(datastorePersistentEntity, "Persistent entity must not be null.");
    PersistentProperty idProp = datastorePersistentEntity.getIdPropertyOrFail();
    Class idPropType = idProp.getType();
    if (!idPropType.equals(Key.class) && !idPropType.equals(Long.class)) {
        throw new DatastoreDataException("Cloud Datastore can only allocate IDs for Long and Key properties. " + "Cannot allocate for type: " + idPropType);
    }
    KeyFactory keyFactory = getKeyFactory().setKind(datastorePersistentEntity.kindName());
    if (ancestors != null && ancestors.length > 0) {
        if (!idPropType.equals(Key.class)) {
            throw new DatastoreDataException("Only Key types are allowed for descendants id");
        }
        for (Key ancestor : ancestors) {
            keyFactory.addAncestor(DatastoreTemplate.keyToPathElement(ancestor));
        }
    }
    Key allocatedKey = this.datastore.get().allocateId(keyFactory.newKey());
    Object value;
    if (idPropType.equals(Key.class)) {
        value = allocatedKey;
    } else if (idPropType.equals(Long.class)) {
        value = allocatedKey.getId();
    } else {
        value = allocatedKey.getId().toString();
    }
    datastorePersistentEntity.getPropertyAccessor(entity).setProperty(idProp, value);
    return allocatedKey;
}
Also used : DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) PersistentProperty(org.springframework.data.mapping.PersistentProperty) KeyFactory(com.google.cloud.datastore.KeyFactory) Key(com.google.cloud.datastore.Key) IncompleteKey(com.google.cloud.datastore.IncompleteKey)

Example 13 with DatastoreDataException

use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.

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() || this.queryMethod.isStreamQuery()) {
        result = convertCollectionResult(returnedItemType, found);
    } else {
        result = convertSingularResult(returnedItemType, isNonEntityReturnType, found);
    }
    return result;
}
Also used : DiscriminatorField(com.google.cloud.spring.data.datastore.core.mapping.DiscriminatorField) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) DatastoreResultsIterable(com.google.cloud.spring.data.datastore.core.DatastoreResultsIterable) GqlQuery(com.google.cloud.datastore.GqlQuery)

Example 14 with DatastoreDataException

use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.

the class GqlDatastoreQuery method setOriginalParamTags.

private void setOriginalParamTags() {
    this.originalParamTags = new ArrayList<>();
    Set<String> seen = new HashSet<>();
    Parameters parameters = getQueryMethod().getParameters();
    for (int i = 0; i < parameters.getNumberOfParameters(); i++) {
        Parameter param = parameters.getParameter(i);
        if (Pageable.class.isAssignableFrom(param.getType()) || Sort.class.isAssignableFrom(param.getType())) {
            continue;
        }
        Optional<String> paramName = param.getName();
        if (!paramName.isPresent()) {
            throw new DatastoreDataException("Query method has a parameter without a valid name: " + getQueryMethod().getName());
        }
        String name = paramName.get();
        if (seen.contains(name)) {
            throw new DatastoreDataException("More than one param has the same name: " + name);
        }
        seen.add(name);
        this.originalParamTags.add(name);
    }
}
Also used : Parameters(org.springframework.data.repository.query.Parameters) Pageable(org.springframework.data.domain.Pageable) DatastoreDataException(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) Parameter(org.springframework.data.repository.query.Parameter) Sort(org.springframework.data.domain.Sort) HashSet(java.util.HashSet)

Example 15 with DatastoreDataException

use of com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by GoogleCloudPlatform.

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(com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

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