LanguagesController.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.Collections;
import java.util.List;
import java.util.UUID;

import org.genesys.server.api.ApiBaseController;
import org.genesys.server.model.vocab.VocabularyTerm;
import org.genesys.server.service.VocabularyService;
import org.genesys.server.service.worker.ISO639VocabularyUpdater;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

/**
 * The Class LanguagesController.
 *
 * @author Maxym Borodenko
 */
@RestController("languageApi1")
@RequestMapping(org.genesys.server.api.v1.LanguagesController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Api(tags = { "vocabulary" })
public class LanguagesController extends ApiBaseController {

	/** The Constant CONTROLLER_URL. */
	public static final String CONTROLLER_URL = VocabularyController.CONTROLLER_URL + "/lang";

	/** The Constant ISO639_3. */
	public static final UUID ISO639_3 = ISO639VocabularyUpdater.ISO639_3;

	@Autowired
	private ISO639VocabularyUpdater iso639VocabularyUpdater;

	@Autowired
	private VocabularyService vocabularyService;

	/**
	 * Update languages.
	 *
	 * @return the string
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@PreAuthorize("hasRole('ADMINISTRATOR')")
	@PostMapping(value = "/update")
	public String updateLanguageVocabulary() throws IOException {
		LOG.info("Updating ISO language codes");
		vocabularyService.autoUpdateOrCreateVocabulary(ISO639_3, iso639VocabularyUpdater.getISO639Vocabulary());
		return "OK";
	}

	/**
	 * Gets the.
	 *
	 * @param code the code
	 * @return the vocabulary term
	 */
	@GetMapping(value = "/{code}", produces = MediaType.APPLICATION_JSON_VALUE)
	public VocabularyTerm getLanguageTerm(@PathVariable("code") final String code) {
		return vocabularyService.getVocabularyTerm(ISO639_3, code);
	}

	/**
	 * Autocomplete.
	 *
	 * @param text the text
	 * @return the list
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@GetMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
	public List<VocabularyTerm> autocompleteLanguageTerm(@RequestParam("l") final String text) throws IOException {
		if (text.length() < 3) {
			return Collections.emptyList();
		}

		return vocabularyService.autocompleteTerms(ISO639_3, text);
	}
}