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.");
}
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;
}
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;
}
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);
}
}
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.");
}
}
Aggregations