Skip to content

Commit

Permalink
Merge branch 'dev_jdk8'
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-lg committed Sep 11, 2024
2 parents 3b0704e + 13de005 commit 05e9b05
Show file tree
Hide file tree
Showing 33 changed files with 2,418 additions and 321 deletions.
Binary file added Roadmap for GBIF vocabularies.pdf
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.gbif.vocabulary.persistence.dto;

import lombok.Data;
import org.gbif.vocabulary.model.LanguageRegion;

@Data
public class LookupDto {

private long key;
private String name;
private String label;
private LanguageRegion labelLang;
private String altLabel;
private LanguageRegion altLabelLang;
private String hiddenLabel;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
package org.gbif.vocabulary.persistence.mappers;

import java.util.List;
import javax.annotation.Nullable;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.gbif.api.model.common.paging.Pageable;
import org.gbif.vocabulary.model.Concept;
import org.gbif.vocabulary.model.Definition;
Expand All @@ -23,15 +27,9 @@
import org.gbif.vocabulary.model.search.ChildrenResult;
import org.gbif.vocabulary.model.search.ConceptSearchParams;
import org.gbif.vocabulary.model.search.KeyNameResult;
import org.gbif.vocabulary.persistence.dto.LookupDto;
import org.gbif.vocabulary.persistence.dto.SuggestDto;

import java.util.List;

import javax.annotation.Nullable;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/** Mapper for {@link Concept}. */
@Mapper
public interface ConceptMapper extends BaseMapper<Concept> {
Expand Down Expand Up @@ -197,4 +195,11 @@ List<HiddenLabel> listHiddenLabelsLatestRelease(

long countHiddenLabelsLatestRelease(
@Param("entityKey") long entityKey, @Param("vocabName") String vocabularyName);

List<LookupDto> lookup(@Param("value") String value, @Param("vocabularyKey") long vocabularyKey);

List<LookupDto> lookupLatestRelease(
@Param("value") String value,
@Param("vocabularyKey") long vocabularyKey,
@Param("vocabName") String vocabularyName);
}
35 changes: 28 additions & 7 deletions core/src/main/java/org/gbif/vocabulary/service/ConceptService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*/
package org.gbif.vocabulary.service;

import java.util.List;
import javax.annotation.Nullable;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import org.gbif.api.model.common.paging.Pageable;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.vocabulary.model.Concept;
Expand All @@ -23,15 +28,9 @@
import org.gbif.vocabulary.model.Tag;
import org.gbif.vocabulary.model.search.ChildrenResult;
import org.gbif.vocabulary.model.search.ConceptSearchParams;
import org.gbif.vocabulary.model.search.LookupResult;
import org.gbif.vocabulary.model.search.SuggestResult;

import java.util.List;

import javax.annotation.Nullable;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/** Services for a {@link Concept}. */
public interface ConceptService extends BaseService<Concept> {

Expand Down Expand Up @@ -248,4 +247,26 @@ PagingResponse<Label> listAlternativeLabelsLatestRelease(
*/
PagingResponse<HiddenLabel> listHiddenLabelsLatestRelease(
long entityKey, @Nullable Pageable page, String vocabularyName);

/**
* Lookups concepts that match the given value.
*
* @param value value to match against the concepts
* @param vocabularyName vocabulary to use in the lookup
* @param languageRegion language to use as discriminator. English is used as fallback.
* @return list of the concepts found
*/
List<LookupResult> lookup(
String value, String vocabularyName, @Nullable LanguageRegion languageRegion);

/**
* It works as
*
* @param value value to match against the concepts
* @param vocabularyName vocabulary to use in the lookup
* @param languageRegion language to use as discriminator. English is used as fallback.
* @return list of the concepts found
*/
List<LookupResult> lookupLatestRelease(
String value, String vocabularyName, @Nullable LanguageRegion languageRegion);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,70 @@
*/
package org.gbif.vocabulary.service.impl;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static org.gbif.vocabulary.model.normalizers.StringNormalizer.*;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import org.gbif.api.model.common.paging.Pageable;
import org.gbif.api.model.common.paging.PagingRequest;
import org.gbif.api.model.common.paging.PagingResponse;
import org.gbif.vocabulary.model.*;
import org.gbif.vocabulary.model.exception.EntityNotFoundException;
import org.gbif.vocabulary.model.normalizers.StringNormalizer;
import org.gbif.vocabulary.model.search.ChildrenResult;
import org.gbif.vocabulary.model.search.ConceptSearchParams;
import org.gbif.vocabulary.model.search.KeyNameResult;
import org.gbif.vocabulary.model.search.LookupResult;
import org.gbif.vocabulary.model.search.SuggestResult;
import org.gbif.vocabulary.model.utils.PathUtils;
import org.gbif.vocabulary.model.utils.PostPersist;
import org.gbif.vocabulary.model.utils.PrePersist;
import org.gbif.vocabulary.persistence.dto.LookupDto;
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.stream.Collectors;

import javax.annotation.Nullable;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static org.gbif.vocabulary.model.normalizers.StringNormalizer.*;

/** Default implementation for {@link ConceptService}. */
@Service
@Validated
public class DefaultConceptService implements ConceptService {

private static final int DEFAULT_SUGGEST_LIMIT = 20;
private static final String CONCEPT_LINK = "%s/vocabularies/%s/concepts/%s";
private final ConceptMapper conceptMapper;
private final VocabularyMapper vocabularyMapper;
private final String apiUrl;

@Autowired
public DefaultConceptService(ConceptMapper conceptMapper, VocabularyMapper vocabularyMapper) {
public DefaultConceptService(
ConceptMapper conceptMapper,
VocabularyMapper vocabularyMapper,
@Value("${ws.apiUrl}") String apiUrl) {
this.conceptMapper = conceptMapper;
this.vocabularyMapper = vocabularyMapper;
this.apiUrl = apiUrl;
}

@Override
Expand Down Expand Up @@ -403,7 +412,10 @@ private void checkSimilarLabels(long entityKey, Label label) {
long vocabularyKey = conceptMapper.getVocabularyKey(entityKey);
List<KeyNameResult> similarities =
conceptMapper.findSimilarities(
normalizeLabel(label.getValue()), label.getLanguage(), vocabularyKey, entityKey);
replaceNonAsciiCharactersWithEquivalents(normalizeLabel(label.getValue())),
label.getLanguage(),
vocabularyKey,
entityKey);
if (!similarities.isEmpty()) {
throw new IllegalArgumentException(
"Cannot create entity because it conflicts with other entities, e.g.: " + similarities);
Expand Down Expand Up @@ -446,7 +458,10 @@ private void checkSimilarHiddenLabels(long entityKey, HiddenLabel label) {
long vocabularyKey = conceptMapper.getVocabularyKey(entityKey);
List<KeyNameResult> similarities =
conceptMapper.findSimilarities(
normalizeName(label.getValue()), null, vocabularyKey, entityKey);
replaceNonAsciiCharactersWithEquivalents(normalizeName(label.getValue())),
null,
vocabularyKey,
entityKey);
if (!similarities.isEmpty()) {
throw new IllegalArgumentException(
"Cannot create entity because it conflicts with other entities, e.g.: " + similarities);
Expand Down Expand Up @@ -563,18 +578,21 @@ public List<ChildrenResult> countChildrenLatestRelease(
@Override
public List<Definition> listDefinitionsLatestRelease(
long entityKey, List<LanguageRegion> languageRegions, String vocabularyName) {
checkReleaseExists(vocabularyName);
return conceptMapper.listDefinitionsLatestRelease(entityKey, languageRegions, vocabularyName);
}

@Override
public List<Label> listLabelsLatestRelease(
long entityKey, List<LanguageRegion> languageRegions, String vocabularyName) {
checkReleaseExists(vocabularyName);
return conceptMapper.listLabelsLatestRelease(entityKey, languageRegions, vocabularyName);
}

@Override
public PagingResponse<Label> listAlternativeLabelsLatestRelease(
long entityKey, List<LanguageRegion> languageRegions, Pageable page, String vocabularyName) {
checkReleaseExists(vocabularyName);
page = page != null ? page : new PagingRequest();
return new PagingResponse<>(
page,
Expand All @@ -587,13 +605,89 @@ public PagingResponse<Label> listAlternativeLabelsLatestRelease(
@Override
public PagingResponse<HiddenLabel> listHiddenLabelsLatestRelease(
long entityKey, Pageable page, String vocabularyName) {
checkReleaseExists(vocabularyName);
page = page != null ? page : new PagingRequest();
return new PagingResponse<>(
page,
conceptMapper.countHiddenLabelsLatestRelease(entityKey, vocabularyName),
conceptMapper.listHiddenLabelsLatestRelease(entityKey, page, vocabularyName));
}

@Override
public List<LookupResult> lookup(
String value, String vocabularyName, @Nullable LanguageRegion languageRegion) {
return lookupInternal(value, vocabularyName, languageRegion, false);
}

@Override
public List<LookupResult> lookupLatestRelease(
String value, String vocabularyName, LanguageRegion languageRegion) {
checkReleaseExists(vocabularyName);
return lookupInternal(value, vocabularyName, languageRegion, true);
}

private List<LookupResult> lookupInternal(
String value, String vocabularyName, LanguageRegion languageRegion, boolean latestRelease) {
if (Strings.isNullOrEmpty(value)) {
return Collections.emptyList();
}

Long vocabularyKey = vocabularyMapper.getKeyByName(vocabularyName);
Objects.requireNonNull(vocabularyKey);

String normalizedValue = StringNormalizer.replaceNonAsciiCharactersWithEquivalents(value);
List<LookupDto> dtos = new ArrayList<>();
if (latestRelease) {
dtos = conceptMapper.lookupLatestRelease(normalizedValue, vocabularyKey, vocabularyName);
} else {
dtos = conceptMapper.lookup(normalizedValue, vocabularyKey);
}

if (!dtos.isEmpty() && languageRegion != null) {
List<LookupDto> dtosByLang =
dtos.stream()
.filter(
d -> d.getLabelLang() == languageRegion || d.getAltLabelLang() == languageRegion)
.collect(Collectors.toList());

if (dtosByLang.isEmpty()) {
// we use English as fallback
dtosByLang =
dtos.stream()
.filter(
d ->
(d.getLabelLang() == null && d.getAltLabelLang() == null)
|| d.getLabelLang() == LanguageRegion.ENGLISH
|| d.getAltLabelLang() == LanguageRegion.ENGLISH)
.collect(Collectors.toList());
}

dtos = dtosByLang;
}

return dtos.stream()
.map(
d -> {
LookupResult lookupResult = new LookupResult();
lookupResult.setConceptName(d.getName());
lookupResult.setMatchedLabel(d.getLabel());
lookupResult.setMatchedLabelLanguage(d.getLabelLang());
lookupResult.setMatchedAlternativeLabel(d.getAltLabel());
lookupResult.setMatchedAlternativeLabelLanguage(d.getAltLabelLang());
lookupResult.setMatchedHiddenLabel(d.getHiddenLabel());
lookupResult.setConceptLink(
String.format(
CONCEPT_LINK,
apiUrl,
vocabularyName,
latestRelease
? PathUtils.LATEST_RELEASE_PATH + "/" + d.getName()
: d.getName()));
return lookupResult;
})
.collect(Collectors.toList());
}

private void checkReleaseExists(String vocabularyName) {
if (!existsLatestReleaseView(vocabularyName)) {
throw new EntityNotFoundException(
Expand Down
Loading

0 comments on commit 05e9b05

Please sign in to comment.