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.v1;

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;

import org.genesys.blocks.model.JsonViews;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.model.impl.Country;
import org.genesys.server.model.impl.GeoRegion;
import org.genesys.server.model.vocab.VocabularyTerm;
import org.genesys.server.service.GeoRegionService;
import org.genesys.server.service.GeoService;
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 com.fasterxml.jackson.annotation.JsonView;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController("geoApi1")
@RequestMapping(value = { GeoController.CONTROLLER_URL })
@Api(tags = { "geo" })
public class GeoController extends ApiBaseController {

	public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/geo";

	@Autowired
	private GeoService geoService;

	@Autowired
	private GeoRegionService geoRegionService;

	@JsonView(JsonViews.Protected.class)
	@RequestMapping(value = "/countries", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public List<Country> listCountries() {
		LOG.info("Listing countries");
		return geoService.listActive(LocaleContextHolder.getLocale());
	}

	@JsonView(JsonViews.Public.class)
	@RequestMapping(value = "/country/details/{iso3code}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public GeoService.CountryDetails getCountryDetails(@PathVariable("iso3code") String iso3code) {
		LOG.info("Getting country details bu ISO3 code {}", iso3code);
		return geoService.getDetails(iso3code);
	}

	@RequestMapping(value = "/country/{iso3code}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public Country getCountry(@PathVariable("iso3code") String iso3code) {
		LOG.info("Getting country {}", iso3code);
		Country country = geoService.getCountry(iso3code);
		if (country == null) {
			throw new NotFoundElement();
		}
		return country;
	}

	@RequestMapping(value = "/regions", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public List<GeoRegion> getGeoRegions() {
		LOG.info("Listing regions");
		return geoRegionService.findAll();
	}

	@RequestMapping(value = "/region/details/{isoCode}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public GeoRegionService.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 VocabularyTerm get(@PathVariable("code") final String code) {
		VocabularyTerm term = geoService.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<VocabularyTerm> autocompleteGeoTerm(@RequestParam("c") final String text) throws IOException {
		return geoService.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(geoService.decode3166Alpha3Terms(codes, Locale.forLanguageTag(locale)));
	}
}