VocabularyController.java
/*
* Copyright 2018 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.v1;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.servlet.ServletException;
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.exception.InvalidApiUsageException;
import org.genesys.server.model.filters.ControlledVocabularyFilter;
import org.genesys.server.model.vocab.ControlledVocabulary;
import org.genesys.server.model.vocab.VocabularyTerm;
import org.genesys.server.service.GeoService;
import org.genesys.server.service.InstituteService;
import org.genesys.server.service.VocabularyService;
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 com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api;
/**
* The Class VocabularyController.
*
* @author Matija Obreza
*/
@RestController("vocabularyApi1")
@RequestMapping(VocabularyController.CONTROLLER_URL)
@Api(tags = { "vocabulary" })
public class VocabularyController extends ApiBaseController {
/** The Constant CONTROLLER_URL. */
public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/vocabulary";
public static final String FAO_WIEWS_UUID = "36b4a674-e2eb-4ba1-a05a-71cfc2af862e";
/**
* ISO 3166-1 alpha-2 representation of names of countries and their
* subdivisions, contains two-letter country codes
* https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
*/
public static final String ISO3166_2ALPHA_UUID = "3e39a73e-d1ed-40b0-9944-ac5795128686";
/**
* ISO 3166-1 alpha-2 representation of names of countries and their
* subdivisions, contains three-letter country codes
* https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
*/
public static final String ISO3166_3ALPHA_UUID = "39a3d6a2-20e6-4fab-8bfe-acb1f9fe774c";
/**
* ISO 3166-1 numeric representation of names of countries and their
* subdivisions, contains three-digit country codes
* https://en.wikipedia.org/wiki/ISO_3166-1_numeric
*/
public static final String ISO3166_NUMERIC_UUID = "bd45f660-853f-4034-a434-ed50679579cc";
@Autowired
private VocabularyService vocabularyService;
/** The short filter service. */
@Autowired
protected ShortFilterProcessor shortFilterProcessor;
/// Proxied vocabularies
@Autowired
private InstituteService instituteService;
@Autowired
private GeoService geoService;
/**
* Gets the.
*
* @param uuid the uuid
* @return the controlled vocabulary
*/
@GetMapping(value = "/{uuid}")
public ControlledVocabulary 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 VocabularyTerm getTerm(@PathVariable("uuid") final UUID uuid, @PathVariable("code") final String code) throws IOException, ServletException {
switch (uuid.toString()) {
case FAO_WIEWS_UUID:
return instituteService.getInstituteTerm(code);
case ISO3166_2ALPHA_UUID:
return geoService.get3166Alpha2Term(code);
case ISO3166_3ALPHA_UUID:
return geoService.get3166Alpha3Term(code);
case ISO3166_NUMERIC_UUID:
return geoService.get3166NumericTerm(code);
default:
return vocabularyService.getVocabularyTerm(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<VocabularyTerm> 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) {
switch (uuid.toString()) {
case FAO_WIEWS_UUID:
return instituteService.decodeCodes(codes);
case ISO3166_3ALPHA_UUID:
return geoService.decode3166Alpha3Terms(codes, Locale.forLanguageTag(locale));
default:
throw new InvalidApiUsageException("No decoding for such vocabulary");
}
}
/**
* List terms.
*
* @param uuid the uuid
* @param page the page
* @return the page
*/
@PostMapping(value = "/{uuid}/terms")
@JsonView({ JsonViews.Public.class })
public Page<VocabularyTerm> listTerms(@PathVariable("uuid") final UUID uuid, @ParameterObject final Pagination page) {
// LOG.warn("Loading vocab");
final ControlledVocabulary vocabulary = vocabularyService.getVocabulary(uuid);
// LOG.warn("Loading terms");
switch (uuid.toString()) {
case FAO_WIEWS_UUID:
return instituteService.listTerms(page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "code"));
case ISO3166_2ALPHA_UUID:
return geoService.list3166Alpha2Terms(page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "code2"));
case ISO3166_3ALPHA_UUID:
return geoService.list3166Alpha3Terms(page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "code3"));
case ISO3166_NUMERIC_UUID:
return geoService.list3166NumericTerms(page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "codeNum"));
default:
return vocabularyService.listTerms(vocabulary, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "code"));
}
}
/**
* Delete.
*
* @param uuid the uuid
* @param version the version
* @return the controlled vocabulary
*/
@DeleteMapping(value = "/{uuid},{version}")
public ControlledVocabulary delete(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
return vocabularyService.deleteVocabulary(vocabularyService.getVocabulary(uuid, version));
}
/**
* Creates the.
*
* @param source the source
* @return the controlled vocabulary
*/
@PostMapping(value = "/create")
public ControlledVocabulary create(@RequestBody final ControlledVocabulary source) {
return vocabularyService.createVocabulary(source);
}
/**
* Update controlled vocabulary.
*
* @param source the source
* @return the controlled vocabulary
*/
@PostMapping(value = "/update")
public ControlledVocabulary updateControlledVocabulary(@RequestBody final ControlledVocabulary 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<ControlledVocabulary, 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")));
}
}