Skip to content

Commit

Permalink
[Blazebit#622] Add support for EntityViewSetting in Spring
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovanni Lovato committed Aug 27, 2018
1 parent 9d30ff5 commit c0d815c
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.blazebit.persistence.criteria.BlazeCriteriaBuilder;
import com.blazebit.persistence.criteria.BlazeCriteriaQuery;
import com.blazebit.persistence.spring.data.base.query.JpaParameters.JpaParameter;
import com.blazebit.persistence.spring.data.repository.EntityViewSettingProcessor;
import com.blazebit.persistence.view.EntityViewManager;
import com.blazebit.persistence.view.EntityViewSetting;

Expand Down Expand Up @@ -85,13 +86,14 @@ public AbstractPartTreeBlazePersistenceQuery(EntityViewAwareJpaQueryMethod metho

this.parameters = method.getJpaParameters();
String methodName = method.getName();
boolean matchesSpecificationSignature = parameters.hasSpecificationParameter()
&& QUERY_PATTERN.matcher(methodName).matches();
String source = matchesSpecificationSignature ? "" : methodName;
boolean skipMethodNamePredicateMatching = parameters.hasSpecificationParameter()
|| QUERY_PATTERN.matcher(methodName).matches();
String source = skipMethodNamePredicateMatching ? "" : methodName;
this.tree = new PartTree(source, domainClass);

boolean hasEntityViewSettingProcessorParameter = parameters.hasEntityViewSettingProcessorParameter();
boolean recreateQueries = parameters.potentiallySortsDynamically() || entityViewClass != null
|| matchesSpecificationSignature;
|| skipMethodNamePredicateMatching || hasEntityViewSettingProcessorParameter;
this.query = isCountProjection(tree) ? new AbstractPartTreeBlazePersistenceQuery.CountQueryPreparer(persistenceProvider,
recreateQueries) : new AbstractPartTreeBlazePersistenceQuery.QueryPreparer(persistenceProvider, recreateQueries);
}
Expand Down Expand Up @@ -310,7 +312,15 @@ Query createPaginatedQuery(Object[] values, boolean withCount) {
return binder.bind(jpaQuery);
}

protected void processSetting(EntityViewSetting<?, ?> setting, Object[] values) {
@SuppressWarnings("unchecked")
protected <T> void processSetting(EntityViewSetting<T, ?> setting, Object[] values) {
int entityViewSettingProcessorIndex = parameters.getEntityViewSettingProcessorIndex();
if (entityViewSettingProcessorIndex >= 0) {
EntityViewSettingProcessor<T> processor = (EntityViewSettingProcessor<T>) values[entityViewSettingProcessorIndex];
if (processor != null) {
setting = processor.acceptEntityViewSetting(setting);
}
}
for (JpaParameter parameter : parameters.getOptionalParameters()) {
String parameterName = parameter.getParameterName();
Object parameterValue = values[parameter.getIndex()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
import org.springframework.core.MethodParameter;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Temporal;

import com.blazebit.persistence.spring.data.annotation.OptionalParam;
import com.blazebit.persistence.spring.data.base.query.JpaParameters.JpaParameter;
import com.blazebit.persistence.spring.data.repository.EntityViewSettingProcessor;

import org.springframework.data.repository.query.Param;
import org.springframework.data.repository.query.Parameter;
import org.springframework.data.repository.query.Parameters;
Expand Down Expand Up @@ -144,6 +149,35 @@ public boolean hasSpecificationParameter() {
return getSpecificationIndex() >= 0;
}

/**
* Returns the index of the {@link EntityViewSettingProcessor} {@link Method} parameter if available. Will return
* {@literal -1} if there is no {@link EntityViewSettingProcessor} parameter in the {@link Method}'s parameter list.
*
* @return the index of the processor parameter, or -1 if not present
*/
public int getEntityViewSettingProcessorIndex() {
int index = 0;

for (JpaParameter candidate : this) {
if (candidate.isEntityViewSettingProcessorParameter()) {
return index;
}
++index;
}

return -1;
}

/**
* Returns whether the method the {@link Parameters} was created for contains a {@link EntityViewSettingProcessor}
* parameter.
*
* @return true if the methods has a processor parameter
*/
public boolean hasEntityViewSettingProcessorParameter() {
return getEntityViewSettingProcessorIndex() >= 0;
}

/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.Parameters#createParameter(org.springframework.core.MethodParameter)
Expand Down Expand Up @@ -214,7 +248,8 @@ public boolean isBindable() {

@Override
public boolean isSpecialParameter() {
return super.isSpecialParameter() || isOptionalParameter() || isSpecificationParameter();
return super.isSpecialParameter() || isOptionalParameter() || isSpecificationParameter()
|| isEntityViewSettingProcessorParameter();
}

boolean isOptionalParameter() {
Expand All @@ -225,6 +260,10 @@ boolean isSpecificationParameter() {
return Specification.class.isAssignableFrom(parameter.getParameterType());
}

boolean isEntityViewSettingProcessorParameter() {
return EntityViewSettingProcessor.class.isAssignableFrom(parameter.getParameterType());
}

/**
* @return {@literal true} if this parameter is of type {@link Date} and has an {@link Temporal} annotation.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2014 - 2018 Blazebit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blazebit.persistence.spring.data.repository;

import com.blazebit.persistence.view.EntityViewSetting;

/**
* @author Giovanni Lovato
* @since 1.3.0
*/
public interface EntityViewSettingProcessor<T> {

/**
* Processes the {@link EntityViewSetting} to allow additional Entity View customization during query creation.
*
* @param setting the {@link EntityViewSetting} to be processed
* @return the final {@link EntityViewSetting} to allow further processing
*/
EntityViewSetting<T, ?> acceptEntityViewSetting(EntityViewSetting<T, ?> setting);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
import com.blazebit.persistence.spring.data.testsuite.tx.TransactionalWorkService;
import com.blazebit.persistence.spring.data.testsuite.tx.TxWork;
import com.blazebit.persistence.spring.data.testsuite.view.DocumentView;
import com.blazebit.persistence.spring.data.repository.EntityViewSettingProcessor;
import com.blazebit.persistence.spring.data.repository.KeysetAwarePage;
import com.blazebit.persistence.spring.data.repository.KeysetPageRequest;
import com.blazebit.persistence.testsuite.base.jpa.category.NoDatanucleus;
import com.blazebit.persistence.testsuite.base.jpa.category.NoEclipselink;
import com.blazebit.persistence.testsuite.base.jpa.category.NoHibernate42;
import com.blazebit.persistence.view.EntityViewSetting;

import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -564,6 +567,28 @@ public Predicate toPredicate(Root<Document> root, CriteriaQuery<?> criteriaQuery
assertEquals(actual.get(0).getOptionalParameter(), param);
}

@Test
public void testEntityViewSettingProcessorParameter() {
// Given
String name = "D1";
String param = "Foo";
createDocument(name);

// When
List<DocumentView> actual = documentRepository.findAll(new EntityViewSettingProcessor<DocumentView>() {

@Override
public EntityViewSetting<DocumentView, ?> acceptEntityViewSetting(EntityViewSetting<DocumentView, ?> setting) {
setting.addOptionalParameter("optionalParameter", "Foo");
return setting;
}
});

// Then
assertEquals(1, actual.size());
assertEquals(actual.get(0).getOptionalParameter(), param);
}

private List<Long> getIdsFromViews(Iterable<DocumentAccessor> views) {
List<Long> ids = new ArrayList<>();
for (DocumentAccessor view : views) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.blazebit.persistence.spring.data.annotation.OptionalParam;
import com.blazebit.persistence.spring.data.repository.EntityViewRepository;
import com.blazebit.persistence.spring.data.repository.EntityViewSettingProcessor;
import com.blazebit.persistence.spring.data.repository.EntityViewSpecificationExecutor;
import com.blazebit.persistence.spring.data.repository.KeysetAwarePage;
import com.blazebit.persistence.spring.data.testsuite.entity.Document;
Expand Down Expand Up @@ -78,4 +79,6 @@ public interface DocumentRepository<T> extends EntityViewRepository<T, Long>, En
Page<DocumentView> findByNameOrderById(String name, Pageable pageable, @OptionalParam("optionalParameter") String optionalParameter);

List<DocumentView> findAll(Specification<Document> specification, @OptionalParam("optionalParameter") String optionalParameter);

List<DocumentView> findAll(EntityViewSettingProcessor<DocumentView> processor);
}

0 comments on commit c0d815c

Please sign in to comment.