use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by spring-cloud.
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 org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by spring-cloud.
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 org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by spring-cloud.
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 org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method validateKey.
private void validateKey(Object entity, PathElement ancestorPE) {
DatastorePersistentEntity datastorePersistentEntity = this.datastoreMappingContext.getPersistentEntity(entity.getClass());
DatastorePersistentProperty idProp = datastorePersistentEntity.getIdPropertyOrFail();
if (!TypeUtils.isAssignable(BaseKey.class, idProp.getType())) {
throw new DatastoreDataException("Only Key types are allowed for descendants id");
}
Key key = getKey(entity, false);
if (key == null || key.getAncestors().stream().anyMatch((pe) -> pe.equals(ancestorPE))) {
return;
}
throw new DatastoreDataException("Descendant object has a key without current ancestor");
}
use of org.springframework.cloud.gcp.data.datastore.core.mapping.DatastoreDataException in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method computeReferencedField.
private <T> T computeReferencedField(BaseEntity entity, ReadContext context, DatastorePersistentProperty referenceProperty, String fieldName, Class<T> type) {
T referenced;
if (referenceProperty.isLazyLoaded()) {
DatastoreReaderWriter originalTx = getDatastoreReadWriter();
referenced = LazyUtil.wrapSimpleLazyProxy(() -> {
if (getDatastoreReadWriter() != originalTx) {
throw new DatastoreDataException("Lazy load should be invoked within the same transaction");
}
return (T) findReferenced(entity, referenceProperty, context);
}, type, entity.getValue(fieldName));
} else {
referenced = (T) findReferenced(entity, referenceProperty, context);
}
return referenced;
}
Aggregations