Search in sources :

Example 1 with PersistentPropertyTranslator

use of org.springframework.data.cassandra.core.mapping.PersistentPropertyTranslator in project spring-data-cassandra by spring-projects.

the class StatementFactory method computeColumnsForProjection.

/**
 * Compute the {@link Columns} to include type if the {@code returnType} is a {@literal DTO projection} or a
 * {@literal closed interface projection}.
 *
 * @param columns must not be {@literal null}.
 * @param domainType must not be {@literal null}.
 * @param returnType must not be {@literal null}.
 * @return {@link Columns} with columns to be included.
 * @since 2.2
 */
Columns computeColumnsForProjection(EntityProjection<?, ?> projection, Columns columns, CassandraPersistentEntity<?> domainType, Class<?> returnType) {
    if (!columns.isEmpty() || ClassUtils.isAssignable(domainType.getType(), returnType)) {
        return columns;
    }
    if (projection.getMappedType().getType().isInterface()) {
        projection.forEach(propertyPath -> columns.include(propertyPath.getPropertyPath().getSegment()));
    } else {
        // DTO projections use merged metadata between domain type and result type
        PersistentPropertyTranslator translator = PersistentPropertyTranslator.create(domainType, Predicates.negate(CassandraPersistentProperty::hasExplicitColumnName));
        CassandraPersistentEntity<?> persistentEntity = getQueryMapper().getConverter().getMappingContext().getRequiredPersistentEntity(projection.getMappedType());
        for (CassandraPersistentProperty property : persistentEntity) {
            columns.include(translator.translate(property).getColumnName());
        }
    }
    Columns projectedColumns = Columns.empty();
    if (returnType.isInterface()) {
        ProjectionInformation projectionInformation = cassandraConverter.getProjectionFactory().getProjectionInformation(returnType);
        if (projectionInformation.isClosed()) {
            for (PropertyDescriptor inputProperty : projectionInformation.getInputProperties()) {
                projectedColumns = projectedColumns.include(inputProperty.getName());
            }
        }
    } else {
        for (PersistentProperty<?> property : domainType) {
            projectedColumns = projectedColumns.include(property.getName());
        }
    }
    return projectedColumns;
}
Also used : ProjectionInformation(org.springframework.data.projection.ProjectionInformation) PropertyDescriptor(java.beans.PropertyDescriptor) Columns(org.springframework.data.cassandra.core.query.Columns) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) PersistentPropertyTranslator(org.springframework.data.cassandra.core.mapping.PersistentPropertyTranslator)

Aggregations

PropertyDescriptor (java.beans.PropertyDescriptor)1 CassandraPersistentProperty (org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty)1 PersistentPropertyTranslator (org.springframework.data.cassandra.core.mapping.PersistentPropertyTranslator)1 Columns (org.springframework.data.cassandra.core.query.Columns)1 ProjectionInformation (org.springframework.data.projection.ProjectionInformation)1