package com.plannaplan.configutils; import java.util.Iterator; import com.plannaplan.entities.Course; import com.plannaplan.entities.Groups; import com.plannaplan.entities.Lecturer; import com.plannaplan.models.FileData; import com.plannaplan.services.CourseService; import com.plannaplan.services.GroupService; import com.plannaplan.services.LecturerService; import com.plannaplan.types.GroupType; import com.plannaplan.types.WeekDay; import org.apache.poi.ss.usermodel.Row; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * FileToDatabaseMigrator is used for migrate data from file (it reads line by line) and push it into database */ @Component public class FileToDatabaseMigrator { private static final String LECTURER_NAME_STRING = "imie"; private static final String LECTURER_SURNAME_STRING = "nazwisko"; private static final String LECTURER_TITLE_STRING = "tytul"; private static final String COURSE_SYMBOL_STRING = "sym"; private static final String COURSE_NAME_STRING = "nazwa"; private static final String groupDay_STRING = "dzien"; private static final String GROUP_TIME_STRING = "godz_od"; private static final String ROOM_STRING = "sala"; private static final String CAPACITY_STRING = "Mc"; private static final String TYPE_GROUP= "typ"; private static final String ZAJ_CYK_ID = "zaj_cyk_id"; private static final String GR_NR = "gr_nr"; @Autowired private LecturerService lecturerService; @Autowired private CourseService courseService; @Autowired private GroupService groupService; public FileToDatabaseMigrator() { } public void migrate(FileData data) { Iterator rows = data.getRows(); int courseNameIndex = data.getIndexOf(FileToDatabaseMigrator.COURSE_NAME_STRING); int symbolIndex = data.getIndexOf(FileToDatabaseMigrator.COURSE_SYMBOL_STRING); int titleIndex = data.getIndexOf(FileToDatabaseMigrator.LECTURER_TITLE_STRING); int surnameIndex = data.getIndexOf(FileToDatabaseMigrator.LECTURER_SURNAME_STRING); int nameIndex = data.getIndexOf(FileToDatabaseMigrator.LECTURER_NAME_STRING); int dayIndex = data.getIndexOf(FileToDatabaseMigrator.groupDay_STRING); int timeIndex = data.getIndexOf(FileToDatabaseMigrator.GROUP_TIME_STRING); int roomIndex = data.getIndexOf(FileToDatabaseMigrator.ROOM_STRING); int capacityIndex = data.getIndexOf(FileToDatabaseMigrator.CAPACITY_STRING); int typeGroupIndex = data.getIndexOf(FileToDatabaseMigrator.TYPE_GROUP); int zajCykIdIndex = data.getIndexOf(FileToDatabaseMigrator.ZAJ_CYK_ID); int grNrIndex = data.getIndexOf(FileToDatabaseMigrator.GR_NR); while (rows.hasNext()) { Row row = rows.next(); String courseName = row.getCell(courseNameIndex).toString().trim(); String symbol = row.getCell(symbolIndex).toString().trim(); String lecturerTitle = row.getCell(titleIndex) != null ? row.getCell(titleIndex).toString().trim() : ""; String lecturerName = row.getCell(nameIndex) != null ? row.getCell(nameIndex).toString().trim() : ""; String lecturerSurname = row.getCell(surnameIndex) != null ? row.getCell(surnameIndex).toString().trim() : ""; Integer zajCykId = row.getCell(zajCykIdIndex) != null ? (int) Double.parseDouble(row.getCell(zajCykIdIndex).toString().trim()) : null; Integer grNr = row.getCell(grNrIndex) != null ? (int) Double.parseDouble(row.getCell(grNrIndex).toString().trim()) : null; int day = row.getCell(dayIndex) != null ? (int) Double.parseDouble(row.getCell(dayIndex).toString()) : 0; WeekDay groupDay = WeekDay.getDay(day - 1); int time = parseTimeToInt(row.getCell(timeIndex).toString()); String room = row.getCell(roomIndex).toString().trim(); int capacity = (int) Double.parseDouble(row.getCell(capacityIndex).toString()); GroupType typeGroup = GroupType.getType(row.getCell(typeGroupIndex).toString()); Course course = this.courseService.getCourseByName(courseName) .orElseGet(() -> this.courseService.save(new Course(courseName, symbol))); Lecturer lecturer = this.lecturerService.getLecturer(lecturerTitle, lecturerName, lecturerSurname) .orElseGet(() -> this.lecturerService .save(new Lecturer(lecturerTitle, lecturerName, lecturerSurname))); Groups group = this.groupService.find(zajCykId, grNr).orElseGet( () -> new Groups(capacity, room, course, time, capacity, groupDay, lecturer, zajCykId, grNr, typeGroup)); group.update(capacity, room, course, time, null, groupDay, lecturer, typeGroup); this.groupService.save(group); } } private static int parseTimeToInt(String time) { String times[] = time.split("\\.|\\:"); return times.length == 2 ? Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]) : 0; } }