Skip to content

Commit

Permalink
[Enhancement kbss-cvut/termit-ui#393] Support to load a label in a sp…
Browse files Browse the repository at this point in the history
…ecific language
  • Loading branch information
lukaskabc authored and ledsoft committed Jul 25, 2024
1 parent 64eaa02 commit 572e500
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
30 changes: 27 additions & 3 deletions src/main/java/cz/cvut/kbss/termit/persistence/dao/DataDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Configuration.Persistence;
import cz.cvut.kbss.termit.util.TypeAwareResource;
import jakarta.annotation.Nullable;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.repository.RepositoryConnection;
Expand All @@ -41,7 +42,13 @@

import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Repository
Expand Down Expand Up @@ -139,13 +146,30 @@ public Optional<RdfsResource> find(URI id) {
/**
* Gets the {@link RDFS#LABEL} of a resource with the specified identifier.
* <p>
* Note that the label has to have matching language tag or no language tag at all (matching tag is preferred).
* Note that the label has to have language tag matching the configured persistence unit language
* or no language tag at all (matching tag is preferred).
*
* @param id Resource ({@link RDFS#RESOURCE}) identifier
* @return Matching resource identifier (if found)
*/
public Optional<String> getLabel(URI id) {
return getLabel(id, null);
}

/**
* Gets the {@link RDFS#LABEL} of a resource with the specified identifier.
* <p>
* Note that the label has to have matching language tag or no language tag at all (matching tag is preferred).
*
* @param id Resource ({@link RDFS#RESOURCE}) identifier
* @param language Label language, if null, configured persistence unit language is used instead
* @return Matching resource identifier (if found)
*/
public Optional<String> getLabel(URI id, @Nullable String language) {
Objects.requireNonNull(id);
if(language == null) {
language = config.getLanguage();
}
if (!id.isAbsolute()) {
return Optional.of(id.toString());
}
Expand All @@ -159,7 +183,7 @@ public Optional<String> getLabel(URI id) {
String.class)
.setParameter("x", id).setParameter("has-label", RDFS_LABEL)
.setParameter("has-title", URI.create(DC.Terms.TITLE))
.setParameter("tag", config.getLanguage(), null).getSingleResult());
.setParameter("tag", language, null).getSingleResult());
} catch (NoResultException | NoUniqueResultException e) {
return Optional.empty();
}
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/cz/cvut/kbss/termit/rest/DataController.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.util.List;
Expand Down Expand Up @@ -92,8 +97,11 @@ public RdfsResource getById(@Parameter(description = "Identifier of the resource
})
@GetMapping(value = "/label")
public String getLabel(@Parameter(description = "Resource identifier.")
@RequestParam("iri") URI id) {
return dataService.getLabel(id).orElseThrow(
@RequestParam("iri") URI id,
@Parameter(description = "Label language")
@RequestParam(value = "language", required = false) String language
) {
return dataService.getLabel(id, language).orElseThrow(
() -> new NotFoundException("Resource with id " + id + " not found or it has no matching label."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private MessageLabelExtractor(DataRepositoryService dataService) {

@Override
public void visitTerm(AbstractTerm term) {
this.label = term.getPrimaryLabel() + " (" + dataService.getLabel(term.getVocabulary()).orElse("") + ")";
this.label = term.getPrimaryLabel() + " (" + dataService.getLabel(term.getVocabulary(), null).orElse("") + ")";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import cz.cvut.kbss.termit.dto.RdfsResource;
import cz.cvut.kbss.termit.persistence.dao.DataDao;
import jakarta.annotation.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -71,9 +72,10 @@ public void persistProperty(RdfsResource property) {
* Gets the label of a resource with the specified identifier.
*
* @param id Resource identifier
* @param language Label language, if null, configured persistence unit language is used instead
* @return Matching resource identifier (if found)
*/
public Optional<String> getLabel(URI id) {
return dataDao.getLabel(id);
public Optional<String> getLabel(URI id, @Nullable String language) {
return dataDao.getLabel(id, language);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void getByIdThrowsNotFoundExceptionForUnknownResourceIdentifier() throws Excepti
void getLabelReturnsLabelOfResourceWithSpecifiedIdAsString() throws Exception {
final URI uri = Generator.generateUri();
final String label = "Test term";
when(dataServiceMock.getLabel(uri)).thenReturn(Optional.of(label));
when(dataServiceMock.getLabel(uri, null)).thenReturn(Optional.of(label));
final MvcResult mvcResult = mockMvc.perform(get("/data/label").param("iri", uri.toString()))
.andExpect(status().isOk()).andReturn();
assertEquals(label, readValue(mvcResult, String.class));
Expand All @@ -109,7 +109,7 @@ void getLabelReturnsLabelOfResourceWithSpecifiedIdAsString() throws Exception {
@Test
void getLabelThrowsNotFoundExceptionWhenLabelIsNotFound() throws Exception {
final URI uri = Generator.generateUri();
when(dataServiceMock.getLabel(any())).thenReturn(Optional.empty());
when(dataServiceMock.getLabel(any(), any())).thenReturn(Optional.empty());
mockMvc.perform(get("/data/label").param("iri", uri.toString())).andExpect(status().isNotFound());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void createAddsTermVocabularyLabelInParenthesesAsMessageAssetLabel() throws Exce
final String vocabularyLabel = "Vocabulary " + Generator.randomInt(0, 1000);
final Term term = Generator.generateTermWithId();
when(linkBuilder.linkTo(term)).thenReturn(term.getUri().toString());
when(dataService.getLabel(term.getVocabulary())).thenReturn(Optional.of(vocabularyLabel));
when(dataService.getLabel(term.getVocabulary(), null)).thenReturn(Optional.of(vocabularyLabel));
// Simulate autowired configuration
final Field configField = Term.class.getDeclaredField("config");
configField.setAccessible(true);
Expand Down

0 comments on commit 572e500

Please sign in to comment.