From edfbbe5bb4b891d98e0b00c3fcb8389aaf81c872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Ka=C5=88ka?= Date: Thu, 14 Nov 2024 14:19:35 +0100 Subject: [PATCH] [Enhancement kbss-cvut/termit-ui#520] Fix change record filter query --- .../dao/changetracking/ChangeRecordDao.java | 34 ++++++++++--------- .../persistence/dao/VocabularyDaoTest.java | 21 +++++------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/termit/persistence/dao/changetracking/ChangeRecordDao.java b/src/main/java/cz/cvut/kbss/termit/persistence/dao/changetracking/ChangeRecordDao.java index 38e640f2..c0e66098 100644 --- a/src/main/java/cz/cvut/kbss/termit/persistence/dao/changetracking/ChangeRecordDao.java +++ b/src/main/java/cz/cvut/kbss/termit/persistence/dao/changetracking/ChangeRecordDao.java @@ -100,17 +100,16 @@ public List findAllFiltered(URI changeContext, ChangeRecor ?hasTime ?timestamp ; ?hasAuthor ?author . """ + /* Find an asset type if it is known (deleted assets does not have a type */ """ - - OPTIONAL { - ?asset a ?assetType - } OPTIONAL { - ?asset a ?assetTypeVal . - BIND(true as ?isAssetType) + ?asset a ?assetType . + OPTIONAL { + ?asset a ?assetTypeValue + BIND(true as ?isAssetType) + } } + FILTER(!BOUND(?assetType) || ?isAssetType) """ + /* filter assets without a type (deleted) or with a matching type */ """ - BIND(?assetTypeVal as ?assetTypeVar) - FILTER(!BOUND(?assetType) || !BOUND(?assetTypeVar) || BOUND(?isAssetType)) + """ + /* Get author's name */ """ ?author ?hasFirstName ?firstName ; ?hasLastName ?lastName . @@ -122,26 +121,29 @@ public List findAllFiltered(URI changeContext, ChangeRecor } """ + /* Get asset's name (but the asset might have been already deleted) */ """ OPTIONAL { - ?asset ?hasLabel ?label . + ?asset ?hasLabel ?assetPrefLabel . + BIND(?assetPrefLabel as ?finalAssetLabel) } OPTIONAL { - ?asset ?hasRdfsLabel ?label . + ?asset ?hasRdfsLabel ?assetRdfsLabel . + BIND(?assetRdfsLabel as ?finalAssetLabel) } """ + /* then try to get the label from (delete) record */ """ OPTIONAL { - ?record ?hasRdfsLabel ?label . + ?record ?hasRdfsLabel ?recordRdfsLabel . + BIND(?recordRdfsLabel as ?finalAssetLabel) } """ + /* When label is still not bound, the term was probably deleted, find the delete record and get the label from it */ """ OPTIONAL { - FILTER(!BOUND(?label)) . ?deleteRecord a ?deleteRecordType; ?hasChangedEntity ?asset; - ?hasRdfsLabel ?label . + ?hasRdfsLabel ?deleteRecordLabel . + BIND(?deleteRecordLabel as ?finalAssetLabel) } BIND(?assetLabelValue as ?assetLabel) BIND(?authorNameValue as ?authorName) BIND(?attributeNameValue as ?changedAttributeName) - FILTER (!BOUND(?assetLabel) || CONTAINS(LCASE(?label), LCASE(?assetLabel))) + FILTER (!BOUND(?assetLabel) || CONTAINS(LCASE(?finalAssetLabel), LCASE(?assetLabel))) FILTER (!BOUND(?authorName) || CONTAINS(LCASE(?authorFullName), LCASE(?authorName))) FILTER (!BOUND(?changedAttributeName) || CONTAINS(LCASE(?changedAttributeLabel), LCASE(?changedAttributeName))) } ORDER BY DESC(?timestamp) ?attribute @@ -161,12 +163,12 @@ public List findAllFiltered(URI changeContext, ChangeRecor .setParameter("hasLabel", URI.create(SKOS.PREF_LABEL)) // Optional asset label - .setParameter("deleteRecordType", Vocabulary.s_c_smazani_entity); + .setParameter("deleteRecordType", URI.create(Vocabulary.s_c_smazani_entity)); if(asset.isPresent() && asset.get().getUri() != null) { query = query.setParameter("asset", asset.get().getUri()); } else if (assetType.isPresent()) { - query = query.setParameter("assetTypeVal", assetType.get()); + query = query.setParameter("assetTypeValue", assetType.get()); } diff --git a/src/test/java/cz/cvut/kbss/termit/persistence/dao/VocabularyDaoTest.java b/src/test/java/cz/cvut/kbss/termit/persistence/dao/VocabularyDaoTest.java index fa795b57..df833d8a 100644 --- a/src/test/java/cz/cvut/kbss/termit/persistence/dao/VocabularyDaoTest.java +++ b/src/test/java/cz/cvut/kbss/termit/persistence/dao/VocabularyDaoTest.java @@ -19,6 +19,7 @@ import cz.cvut.kbss.jopa.model.EntityManager; import cz.cvut.kbss.jopa.model.MultilingualString; +import cz.cvut.kbss.jopa.model.annotations.OWLClass; import cz.cvut.kbss.jopa.model.descriptors.Descriptor; import cz.cvut.kbss.jopa.vocabulary.RDFS; import cz.cvut.kbss.jopa.vocabulary.SKOS; @@ -1056,8 +1057,10 @@ void getDetailedHistoryOfContentReturnsRecordsOfDeletedTermFilteredByTermName() final Term firstTerm = Generator.generateTermWithId(vocabulary.getUri()); // the needle is placed in the term which will be removed firstTerm.getLabel().set(Environment.LANGUAGE, mud); + firstTerm.setVocabulary(vocabulary.getUri()); final Term termToRemove = Generator.generateTermWithId(vocabulary.getUri()); termToRemove.getLabel().set(Environment.LANGUAGE, haystack); + termToRemove.setVocabulary(vocabulary.getUri()); final List firstChanges = Generator.generateChangeRecords(firstTerm, author); final List termToRemoveChanges = Generator.generateChangeRecords(termToRemove, author); @@ -1213,20 +1216,14 @@ void getDetailedHistoryOfContentReturnsRecordsOfExistingTermFilteredByAuthorName } @ParameterizedTest - @ValueSource(strings = { - cz.cvut.kbss.termit.util.Vocabulary.s_c_uprava_entity, - cz.cvut.kbss.termit.util.Vocabulary.s_c_vytvoreni_entity, - cz.cvut.kbss.termit.util.Vocabulary.s_c_smazani_entity, + @ValueSource(classes = { + UpdateChangeRecord.class, + PersistChangeRecord.class, + DeleteChangeRecord.class }) - void getDetailedHistoryOfContentReturnsRecordsOfExistingTermFilteredByChangeType(String type) { + void getDetailedHistoryOfContentReturnsRecordsOfExistingTermFilteredByChangeType(Class typeClass) { enableRdfsInference(em); - final URI typeUri = URI.create(type); - final Class typeClass = switch (type) { - case cz.cvut.kbss.termit.util.Vocabulary.s_c_uprava_entity -> UpdateChangeRecord.class; - case cz.cvut.kbss.termit.util.Vocabulary.s_c_vytvoreni_entity -> PersistChangeRecord.class; - case cz.cvut.kbss.termit.util.Vocabulary.s_c_smazani_entity -> DeleteChangeRecord.class; - default -> throw new IllegalArgumentException("Unknown change type: " + type); - }; + final URI typeUri = URI.create(typeClass.getAnnotation(OWLClass.class).iri()); // Two terms with needle in the label, one term without needle in the label final Vocabulary vocabulary = Generator.generateVocabularyWithId();