Search in sources :

Example 1 with FirestorePersistentProperty

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());
}
Also used : FirestorePersistentProperty(com.google.cloud.spring.data.firestore.mapping.FirestorePersistentProperty)

Example 2 with FirestorePersistentProperty

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;
}
Also used : Order(org.springframework.data.domain.Sort.Order) StructuredQuery(com.google.firestore.v1.StructuredQuery) FieldReference(com.google.firestore.v1.StructuredQuery.FieldReference) FirestorePersistentProperty(com.google.cloud.spring.data.firestore.mapping.FirestorePersistentProperty) ArrayList(java.util.ArrayList)

Example 3 with FirestorePersistentProperty

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);
}
Also used : FirestorePersistentProperty(com.google.cloud.spring.data.firestore.mapping.FirestorePersistentProperty)

Example 4 with FirestorePersistentProperty

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();
}
Also used : FirestorePersistentProperty(com.google.cloud.spring.data.firestore.mapping.FirestorePersistentProperty) Builder(com.google.firestore.v1.Write.Builder) UpdateTime(com.google.cloud.spring.data.firestore.mapping.UpdateTime) Document(com.google.firestore.v1.Document)

Aggregations

FirestorePersistentProperty (com.google.cloud.spring.data.firestore.mapping.FirestorePersistentProperty)4 UpdateTime (com.google.cloud.spring.data.firestore.mapping.UpdateTime)1 Document (com.google.firestore.v1.Document)1 StructuredQuery (com.google.firestore.v1.StructuredQuery)1 FieldReference (com.google.firestore.v1.StructuredQuery.FieldReference)1 Builder (com.google.firestore.v1.Write.Builder)1 ArrayList (java.util.ArrayList)1 Order (org.springframework.data.domain.Sort.Order)1