Commit 789d89c4 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza

Vocabularies update

- Added vocabulary methods to CountryService, InstituteService
parent 92d619ff
......@@ -57,7 +57,7 @@ public interface ControlledVocabularyRepository extends JpaRepository<Controlled
* @param code the code
* @return the vocabulary term
*/
@Query("select vt from ControlledVocabulary cv inner join cv.terms as vt where cv.uuid = ?1 and vt.code = ?2")
@Query("from VocabularyTerm vt where vt.vocabulary.uuid = ?1 and vt.code = ?2")
VocabularyTerm getVocabularyTerm(UUID vocabularyUuid, String code);
/**
......@@ -68,7 +68,7 @@ public interface ControlledVocabularyRepository extends JpaRepository<Controlled
* @param page the page
* @return the list
*/
@Query("select vt from ControlledVocabulary cv inner join cv.terms as vt where cv.uuid = ?1 and (vt.code like concat(?2, '%') or vt.title like concat(?2, '%') or vt.description like concat(?2, '%'))")
@Query("from VocabularyTerm vt where vt.vocabulary.uuid = ?1 and (vt.code like concat(?2, '%') or vt.title like concat(?2, '%') or vt.description like concat(?2, '%'))")
List<VocabularyTerm> autocompleteVocabularyTerm(UUID vocabularyUuid, String text, Pageable page);
/**
......@@ -78,6 +78,6 @@ public interface ControlledVocabularyRepository extends JpaRepository<Controlled
* @param page the page
* @return the page
*/
@Query("select vt from ControlledVocabulary cv inner join cv.terms as vt where cv = ?1")
@Query("from VocabularyTerm vt where vt.vocabulary = ?1")
Page<VocabularyTerm> listVocabularyTerms(ControlledVocabulary vocabulary, Pageable page);
}
......@@ -258,7 +258,7 @@ public class VocabularyServiceImpl implements VocabularyService {
@Override
public List<VocabularyTerm> autocompleteTerms(final UUID vocabularyUuid, final String text) {
return vocabRepository.autocompleteVocabularyTerm(vocabularyUuid, text, new PageRequest(0, 20, new Sort("vt.code")));
return vocabRepository.autocompleteVocabularyTerm(vocabularyUuid, text, new PageRequest(0, 20, new Sort("code")));
}
@Override
......
......@@ -24,6 +24,7 @@ import io.swagger.annotations.ApiOperation;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.GeoRegion;
import org.genesys2.server.service.GeoRegionService;
......@@ -88,15 +89,19 @@ public class GeoController extends ApiBaseController {
}
/**
* Gets the country vocabulary term
* Gets the country vocabulary term by ISO3166-alpha3 code
*
* @param code the code
* @param code the alpha3 code
* @return the vocabulary term
*/
@GetMapping(value = "/iso3166/{code}", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation("Lookup ISO-3166 country by code")
@ApiOperation("Lookup ISO-3166 vocabulary term by code")
public VocabularyTerm get(@PathVariable("code") final String code) {
return geoService.getCountryTerm(code);
VocabularyTerm term = geoService.get3166Alpha3Term(code);
if (term == null) {
throw new NotFoundElement("No ISO3166 with code " + code);
}
return term;
}
/**
......
......@@ -20,8 +20,9 @@ import java.io.IOException;
import java.util.List;
import java.util.UUID;
import io.swagger.annotations.Api;
import javax.servlet.ServletException;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.model.filters.ControlledVocabularyFilter;
import org.genesys.catalog.model.vocab.ControlledVocabulary;
import org.genesys.catalog.model.vocab.VocabularyTerm;
......@@ -31,6 +32,8 @@ import org.genesys.catalog.service.VocabularyService;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.FilteredPage;
import org.genesys2.server.api.Pagination;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
......@@ -43,6 +46,10 @@ 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.
*
......@@ -56,6 +63,29 @@ public class VocabularyController {
/** The Constant API_BASE. */
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;
......@@ -63,6 +93,13 @@ public class VocabularyController {
@Autowired
protected ShortFilterService shortFilterService;
/// Proxied vocabularies
@Autowired
private InstituteService instituteService;
@Autowired
private GeoService geoService;
/**
* Gets the.
*
......@@ -82,8 +119,20 @@ public class VocabularyController {
* @return the term
*/
@GetMapping(value = "/{UUID}/{code}")
public VocabularyTerm getTerm(@PathVariable("UUID") final UUID uuid, @PathVariable("code") final String code) {
return vocabularyService.getVocabularyTerm(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);
}
}
/**
......@@ -94,6 +143,7 @@ public class VocabularyController {
* @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);
}
......@@ -106,13 +156,24 @@ public class VocabularyController {
* @return the page
*/
@PostMapping(value = "/{UUID}/terms")
@JsonView({ JsonViews.Public.class })
public Page<VocabularyTerm> listTerms(@PathVariable("UUID") final UUID uuid, final Pagination page) {
// LOG.warn("Loading vocab");
final ControlledVocabulary vocabulary = vocabularyService.getVocabulary(uuid);
// LOG.warn("Loading terms");
final Page<VocabularyTerm> x = vocabularyService.listTerms(vocabulary, page.toPageRequest(100, Sort.Direction.ASC, "id"));
// LOG.warn("Returning terms");
return x;
switch (uuid.toString()) {
case FAO_WIEWS_UUID:
return instituteService.listTerms(page.toPageRequest(100, Sort.Direction.ASC, "code"));
case ISO3166_2ALPHA_UUID:
return geoService.list3166Alpha2Terms(page.toPageRequest(100, Sort.Direction.ASC, "code2"));
case ISO3166_3ALPHA_UUID:
return geoService.list3166Alpha3Terms(page.toPageRequest(100, Sort.Direction.ASC, "code3"));
case ISO3166_NUMERIC_UUID:
return geoService.list3166NumericTerms(page.toPageRequest(100, Sort.Direction.ASC, "codeNum"));
default:
return vocabularyService.listTerms(vocabulary, page.toPageRequest(100, Sort.Direction.ASC, "code"));
}
}
/**
......@@ -167,6 +228,7 @@ public class VocabularyController {
filterCode = shortFilterService.getCode(filter);
}
FilterInfo<ControlledVocabularyFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, ControlledVocabularyFilter.class);
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, vocabularyService.listVocabularies(filterInfo.filter, page.toPageRequest(100, Sort.Direction.ASC, "id")));
return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, vocabularyService.listVocabularies(filterInfo.filter, page.toPageRequest(100, Sort.Direction.ASC,
"id")));
}
}
......@@ -24,14 +24,19 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.query.Param;
public interface CountryRepository extends JpaRepository<Country, Long> {
public interface CountryRepository extends JpaRepository<Country, Long>, QueryDslPredicateExecutor<Country> {
Country findByName(String name);
// @Query("from Country c where c.code3= ?1 and c.current=true")
Country findByCode3(String code3);
Country findByCode2(String code);
Country findByCodeNum(String code);
Country findByCode2AndCurrent(String code2, boolean current);
// List<Country> findByRegion(Region region);
......@@ -66,4 +71,5 @@ public interface CountryRepository extends JpaRepository<Country, Long> {
@Query("select distinct c from Country c where c.region.id = ?1")
List<Country> findByRegions(Long id );
}
......@@ -22,16 +22,18 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonView;
import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.ITPGRFAStatus;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
public interface GeoService {
......@@ -56,14 +58,6 @@ public interface GeoService {
*/
Country getCountry(String isoCode);
/**
* Gets country vocabulary term.
*
* @param isoCode country code (numeric, iso2 or iso3)
* @return country vocabulary term
*/
VocabularyTerm getCountryTerm(String isoCode);
/**
* Tries to find country by ISO CODE2, ISO CODE3, Country name and
* translations
......@@ -174,4 +168,12 @@ public interface GeoService {
}
}
Page<VocabularyTerm> list3166Alpha2Terms(Pageable page);
Page<VocabularyTerm> list3166Alpha3Terms(Pageable page);
Page<VocabularyTerm> list3166NumericTerms(Pageable page);
VocabularyTerm get3166Alpha2Term(String code);
VocabularyTerm get3166Alpha3Term(String code);
VocabularyTerm get3166NumericTerm(String code);
}
......@@ -22,6 +22,7 @@ import java.util.Locale;
import java.util.Map;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys2.server.api.Pagination;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.filter.InstituteFilter;
......@@ -72,6 +73,7 @@ public interface InstituteService {
List<FaoInstitute> autocomplete(String ac);
List<VocabularyTerm> autocompleteTerm(String ac);
Page<VocabularyTerm> listTerms(Pageable pageable);
Page<FaoInstitute> listPGRInstitutes(Pageable pageable);
......
......@@ -36,6 +36,7 @@ import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.ITPGRFAStatus;
import org.genesys2.server.model.impl.QCountry;
import org.genesys2.server.persistence.CountryRepository;
import org.genesys2.server.persistence.ITPGRFAStatusRepository;
import org.genesys2.server.service.AccessionService;
......@@ -48,16 +49,18 @@ import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.filter.CountryFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.genesys2.server.service.worker.CountryInfo;
import org.genesys2.server.service.worker.DavrosCountrySource;
import org.genesys2.server.service.worker.GeoNamesCountrySource;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
......@@ -67,6 +70,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Sets;
@Service
@Transactional(readOnly = true)
......@@ -157,10 +161,6 @@ public class GeoServiceImpl implements GeoService {
return country;
}
@Override
public VocabularyTerm getCountryTerm(String isoCode) {
return toVocabularyTerm(getCountry(isoCode), isoCode);
}
@Override
public Country findCountry(String countryString) {
......@@ -491,7 +491,7 @@ public class GeoServiceImpl implements GeoService {
AccessionFilter byCountry = new AccessionFilter();
byCountry.origin = new CountryFilter();
byCountry.origin.iso3 = new HashSet<String>(){{ add(country.getCode3()); }};
byCountry.origin.iso3 = Sets.newHashSet(country.getCode3());
long accessionCount = accessionService.countAccessions(byCountry);
long countByLocation = genesysService.countByLocation(country);
......@@ -570,4 +570,41 @@ public class GeoServiceImpl implements GeoService {
return countryTerm;
}
@Override
public Page<VocabularyTerm> list3166Alpha2Terms(Pageable page) {
Page<Country> res = countryRepository.findAll(QCountry.country.code2.isNotNull(), page);
return res.map(c -> toVocabularyTerm(c, c.getCode2()));
}
@Override
public Page<VocabularyTerm> list3166Alpha3Terms(Pageable page) {
Page<Country> res = countryRepository.findAll(QCountry.country.code3.isNotNull(), page);
return res.map(c -> toVocabularyTerm(c, c.getCode3()));
}
@Override
public Page<VocabularyTerm> list3166NumericTerms(Pageable page) {
Page<Country> res = countryRepository.findAll(QCountry.country.codeNum.isNotNull(), page);
return res.map(c -> toVocabularyTerm(c, c.getCodeNum() != null ? c.getCodeNum().toString() : null));
}
@Override
public VocabularyTerm get3166Alpha2Term(String code) {
Country c = countryRepository.findByCode2(code);
return c == null ? null : toVocabularyTerm(c, c.getCode2());
}
@Override
public VocabularyTerm get3166Alpha3Term(String code) {
Country c = countryRepository.findByCode3(code);
return c == null ? null : toVocabularyTerm(c, c.getCode3());
}
@Override
public VocabularyTerm get3166NumericTerm(String code) {
Country c = countryRepository.findByCodeNum(code);
return c == null ? null : toVocabularyTerm(c, c.getCodeNum().toString());
}
}
......@@ -41,7 +41,6 @@ import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.filter.InstituteFilter;
import org.genesys2.spring.ResourceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -325,4 +324,10 @@ public class InstituteServiceImpl implements InstituteService {
return builder.toString();
}
@Override
public Page<VocabularyTerm> listTerms(Pageable page) {
Page<FaoInstitute> res = instituteRepository.findAll(page);
return res.map(inst -> toVocabularyTerm(inst));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment