CodeValuesController.java
/*
* Copyright 2026 Global Crop Diversity Trust
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root folder or http://www.apache.org/licenses/LICENSE-2.0
*/
package org.gringlobal.api.v2.impl;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.gringlobal.api.ApiBaseController;
import org.gringlobal.api.model.CodeValueDTO;
import org.gringlobal.api.model.CodeValueLangDTO;
import org.gringlobal.api.model.TranslatedCodeValueDTO;
import org.gringlobal.api.v2.TranslatedCRUDController;
import org.gringlobal.api.v2.facade.CodeValueApiService;
import org.gringlobal.model.CodeValue;
import org.gringlobal.model.CodeValueLang;
import org.gringlobal.service.filter.CodeValueFilter;
import org.gringlobal.service.impl.CodeValueServiceImpl;
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.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
@RestController("codeValuesApi2")
@RequestMapping(CodeValuesController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "CodeValues")
public class CodeValuesController extends TranslatedCRUDController<CodeValueDTO, TranslatedCodeValueDTO,
CodeValueLangDTO, CodeValue, CodeValueLang, CodeValueApiService, CodeValueFilter> {
/** The Constant API_URL. */
public static final String API_URL = ApiBaseController.APIv2_BASE + "/codes";
/**
* Replace an existing value with another value
*/
@PostMapping(value = "/replace", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "replace", description = "Replace and remove a code value", summary = "Replace")
public CodeValueDTO replaceAndRemove(@RequestBody @Valid @NotNull final ReplaceAndRemove replaceArgs) {
return translatedApiService.replaceAndRemove(replaceArgs.current, replaceArgs.replacement);
}
/**
* Delete given CodeValue
*/
@DeleteMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "deleteGiven", description = "Delete given CodeValue", summary = "Delete given")
public CodeValueDTO remove(@RequestBody @Valid @NotNull final CodeValueDTO target) {
return translatedApiService.remove(target);
}
/**
* Get CodeValue statistics list
*/
@GetMapping(value = "/{groupName}/stats", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "stats", description = "Get current usage numbers for this code value", summary = "Statistics")
public List<CodeValueServiceImpl.CodeValueStats> getStatistics(@PathVariable String groupName) {
return translatedApiService.getStatistics(groupName);
}
public static class ReplaceAndRemove {
@NotNull public CodeValueDTO current;
@NotNull public CodeValueDTO replacement;
}
}