GeoController.java
/*
* Copyright 2019 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 io.swagger.annotations.ApiOperation;
import org.genesys.blocks.model.JsonViews;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.v2.facade.CountryApiService;
import org.genesys.server.api.v2.facade.GeoRegionApiService;
import org.genesys.server.api.v2.model.impl.CountryDTO;
import org.genesys.server.api.v2.model.impl.GeoRegionDTO;
import org.genesys.server.api.v2.model.impl.VocabularyTermDTO;
import org.genesys.server.exception.NotFoundElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@RestController("geoApi2")
@RequestMapping(value = { GeoController.CONTROLLER_URL })
@Api(tags = { "geo" })
public class GeoController extends ApiBaseController {
public static final String CONTROLLER_URL = ApiBaseController.APIv2_BASE + "/geo";
@Autowired
private CountryApiService countryService;
@Autowired
private GeoRegionApiService geoRegionService;
@JsonView(JsonViews.Protected.class)
@RequestMapping(value = "/countries", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<CountryDTO> listCountries() {
LOG.info("Listing countries");
return countryService.listActive(LocaleContextHolder.getLocale());
}
@JsonView(JsonViews.Public.class)
@RequestMapping(value = "/country/details/{iso3code}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public CountryApiService.CountryDetails getCountryDetails(@PathVariable("iso3code") String iso3code) {
LOG.info("Getting country details bu ISO3 code {}", iso3code);
return countryService.getDetails(iso3code);
}
@RequestMapping(value = "/country/{iso3code}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public CountryDTO getCountry(@PathVariable("iso3code") String iso3code) {
LOG.info("Getting country {}", iso3code);
CountryDTO country = countryService.getCountry(iso3code);
if (country == null) {
throw new NotFoundElement();
}
return country;
}
@RequestMapping(value = "/regions", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<GeoRegionDTO> getGeoRegions() {
LOG.info("Listing regions");
return geoRegionService.findAll();
}
@RequestMapping(value = "/region/details/{isoCode}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public GeoRegionApiService.RegionDetails getGeoRegionDetails(@PathVariable("isoCode") String isoCode) {
LOG.info("Getting region {}", isoCode);
return geoRegionService.getDetails(isoCode);
}
/**
* Gets the country vocabulary term by ISO3166-alpha3 code
*
* @param code the alpha3 code
* @return the vocabulary term
*/
@GetMapping(value = "/iso3166/{code}", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation("Lookup ISO-3166 vocabulary term by code")
public VocabularyTermDTO get(@PathVariable("code") final String code) {
VocabularyTermDTO term = countryService.get3166Alpha3Term(code);
if (term == null) {
throw new NotFoundElement("No ISO3166 with code " + code);
}
return term;
}
/**
* Autocomplete.
*
* @param text the text
* @return the list
* @throws IOException Signals that an I/O exception has occurred.
*/
@GetMapping(value = "/iso3166/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
public List<VocabularyTermDTO> autocompleteGeoTerm(@RequestParam("c") final String text) throws IOException {
return countryService.autoCompleteTerm(text);
}
/**
* Decode codes
* @return the Map of iso3166 codes -> country names
*/
@PostMapping(value = "/iso3166/decode", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, String>> decode3166Alpha3Terms(@RequestParam(value = "locale", defaultValue = "en", required = false) final String locale,
@RequestBody(required = false) Set<String> codes) {
return ResponseEntity.ok().cacheControl(CacheControl.maxAge(5, TimeUnit.MINUTES)).body(countryService.decode3166Alpha3Terms(codes, Locale.forLanguageTag(locale)));
}
}