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.gringlobal.api.v2;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys.blocks.model.EmptyModel;
import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.gringlobal.api.exception.InvalidApiUsageException;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.MultiOp;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.api.v2.facade.APIFilteredServiceFacade;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.service.ShortFilterService.FilterInfo;
import org.springdoc.api.annotations.ParameterObject;
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 io.swagger.v3.oas.annotations.Operation;
/**
* 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")
private Class<F> filterType = ((Class<F>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[FILTER_GENERIC_INDEX]);
@Operation(hidden = true)
@Override
public Page<DTO> list(@ParameterObject final Pagination page) {
throw new InvalidApiUsageException("Not supported");
}
/**
* 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 normalizedFilter = shortFilterService.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, serviceFacade.list(normalizedFilter, 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 final Pagination page,
@RequestBody(required = false) F filter) throws IOException, SearchException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, filterType());
} else {
filterCode = shortFilterService.getCode(filter);
}
FilterInfo<F> filterInfo = shortFilterService.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));
}
/**
* Filter type
*
* @return the class
*/
protected final Class<F> filterType() {
return this.filterType;
}
/**
* 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);
}
}