Skip to content

Commit

Permalink
#135 fallback language in suggest + parents
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Jun 12, 2024
1 parent e8c0be0 commit 471ce4a
Show file tree
Hide file tree
Showing 17 changed files with 452 additions and 84 deletions.
47 changes: 46 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pipeline {
when {
allOf {
not { expression { params.RELEASE } };
branch 'dev';
branch 'dev_jdk8';
}
}
steps {
Expand Down Expand Up @@ -147,6 +147,51 @@ pipeline {
}
}
}
stage('Deploy to DEV2') {
environment {
GIT_CREDENTIALS = credentials('4b740850-d7e0-4ab2-9eee-ecd1607e1e02')
SERVICE_VOCABULARY = "${env.WORKSPACE}/service-vocabulary.yml"
HOSTS_VOCABULARY = "${env.WORKSPACE}/hosts-vocabulary"
BUILD_HOSTS = "${BUILD_ID}_hosts"
}
when {
allOf {
not { expression { params.RELEASE } };
branch 'dev';
}
}
steps {
sshagent(['85f1747d-ea03-49ca-9e5d-aa9b7bc01c5f']) {
sh '''
rm -rf *
git clone -b master [email protected]:gbif/gbif-configuration.git
git clone -b master [email protected]:gbif/c-deploy.git
'''

createServiceFile("${env.WORKSPACE}/gbif-configuration/environments/dev2/services.yml")
createHostsFile()

sh """
cd c-deploy/services
echo "Creating group_vars directory"
mkdir group_vars
# Configuration and services files are concatenated into a single file, that will contain the Ansible variables
cat ../../gbif-configuration/environments/dev2/configuration.yml \
../../gbif-configuration/environments/dev2/monitoring.yml \
${SERVICE_VOCABULARY} >> group_vars/${BUILD_ID}
# The default Ansible inventory file 'hosts' is concatenated with the input HOSTS file
cat ../../gbif-configuration/environments/dev2/hosts \
${HOSTS_VOCABULARY} >> ${BUILD_HOSTS}
# Executes the Ansible playbook
echo "Executing Ansible playbook"
ansible-playbook -vvv -i ${BUILD_HOSTS} services.yml --private-key=~/.ssh/id_rsa --extra-vars "git_credentials=${GIT_CREDENTIALS}"
"""
}
}
}
}
post {
failure {
Expand Down
22 changes: 6 additions & 16 deletions api/src/main/java/org/gbif/vocabulary/api/ConceptView.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public class ConceptView implements Serializable, EntityView<Concept> {

@JsonUnwrapped private Concept concept;
private String vocabularyName;
private List<String> parents;
private Integer childrenCount;
private List<String> children;
Expand All @@ -48,37 +49,21 @@ public ConceptView(Concept concept, Integer childrenCount) {
this.childrenCount = childrenCount;
}

public Concept getConcept() {
return concept;
}

public ConceptView setConcept(Concept concept) {
this.concept = concept;
return this;
}

public List<String> getParents() {
return parents;
}

public ConceptView setParents(List<String> parents) {
this.parents = parents;
return this;
}

public Integer getChildrenCount() {
return childrenCount;
}

public ConceptView setChildrenCount(Integer childrenCount) {
this.childrenCount = childrenCount;
return this;
}

public List<String> getChildren() {
return children;
}

public ConceptView setChildren(List<String> children) {
this.children = children;
return this;
Expand All @@ -94,6 +79,11 @@ public ConceptView setHiddenLabelsLink(String hiddenLabelsLink) {
return this;
}

public ConceptView setVocabularyName(String vocabularyName) {
this.vocabularyName = vocabularyName;
return this;
}

@Override
public Concept getEntity() {
return concept;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.gbif.vocabulary.persistence.dto;

import lombok.Data;

import org.gbif.vocabulary.model.LanguageRegion;

@Data
public class ParentDto {
long key;
String name;
int depth;
Long labelKey;
LanguageRegion labelLanguage;
String labelValue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.gbif.vocabulary.persistence.dto;

import org.gbif.vocabulary.model.Label;

import java.util.List;

import lombok.Data;

@Data
public class SuggestDto {

private long key;
private String name;
private List<Label> labels;
private List<ParentDto> parentDtos;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import org.gbif.vocabulary.persistence.dto.SuggestDto;

/** Mapper for {@link Concept}. */
@Mapper
public interface ConceptMapper extends BaseMapper<Concept> {
Expand All @@ -53,10 +55,11 @@ void deprecateInBulk(

void updateParent(@Param("keys") List<Long> conceptKeys, @Param("parentKey") long parentKey);

List<KeyNameResult> suggest(
List<SuggestDto> suggest(
@Param("query") String query,
@Param("vocabularyKey") long vocabularyKey,
@Nullable @Param("lang") LanguageRegion language);
@Nullable @Param("lang") LanguageRegion language,
@Nullable @Param("fallbackLang") LanguageRegion fallbackLang);

/**
* Given a deprecated concept, it finds the current replacement, that's to say, the first
Expand Down Expand Up @@ -148,10 +151,11 @@ long countLatestRelease(
@Nullable @Param("params") ConceptSearchParams params,
@Param("vocabName") String vocabularyName);

List<KeyNameResult> suggestLatestRelease(
List<SuggestDto> suggestLatestRelease(
@Param("query") String query,
@Param("vocabularyKey") long vocabularyKey,
@Nullable @Param("lang") LanguageRegion language,
@Nullable @Param("lang") LanguageRegion languageFilter,
@Nullable @Param("fallbackLang") LanguageRegion fallbackLang,
@Param("vocabName") String vocabularyName);

List<String> findParentsLatestRelease(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ public interface ConceptService extends BaseService<Concept> {
* @param query suggestion
* @param vocabularyKey key of the vocabulary
* @param languageRegion locale to filter by
* @param fallbackLanguageRegion fallback locale to show in the response
* @return a list of up to 20 suggested concepts
*/
List<KeyNameResult> suggest(
String query, long vocabularyKey, @Nullable LanguageRegion languageRegion);
String query,
long vocabularyKey,
@Nullable LanguageRegion languageRegion,
@Nullable LanguageRegion fallbackLanguageRegion);

/**
* Deprecates a concept with a replacement.
Expand Down Expand Up @@ -183,13 +187,14 @@ PagingResponse<Concept> listLatestRelease(
@Nullable ConceptSearchParams params, @Nullable Pageable page, String vocabularyName);

/**
* It works as {@link #suggest(String, long, LanguageRegion)} but it queries the latest release of
* the vocabulary instead of the actual data.
* It works as {@link #suggest(String, long, LanguageRegion, LanguageRegion)} but it queries the
* latest release of the vocabulary instead of the actual data.
*/
List<KeyNameResult> suggestLatestRelease(
String query,
long vocabularyKey,
@Nullable LanguageRegion languageRegion,
@Nullable LanguageRegion fallbackLanguageRegion,
String vocabularyName);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@
import org.gbif.vocabulary.model.search.KeyNameResult;
import org.gbif.vocabulary.model.utils.PostPersist;
import org.gbif.vocabulary.model.utils.PrePersist;
import org.gbif.vocabulary.persistence.dto.ParentDto;
import org.gbif.vocabulary.persistence.dto.SuggestDto;
import org.gbif.vocabulary.persistence.mappers.ConceptMapper;
import org.gbif.vocabulary.persistence.mappers.VocabularyMapper;
import org.gbif.vocabulary.service.ConceptService;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -174,9 +178,81 @@ public PagingResponse<Concept> list(

@Override
public List<KeyNameResult> suggest(
String query, long vocabularyKey, @Nullable LanguageRegion languageRegion) {
String query,
long vocabularyKey,
@Nullable LanguageRegion languageRegion,
@Nullable LanguageRegion fallbackLanguageRegion) {
query = query != null ? query : "";
return conceptMapper.suggest(query, vocabularyKey, languageRegion);
List<SuggestDto> dtos =
conceptMapper.suggest(query, vocabularyKey, languageRegion, fallbackLanguageRegion);

return convertSuggestResults(languageRegion, fallbackLanguageRegion, dtos);
}

private static List<KeyNameResult> convertSuggestResults(
LanguageRegion languageRegion, LanguageRegion fallbackLanguageRegion, List<SuggestDto> dtos) {
return dtos.stream()
.map(
dto -> {
KeyNameResult keyNameResult = new KeyNameResult();
keyNameResult.setKey(dto.getKey());
keyNameResult.setName(dto.getName());
keyNameResult.setLabels(dto.getLabels());

Function<List<Label>, List<Label>> filterLabels =
labels -> {
if (languageRegion != null && fallbackLanguageRegion != null) {
// keep only the ones of the language requested if they exist
List<Label> langLabels =
labels.stream()
.filter(l -> l.getLanguage().equals(languageRegion))
.collect(Collectors.toList());

if (!langLabels.isEmpty()) {
return langLabels;
}
}
return labels;
};

keyNameResult.setLabels(filterLabels.apply(dto.getLabels()));

keyNameResult.setParents(
dto.getParentDtos().stream()
.collect(
Collectors.toMap(
ParentDto::getKey,
p -> {
KeyNameResult.Parent parent = new KeyNameResult.Parent();
parent.setKey(p.getKey());
parent.setName(p.getName());
parent.setDepth(p.getDepth());
Label label =
Label.builder()
.key(p.getLabelKey())
.value(p.getLabelValue())
.language(p.getLabelLanguage())
.build();
parent.getLabels().add(label);
return parent;
},
(p1, p2) -> {
p1.getLabels().addAll(p2.getLabels());
return p1;
}))
.values()
.stream()
.map(
p -> {
p.setLabels(filterLabels.apply(p.getLabels()));
return p;
})
.sorted(Comparator.comparing(KeyNameResult.Parent::getDepth))
.collect(Collectors.toList()));

return keyNameResult;
})
.collect(Collectors.toList());
}

@Secured({UserRoles.VOCABULARY_ADMIN, UserRoles.VOCABULARY_EDITOR})
Expand Down Expand Up @@ -480,13 +556,24 @@ public PagingResponse<Concept> listLatestRelease(

@Override
public List<KeyNameResult> suggestLatestRelease(
String query, long vocabularyKey, LanguageRegion languageRegion, String vocabularyName) {
String query,
long vocabularyKey,
LanguageRegion languageRegion,
LanguageRegion fallbackLanguageRegion,
String vocabularyName) {
checkArgument(!Strings.isNullOrEmpty(vocabularyName));
checkArgument(conceptMapper.existsReleaseView(vocabularyName.toLowerCase()));

query = query != null ? query : "";
return conceptMapper.suggestLatestRelease(
query, vocabularyKey, languageRegion, vocabularyName.toLowerCase());
List<SuggestDto> dtos =
conceptMapper.suggestLatestRelease(
query,
vocabularyKey,
languageRegion,
fallbackLanguageRegion,
vocabularyName.toLowerCase());

return convertSuggestResults(languageRegion, fallbackLanguageRegion, dtos);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/resources/core.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mybatis.type-handlers-package=org.gbif.vocabulary.persistence.handlers
mybatis.type-aliases-package=org.gbif.vocabulary.model;org.gbif.vocabulary.persistence.handlers
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=org.gbif.vocabulary.model;org.gbif.vocabulary.persistence.handlers;org.gbif.vocabulary.persistence.dto
mybatis.configuration.map-underscore-to-camel-case=true
Loading

0 comments on commit 471ce4a

Please sign in to comment.