Checkpoint

This commit is contained in:
Filip Izydorczyk 2020-07-28 18:04:38 +02:00
parent 7670401d6e
commit 821f1ffa75
6 changed files with 64 additions and 4 deletions

View File

@ -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<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);
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);
}
}
}

View File

@ -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;
}
}

View File

@ -31,4 +31,9 @@ public class FileData {
this.keys = keys;
}
public int getIndexOf(String key) {
int index = this.keys.get(key);
return index;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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