FilteredCRUDServiceImpl.java

/*
 * Copyright 2021 Global Crop Diversity Trust
 *
 * 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 org.genesys.server.service.impl;

import java.lang.reflect.ParameterizedType;

import org.genesys.blocks.model.EmptyModel;
import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.genesys.server.exception.SearchException;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.FilteredCRUDService;
import org.genesys.server.service.filter.IFullTextFilter;
import org.genesys.spring.persistence.ExtendedJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

/**
 * The basic FilteredCRUDServiceImpl.
 *
 * @param <E> the model type
 * @param <F> the model filter type
 * @param <R> the repository type
 */
@Transactional(readOnly = true)
@Validated
public abstract class FilteredCRUDServiceImpl<E extends EmptyModel, F extends EmptyModelFilter<F, E>, R extends JpaRepository<E, Long> & QuerydslPredicateExecutor<E>> extends
	CRUDServiceImpl<E, R> implements FilteredCRUDService<E, F> {

	@Autowired(required = false)
	protected ElasticsearchService elasticsearchService;

	@SuppressWarnings("unchecked")
	protected final Class<E> targetType = ((Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]);

	@Override
	public Page<E> list(F filter, Pageable page) throws SearchException {
		return list(targetType, filter, page);
	}

	protected Page<E> list(Class<E> clazz, F filter, Pageable page, String... boostFields) throws SearchException {
		if (filter instanceof IFullTextFilter) {
			IFullTextFilter ftf = (IFullTextFilter) filter;
			// if (ftf.isFulltextQuery() && elasticsearchService != null) { // This could be quietly ignoring full-test search
			if (ftf.isFulltextQuery()) {
				return elasticsearchService.findAll(clazz, filter, null, page, (entityIds) -> list(entityIds), boostFields);
			}
		}

		BooleanBuilder predicate = new BooleanBuilder();
		if (filter != null) {
			predicate.and(filter.buildPredicate());
		}

		if (repository instanceof ExtendedJpaRepository && entityListQuery() != null) {
			JPAQuery<E> query = entityListQuery().where(predicate);
			return ((ExtendedJpaRepository<E, Long>)repository).findAll(query, page);
		} else {
			// default implementation without custom loading
			return repository.findAll(predicate, page);
		}
	}

}