From 572e500589c034d2d1881f5821e57ff0b4b7c0fa Mon Sep 17 00:00:00 2001 From: lukaskabc Date: Thu, 25 Jul 2024 09:03:04 +0200 Subject: [PATCH] [Enhancement kbss-cvut/termit-ui#393] Support to load a label in a specific language --- .../kbss/termit/persistence/dao/DataDao.java | 30 +++++++++++++++++-- .../cvut/kbss/termit/rest/DataController.java | 14 +++++++-- .../notification/MessageAssetFactory.java | 2 +- .../repository/DataRepositoryService.java | 6 ++-- .../kbss/termit/rest/DataControllerTest.java | 4 +-- .../notification/MessageAssetFactoryTest.java | 2 +- 6 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/termit/persistence/dao/DataDao.java b/src/main/java/cz/cvut/kbss/termit/persistence/dao/DataDao.java index ded878cee..9d7838fd3 100644 --- a/src/main/java/cz/cvut/kbss/termit/persistence/dao/DataDao.java +++ b/src/main/java/cz/cvut/kbss/termit/persistence/dao/DataDao.java @@ -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; @@ -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 @@ -139,13 +146,30 @@ public Optional find(URI id) { /** * Gets the {@link RDFS#LABEL} of a resource with the specified identifier. *

- * 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 getLabel(URI id) { + return getLabel(id, null); + } + + /** + * Gets the {@link RDFS#LABEL} of a resource with the specified identifier. + *

+ * 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 getLabel(URI id, @Nullable String language) { Objects.requireNonNull(id); + if(language == null) { + language = config.getLanguage(); + } if (!id.isAbsolute()) { return Optional.of(id.toString()); } @@ -159,7 +183,7 @@ public Optional 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(); } diff --git a/src/main/java/cz/cvut/kbss/termit/rest/DataController.java b/src/main/java/cz/cvut/kbss/termit/rest/DataController.java index 23570df99..b072fc78c 100644 --- a/src/main/java/cz/cvut/kbss/termit/rest/DataController.java +++ b/src/main/java/cz/cvut/kbss/termit/rest/DataController.java @@ -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; @@ -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.")); } } diff --git a/src/main/java/cz/cvut/kbss/termit/service/notification/MessageAssetFactory.java b/src/main/java/cz/cvut/kbss/termit/service/notification/MessageAssetFactory.java index f7e40da89..ae143419c 100644 --- a/src/main/java/cz/cvut/kbss/termit/service/notification/MessageAssetFactory.java +++ b/src/main/java/cz/cvut/kbss/termit/service/notification/MessageAssetFactory.java @@ -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 diff --git a/src/main/java/cz/cvut/kbss/termit/service/repository/DataRepositoryService.java b/src/main/java/cz/cvut/kbss/termit/service/repository/DataRepositoryService.java index fdbb2e60e..486f82cd5 100644 --- a/src/main/java/cz/cvut/kbss/termit/service/repository/DataRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/termit/service/repository/DataRepositoryService.java @@ -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; @@ -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 getLabel(URI id) { - return dataDao.getLabel(id); + public Optional getLabel(URI id, @Nullable String language) { + return dataDao.getLabel(id, language); } } diff --git a/src/test/java/cz/cvut/kbss/termit/rest/DataControllerTest.java b/src/test/java/cz/cvut/kbss/termit/rest/DataControllerTest.java index 33771355a..22a26ce6d 100644 --- a/src/test/java/cz/cvut/kbss/termit/rest/DataControllerTest.java +++ b/src/test/java/cz/cvut/kbss/termit/rest/DataControllerTest.java @@ -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)); @@ -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()); } diff --git a/src/test/java/cz/cvut/kbss/termit/service/notification/MessageAssetFactoryTest.java b/src/test/java/cz/cvut/kbss/termit/service/notification/MessageAssetFactoryTest.java index c52936f5b..4f2522d9d 100644 --- a/src/test/java/cz/cvut/kbss/termit/service/notification/MessageAssetFactoryTest.java +++ b/src/test/java/cz/cvut/kbss/termit/service/notification/MessageAssetFactoryTest.java @@ -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);