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

107 lines
4.7 KiB
Java
Raw Normal View History

package com.plannaplan.configutils;
2020-07-28 18:04:38 +02:00
import java.util.Iterator;
2020-08-05 21:29:48 +02:00
import com.plannaplan.entities.Course;
import com.plannaplan.entities.Groups;
2020-07-28 18:04:38 +02:00
import com.plannaplan.entities.Lecturer;
2020-07-28 17:38:34 +02:00
import com.plannaplan.models.FileData;
2020-08-05 21:29:48 +02:00
import com.plannaplan.services.CourseService;
import com.plannaplan.services.GroupService;
2020-07-28 18:04:38 +02:00
import com.plannaplan.services.LecturerService;
import com.plannaplan.types.WeekDay;
2020-07-28 18:04:38 +02:00
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 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() {
}
2020-07-28 17:38:34 +02:00
public void migrate(FileData data) {
2020-07-28 18:04:38 +02:00
Iterator<Row> rows = data.getRows();
int courseNameIndex = data.getIndexOf(FileToDatabaseMigrator.COURSE_NAME_STRING);
int symbolIndex = data.getIndexOf(FileToDatabaseMigrator.COURSE_SYMBOL_STRING);
2020-07-28 18:04:38 +02:00
int titleIndex = data.getIndexOf(FileToDatabaseMigrator.LECTURER_TITLE_STRING);
int surnameIndex = data.getIndexOf(FileToDatabaseMigrator.LECTURER_SURNAME_STRING);
int nameIndex = data.getIndexOf(FileToDatabaseMigrator.LECTURER_NAME_STRING);
2020-07-28 18:04:38 +02:00
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 zajCykIdIndex = data.getIndexOf(FileToDatabaseMigrator.ZAJ_CYK_ID);
int grNrIndex = data.getIndexOf(FileToDatabaseMigrator.GR_NR);
2020-07-28 18:04:38 +02:00
while (rows.hasNext()) {
Row row = rows.next();
2020-08-04 18:15:00 +02:00
String courseName = row.getCell(courseNameIndex).toString().trim();
String symbol = row.getCell(symbolIndex).toString().trim();
2020-08-04 18:15:00 +02:00
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 = (int) Double.parseDouble(row.getCell(dayIndex).toString());
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());
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)));
this.groupService.find(zajCykId, grNr).orElseGet(
() -> this.groupService.save(new Groups(capacity, room, course, time, groupDay, lecturer, zajCykId, grNr)));
2020-08-04 18:15:00 +02:00
2020-07-28 18:04:38 +02:00
}
}
2020-08-08 00:08:36 +02:00
private static int parseTimeToInt(String time) {
String times[] = time.split("\\.|\\:");
return times.length == 2 ? Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]) : 0;
2020-08-08 00:08:36 +02:00
}
}