Commit ef4dc592 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov

Added decoding for wiews and iso3166 codes

Added possibility to decode certain wiews/iso3 codes
parent 0ff8fee4
...@@ -18,6 +18,10 @@ package org.genesys2.server.api.v1; ...@@ -18,6 +18,10 @@ package org.genesys2.server.api.v1;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import com.fasterxml.jackson.annotation.JsonView; import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
...@@ -34,6 +38,8 @@ import org.springframework.context.i18n.LocaleContextHolder; ...@@ -34,6 +38,8 @@ import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
...@@ -114,4 +120,15 @@ public class GeoController extends ApiBaseController { ...@@ -114,4 +120,15 @@ public class GeoController extends ApiBaseController {
public List<VocabularyTerm> autocompleteGeoTerm(@RequestParam("c") final String text) throws IOException { public List<VocabularyTerm> autocompleteGeoTerm(@RequestParam("c") final String text) throws IOException {
return geoService.autoCompleteTerm(text); return geoService.autoCompleteTerm(text);
} }
}
\ No newline at end of file /**
* Decode codes
* @return the Map of iso3166 codes -> country names
*/
@PostMapping(value = "/iso3166/decode", produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, String> decode3166Alpha3Terms(@RequestParam(value = "locale", defaultValue = "en", required = false) final String locale,
@RequestBody(required = false) Set<String> codes) {
return geoService.decode3166Alpha3Terms(codes, Locale.forLanguageTag(locale));
}
}
...@@ -18,6 +18,9 @@ package org.genesys2.server.api.v1; ...@@ -18,6 +18,9 @@ package org.genesys2.server.api.v1;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -32,6 +35,7 @@ import org.genesys.catalog.service.VocabularyService; ...@@ -32,6 +35,7 @@ import org.genesys.catalog.service.VocabularyService;
import org.genesys2.server.api.ApiBaseController; import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.FilteredPage; import org.genesys2.server.api.FilteredPage;
import org.genesys2.server.api.Pagination; import org.genesys2.server.api.Pagination;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.service.GeoService; import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService; import org.genesys2.server.service.InstituteService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -148,6 +152,19 @@ public class VocabularyController { ...@@ -148,6 +152,19 @@ public class VocabularyController {
return vocabularyService.autocompleteTerms(uuid, like); return vocabularyService.autocompleteTerms(uuid, like);
} }
@PostMapping(value = "/{uuid}/decode")
public Map<String, String> decodeTerms(@PathVariable("uuid") final UUID uuid,
@RequestParam(value = "locale", defaultValue = "en", required = false) final String locale, @RequestBody(required = false) Set<String> codes) {
switch (uuid.toString()) {
case FAO_WIEWS_UUID:
return instituteService.decodeCodes(codes);
case ISO3166_3ALPHA_UUID:
return geoService.decode3166Alpha3Terms(codes, Locale.forLanguageTag(locale));
default:
throw new InvalidApiUsageException("No decoding for such vocabulary");
}
}
/** /**
* List terms. * List terms.
* *
......
...@@ -19,6 +19,8 @@ package org.genesys2.server.api.v1; ...@@ -19,6 +19,8 @@ package org.genesys2.server.api.v1;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import org.genesys.catalog.model.vocab.VocabularyTerm; import org.genesys.catalog.model.vocab.VocabularyTerm;
...@@ -30,6 +32,8 @@ import org.springframework.http.MediaType; ...@@ -30,6 +32,8 @@ import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -79,4 +83,13 @@ public class WiewsController extends ApiBaseController { ...@@ -79,4 +83,13 @@ public class WiewsController extends ApiBaseController {
return instituteService.autocompleteTerm(text); return instituteService.autocompleteTerm(text);
} }
/**
* Decode codes
* @return the Map of wiews codes -> institute names
*/
@PostMapping(value = "/decode", produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, String> decodeWiewsCodes(@RequestBody(required = false) Set<String> codes) {
return instituteService.decodeCodes(codes);
}
} }
...@@ -21,6 +21,7 @@ import java.io.Serializable; ...@@ -21,6 +21,7 @@ import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.genesys.blocks.model.JsonViews; import org.genesys.blocks.model.JsonViews;
import org.genesys.catalog.model.vocab.VocabularyTerm; import org.genesys.catalog.model.vocab.VocabularyTerm;
...@@ -170,6 +171,7 @@ public interface GeoService { ...@@ -170,6 +171,7 @@ public interface GeoService {
Page<VocabularyTerm> list3166Alpha2Terms(Pageable page); Page<VocabularyTerm> list3166Alpha2Terms(Pageable page);
Page<VocabularyTerm> list3166Alpha3Terms(Pageable page); Page<VocabularyTerm> list3166Alpha3Terms(Pageable page);
Map<String, String> decode3166Alpha3Terms(Set<String> codes, Locale locale);
Page<VocabularyTerm> list3166NumericTerms(Pageable page); Page<VocabularyTerm> list3166NumericTerms(Pageable page);
VocabularyTerm get3166Alpha2Term(String code); VocabularyTerm get3166Alpha2Term(String code);
......
...@@ -20,6 +20,7 @@ import java.util.Collection; ...@@ -20,6 +20,7 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.genesys.catalog.model.vocab.VocabularyTerm; import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys2.server.model.impl.Country; import org.genesys2.server.model.impl.Country;
...@@ -75,6 +76,7 @@ public interface InstituteService { ...@@ -75,6 +76,7 @@ public interface InstituteService {
List<FaoInstitute> autocomplete(String ac); List<FaoInstitute> autocomplete(String ac);
List<VocabularyTerm> autocompleteTerm(String ac); List<VocabularyTerm> autocompleteTerm(String ac);
Map<String, String> decodeCodes(Set<String> codes);
Page<VocabularyTerm> listTerms(Pageable pageable); Page<VocabularyTerm> listTerms(Pageable pageable);
Page<FaoInstitute> listPGRInstitutes(Pageable pageable); Page<FaoInstitute> listPGRInstitutes(Pageable pageable);
......
...@@ -20,15 +20,21 @@ import java.io.IOException; ...@@ -20,15 +20,21 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import net.sf.oval.constraint.exclusion.Nullable;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.genesys.catalog.model.vocab.VocabularyTerm; import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys2.server.exception.NotFoundElement; import org.genesys2.server.exception.NotFoundElement;
...@@ -79,6 +85,9 @@ import com.google.common.collect.Sets; ...@@ -79,6 +85,9 @@ import com.google.common.collect.Sets;
public class GeoServiceImpl implements GeoService { public class GeoServiceImpl implements GeoService {
public static final Logger LOG = LoggerFactory.getLogger(GeoServiceImpl.class); public static final Logger LOG = LoggerFactory.getLogger(GeoServiceImpl.class);
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Autowired @Autowired
CountryRepository countryRepository; CountryRepository countryRepository;
...@@ -638,6 +647,22 @@ public class GeoServiceImpl implements GeoService { ...@@ -638,6 +647,22 @@ public class GeoServiceImpl implements GeoService {
} }
@Override @Override
public Map<String, String> decode3166Alpha3Terms(Set<String> codes, Locale locale) {
Predicate whereClause = codes != null ? QCountry.country.code3.in(codes) : null;
List<Tuple> query = jpaQueryFactory.select(QCountry.country.code3, QCountry.country.nameL, QCountry.country.name)
.from(QCountry.country)
.where(whereClause)
.fetch();
return query.stream().collect(
Collectors.toMap(
tuple -> tuple.get(QCountry.country.code3),
tuple -> decodeNameLocal(locale, tuple.get(QCountry.country.nameL), tuple.get(QCountry.country.name))
)
);
}
@Override
public Page<VocabularyTerm> list3166NumericTerms(Pageable page) { public Page<VocabularyTerm> list3166NumericTerms(Pageable page) {
Page<Country> res = countryRepository.findAll(QCountry.country.codeNum.isNotNull(), page); Page<Country> res = countryRepository.findAll(QCountry.country.codeNum.isNotNull(), page);
return res.map(c -> toVocabularyTerm(c, c.getCodeNum() != null ? c.getCodeNum().toString() : null)); return res.map(c -> toVocabularyTerm(c, c.getCodeNum() != null ? c.getCodeNum().toString() : null));
...@@ -661,4 +686,19 @@ public class GeoServiceImpl implements GeoService { ...@@ -661,4 +686,19 @@ public class GeoServiceImpl implements GeoService {
Country c = countryRepository.findByCodeNum(code); Country c = countryRepository.findByCodeNum(code);
return c == null ? null : toVocabularyTerm(c, c.getCodeNum().toString()); return c == null ? null : toVocabularyTerm(c, c.getCodeNum().toString());
} }
private String decodeNameLocal(Locale locale, String nameL, String name){
ObjectMapper mapper = new ObjectMapper();
if (nameL == null)
return name;
try {
JsonNode nameJ = nameJ = mapper.readTree(nameL);
return nameJ.has(locale.getLanguage()) ? nameJ.get(locale.getLanguage()).textValue() : name;
} catch (IOException e) {
LOG.warn("Error while decoding country code: ", e.getMessage());
return name;
}
}
} }
...@@ -22,8 +22,12 @@ import java.util.List; ...@@ -22,8 +22,12 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.collections4.ListUtils; import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.security.SecurityContextUtil; import org.genesys.blocks.security.SecurityContextUtil;
...@@ -34,6 +38,7 @@ import org.genesys2.server.exception.NotFoundElement; ...@@ -34,6 +38,7 @@ import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.impl.Country; import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute; import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.FaoInstituteSetting; import org.genesys2.server.model.impl.FaoInstituteSetting;
import org.genesys2.server.model.impl.QFaoInstitute;
import org.genesys2.server.persistence.AccessionRepository; import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository; import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.FaoInstituteSettingRepository; import org.genesys2.server.persistence.FaoInstituteSettingRepository;
...@@ -71,6 +76,9 @@ public class InstituteServiceImpl implements InstituteService { ...@@ -71,6 +76,9 @@ public class InstituteServiceImpl implements InstituteService {
private final static String HIBERNATE_CACHENAME = "hibernate.org.genesys2.server.model.impl.FaoInstitute"; private final static String HIBERNATE_CACHENAME = "hibernate.org.genesys2.server.model.impl.FaoInstitute";
private static final List<FaoInstitute> EMPTY_LIST = ListUtils.unmodifiableList(new ArrayList<FaoInstitute>()); private static final List<FaoInstitute> EMPTY_LIST = ListUtils.unmodifiableList(new ArrayList<FaoInstitute>());
@Autowired
private JPAQueryFactory jpaQueryFactory;
@Autowired @Autowired
private FaoInstituteRepository instituteRepository; private FaoInstituteRepository instituteRepository;
...@@ -308,6 +316,21 @@ public class InstituteServiceImpl implements InstituteService { ...@@ -308,6 +316,21 @@ public class InstituteServiceImpl implements InstituteService {
return autocomplete(ac).stream().map(this::toVocabularyTerm).collect(Collectors.toList()); return autocomplete(ac).stream().map(this::toVocabularyTerm).collect(Collectors.toList());
} }
@Override
public Map<String, String> decodeCodes(Set<String> codes) {
Predicate whereClause = codes != null
? QFaoInstitute.faoInstitute.code.in(codes)
: QFaoInstitute.faoInstitute.accessionCount.gt(0);
List<Tuple> query = jpaQueryFactory.select(QFaoInstitute.faoInstitute.code, QFaoInstitute.faoInstitute.fullName)
.from(QFaoInstitute.faoInstitute)
.where(whereClause)
.fetch();
return query.stream().collect(Collectors.toMap(tuple -> tuple.get(QFaoInstitute.faoInstitute.code), tuple -> tuple.get(QFaoInstitute.faoInstitute.fullName)));
}
private VocabularyTerm toVocabularyTerm(FaoInstitute institute) { private VocabularyTerm toVocabularyTerm(FaoInstitute institute) {
if (institute == null) { if (institute == null) {
throw new NotFoundElement("No such wiews term"); throw new NotFoundElement("No such wiews term");
......
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