backend/buisnesslogic/src/main/java/com/plannaplan/configutils/FileToDatabaseMigrator.java

57 lines
1.8 KiB
Java

package com.plannaplan.configutils;
import java.util.Iterator;
import com.plannaplan.entities.Lecturer;
import com.plannaplan.models.FileData;
import com.plannaplan.services.LecturerService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class FileToDatabaseMigrator {
public static String LECTURER_NAME_STRING = "imie";
public static String LECTURER_SURNAME_STRING = "nazwisko";
public static String LECTURER_TITLE_STRING = "tytul";
LecturerService lecturerService;
public FileToDatabaseMigrator(LecturerService lecturerService) {
this.lecturerService = lecturerService;
}
public void migrate(FileData data) {
Iterator<Row> rows = data.getRows();
int title_index = data.getIndexOf(FileToDatabaseMigrator.LECTURER_TITLE_STRING);
int surname_index = data.getIndexOf(FileToDatabaseMigrator.LECTURER_SURNAME_STRING);
int name_index = data.getIndexOf(FileToDatabaseMigrator.LECTURER_NAME_STRING);
while (rows.hasNext()) {
Row row = rows.next();
Cell title_cell = row.getCell(title_index);
Cell name_cell = row.getCell(name_index);
Cell surname_cell = row.getCell(surname_index);
String lecturer_title = "";
String lecturer_surname = "";
String lecturer_name = "";
if (title_cell != null) {
lecturer_title = title_cell.toString();
}
if (name_cell != null) {
lecturer_name = name_cell.toString();
}
if (surname_cell != null) {
lecturer_surname = surname_cell.toString();
}
Lecturer newLecturer = new Lecturer(lecturer_title, lecturer_name, lecturer_surname);
lecturerService.save(newLecturer);
}
}
}