Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.github.two_rk_dev.pointeurback.datasync.filecodec;

import io.github.two_rk_dev.pointeurback.dto.datasync.TableData;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -55,32 +53,42 @@ public byte[] encode(@NotNull List<TableData> dataset) throws IOException {
*/
@Override
public List<@NotNull TableData> decode(InputStream inputStream) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
List<TableData> tableDataList = new ArrayList<>();
for (Sheet sheet : workbook) {
tableDataList.add(parseSheet(sheet));
try (Workbook workbook = WorkbookFactory.create(inputStream)) {
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
List<TableData> tableDataList = new ArrayList<>();
for (Sheet sheet : workbook) tableDataList.add(parseSheet(sheet, evaluator));
return tableDataList;
}
return tableDataList;
}

@Override
public @NotNull Type getType() {
return Type.EXCEL;
}

@Contract("_ -> new")
private @NotNull TableData parseSheet(@NotNull Sheet sheet) {
@Contract("_, _ -> new")
private @NotNull TableData parseSheet(@NotNull Sheet sheet, FormulaEvaluator evaluator) {
Iterator<Row> rows = sheet.iterator();
if (!rows.hasNext()) return TableData.EMPTY;

DataFormatter formatter = new DataFormatter();
List<String> headers = new ArrayList<>();
rows.next().forEach(c -> headers.add(c.getStringCellValue()));
List<Integer> headerColumnIndices = new ArrayList<>();
rows.next().forEach(c -> {
String cellValue = formatter.formatCellValue(c, evaluator);
if (cellValue == null || cellValue.isBlank()) return;
headerColumnIndices.add(c.getColumnIndex());
headers.add(cellValue);
});

List<List<String>> data = new ArrayList<>();
rows.forEachRemaining(row -> {
List<String> rowValues = new ArrayList<>();
for (int i = 0; i < headers.size(); i++) {
rowValues.add(Optional.ofNullable(row.getCell(i)).map(Cell::toString).orElse(null));
for (int i : headerColumnIndices) {
rowValues.add(Optional.ofNullable(row.getCell(i))
.map(c -> formatter.formatCellValue(c, evaluator))
.orElse(null)
);
}
data.add(rowValues);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -119,7 +120,7 @@ void shouldImportExcelFileSuccessfully() throws Exception {
int teachingUnitCount = 10;
int teacherCount = 6;
mockMvc.perform(multipart("/import/upload")
.file(getExcelMetadataFile())
.file(getExcelMetadataFile("level_room_teacher_teaching_unit_group.xlsx"))
.file(excelFile))
.andExpect(status().isOk())
.andExpect(jsonPath("$.entitySummary.room").value(roomCount))
Expand Down Expand Up @@ -150,6 +151,31 @@ void shouldImportExcelFileSuccessfully() throws Exception {
.containsExactlyInAnyOrder("PROG", "BDD", "MATH", "TRES", "LANG", "MATH", "PROG", "BDD", "TRES", "LANG");
}

@Test
void shouldEvaluateFormulasInExcelAndSkipColumnWithBlankHeader() throws Exception {
MockMultipartFile excelFile = new MockMultipartFile(
"files",
"prof.xlsx",
FileCodec.Type.EXCEL.inputMediaType().toString(),
new ClassPathResource("prof.xlsx").getInputStream()
);
int teacherCount = 3;
mockMvc.perform(multipart("/import/upload")
.file(getExcelMetadataFile("prof.xlsx"))
.file(excelFile))
.andExpect(status().isOk())
.andExpect(jsonPath("$.entitySummary.teacher").value(teacherCount));

assertThat(teacherRepository.findAll())
.hasSize(teacherCount)
.extracting("name", "abbreviation", "id")
.contains(
tuple("TSIRIHERIVONJY Flavien", "TSF", 1L),
tuple("RAKOTO François", "RKF", 2L),
tuple("BOTO Keky", "BTK", 3L)
);
}

@Test
void shouldImportJSONFileSuccessfully() throws Exception {
int roomCount = 6;
Expand Down Expand Up @@ -503,61 +529,8 @@ void shouldSaveGroupTypeAndClasse() throws Exception {
);
}

private static @NotNull MockMultipartFile getExcelMetadataFile() {
@Language("JSON") String metadata = """
{
"metadata": {
"level_room_teacher_teaching_unit_group.xlsx": {
"room": {
"entityType": "room",
"headersMapping": {
"id": "id",
"name": "name",
"abbreviation": "abbreviation",
"size": "size"
}
},
"level": {
"entityType": "level",
"headersMapping": {
"id": "id",
"name": "name",
"abbreviation": "abbreviation"
}
},
"teacher": {
"entityType": "teacher",
"headersMapping": {
"id": "id",
"name": "name",
"abbreviation": "abbreviation"
}
},
"group": {
"entityType": "group",
"headersMapping": {
"id": "id",
"name": "name",
"abbreviation": "abbreviation",
"size": "size",
"levelId": "levelId",
"type": "type",
"classe": "classe"
}
},
"teaching_unit": {
"entityType": "teaching_unit",
"headersMapping": {
"id": "id",
"name": "name",
"abbreviation": "abbreviation",
"levelId": "levelId"
}
}
}
}
}
""";
private static @NotNull MockMultipartFile getExcelMetadataFile(String excelFilename) throws IOException {
String metadata = new ClassPathResource("__%s.json".formatted(excelFilename)).getContentAsString(StandardCharsets.UTF_8);

return new MockMultipartFile(
"metadata",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"metadata": {
"level_room_teacher_teaching_unit_group.xlsx": {
"room": {
"entityType": "room",
"headersMapping": {
"id": "id",
"name": "name",
"abbreviation": "abbreviation",
"size": "size"
}
},
"level": {
"entityType": "level",
"headersMapping": {
"id": "id",
"name": "name",
"abbreviation": "abbreviation"
}
},
"teacher": {
"entityType": "teacher",
"headersMapping": {
"id": "id",
"name": "name",
"abbreviation": "abbreviation"
}
},
"group": {
"entityType": "group",
"headersMapping": {
"id": "id",
"name": "name",
"abbreviation": "abbreviation",
"size": "size",
"levelId": "levelId",
"type": "type",
"classe": "classe"
}
},
"teaching_unit": {
"entityType": "teaching_unit",
"headersMapping": {
"id": "id",
"name": "name",
"abbreviation": "abbreviation",
"levelId": "levelId"
}
}
}
}
}
14 changes: 14 additions & 0 deletions src/test/resources/__prof.xlsx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"metadata": {
"prof.xlsx": {
"Feuil1": {
"entityType": "teacher",
"headersMapping": {
"id": "id",
"Nom et prenoms": "name",
"abr": "abbreviation"
}
}
}
}
}
Binary file added src/test/resources/prof.xlsx
Binary file not shown.