DATACASS-376 - Polish.

Resolves gh-108.
This commit is contained in:
John Blum
2017-07-06 17:10:55 -07:00
parent add94d2ea6
commit 60eeae9d31
8 changed files with 48 additions and 34 deletions

View File

@@ -15,7 +15,8 @@
*/
package org.springframework.data.cassandra.core.query;
import static org.springframework.util.ObjectUtils.*;
import static org.springframework.util.ObjectUtils.nullSafeEquals;
import static org.springframework.util.ObjectUtils.nullSafeHashCode;
import java.util.ArrayList;
import java.util.Arrays;
@@ -256,8 +257,8 @@ public class Query implements Filter {
* @return a new {@link Query} object containing the former settings with {@code allowFiltering} applied.
*/
public Query withAllowFiltering() {
return new Query(this.criteriaDefinitions, this.columns, this.sort, this.pagingState, this.queryOptions, this.limit,
true);
return new Query(this.criteriaDefinitions, this.columns, this.sort, this.pagingState, this.queryOptions,
this.limit, true);
}
/**

View File

@@ -27,8 +27,8 @@ import java.lang.annotation.Target;
* @author Mark Paluch
* @since 2.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Query(allowFiltering = true)
public @interface AllowFiltering {

View File

@@ -30,8 +30,8 @@ import org.springframework.data.annotation.QueryAnnotation;
* @author Matthew T. Adams
* @author Mark Paluch
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@QueryAnnotation
public @interface Query {
@@ -48,4 +48,5 @@ public @interface Query {
* @since 2.0
*/
boolean allowFiltering() default false;
}

View File

@@ -96,6 +96,7 @@ public class CassandraQueryMethod extends QueryMethod {
public CassandraEntityMetadata<?> getEntityInformation() {
if (entityMetadata == null) {
Class<?> returnedObjectType = getReturnedObjectType();
Class<?> domainClass = getDomainClass();
@@ -104,19 +105,18 @@ public class CassandraQueryMethod extends QueryMethod {
mappingContext.getRequiredPersistentEntity(domainClass));
} else {
CassandraPersistentEntity<?> entity = mappingContext.getPersistentEntity(returnedObjectType);
CassandraPersistentEntity<?> managedEntity = mappingContext.getRequiredPersistentEntity(domainClass);
CassandraPersistentEntity<?> returnedEntity = entity != null && entity.getType().isInterface() ? entity
: managedEntity;
CassandraPersistentEntity<?> returnedEntity =
entity != null && entity.getType().isInterface() ? entity : managedEntity;
// TODO collectionEntity?
CassandraPersistentEntity<?> collectionEntity = domainClass.isAssignableFrom(returnedObjectType)
? returnedEntity : managedEntity;
CassandraPersistentEntity<?> collectionEntity =
domainClass.isAssignableFrom(returnedObjectType) ? returnedEntity : managedEntity;
this.entityMetadata = new SimpleCassandraEntityMetadata<>((Class<Object>) returnedEntity.getType(),
collectionEntity);
this.entityMetadata =
new SimpleCassandraEntityMetadata<>((Class<Object>) returnedEntity.getType(), collectionEntity);
}
}

View File

@@ -97,7 +97,8 @@ public class PartTreeCassandraQuery extends AbstractCassandraQuery {
@Override
protected Statement createQuery(CassandraParameterAccessor parameterAccessor) {
CassandraQueryCreator queryCreator = new CassandraQueryCreator(getTree(), parameterAccessor, getMappingContext());
CassandraQueryCreator queryCreator =
new CassandraQueryCreator(getTree(), parameterAccessor, getMappingContext());
Query query = queryCreator.createQuery();
@@ -107,14 +108,17 @@ public class PartTreeCassandraQuery extends AbstractCassandraQuery {
query = query.limit(getTree().getMaxResults());
}
if (getQueryMethod().getQueryAnnotation().map(q -> q.allowFiltering()).orElse(false)) {
if (getQueryMethod().getQueryAnnotation()
.map(org.springframework.data.cassandra.repository.Query::allowFiltering).orElse(false)) {
query = query.withAllowFiltering();
}
CassandraPersistentEntity<?> persistentEntity = getMappingContext()
.getRequiredPersistentEntity(getQueryMethod().getDomainClass());
CassandraPersistentEntity<?> persistentEntity =
getMappingContext().getRequiredPersistentEntity(getQueryMethod().getDomainClass());
return getStatementFactory().select(query, persistentEntity);
} catch (RuntimeException e) {
throw QueryCreationException.create(getQueryMethod(), e);
}

View File

@@ -97,7 +97,8 @@ public class ReactivePartTreeCassandraQuery extends AbstractReactiveCassandraQue
@Override
protected Statement createQuery(CassandraParameterAccessor parameterAccessor) {
CassandraQueryCreator queryCreator = new CassandraQueryCreator(getTree(), parameterAccessor, getMappingContext());
CassandraQueryCreator queryCreator =
new CassandraQueryCreator(getTree(), parameterAccessor, getMappingContext());
Query query = queryCreator.createQuery();
@@ -107,14 +108,17 @@ public class ReactivePartTreeCassandraQuery extends AbstractReactiveCassandraQue
query = query.limit(getTree().getMaxResults());
}
if (getQueryMethod().getQueryAnnotation().map(q -> q.allowFiltering()).orElse(false)) {
if (getQueryMethod().getQueryAnnotation()
.map(org.springframework.data.cassandra.repository.Query::allowFiltering).orElse(false)) {
query = query.withAllowFiltering();
}
CassandraPersistentEntity<?> persistentEntity = getMappingContext()
.getRequiredPersistentEntity(getQueryMethod().getDomainClass());
CassandraPersistentEntity<?> persistentEntity =
getMappingContext().getRequiredPersistentEntity(getQueryMethod().getDomainClass());
return getStatementFactory().select(query, persistentEntity);
} catch (RuntimeException e) {
throw QueryCreationException.create(getQueryMethod(), e);
}

View File

@@ -15,8 +15,8 @@
*/
package org.springframework.data.cassandra.repository.query;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import java.lang.reflect.Method;
import java.util.Arrays;
@@ -30,6 +30,7 @@ import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.convert.MappingCassandraConverter;
@@ -180,10 +181,11 @@ public class PartTreeCassandraQueryUnitTests {
PartTreeCassandraQuery partTreeQuery = createQueryForMethod(repositoryInterface, method, types);
CassandraParameterAccessor accessor = new CassandraParametersParameterAccessor(partTreeQuery.getQueryMethod(),
args);
CassandraParameterAccessor accessor =
new CassandraParametersParameterAccessor(partTreeQuery.getQueryMethod(), args);
return partTreeQuery.createQuery(new ConvertingParameterAccessor(mockCassandraOperations.getConverter(), accessor));
return partTreeQuery.createQuery(
new ConvertingParameterAccessor(mockCassandraOperations.getConverter(), accessor));
}
private PartTreeCassandraQuery createQueryForMethod(Class<?> repositoryInterface, String methodName,

View File

@@ -15,14 +15,13 @@
*/
package org.springframework.data.cassandra.repository.query;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import java.lang.reflect.Method;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import rx.Single;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Rule;
@@ -31,6 +30,7 @@ import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
import org.springframework.data.cassandra.core.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
@@ -42,6 +42,8 @@ import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import rx.Single;
/**
* Unit tests for {@link ReactivePartTreeCassandraQuery}.
*
@@ -112,11 +114,11 @@ public class ReactivePartTreeCassandraQueryUnitTests {
ReactivePartTreeCassandraQuery partTreeQuery = createQueryForMethod(method, types);
CassandraParameterAccessor accessor = new CassandraParametersParameterAccessor(partTreeQuery.getQueryMethod(),
args);
CassandraParameterAccessor accessor =
new CassandraParametersParameterAccessor(partTreeQuery.getQueryMethod(), args);
return partTreeQuery.createQuery(new ConvertingParameterAccessor(mockCassandraOperations.getConverter(), accessor))
.toString();
return partTreeQuery.createQuery(
new ConvertingParameterAccessor(mockCassandraOperations.getConverter(), accessor)).toString();
}
private ReactivePartTreeCassandraQuery createQueryForMethod(String methodName, Class<?>... paramTypes) {