VocabularyController.java

/*
 * Copyright 2025 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 com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
import org.genesys.blocks.model.JsonViews;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.FilteredPage;
import org.genesys.server.api.Pagination;
import org.genesys.server.api.v2.facade.VocabularyApiService;
import org.genesys.server.api.v2.model.impl.VocabularyTermDTO;
import org.genesys.server.api.v2.model.vocab.ControlledVocabularyDTO;
import org.genesys.server.model.filters.ControlledVocabularyFilter;
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.Sort;
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.RestController;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

@RestController("vocabularyApi2")
@RequestMapping(VocabularyController.CONTROLLER_URL)
@Api(tags = { "vocabulary" })
public class VocabularyController extends ApiBaseController {

	/** The Constant CONTROLLER_URL. */
	public static final String CONTROLLER_URL = ApiBaseController.APIv2_BASE + "/vocabulary";

	@Autowired
	private VocabularyApiService vocabularyService;

	/** The short filter service. */
	@Autowired
	protected ShortFilterProcessor shortFilterProcessor;

	/**
	 * Gets the.
	 *
	 * @param uuid the uuid
	 * @return the controlled vocabulary
	 */
	@GetMapping(value = "/{uuid}")
	public ControlledVocabularyDTO get(@PathVariable("uuid") final UUID uuid) {
		return vocabularyService.getVocabulary(uuid);
	}

	/**
	 * Gets the term.
	 *
	 * @param uuid the uuid
	 * @param code the code
	 * @return the term
	 */
	@GetMapping(value = "/{uuid}/{code}")
	public VocabularyTermDTO getTerm(@PathVariable("uuid") final UUID uuid, @PathVariable("code") final String code) throws IOException, ServletException {
		return vocabularyService.getTerm(uuid, code);
	}

	/**
	 * Autocomplete term.
	 *
	 * @param uuid the uuid
	 * @param like the like
	 * @return the list
	 */
	@PostMapping(value = "/{uuid}/autocomplete")
	@JsonView({ JsonViews.Public.class })
	public List<VocabularyTermDTO> autocompleteTerm(@PathVariable("uuid") final UUID uuid, @RequestParam("q") final String like) {
		return vocabularyService.autocompleteTerms(uuid, like);
	}

	@PostMapping(value = "/{uuid}/decode")
	public Map<String, String> decodeTerms(@PathVariable("uuid") final UUID uuid,
		@RequestParam(value = "locale", defaultValue = "en", required = false) final String locale, @RequestBody(required = false) Set<String> codes) {
		
		return vocabularyService.decodeTerms(uuid, locale, codes);
	}

	/**
	 * List terms.
	 *
	 * @param uuid the uuid
	 * @param page the page
	 * @return the page
	 */
	@PostMapping(value = "/{uuid}/terms")
	@JsonView({ JsonViews.Public.class })
	public Page<VocabularyTermDTO> listTerms(@PathVariable("uuid") final UUID uuid, @ParameterObject final Pagination page) {
		return vocabularyService.listTerms(uuid, page);
	}

	/**
	 * Delete.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return the controlled vocabulary
	 */
	@DeleteMapping(value = "/{uuid},{version}")
	public ControlledVocabularyDTO delete(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		return vocabularyService.delete(uuid, version);
	}

	/**
	 * Creates the.
	 *
	 * @param source the source
	 * @return the controlled vocabulary
	 */
	@PostMapping(value = "/create")
	public ControlledVocabularyDTO create(@RequestBody final ControlledVocabularyDTO source) {
		return vocabularyService.createVocabulary(source);
	}

	/**
	 * Update controlled vocabulary.
	 *
	 * @param source the source
	 * @return the controlled vocabulary
	 */
	@PostMapping(value = "/update")
	public ControlledVocabularyDTO updateControlledVocabulary(@RequestBody final ControlledVocabularyDTO source) {
		return vocabularyService.updateVocabulary(source);
	}

	/**
	 * List controlled vocabularies.
	 *
	 * @param page the page
	 * @param filter the controlled vocabulary filter
	 * @return the page
	 * @throws IOException
	 */
	@PostMapping(value = "/list")
	public FilteredPage<ControlledVocabularyDTO, ControlledVocabularyFilter> list(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
		@RequestBody(required = false) ControlledVocabularyFilter filter) throws IOException {

		FilterInfo<ControlledVocabularyFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, ControlledVocabularyFilter.class);
		return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, vocabularyService.listVocabularies(filterInfo.filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC,
			"id")));
	}
}