Skip to content

Commit

Permalink
[kbss-cvut/termit-ui#581] Add a template file for term translations i…
Browse files Browse the repository at this point in the history
…mport.
  • Loading branch information
ledsoft committed Dec 2, 2024
1 parent 085507b commit 8303290
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,13 @@ public ResponseEntity<Void> createVocabulary(
@ApiResponse(responseCode = "200", description = "Template Excel file is returned as attachment")
@GetMapping("/import/template")
@PreAuthorize("permitAll()")
public ResponseEntity<TypeAwareResource> getExcelTemplateFile() {
final TypeAwareResource template = vocabularyService.getExcelTemplateFile();
public ResponseEntity<TypeAwareResource> getExcelTemplateFile(
@Parameter(description = "Whether the file will be used to import only term translations")
@RequestParam(name = "translationsOnly", required = false,
defaultValue = "false") boolean translationsOnly) {
final TypeAwareResource template =
translationsOnly ? vocabularyService.getExcelTranslationsImportTemplateFile() :
vocabularyService.getExcelImportTemplateFile();
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(
template.getMediaType().orElse(MediaType.APPLICATION_OCTET_STREAM_VALUE)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,30 @@ public Vocabulary importTermTranslations(URI vocabularyIri, MultipartFile file)
*
* @return Template file as a resource
*/
public TypeAwareResource getExcelTemplateFile() {
public TypeAwareResource getExcelImportTemplateFile() {
return getExcelTemplate("termit-import");
}

private TypeAwareResource getExcelTemplate(String fileName) {
final Configuration config = context.getBean(Configuration.class);
return config.getTemplate().getExcelImport().map(File::new)
.map(f -> (TypeAwareResource) new TypeAwareFileSystemResource(f,
ExportFormat.EXCEL.getMediaType()))
.orElseGet(() -> {
assert getClass().getClassLoader().getResource("template/termit-import.xlsx") != null;
return new TypeAwareClasspathResource("template/termit-import.xlsx",
assert getClass().getClassLoader().getResource("template/" + fileName + ExportFormat.EXCEL.getFileExtension()) != null;
return new TypeAwareClasspathResource("template/" + fileName + ExportFormat.EXCEL.getFileExtension(),
ExportFormat.EXCEL.getMediaType());
});
}

/**
* Gets an Excel template file that can be used to import term translations into TermIt.
* @return Template file as a resource
*/
public TypeAwareResource getExcelTranslationsImportTemplateFile() {
return getExcelTemplate("termit-translations-import");
}

@Override
public List<AbstractChangeRecord> getChanges(Vocabulary asset, ChangeRecordFilterDto filterDto) {
return changeRecordService.getChanges(asset, filterDto);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -638,14 +638,30 @@ void getAccessLevelRetrievesAccessLevelToSpecifiedVocabulary() throws Exception

@Test
void getExcelTemplateFileReturnsExcelTemplateFileRetrievedFromServiceAsAttachment() throws Exception {
when(serviceMock.getExcelTemplateFile()).thenReturn(new TypeAwareFileSystemResource(
when(serviceMock.getExcelImportTemplateFile()).thenReturn(new TypeAwareFileSystemResource(
new File(getClass().getClassLoader().getResource("template/termit-import.xlsx").toURI()),
Constants.MediaType.EXCEL));

final MvcResult mvcResult = mockMvc.perform(get(PATH + "/import/template")).andReturn();
assertThat(mvcResult.getResponse().getHeader(HttpHeaders.CONTENT_DISPOSITION), containsString("attachment"));
assertThat(mvcResult.getResponse().getHeader(HttpHeaders.CONTENT_DISPOSITION),
containsString("filename=\"termit-import.xlsx\""));
verify(serviceMock).getExcelImportTemplateFile();
}

@Test
void getExcelTemplateFileReturnsExcelTermTranslationsTemplateFileRetrievedFromServiceAsAttachment()
throws Exception {
when(serviceMock.getExcelTranslationsImportTemplateFile()).thenReturn(new TypeAwareFileSystemResource(
new File(getClass().getClassLoader().getResource("template/termit-translations-import.xlsx").toURI()),
Constants.MediaType.EXCEL));

final MvcResult mvcResult = mockMvc.perform(
get(PATH + "/import/template").queryParam("translationsOnly", Boolean.toString(true))).andReturn();
assertThat(mvcResult.getResponse().getHeader(HttpHeaders.CONTENT_DISPOSITION), containsString("attachment"));
assertThat(mvcResult.getResponse().getHeader(HttpHeaders.CONTENT_DISPOSITION),
containsString("filename=\"termit-translations-import.xlsx\""));
verify(serviceMock).getExcelTranslationsImportTemplateFile();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,9 @@ void importNewVocabularyPublishesVocabularyCreatedEvent() {
}

@Test
void getExcelTemplateFileReturnsResourceRepresentingExcelTemplateFile() throws Exception {
void getExcelTemplateFileReturnsResourceRepresentingExcelImportTemplateFile() throws Exception {
when(appContext.getBean(Configuration.class)).thenReturn(new Configuration());
final TypeAwareResource result = sut.getExcelTemplateFile();
final TypeAwareResource result = sut.getExcelImportTemplateFile();
assertTrue(result.getFileExtension().isPresent());
assertEquals(ExportFormat.EXCEL.getFileExtension(), result.getFileExtension().get());
assertTrue(result.getMediaType().isPresent());
Expand Down

0 comments on commit 8303290

Please sign in to comment.