Commit 99ec18fb authored by Matija Obreza's avatar Matija Obreza

Merge branch 'vocabularies-update' into 'master'

Vocabularies update

See merge request genesys-pgr/genesys-server!281
parents 4d84d753 789d89c4
/*
* 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.catalog.api.v0;
import org.genesys2.server.api.ApiBaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import java.net.UnknownHostException;
/**
* The Class ApiInfoController.
*
* @author Andrey Lugovskoy.
*/
@RestController
@RequestMapping(ApiInfoController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Api(tags = { "api-info" })
public class ApiInfoController implements InitializingBean {
private static final Logger LOG = LoggerFactory.getLogger(ApiInfoController.class);
/** The Constant API_BASE. */
public static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/info";
@Value("${build.version}")
private String version;
@Value("${build.artifactId}")
private String artifactId;
@Value("${build.revision}")
private String revision;
private ApiInfo apiInfo;
@Override
public void afterPropertiesSet() throws Exception {
String hN;
try {
hN = java.net.InetAddress.getLocalHost().getHostName();
} catch (final UnknownHostException e) {
LOG.error("Error getting Host Name");
hN = "";
}
apiInfo = new ApiInfo(artifactId, version, revision, hN);
LOG.info("API Info: art=" + artifactId + " ver=" + version + " rev=" + revision);
}
/**
* Api info.
*
* @return the api info
*/
@RequestMapping(value = "/version", method = RequestMethod.GET)
public ApiInfo apiInfo() {
return apiInfo;
}
/**
* The Class ApiInfo.
*/
public static class ApiInfo {
private final String artifactId;
private final String version;
private final String revision;
private final String hostName;
/**
* Instantiates a new api info.
*
* @param artifactId the artifact id
* @param version the version
* @param revision the revision
* @param hostName the host name
*/
public ApiInfo(final String artifactId, final String version, final String revision, final String hostName) {
this.artifactId = artifactId;
this.version = version;
this.revision = revision;
this.hostName = hostName;
}
/**
* Gets the host name.
*
* @return the host name
*/
public String getHostName() {
return hostName;
}
/**
* Gets the artifact id.
*
* @return the artifactId
*/
public String getArtifactId() {
return artifactId;
}
/**
* Gets the version.
*
* @return the version
*/
public String getVersion() {
return version;
}
/**
* Gets the revision.
*
* @return the revision
*/
public String getRevision() {
return revision;
}
}
}
/*
* 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.catalog.api.v0;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.genesys.catalog.service.FileMetadataService;
import org.genesys2.server.api.ApiBaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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 org.springframework.web.multipart.MultipartFile;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.annotations.Api;
/**
* The Class FileMetadataController.
*
* @author Maxym Borodenko
*/
@RestController("catalogFileMetadataApi0")
@RequestMapping(FileMetadataController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Api(tags = { "repository" })
public class FileMetadataController {
/**
* The Constant API_BASE.
*/
protected static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/api/v0/fileinfo";
/**
* The Constant LOG.
*/
private static final Logger LOG = LoggerFactory.getLogger(FileMetadataController.class);
private static final String APPLICATION_OXML_SHEET = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
private static final String APPLICATION_MS_EXCEL = "application/vnd.ms-excel";
@Autowired
private FileMetadataService fileMetadataService;
/**
* Metadata.
*
* @param file the file
* @return the object node
* @throws IOException Signals that an I/O exception has occurred.
*/
@PostMapping(consumes = { "multipart/form-data" })
public ObjectNode metadata(@RequestParam("file") final MultipartFile file) throws IOException {
// TODO store the file to the repository
if (APPLICATION_MS_EXCEL.equals(file.getContentType()) || APPLICATION_OXML_SHEET.equals(file.getContentType())) {
return excelMetadata(file);
} else {
throw new UnsupportedOperationException(file.getContentType());
}
}
/**
* Excel metadata.
*
* @param file the file
* @return the object node
* @throws IOException Signals that an I/O exception has occurred.
*/
@PostMapping(consumes = { APPLICATION_MS_EXCEL, APPLICATION_OXML_SHEET })
public ObjectNode excelMetadata(@RequestBody final MultipartFile file) throws IOException {
LOG.info("Analyzing Excel file {}", file.getOriginalFilename());
final File tempFile = File.createTempFile("metadata", ".xlsx");
try {
FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile);
return fileMetadataService.getExcelMetadata(tempFile);
} finally {
FileUtils.deleteQuietly(tempFile);
}
}
}
/*
* 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.catalog.api.v0;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys.catalog.service.VocabularyService;
import org.genesys.catalog.service.worker.ISO3166VocabularyUpdater;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.exception.NotFoundElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
* The Class GeoController.
*
* @author Maxym Borodenko
* @deprecated Use {@link org.genesys2.server.api.v1.GeoController}
*/
@Deprecated
@RestController("cataloGeoApi0")
@RequestMapping(GeoController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Api(tags = { "geo" })
public class GeoController {
/** The Constant API_BASE. */
public static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/geo";
/** The Constant ISO3166_2ALPHA. */
public static final UUID ISO3166_2ALPHA = ISO3166VocabularyUpdater.ISO3166_2ALPHA;
/** The Constant ISO3166_3ALPHA. */
public static final UUID ISO3166_3ALPHA = ISO3166VocabularyUpdater.ISO3166_3ALPHA;
/** The Constant ISO3166_NUMERIC. */
public static final UUID ISO3166_NUMERIC = ISO3166VocabularyUpdater.ISO3166_NUMERIC;
private static final Logger LOG = LoggerFactory.getLogger(GeoController.class);
@Autowired
private ISO3166VocabularyUpdater iso3166VocabularyUpdater;
@Autowired
private VocabularyService vocabularyService;
/**
* Update countries codes.
*
* @return the string
* @throws IOException Signals that an I/O exception has occurred.
*/
@PreAuthorize("hasRole('ADMINISTRATOR')")
@PostMapping(value = "/update")
@ApiOperation("Triggers update of ISO country code vocabularies")
public @ResponseBody String updateCountriesCodes() throws IOException {
LOG.info("Updating ISO country codes");
vocabularyService.autoUpdateOrCreateVocabulary(ISO3166_2ALPHA, iso3166VocabularyUpdater.getISO3166Alpha2Vocabulary());
vocabularyService.autoUpdateOrCreateVocabulary(ISO3166_3ALPHA, iso3166VocabularyUpdater.getISO3166Alpha3Vocabulary());
vocabularyService.autoUpdateOrCreateVocabulary(ISO3166_NUMERIC, iso3166VocabularyUpdater.getISO3166NumericVocabulary());
return "OK";
}
/**
* Gets the.
*
* @param code the code
* @return the vocabulary term
*/
@GetMapping(value = "/iso3166/{code}", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation("Lookup ISO-3166 country by code")
public VocabularyTerm get(@PathVariable("code") final String code) {
final boolean isNumeric = code.chars().allMatch(Character::isDigit);
if (isNumeric) {
return vocabularyService.getVocabularyTerm(ISO3166_NUMERIC, code);
} else if (code.length() == 2) {
return vocabularyService.getVocabularyTerm(ISO3166_2ALPHA, code);
} else if (code.length() == 3) {
return vocabularyService.getVocabularyTerm(ISO3166_3ALPHA, code);
}
throw new NotFoundElement("Code is not in valid format: 3-letter | 2-letter | numeric");
}
/**
* 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 vocabularyService.autocompleteTerms(ISO3166_3ALPHA, text);
}
}
/*
* 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.catalog.api.v0;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys.catalog.service.VocabularyService;
import org.genesys.catalog.service.worker.ISO639VocabularyUpdater;
import org.genesys2.server.api.ApiBaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
/**
* The Class LanguagesController.
*
* @author Maxym Borodenko
* @deprecated Use {@link org.genesys2.server.api.v1.LanguagesController}
*/
@Deprecated
@RestController("languageApi0")
@RequestMapping(LanguagesController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Api(tags = { "vocabulary" })
public class LanguagesController {
/** The Constant API_BASE. */
public static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/lang";
/** The Constant ISO639_3. */
public static final UUID ISO639_3 = ISO639VocabularyUpdater.ISO639_3;
private static final Logger LOG = LoggerFactory.getLogger(LanguagesController.class);
@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 @ResponseBody 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);
}
}
/*
* 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.catalog.api.v0;
import java.io.IOException;
import java.util.UUID;
import org.genesys.catalog.api.FilteredPage;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.filters.PartnerFilter;
import org.genesys.catalog.service.PartnerService;
import org.genesys.catalog.service.ShortFilterService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
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.PutMapping;
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 io.swagger.annotations.Api;
/**
* Rest controller for Partner.
*
* @author Andrey Lugovskoy.
* @deprecated Use {@link org.genesys2.server.api.v1.PartnerController}
*/
@Deprecated
@RestController("partnerApi0")
@RequestMapping(PartnerController.CONTROLLER_URL)
@Api(tags = { "partner" })
public class PartnerController {
/** The Constant API_BASE. */
public static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/partner";
@Autowired
private PartnerService partnerService;
/** The short filter service. */
@Autowired
protected ShortFilterService shortFilterService;
/**
* My partners.
*
* @param page the page
* @param partnerFilter the partner filter
* @return the page
*/
@PostMapping(value = "/list-mine")
public Page<Partner> myPartners(final Pagination page, @RequestBody final PartnerFilter partnerFilter) {
return partnerService.listPartnersForCurrentUser(partnerFilter, page.toPageRequest(100));
}
/**
* List partners.
*
* @param page the page
* @param filter the partner filter
* @return the page
* @throws IOException
*/
@PostMapping(value = "/list")
public FilteredPage<Partner> listPartners(@RequestParam(name = "f", required = false) String filterCode, final Pagination page,
@RequestBody(required = false) PartnerFilter filter) throws IOException {
if (filterCode != null) {
filter = shortFilterService.filterByCode(filterCode, PartnerFilter.class);
} else {
filterCode = shortFilterService.getCode(filter);
}
return new FilteredPage<>(filterCode, filter, partnerService.listPartners(filter, page.toPageRequest(100)));
}
/**
* Gets the partner.
*
* @param uuid the uuid
* @return the partner
*/
@GetMapping(value = "/{UUID}")
public Partner getPartner(@PathVariable("UUID") final UUID uuid) {
return partnerService.loadPartner(uuid);
}
/**
* Creates the partner.
*
* @param source the source
* @return the partner
*/
@PreAuthorize("hasRole('ADMINISTRATOR')")
@PostMapping(value = "/create")
public Partner createPartner(@RequestBody final Partner source) {
return partnerService.createPartner(source);
}
/**
* Update partner.
*
* @param source the source
* @return the partner
*/
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#source.getId(), #source.class.getName(), 'write')")
@PutMapping(value = "/update")
public Partner updatePartner(@RequestBody final Partner source) {
return partnerService.updatePartner(source);
}
/**
* Delete partner.
*
* @param uuid the uuid
* @param version the version
* @return the partner
*/
@DeleteMapping(value = "/{UUID},{version}")
public Partner deletePartner(@PathVariable("UUID") final UUID uuid, @PathVariable("version") final int version) {
return partnerService.removePartner(partnerService.loadPartner(uuid, version));
}
}
/*
* 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.catalog.api.v0;
import static com.google.common.collect.Sets.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;