Skip to content

Commit

Permalink
[Fix] Fail gracefully when imported Excel references unknown terms.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Jan 19, 2025
1 parent 9cc2aba commit ae1c089
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ private void mapSkosRelated(Term subject, Set<String> objects) {
try {
final Term objectTerm = getTerm(object);
if (objectTerm == null) {
LOG.warn("No term with label '{}' found for term '{}' and relationship <{}>.", object,
LOG.warn("No term identified by '{}' found for term '{}' and relationship <{}>.", object,
subject.getLabel().get(langTag), SKOS.RELATED);
} else {
// Term IDs may not be generated, yet
Expand All @@ -254,8 +254,13 @@ private void mapSkosRelated(Term subject, Set<String> objects) {
}

private Term getTerm(String identification) {
return labelToTerm.containsKey(identification) ? labelToTerm.get(identification) :
idToTerm.get(URI.create(prefixMap.resolvePrefixed(identification)));
try {
return labelToTerm.containsKey(identification) ? labelToTerm.get(identification) :
idToTerm.get(URI.create(prefixMap.resolvePrefixed(identification)));
} catch (IllegalArgumentException e) {
LOG.warn("'{}' is not a known term label nor is it a valid URI. Skipping it.", identification);
return null;
}
}

private void mapSkosMatchProperties(Term subject, String property, Set<String> objects) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import cz.cvut.kbss.termit.service.repository.TermRepositoryService;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Constants;
import cz.cvut.kbss.termit.util.Utils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
Expand Down Expand Up @@ -768,4 +769,29 @@ void importTermTranslationsThrowsVocabularyImportExceptionWhenExcelDoesNotContai
assertEquals("error.vocabulary.import.excel.missingIdentifierOrLabel", ex.getMessageId());
verify(termService, never()).update(any());
}

@Test
void importSkipsUnknownParentDeclaration() throws Exception {
initVocabularyResolution();
final Workbook input = new XSSFWorkbook(Environment.loadFile("template/termit-import.xlsx"));
final Sheet sheet = input.getSheet("English");
sheet.shiftColumns(0, 12, 1);
sheet.getRow(0).createCell(0).setCellValue("Identifier");
sheet.getRow(1).createCell(0).setCellValue("http://example.com/terms/Construction");
sheet.getRow(1).getCell(1).setCellValue("Construction");
sheet.getRow(1).getCell(8).setCellValue("Unknown parent");
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));
assertTrue(Utils.emptyIfNull(captor.getValue().getParentTerms()).isEmpty());
}
}

0 comments on commit ae1c089

Please sign in to comment.