diff --git a/buisnesslogic/src/main/java/com/plannaplan/configutils/FileToDatabaseMigrator.java b/buisnesslogic/src/main/java/com/plannaplan/configutils/FileToDatabaseMigrator.java index 9685468..38addf5 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/configutils/FileToDatabaseMigrator.java +++ b/buisnesslogic/src/main/java/com/plannaplan/configutils/FileToDatabaseMigrator.java @@ -1,12 +1,37 @@ 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.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"; + public FileToDatabaseMigrator() { } public void migrate(FileData data) { + Iterator 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); + + LecturerService lecturerService = new LecturerService(); + + while (rows.hasNext()) { + Row row = rows.next(); + Lecturer newLecturer = new Lecturer(row.getCell(title_index).toString(), row.getCell(name_index).toString(), + row.getCell(surname_index).toString()); + + lecturerService.save(newLecturer); + } } } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Lecturer.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Lecturer.java index 61d5e9a..c8ca7e9 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Lecturer.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Lecturer.java @@ -14,9 +14,6 @@ public class Lecturer { private String name; private String surname; - public Lecturer() { - } - public String getTitle() { return title; } @@ -41,4 +38,10 @@ public class Lecturer { this.title = title; } + public Lecturer(String title, String name, String surname) { + this.title = title; + this.name = name; + this.surname = surname; + } + } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/models/FileData.java b/buisnesslogic/src/main/java/com/plannaplan/models/FileData.java index 6505788..a058206 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/models/FileData.java +++ b/buisnesslogic/src/main/java/com/plannaplan/models/FileData.java @@ -31,4 +31,9 @@ public class FileData { this.keys = keys; } + public int getIndexOf(String key) { + int index = this.keys.get(key); + return index; + } + } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/LecturerService.java b/buisnesslogic/src/main/java/com/plannaplan/services/LecturerService.java index 306ff1e..7706d85 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/LecturerService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/LecturerService.java @@ -1,5 +1,6 @@ package com.plannaplan.services; +import com.plannaplan.entities.Lecturer; import com.plannaplan.repositories.LecturerRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -9,4 +10,8 @@ import org.springframework.stereotype.Service; public class LecturerService { @Autowired private LecturerRepository repo; + + public void save(Lecturer lecturer) { + repo.save(lecturer); + } } \ No newline at end of file diff --git a/buisnesslogic/src/test/java/com/plannaplan/configutils/FileToDatabaseMigratorTest.java b/buisnesslogic/src/test/java/com/plannaplan/configutils/FileToDatabaseMigratorTest.java new file mode 100644 index 0000000..aa04b72 --- /dev/null +++ b/buisnesslogic/src/test/java/com/plannaplan/configutils/FileToDatabaseMigratorTest.java @@ -0,0 +1,18 @@ +package com.plannaplan.configutils; + +import java.io.InputStream; + +import com.plannaplan.models.FileData; + +import org.junit.Test; + +public class FileToDatabaseMigratorTest { + @Test + public void shouldInsertToDatabase() { + FileToDatabaseMigrator migrator = new FileToDatabaseMigrator(); + InputStream inputStream = getClass().getClassLoader().getResourceAsStream("Zajecia.xlsx"); + FileReader r = new FileReader(inputStream); + FileData d = r.read(); + migrator.migrate(d); + } +} \ No newline at end of file diff --git a/buisnesslogic/src/test/resources/application.properties b/buisnesslogic/src/test/resources/application.properties index 0a4a0a2..bd77a4e 100644 --- a/buisnesslogic/src/test/resources/application.properties +++ b/buisnesslogic/src/test/resources/application.properties @@ -1,6 +1,10 @@ spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect -spring.datasource.url=jdbc:mysql://172.20.0.2:3306/test +spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=yes&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=example spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.jpa.open-in-view=true spring.jpa.hibernate.ddl-auto=create-drop +spring.jackson.serialization.fail-on-empty-beans=false + +server.port=1285 \ No newline at end of file