FilteredCRUDController.java
/*
* Copyright 2020 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.v2;
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.api.FilteredPage;
import org.genesys.server.api.Pagination;
import org.genesys.server.api.v2.facade.APIFilteredServiceFacade;
import org.genesys.server.exception.InvalidApiUsageException;
import org.genesys.server.exception.SearchException;
import org.genesys.server.service.ShortFilterService;
import org.genesys.server.service.ShortFilterService.FilterInfo;
import org.genesys.server.service.worker.ShortFilterProcessor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
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;
import java.lang.reflect.ParameterizedType;
/**
* 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<DTO, E extends EmptyModel, S extends APIFilteredServiceFacade<DTO, E, F>, F extends EmptyModelFilter<F, E>> extends CRUDController<DTO, E, S> {
public static final String ENDPOINT_FILTER = "/filter";
protected static final int FILTER_GENERIC_INDEX = 3;
@SuppressWarnings("unchecked")
protected final Class<F> filterType = ((Class<F>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[FILTER_GENERIC_INDEX]);
@Autowired
private ShortFilterService shortFilterService;
@Autowired
private ShortFilterProcessor shortFilterProcessor;
/**
* Get normalized filter from filterCode or filter body. Filter by code takes precedence over filter object.
*
* @param filterCode filter code takes precedence and loads an existing filter
* @param filter filter
* @return normalized filter info
*/
protected final FilterInfo<F> processFilter(String filterCode, F filter, Class<F> filterType) throws IOException {
return shortFilterProcessor.processFilter(filterCode, filter, filterType);
}
/**
* Get normalized filter from filterCode or filter body. Filter by code takes precedence over filter object.
*
* @param filterCode filter code takes precedence and loads an existing filter
* @param filter filter
* @param filterType filter type
* @return normalized filter info
*/
protected final F normalizeFilter(F filter, Class<F> filterType) throws IOException {
return shortFilterService.normalizeFilter(filter, filterType);
}
@Override
@Operation(hidden = true) // Hide basic list operation that uses GET
public Page<DTO> list(@ParameterObject Pagination page) {
throw new InvalidApiUsageException("Not applicable");
}
/**
* 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<DTO, F> list(@ParameterObject final Pagination page, @RequestBody(required = false) final F filter) throws SearchException, IOException {
F cleanFilter = 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<>(cleanFilter, serviceFacade.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<DTO, F> filter(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject Pagination page,
@RequestBody(required = false) F filter) throws IOException, SearchException {
var filterInfo = 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, serviceFacade.list(filterInfo.filter, pageable));
}
// /**
// * Override to assist Jackson with type detection
// *
// * @param entity the site
// * @return the recorded record
// */
// @Override
// public DTO create(@RequestBody DTO entity) {
// return super.create(entity);
// }
// /**
// * Override to assist Jackson with type detection
// *
// * @param inserts multiple entities to insert
// * @return the {@link MultiOp} response
// */
// @Override
// public MultiOp<DTO> createMany(@RequestBody final List<DTO> inserts) {
// return super.createMany(inserts);
// }
// /**
// * Override to assist Jackson with type detection
// *
// * @param entity the entity
// * @return the updated record
// */
// @Override
// public DTO update(@RequestBody DTO entity) {
// return super.update(entity);
// }
// /**
// * Override to assist Jackson with type detection
// *
// * @param updates multiple entities with updates
// * @return the {@link MultiOp} response
// */
// @Override
// public MultiOp<DTO> updateMany(@RequestBody final List<DTO> updates) {
// return super.updateMany(updates);
// }
// /**
// * Override to assist Jackson with type detection
// *
// * @param updates multiple entities to insert or update
// * @return the {@link MultiOp} response
// */
// @Override
// public MultiOp<DTO> upsertMany(@RequestBody final List<DTO> updates) {
// return super.upsertMany(updates);
// }
// /**
// * Override to assist Jackson with type detection
// *
// * @param deletes records to delete
// * @return the {@link MultiOp} result
// */
// @Override
// public MultiOp<DTO> removeMany(@RequestBody final List<DTO> deletes) {
// return super.removeMany(deletes);
// }
}