use of com.google.cloud.spring.data.firestore.mapping.FirestorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.
the class FirestoreTemplate method buildResourceName.
private <T> String buildResourceName(T entity) {
FirestorePersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(entity.getClass());
if (persistentEntity == null) {
throw new IllegalArgumentException(entity.getClass().toString() + " is not a valid Firestore entity class.");
}
FirestorePersistentProperty idProperty = persistentEntity.getIdPropertyOrFail();
Object idVal = persistentEntity.getPropertyAccessor(entity).getProperty(idProperty);
if (idVal == null) {
idVal = Internal.autoId();
persistentEntity.getPropertyAccessor(entity).setProperty(idProperty, idVal);
}
return buildResourceName(persistentEntity, idVal.toString());
}
use of com.google.cloud.spring.data.firestore.mapping.FirestorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.
the class PartTreeFirestoreQuery method createFirestoreSortOrders.
/**
* This method converts {@link org.springframework.data.domain.Sort.Order} to {@link
* StructuredQuery.Order} for Firestore.
*/
private List<StructuredQuery.Order> createFirestoreSortOrders(Sort sort) {
List<StructuredQuery.Order> sortOrders = new ArrayList<>();
for (Order order : sort) {
if (order.isIgnoreCase()) {
throw new IllegalArgumentException("Datastore does not support ignore case sort orders.");
}
// Get the name of the field to sort on
FirestorePersistentProperty persistentProperty = this.persistentEntity.getPersistentProperty(order.getProperty());
if (persistentProperty == null) {
throw new IllegalArgumentException("Persistent property does not exist: " + order.getProperty());
}
String fieldName = persistentProperty.getFieldName();
StructuredQuery.Direction dir = order.getDirection() == Direction.DESC ? StructuredQuery.Direction.DESCENDING : StructuredQuery.Direction.ASCENDING;
FieldReference ref = FieldReference.newBuilder().setFieldPath(fieldName).build();
com.google.firestore.v1.StructuredQuery.Order firestoreOrder = com.google.firestore.v1.StructuredQuery.Order.newBuilder().setField(ref).setDirection(dir).build();
sortOrders.add(firestoreOrder);
}
return sortOrders;
}
use of com.google.cloud.spring.data.firestore.mapping.FirestorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.
the class FirestoreTemplate method getIdValue.
private Object getIdValue(Object entity) {
FirestorePersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(entity.getClass());
Assert.notNull(persistentEntity, "Persistent entity cannot be null: " + entity.getClass());
FirestorePersistentProperty idProperty = persistentEntity.getIdPropertyOrFail();
return persistentEntity.getPropertyAccessor(entity).getProperty(idProperty);
}
use of com.google.cloud.spring.data.firestore.mapping.FirestorePersistentProperty in project spring-cloud-gcp by GoogleCloudPlatform.
the class FirestoreTemplate method createUpdateWrite.
private <T> Write createUpdateWrite(T entity) {
Builder builder = Write.newBuilder();
if (getIdValue(entity) == null) {
builder.setCurrentDocument(Precondition.newBuilder().setExists(false).build());
}
String resourceName = buildResourceName(entity);
Document document = getClassMapper().entityToDocument(entity, resourceName);
FirestorePersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(entity.getClass());
FirestorePersistentProperty updateTimeProperty = Objects.requireNonNull(persistentEntity).getUpdateTimeProperty();
if (updateTimeProperty != null && Objects.requireNonNull(updateTimeProperty.findAnnotation(UpdateTime.class)).version()) {
Object version = persistentEntity.getPropertyAccessor(entity).getProperty(updateTimeProperty);
if (version != null) {
builder.setCurrentDocument(Precondition.newBuilder().setUpdateTime(((Timestamp) version).toProto()).build());
} else {
// If an entity with an empty update time field is being saved, it must be new.
// Otherwise it will overwrite an existing document.
builder.setCurrentDocument(Precondition.newBuilder().setExists(false).build());
}
}
return builder.setUpdate(document).build();
}
Aggregations