Skip to content

Commit

Permalink
[kbss-cvut/termit-ui#449] Use first state and type encountered for a …
Browse files Browse the repository at this point in the history
…term.
  • Loading branch information
ledsoft committed Aug 29, 2024
1 parent 01c8be4 commit a4e54d6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ private void mapRowToTermAttributes(Term term, Row termRow) {
rtm -> mapSkosMatchProperties(term, SKOS.RELATED_MATCH, splitIntoMultipleValues(rtm)));
getAttributeValue(termRow, SKOS.EXACT_MATCH).ifPresent(
exm -> mapSkosMatchProperties(term, SKOS.EXACT_MATCH, splitIntoMultipleValues(exm)));
getAttributeValue(termRow, JsonLd.TYPE).flatMap(this::resolveTermType).ifPresent(t -> term.setTypes(Set.of(t)));
resolveTermState(getAttributeValue(termRow, Vocabulary.s_p_ma_stav_pojmu).orElse(null)).ifPresent(
getAttributeValue(termRow, JsonLd.TYPE).flatMap(t -> resolveTermType(t, term)).ifPresent(t -> term.setTypes(Set.of(t)));
resolveTermState(getAttributeValue(termRow, Vocabulary.s_p_ma_stav_pojmu).orElse(null), term).ifPresent(
term::setState);

}
Expand Down Expand Up @@ -296,14 +296,22 @@ private void mapSkosMatchProperties(Term subject, String property, Set<String> o
new ExcelImporter.TermRelationship(subject, propertyUri, new Term(uri))));
}

private Optional<String> resolveTermType(String value) {
private Optional<String> resolveTermType(String value, Term term) {
if (!Utils.emptyIfNull(term.getTypes()).isEmpty()) {
// Type already present from previous sheet
return Optional.empty();
}
return languageService.getTermTypes().stream()
.filter(t -> value.equals(t.getLabel().get(langTag)) || value.equals(
t.getUri().toString())).findFirst()
.map(t -> t.getUri().toString());
}

private Optional<URI> resolveTermState(String value) {
private Optional<URI> resolveTermState(String value, Term term) {
if (term.getState() != null) {
// State already present from previous sheet
return Optional.empty();
}
if (value == null) {
return languageService.getInitialTermState().map(RdfsResource::getUri);
}
Expand Down
Binary file modified src/main/resources/template/termit-import.xlsx
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import java.util.Set;
import java.util.function.Consumer;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -589,9 +591,14 @@ void importThrowsVocabularyImportExceptionWhenSheetContainsDuplicateLabels() thr
input.write(bos);

final VocabularyImportException ex = assertThrows(VocabularyImportException.class,
() -> sut.importVocabulary(
new VocabularyImporter.ImportConfiguration(false, vocabulary.getUri(), prePersist),
new VocabularyImporter.ImportInput(Constants.MediaType.EXCEL, new ByteArrayInputStream(bos.toByteArray()))));
() -> sut.importVocabulary(
new VocabularyImporter.ImportConfiguration(false,
vocabulary.getUri(),
prePersist),
new VocabularyImporter.ImportInput(
Constants.MediaType.EXCEL,
new ByteArrayInputStream(
bos.toByteArray()))));
assertEquals("error.vocabulary.import.excel.duplicateLabel", ex.getMessageId());
verify(termService, never()).addRootTermToVocabulary(any(), eq(vocabulary));
}
Expand All @@ -614,9 +621,51 @@ void importThrowsVocabularyImportExceptionWhenSheetContainsDuplicateIdentifiers(

final VocabularyImportException ex = assertThrows(VocabularyImportException.class,
() -> sut.importVocabulary(
new VocabularyImporter.ImportConfiguration(false, vocabulary.getUri(), prePersist),
new VocabularyImporter.ImportInput(Constants.MediaType.EXCEL, new ByteArrayInputStream(bos.toByteArray()))));
new VocabularyImporter.ImportConfiguration(false,
vocabulary.getUri(),
prePersist),
new VocabularyImporter.ImportInput(
Constants.MediaType.EXCEL,
new ByteArrayInputStream(
bos.toByteArray()))));
assertEquals("error.vocabulary.import.excel.duplicateIdentifier", ex.getMessageId());
verify(termService, never()).addRootTermToVocabulary(any(), eq(vocabulary));
}

@Test
void importSupportsSpecifyingStateAndTypeOnlyInOneSheet() throws Exception {
vocabulary.setUri(URI.create("http://example.com"));
when(vocabularyDao.exists(vocabulary.getUri())).thenReturn(true);
when(vocabularyDao.find(vocabulary.getUri())).thenReturn(Optional.of(vocabulary));
final Workbook input = new XSSFWorkbook(Environment.loadFile("template/termit-import.xlsx"));
final Sheet englishSheet = input.getSheet("English");
englishSheet.getRow(1).createCell(0).setCellValue("Construction");
final Sheet czechSheet = input.getSheet("Czech");
czechSheet.getRow(1).createCell(0).setCellValue("Konstrukce");
czechSheet.getRow(1).createCell(9).setCellValue("Publikovaný pojem");
czechSheet.getRow(1).createCell(5).setCellValue("Typ objektu");
final Term type = Generator.generateTermWithId();
type.setUri(URI.create("http://onto.fel.cvut.cz/ontologies/ufo/object-type"));
type.setLabel(MultilingualString.create("Object Type", Constants.DEFAULT_LANGUAGE));
type.getLabel().set("cs", "Typ objektu");
when(languageService.getTermTypes()).thenReturn(List.of(type));
final RdfsResource state = new RdfsResource(
URI.create("http://onto.fel.cvut.cz/ontologies/application/termit/pojem/publikovaný-pojem"),
MultilingualString.create("Published term", Constants.DEFAULT_LANGUAGE), null,
"http://onto.fel.cvut.cz/ontologies/slovník/agendový/popis-dat/pojem/stav-pojmu");
state.getLabel().set("cs", "Publikovaný pojem");
when(languageService.getTermStates()).thenReturn(List.of(state));
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
input.write(bos);

sut.importVocabulary(
new VocabularyImporter.ImportConfiguration(false, vocabulary.getUri(), prePersist),
new VocabularyImporter.ImportInput(Constants.MediaType.EXCEL,
new ByteArrayInputStream(bos.toByteArray())));
final ArgumentCaptor<Term> captor = ArgumentCaptor.forClass(Term.class);
verify(termService).addRootTermToVocabulary(captor.capture(), eq(vocabulary));
assertThat(captor.getValue().getTypes(), hasItem(type.getUri().toString()));
assertEquals(state.getUri(), captor.getValue().getState());
verify(languageService, never()).getInitialTermState();
}
}
Binary file modified src/test/resources/data/import-with-type-state-en.xlsx
Binary file not shown.

0 comments on commit a4e54d6

Please sign in to comment.