Skip to content

Commit

Permalink
[kbss-cvut/termit-ui#449] Support multiple types per term in Excel im…
Browse files Browse the repository at this point in the history
…port.
  • Loading branch information
ledsoft committed Aug 30, 2024
1 parent 9316518 commit c1bf874
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-rdfxml</artifactId>
<version>5.0.0</version>
<version>5.0.2</version>
</dependency>

<!-- Spring declarative transactions with JOPA -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,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(t -> resolveTermType(t, term))
.ifPresent(t -> term.setTypes(Set.of(t)));
getAttributeValue(termRow, JsonLd.TYPE).map(str -> resolveTermTypes(splitIntoMultipleValues(str), term))
.ifPresent(term::setTypes);
resolveTermState(getAttributeValue(termRow, Vocabulary.s_p_ma_stav_pojmu).orElse(null), term).ifPresent(
term::setState);

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

private Optional<String> resolveTermType(String value, Term term) {
private Set<String> resolveTermTypes(Set<String> values, Term term) {
if (!Utils.emptyIfNull(term.getTypes()).isEmpty()) {
// Type already present from previous sheet
return Optional.empty();
return term.getTypes();
}
return languageService.getTermTypes().stream()
.filter(t -> value.equals(t.getLabel().get(langTag)) || value.equals(
t.getUri().toString())).findFirst()
.map(t -> t.getUri().toString());
final List<Term> availableTypes = languageService.getTermTypes();
return values.stream().map(str -> availableTypes.stream()
.filter(t -> str.equals(
t.getLabel().get(langTag)) || str.equals(
t.getUri().toString())).findFirst()
.map(t -> t.getUri().toString())
.orElse(str))
.collect(Collectors.toSet());
}

private Optional<URI> resolveTermState(String value, Term term) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
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 @@ -647,4 +648,34 @@ void importThrowsVocabularyImportExceptionWhenVocabularyAlreadyContainsTermWithS
Environment.loadFile(
"data/import-simple-en.xlsx"))));
}

@Test
void importSupportsMultipleTypesDeclaredForTerm() 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 Term objectType = Generator.generateTermWithId();
objectType.setUri(URI.create("http://onto.fel.cvut.cz/ontologies/ufo/object-type"));
objectType.setLabel(MultilingualString.create("Object Type", Constants.DEFAULT_LANGUAGE));
final Term eventType = Generator.generateTermWithId();
eventType.setUri(URI.create("http://onto.fel.cvut.cz/ontologies/ufo/event-type"));
eventType.setLabel(MultilingualString.create("Event Type", Constants.DEFAULT_LANGUAGE));
when(languageService.getTermTypes()).thenReturn(List.of(objectType, eventType));
englishSheet.getRow(1).createCell(5)
.setCellValue(objectType.getLabel().get() + ";" + eventType.getLabel().get());
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(),
hasItems(objectType.getUri().toString(), eventType.getUri().toString()));
}
}

0 comments on commit c1bf874

Please sign in to comment.