CropController.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.impl;
import com.querydsl.core.types.OrderSpecifier;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.ApiBaseController;
import org.gringlobal.api.FilteredPage;
import org.gringlobal.api.Pagination;
import org.gringlobal.api.model.CropAttachDTO;
import org.gringlobal.api.model.CropDTO;
import org.gringlobal.api.model.TaxonomyCropMapDTO;
import org.gringlobal.api.model.TaxonomySpeciesDTO;
import org.gringlobal.api.v2.CRUDController;
import org.gringlobal.api.v2.FilteredCRUDController;
import org.gringlobal.api.v2.facade.CropApiService;
import org.gringlobal.api.v2.facade.CropAttachmentApiService;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Crop;
import org.gringlobal.model.CropAttach;
import org.gringlobal.model.QCrop;
import org.gringlobal.service.filter.CropFilter;
import org.gringlobal.service.filter.TaxonomySpeciesFilter;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
@RestController("cropApi2")
@RequestMapping(CropController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Crop")
public class CropController extends FilteredCRUDController<CropDTO, Crop, CropApiService, CropFilter> {
/** The Constant API_URL. */
public static final String API_URL = ApiBaseController.APIv2_BASE + "/crop";
@Autowired
private CropAttachmentApiService cropAttachmentApiService;
@Override
protected OrderSpecifier<?>[] defaultSort() {
return new OrderSpecifier[] { QCrop.crop.id.asc() };
}
@RestController("taxonomyCropMapApi2")
@RequestMapping(CropController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "taxonomyCropMap")
public static class TaxonomyCropMapController extends ApiBaseController {
@Autowired
private CropApiService cropApiService;
/**
* Adding taxonomy species to the crop.
*
* @param cropId the crop ID
* @return added records
*/
@PostMapping(value = "/{id}/species", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "addSpecies", description = "Add taxonomy species to the crop")
public List<TaxonomyCropMapDTO> addSpecies(@PathVariable("id") final Long cropId, @RequestBody final List<Long> taxonomySpeciesIds) {
var species = taxonomySpeciesIds.stream().map(TaxonomySpeciesDTO::new).collect(Collectors.toList());
return cropApiService.addSpecies(cropId, species, null);
}
/**
* Removing taxonomy species for the crop.
*
* @param cropId the crop ID
* @param taxonomySpeciesIds the list of taxonomy species ids
* @return removed records
*/
@DeleteMapping(value = "/{id}/species", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "removeSpecies", description = "Remove taxonomy species for the crop")
public List<TaxonomyCropMapDTO> removeSpecies(@PathVariable("id") final Long cropId, @RequestBody final List<Long> taxonomySpeciesIds) {
var species = taxonomySpeciesIds.stream().map(TaxonomySpeciesDTO::new).collect(Collectors.toList());
return cropApiService.removeSpecies(cropId, species);
}
}
/**
* Retrieve crop details by id
*
* @param id the id
* @return the crop details
*/
@GetMapping(value = "/details/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "cropDetails", description = "Retrieve crop details by ID", summary = "Details")
public CropApiService.CropDetails details(@PathVariable("id") final long id) {
return serviceFacade.getCropDetails(id);
}
@Override
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Operation(operationId = "createCrop", description = "Create a Crop", summary = "Add")
public CropDTO create(@RequestBody final CropDTO entity) {
return super.create(entity);
}
@Override
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Operation(operationId = "updateCrop", description = "Update a Crop", summary = "Update")
public CropDTO update(@RequestBody final CropDTO entity) {
return super.update(entity);
}
@Override
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Operation(operationId = "deleteCrop", description = "Delete a Crop by ID", summary = "Delete")
public CropDTO remove(@PathVariable("id") final long id) {
return super.remove(id);
}
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@PostMapping(value = "/attach/{cropId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "uploadFile", description = "Attach crop file", summary = "Attach file")
public CropAttachDTO uploadFile(@PathVariable(name = "cropId") final Long cropId, @RequestPart(name = "file") final MultipartFile file,
@RequestPart(name = "metadata") final CropAttachmentApiService.CropAttachmentRequestDTO metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {
return cropAttachmentApiService.uploadFile(cropId, file, metadata);
}
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@DeleteMapping(value = "/attach/{cropId}/{attachmentId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "removeFile", description = "Remove attached file", summary = "Remove file")
public CropAttachDTO removeFile(@PathVariable(name = "cropId") final Long cropId, @PathVariable(name = "attachmentId") final Long attachmentId) {
return cropAttachmentApiService.removeFile(cropId, attachmentId);
}
@Override
@Operation(operationId = "listCrops", description = "List Crops", summary = "List")
public FilteredPage<CropDTO, CropFilter> list(@ParameterObject final Pagination page, @RequestBody(required = false) final CropFilter filter) throws SearchException, IOException {
return super.list(page, filter);
}
@Override
@Operation(operationId = "filterCrops", description = "Filter Crops", summary = "Filter")
public FilteredPage<CropDTO, CropFilter> filter(@RequestParam(name = "f", required = false) final String filterCode, @ParameterObject final Pagination page,
@RequestBody(required = false) final CropFilter filter) throws IOException, SearchException {
return super.filter(filterCode, page, filter);
}
@PostMapping(value = "/species/{cropId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "listCropSpecies", description = "List species of selected crop", summary = "List species")
public FilteredPage<TaxonomySpeciesDTO, TaxonomySpeciesFilter> listSpecies(@PathVariable("cropId") final long id, @ParameterObject final Pagination page, @RequestBody(required = false) final TaxonomySpeciesFilter filter) throws IOException {
TaxonomySpeciesFilter normalizedFilter = shortFilterService.normalizeFilter(filter, TaxonomySpeciesFilter.class);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
FilteredPage<TaxonomySpeciesDTO, TaxonomySpeciesFilter> results = new FilteredPage<>(normalizedFilter, serviceFacade.listSpecies(id, filter, pageable));
return results;
}
/**
* Auto-complete crops.
*
* @param term the search term
*/
@GetMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
public List<CropDTO> autocompleteCrops(@RequestParam("term") final String term) {
return serviceFacade.autocompleteCrops(term);
}
@RestController("cropAttachApi2")
@RequestMapping(CropAttachController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Crop")
public static class CropAttachController extends CRUDController<CropAttachDTO, CropAttach, CropAttachmentApiService> {
/** The Constant API_URL. */
public static final String API_URL = CropController.API_URL + "/attach/meta";
@Override
@Operation(operationId = "createCropAttach", description = "Create CropAttach", summary = "Create")
public CropAttachDTO create(@RequestBody CropAttachDTO entity) {
// Throws UnsupportedOperationException
return super.create(entity);
}
@Override
@Operation(operationId = "updateCropAttach", description = "Update an existing record", summary = "Update")
public CropAttachDTO update(@RequestBody CropAttachDTO entity) {
return super.update(entity);
}
@Override
@Operation(operationId = "getCropAttach", description = "Get record by ID", summary = "Get")
public CropAttachDTO get(@PathVariable long id) {
return super.get(id);
}
@Override
@Operation(operationId = "deleteCropAttach", description = "Delete existing record by ID", summary = "Delete")
public CropAttachDTO remove(@PathVariable long id) {
return super.remove(id);
}
}
}