FilteredCRUDController.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.api;
import com.querydsl.core.types.OrderSpecifier;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys.blocks.model.EmptyModel;
import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.genesys.server.service.FilteredCRUDService;
import org.genesys.server.service.ShortFilterService;
import org.genesys.server.exception.SearchException;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;
/**
* A basic API controller for CRUD operations with filtering
*
* @param <E> the entity type
* @param <S> the CRUD service
* @param <F> the generic type
*/
@Validated
public abstract class FilteredCRUDController<E extends EmptyModel, S extends FilteredCRUDService<E, F>, F extends EmptyModelFilter<F, E>> extends CRUDController<E, S> {
public static final String ENDPOINT_LIST = "/list";
public static final String ENDPOINT_FILTER = "/filter";
/**
* Get filtered list of entities.
*
* @param page the page
* @param filter the filter
* @return the page
* @throws SearchException
*/
@PostMapping(value = ENDPOINT_LIST, produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Retrieve list of records matching the filter", summary = "List by filter")
public FilteredPage<E, F> list(@ParameterObject final Pagination page, @RequestBody(required = false) final F filter) throws SearchException, IOException {
F normalizedFilter = shortFilterProcessor.normalizeFilter(filter, filterType());
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return new FilteredPage<>(normalizedFilter, crudService.list(filter, pageable));
}
/**
* Get filtered list of entities by filterCode or filter.
*
* @param page the page
* @param filterCode short filter code
* @param filter the filter
* @return the page
* @throws IOException
* @throws SearchException
*/
@PostMapping(value = ENDPOINT_FILTER, produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Retrieve list of records matching the filter or filter code", summary = "List by filter code or filter")
public FilteredPage<E, F> filter(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
@RequestBody(required = false) final F filter) throws IOException, SearchException {
ShortFilterService.FilterInfo<F> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, filterType());
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, crudService.list(filterInfo.filter, pageable));
}
/**
* Default order specifier for this entity.
*
* @return the order specifier
*/
protected OrderSpecifier<?>[] defaultSort() {
return null;
}
/**
* Filter type
*
* @return the class
*/
protected abstract Class<F> filterType();
/**
* Override {@link CRUDController#create(EmptyModel)} to assist Jackson with type detection
*
* @param entity the site
* @return the recorded record
*/
@Override
public E create(@RequestBody E entity) {
return super.create(entity);
}
/**
* Override {@link CRUDController#update(EmptyModel)} to assist Jackson with type detection
*
* @param entity the site
* @return the updated record
*/
@Override
public E update(@RequestBody E entity) {
return super.update(entity);
}
}