-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kbss-cvut/termit-ui#449] Resolve vocabulary importer based on provid…
…ed media type.
- Loading branch information
Showing
10 changed files
with
226 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/cz/cvut/kbss/termit/service/importer/ExcelImporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cz.cvut.kbss.termit.service.importer; | ||
|
||
import cz.cvut.kbss.termit.model.Vocabulary; | ||
import cz.cvut.kbss.termit.util.Constants; | ||
import org.springframework.beans.factory.config.ConfigurableBeanFactory; | ||
import org.springframework.context.annotation.Scope; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) | ||
public class ExcelImporter implements VocabularyImporter { | ||
|
||
/** | ||
* Media type for legacy .xls files. | ||
*/ | ||
private static final String XLS_MEDIA_TYPE = "application/vnd.ms-excel"; | ||
|
||
@Override | ||
public Vocabulary importVocabulary(ImportConfiguration config, ImportInput data) { | ||
// TODO | ||
return null; | ||
} | ||
|
||
/** | ||
* Checks whether this importer supports the specified media type. | ||
* | ||
* @param mediaType Media type to check | ||
* @return {@code true} when media type is supported, {@code false} otherwise | ||
*/ | ||
public static boolean supportsMediaType(@NonNull String mediaType) { | ||
return Constants.MediaType.EXCEL.equals(mediaType) || XLS_MEDIA_TYPE.equals(mediaType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/cz/cvut/kbss/termit/service/importer/VocabularyImporters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cz.cvut.kbss.termit.service.importer; | ||
|
||
import cz.cvut.kbss.termit.exception.importing.UnsupportedImportMediaTypeException; | ||
import cz.cvut.kbss.termit.model.Vocabulary; | ||
import cz.cvut.kbss.termit.persistence.dao.skos.SKOSImporter; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Ensures correct importer is invoked for provided media types. | ||
*/ | ||
@Component | ||
public class VocabularyImporters implements VocabularyImporter { | ||
|
||
private final ApplicationContext appContext; | ||
|
||
public VocabularyImporters(ApplicationContext appContext) { | ||
this.appContext = appContext; | ||
} | ||
|
||
@Override | ||
public Vocabulary importVocabulary(@NonNull ImportConfiguration config, @NonNull ImportInput data) { | ||
if (SKOSImporter.supportsMediaType(data.mediaType())) { | ||
return getSkosImporter().importVocabulary(config, data); | ||
} | ||
if (ExcelImporter.supportsMediaType(data.mediaType())) { | ||
return getExcelImporter().importVocabulary(config, data); | ||
} | ||
throw new UnsupportedImportMediaTypeException( | ||
"Unsupported media type '" + data.mediaType() + "' for vocabulary import."); | ||
} | ||
|
||
private VocabularyImporter getSkosImporter() { | ||
return appContext.getBean(SKOSImporter.class); | ||
} | ||
|
||
private VocabularyImporter getExcelImporter() { | ||
return appContext.getBean(ExcelImporter.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/test/java/cz/cvut/kbss/termit/service/importer/ExcelImporterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cz.cvut.kbss.termit.service.importer; | ||
|
||
import cz.cvut.kbss.termit.util.Constants; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class ExcelImporterTest { | ||
|
||
@ParameterizedTest | ||
@CsvSource({Constants.MediaType.EXCEL, "application/vnd.ms-excel"}) | ||
void supportsMediaTypeReturnsTrueForSupportedExcelMediaType(String mediaType) { | ||
assertTrue(ExcelImporter.supportsMediaType(mediaType)); | ||
} | ||
|
||
@Test | ||
void supportsMediaTypeReturnsFalseForUnsupportedMediaType() { | ||
assertFalse(ExcelImporter.supportsMediaType("application/json")); | ||
} | ||
} |
Oops, something went wrong.