Skip to content

Commit

Permalink
Multilang label ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaskabc authored and ledsoft committed Aug 1, 2024
1 parent 528857e commit e61de0e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public List<TermDto> findAllRoots(Vocabulary vocabulary, Pageable pageSpec, Coll
"?vocabulary ?hasGlossary/?hasTerm ?term ." +
"BIND((lang(?label) = ?labelLang) as ?hasLocaleLabel) ." +
"FILTER (?term NOT IN (?included))" +
"}} ORDER BY DESC(?hasLocaleLabel) " + orderSentence("?label") + "}",
"}} ORDER BY DESC(?hasLocaleLabel) lang(?label) " + orderSentence("?label") + "}",
TermDto.class);
query = setCommonFindAllRootsQueryParams(query, false);
try {
Expand Down Expand Up @@ -474,7 +474,7 @@ public List<TermDto> findAllRoots(Pageable pageSpec, Collection<URI> includeTerm
"BIND((lang(?label) = ?labelLang) as ?hasLocaleLabel) ." +
"FILTER (?term NOT IN (?included)) . " +
"FILTER NOT EXISTS {?term a ?snapshot .} " +
"} ORDER BY DESC(?hasLocaleLabel) " + orderSentence("?label") + "}",
"} ORDER BY DESC(?hasLocaleLabel) lang(?label) " + orderSentence("?label") + "}",
TermDto.class);
query = setCommonFindAllRootsQueryParams(query, false);
try {
Expand Down Expand Up @@ -559,7 +559,7 @@ public List<TermDto> findAllRootsIncludingImports(Vocabulary vocabulary, Pageabl
"?parent ?hasGlossary/?hasTerm ?term ." +
"BIND((lang(?label) = ?labelLang) as ?hasLocaleLabel) ." +
"FILTER (?term NOT IN (?included))" +
"} ORDER BY DESC(?hasLocaleLabel) " + orderSentence("?label") + "}",
"} ORDER BY DESC(?hasLocaleLabel) lang(?label) " + orderSentence("?label") + "}",
TermDto.class);
query = setCommonFindAllRootsQueryParams(query, true);
try {
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/cz/cvut/kbss/termit/persistence/dao/TermDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,37 @@ void findAllRootsReturnsTermsThatAreMissingDefaultLanguageLabel() {
.toList());
}

/**
* terms should be ordered - by language and then lexicographically, with the default language always first
*/
@Test
void findAllRootsReturnsTermsInMultipleLanguagesWithoutPrimaryLabelInCorrectOrder() {
configuration.getPersistence().setLanguage("cs");
persistTerms("cs", "Německo", "Čína");
persistTerms("af", "Duitsland", "Sjina");
persistTerms("en", "Germany", "China");
persistTerms("pl", "Niemcy", "Chiny");
persistTerms("da", "Tyskland", "Kina");
// lang order is: af, cs, da, en, pl
final List<TermDto> result = sut.findAllRoots(vocabulary, Constants.DEFAULT_PAGE_SPEC, Collections.emptyList());

// map results to another language than English if possible
final List<String> labels = result.stream().map(r -> {
final Optional<String> lang = r.getLabel().getLanguages().stream().filter(l -> !l.equals("en")).findAny();
return r.getLabel().get(lang.orElse("en"));
}).toList();

final List<String> expectedOrder = Arrays.asList(
"Čína", "Německo", // Czech as first, its default language
"Duitsland", "Sjina",
"Kina", "Tyskland",
"China", "Germany",
"Chiny", "Niemcy");

assertEquals(10, result.size());
assertEquals(expectedOrder, labels);
}

private void persistTerms(String lang, String... labels) {
transactional(() -> Arrays.stream(labels).forEach(label -> {
final Term parent = Generator.generateTermWithId();
Expand Down

0 comments on commit e61de0e

Please sign in to comment.