CropController.java
/*
* Copyright 2024 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.impl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.v2.FilteredCRUDController;
import org.genesys.server.api.v2.TranslatedCRUDController;
import org.genesys.server.api.v2.facade.CropApiService;
import org.genesys.server.api.v2.facade.CropLangApiService;
import org.genesys.server.api.v2.model.impl.CropDetailsDTO;
import org.genesys.server.api.v2.model.impl.CropInfo;
import org.genesys.server.api.v2.model.impl.CropLangDTO;
import org.genesys.server.api.v2.model.impl.CropDTO;
import org.genesys.server.api.v2.model.impl.TranslatedCropDTO;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.model.impl.CropLang;
import org.genesys.server.model.impl.Crop;
import org.genesys.server.service.filter.CropFilter;
import org.genesys.server.service.filter.CropLangFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.concurrent.TimeUnit;
@RestController("cropApi2")
@RequestMapping(CropController.CONTROLLER_URL)
@Tag(name = "Crop")
public class CropController extends TranslatedCRUDController<CropDTO, TranslatedCropDTO,
CropLangDTO, Crop, CropLang, CropApiService, CropFilter> {
/** The Constant CONTROLLER_URL. */
public static final String CONTROLLER_URL = ApiBaseController.APIv2_BASE + "/crop";
@Autowired
private TaskExecutor taskExecutor;
@RestController("cropLangApi2")
@RequestMapping(CropController.CropLangController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Crop")
public static class CropLangController extends FilteredCRUDController<CropLangDTO, CropLang, CropLangApiService, CropLangFilter> {
/** The Constant API_URL. */
public static final String API_URL = CropController.CONTROLLER_URL + "/lang";
}
/**
* Get crop /crops/{shortName}
*
* @return
*/
@RequestMapping(value = "/{shortName:^[^\\s\\d].+}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public CropDTO getCrop(@PathVariable("shortName") String shortName) {
LOG.info("Getting crop {}", shortName);
return translatedApiService.getCrop(shortName);
}
/**
* Get crop details /crops/{shortName}/details
*
* @throws InvalidRepositoryPathException
*/
@RequestMapping(value = "/{shortName}/details", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public CropDetailsDTO getCropDetails(@PathVariable("shortName") String shortName) throws InvalidRepositoryPathException {
LOG.info("Getting crop details {}", shortName);
return translatedApiService.getDetails(shortName, LocaleContextHolder.getLocale());
}
/**
* Link accession#cropName with crop
*
* @return
*/
@PreAuthorize("hasRole('ADMINISTRATOR')")
@RequestMapping(value = "{shortName}/relink", params = { "accessions" }, method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
public void relinkAccessions(@PathVariable("shortName") String shortName) {
LOG.info("Linking accessions to crop {}", shortName);
final CropDTO crop = translatedApiService.getCrop(shortName);
if (crop == null)
throw new NotFoundElement("No crop " + shortName);
taskExecutor.execute(() -> {
translatedApiService.unlinkAccessionsForCrop(crop);
translatedApiService.linkAccessionsWithCrop(crop);
});
}
@GetMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Retrieve list of all records", summary = "Get all")
public ResponseEntity<List<CropInfo>> listAll() throws Exception{
StopWatch stopWatch = StopWatch.createStarted();
var crops = translatedApiService.listCropsInfo(LocaleContextHolder.getLocale());
LOG.debug("all crops after {}ms", stopWatch.getTime());
return ResponseEntity.ok().cacheControl(CacheControl.maxAge(5, TimeUnit.MINUTES).cachePublic().noTransform()).varyBy("Accept-Language").body(crops);
}
}