Major refactor of reading from excel to db

This commit is contained in:
Maciek Głowacki 2020-09-21 17:45:52 +02:00
parent e91965e9b5
commit c449bc22e1
16 changed files with 158 additions and 167 deletions

View File

@ -3,9 +3,6 @@ package com.plannaplan;
import com.plannaplan.interfaces.ProtectedAction;
import com.plannaplan.models.ConfigData;
import com.plannaplan.models.FileData;
import com.plannaplan.services.CourseService;
import com.plannaplan.services.GroupService;
import com.plannaplan.services.LecturerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -16,24 +13,16 @@ import com.plannaplan.configutils.*;
public class Configurator implements ProtectedAction {
@Autowired
private LecturerService lecturerService;
@Autowired
private CourseService courseService;
@Autowired
private GroupService groupService;
FileToDatabaseMigrator migrator;
public Configurator() {
}
public void config(ConfigData data) {
FileReader reader = new FileReader(data.getFilestream());
FileData coursesData = reader.read();
FileToDatabaseMigrator mgtr = new FileToDatabaseMigrator(lecturerService, courseService, groupService);
mgtr.migrate(coursesData);
migrator.migrate(coursesData);
}
//what should be implemented here???
@Override
public void validateAction() {
// TODO Auto-generated method stub

View File

@ -20,7 +20,7 @@ public abstract class EventWatcher {
public void detach(EventCreator creator) {
this.creators.remove(creator);
}
//why update method is both in eventwatcher and eventcreator???
public void update() {
}
}

View File

@ -2,7 +2,7 @@ package com.plannaplan.configutils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFSheet;
@ -22,7 +22,7 @@ public class FileReader {
public FileData read() {
FileData result = null;
try {
InputStream fis = this.fileInputStream;
XSSFWorkbook workbook = new XSSFWorkbook(fis);
@ -33,12 +33,12 @@ public class FileReader {
Row row = rowIt.next();
Iterator<Cell> cellIt = row.cellIterator();
Hashtable<String, Integer> keys = new Hashtable<>();
HashMap<String, Integer> keys = new HashMap<>();
int index = 0;
while (cellIt.hasNext()) {
Cell c = cellIt.next();
keys.put(c.toString(), index);
index+=1;
index += 1;
}
rowIt.remove();

View File

@ -1,7 +1,6 @@
package com.plannaplan.configutils;
import java.util.Iterator;
import com.plannaplan.entities.Course;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.Lecturer;
@ -9,140 +8,101 @@ 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.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FileToDatabaseMigrator {
public static String LECTURER_NAME_STRING = "imie";
public static String LECTURER_SURNAME_STRING = "nazwisko";
public static String LECTURER_TITLE_STRING = "tytul";
// only used in this class, compile-time constants
private static final String LECTURER_NAME_STRING = "imie";
private static final String LECTURER_SURNAME_STRING = "nazwisko";
private static final String LECTURER_TITLE_STRING = "tytul";
public static String COURSE_SYMBOL_STRING = "sym";
public static String COURSE_NAME_STRING = "nazwa";
private static final String COURSE_SYMBOL_STRING = "sym";
private static final String COURSE_NAME_STRING = "nazwa";
public static String GROUP_DAY_STRING = "dzien";
public static String GROUP_TIME_STRING = "godz_od";
public static String ROOM_STRING = "sala";
public static String CAPACITY_STRING = "Mc";
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";
LecturerService lecturerService;
CourseService courseService;
GroupService groupService;
// why no specifier, should it be default or is it an error???
// could you use @autowired annotation here???
@Autowired
private LecturerService lecturerService;
@Autowired
private CourseService courseService;
@Autowired
private GroupService groupService;
public FileToDatabaseMigrator(LecturerService lecturerService, CourseService courseService,
GroupService groupService) {
this.lecturerService = lecturerService;
this.groupService = groupService;
this.courseService = courseService;
// public FileToDatabaseMigrator(LecturerService lecturerService, CourseService
// courseService,
// GroupService groupService) {
// this.lecturerService = lecturerService;
// this.groupService = groupService;
// this.courseService = courseService;
// }
public FileToDatabaseMigrator() {
}
public void migrate(FileData data) {
Iterator<Row> rows = data.getRows();
// why it looks like a python code xDD. To be consistent should use camelCase
int courseNameIndex = data.getIndexOf(FileToDatabaseMigrator.COURSE_NAME_STRING);
int symbolIndex = data.getIndexOf(FileToDatabaseMigrator.COURSE_SYMBOL_STRING);
int course_name_index = data.getIndexOf(FileToDatabaseMigrator.COURSE_NAME_STRING);
int sym_index = 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 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);
int day_index = data.getIndexOf(FileToDatabaseMigrator.GROUP_DAY_STRING);
int time_index = data.getIndexOf(FileToDatabaseMigrator.GROUP_TIME_STRING);
int room_index = data.getIndexOf(FileToDatabaseMigrator.ROOM_STRING);
int capacity_index = data.getIndexOf(FileToDatabaseMigrator.CAPACITY_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);
while (rows.hasNext()) {
Row row = rows.next();
Cell course_name_cell = row.getCell(course_name_index);
Cell sym = row.getCell(sym_index);
String courseName = row.getCell(courseNameIndex).toString().trim();
String symbol = row.getCell(symbolIndex).toString().trim();
Cell title_cell = row.getCell(title_index);
Cell name_cell = row.getCell(name_index);
Cell surname_cell = row.getCell(surname_index);
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() : "";
Cell day_cell = row.getCell(day_index);
Cell time_cell = row.getCell(time_index);
Cell room_cell = row.getCell(room_index);
Cell capacity_cell = row.getCell(capacity_index);
// why isn't it parsed to int but first to double and then to int??? it looks a
// bit cryptic
int day = Integer.parseInt(row.getCell(dayIndex).toString());
WeekDay groupDay = WeekDay.getDay(day - 1);
int time = parseTimeToInt(row.getCell(timeIndex).toString());
String room = row.getCell(roomIndex).toString().trim();
// why isn't it parsed to int but first to double and then to int??? it looks a
// bit cryptic
int capacity = Integer.parseInt(row.getCell(capacityIndex).toString());
String lecturer_title = "";
String lecturer_surname = "";
String lecturer_name = "";
// more idiomatic way to handling nulls is optional
// why not use this in here???
Course course = this.courseService.getCourseByName(courseName)
.orElseGet(() -> this.courseService.save(new Course(courseName, symbol)));
String course_name = course_name_cell.toString().trim();
String sym_str = sym.toString().trim();
Lecturer lecturer = this.lecturerService.getLecturer(lecturerTitle, lecturerName, lecturerSurname)
.orElseGet(() -> this.lecturerService
.save(new Lecturer(lecturerTitle, lecturerName, lecturerSurname)));
int day = (int) Double.parseDouble(day_cell.toString());
WeekDay group_day = WeekDay.getDay(day - 1);
String room = room_cell.toString().trim();
int time = this.parseTimeToInt(time_cell.toString());
int capacity = (int) Double.parseDouble(capacity_cell.toString());
Groups group = groupService.find(time, capacity, room);
Course course = this.courseService.getCourseByName(course_name);
if (course == null) {
course = new Course();
course.setName(course_name);
course.setSymbol(sym_str);
}
courseService.save(course);
if (title_cell != null) {
lecturer_title = title_cell.toString().trim();
}
if (name_cell != null) {
lecturer_name = name_cell.toString().trim();
}
if (surname_cell != null) {
lecturer_surname = surname_cell.toString().trim();
}
Lecturer lecturer = lecturerService.getLecturer(lecturer_title, lecturer_name, lecturer_surname);
if (lecturer == null) {
lecturer = new Lecturer(lecturer_title, lecturer_name, lecturer_surname);
lecturerService.save(lecturer);
}
if (group == null) {
group = new Groups();
group.setCapacity(capacity);
group.setRoom(room);
group.setCourseId(course);
group.setTime(time);
if (capacity >= 50) {
group.setType(GroupType.LECTURE);
} else {
group.setType(GroupType.CLASS);
}
group.setDay(group_day);
group.setLecturer(lecturer);
groupService.save(group);
}
Groups group = this.groupService.find(time, capacity, room).orElseGet(
() -> this.groupService.save(new Groups(capacity, room, course, time, groupDay, lecturer)));
}
}
// why this method is private??? I think it should be static
private static int parseTimeToInt(String time) {
// why not combine regexes??
String times[] = time.split("\\.|\\:");
return times.length == 2 ? Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]) : 0;
private Integer parseTimeToInt(String time) {
String times[] = time.split("\\.");
if (times.length == 2) {
return Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]);
}
times = time.split("\\:");
if (times.length == 2) {
return Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]);
}
return 0;
}
}

View File

@ -17,12 +17,17 @@ public class Course {
private Long id;
private String name;
private String symbol;
@OneToMany(mappedBy = "courseId", fetch = FetchType.EAGER)
@OneToMany(mappedBy = "course", fetch = FetchType.EAGER)
private List<Groups> groups = new ArrayList<>();
public Course() {
}
public Course(String name, String symbol) {
this.name = name;
this.symbol = symbol;
}
public Long getId() {
return this.id;
}
@ -43,8 +48,8 @@ public class Course {
this.name = name;
}
public List<Groups> getGroups(){
public List<Groups> getGroups() {
return this.groups;
}
}
}

View File

@ -10,6 +10,8 @@ import javax.persistence.ManyToOne;
import com.plannaplan.types.GroupType;
import com.plannaplan.types.WeekDay;
@Entity
public class Groups {
@Id
@ -17,7 +19,7 @@ public class Groups {
private Long id;
@ManyToOne
@JoinColumn(name = "course_id")
private Course courseId;
private Course course;
private int time;
private String room;
private int capacity;
@ -30,6 +32,16 @@ public class Groups {
public Groups() {
}
public Groups(int capacity, String room, Course course, int time, WeekDay day, Lecturer lecturer) {
this.capacity = capacity;
this.room = room;
this.course = course;
this.time = time;
this.day = day;
this.lecturer = lecturer;
this.type = capacity >= 50 ? GroupType.LECTURE : GroupType.CLASS;
}
public Long getId() {
return this.id;
}
@ -83,11 +95,11 @@ public class Groups {
}
public Course getCourseId() {
return courseId;
return course;
}
public void setCourseId(Course courseId) {
this.courseId = courseId;
this.course = courseId;
}
public String getTimeString() {
@ -99,4 +111,4 @@ public class Groups {
}
return String.format("%s.%s", hoursString, minutesString);
}
}
}

View File

@ -8,7 +8,7 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import com.plannaplan.types.UserRoles;
//should setter be public?
@Entity
public class User {
@Id
@ -68,4 +68,5 @@ public class User {
public void setName(String name) {
this.name = name;
}
}

View File

@ -1,20 +1,25 @@
package com.plannaplan.models;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Row;
//dictionary is deprecated use hashmap instead
public class FileData {
private Dictionary<String, Integer> keys;
private HashMap<String, Integer> keys;
private Iterator<Row> rows;
public FileData(Dictionary<String, Integer> keys, Iterator<Row> rows) {
this.setKeys(keys);
this.setRows(rows);
//why variable is called keys2, could it be more specific???
//why use setters in constructor here instead of assignments???
// thought that setters are used to change values in runtime
public FileData(HashMap<String, Integer> keys, Iterator<Row> rows) {
this.keys = keys;
this.rows = rows;
}
public Iterator<Row> getRows() {
return rows;
}
@ -23,11 +28,11 @@ public class FileData {
this.rows = rows;
}
public Dictionary<String, Integer> getKeys() {
public HashMap<String, Integer> getKeys() {
return keys;
}
public void setKeys(Dictionary<String, Integer> keys) {
public void setKeys(HashMap<String, Integer> keys) {
this.keys = keys;
}

View File

@ -1,5 +1,7 @@
package com.plannaplan.repositories;
import java.util.Optional;
import com.plannaplan.entities.Course;
import org.springframework.data.jpa.repository.JpaRepository;
@ -10,5 +12,5 @@ import org.springframework.stereotype.Repository;
@Repository
public interface CourseRepository extends JpaRepository<Course, Long> {
@Query("FROM Course WHERE name = ?1")
Course findByName(@Param("name") String name);
Optional<Course> findByName(@Param("name") String name);
}

View File

@ -1,6 +1,7 @@
package com.plannaplan.repositories;
import java.util.List;
import java.util.Optional;
import com.plannaplan.entities.Groups;
@ -12,7 +13,7 @@ import org.springframework.stereotype.Repository;
@Repository
public interface GroupRepository extends JpaRepository<Groups, Long> {
@Query("FROM Groups WHERE time = ?1 AND room = ?2 AND capacity = ?3")
Groups find(@Param("time") int time, @Param("room") String room, @Param("capacity") int capacity);
Optional<Groups> find(@Param("time") int time, @Param("room") String room, @Param("capacity") int capacity);
@Query("FROM Groups WHERE course_id = ?1")
List<Groups> getByCourse(@Param("id") Long id);

View File

@ -1,5 +1,7 @@
package com.plannaplan.repositories;
import java.util.Optional;
import com.plannaplan.entities.Lecturer;
import org.springframework.data.jpa.repository.JpaRepository;
@ -10,5 +12,5 @@ import org.springframework.stereotype.Repository;
@Repository
public interface LecturerRepository extends JpaRepository<Lecturer, Long> {
@Query("FROM Lecturer WHERE title = ?1 AND name = ?2 AND surname = ?3")
Lecturer find(@Param("title") String title, @Param("name") String name, @Param("surname") String surname);
Optional<Lecturer> find(@Param("title") String title, @Param("name") String name, @Param("surname") String surname);
}

View File

@ -1,5 +1,7 @@
package com.plannaplan.repositories;
import java.util.Optional;
import com.plannaplan.entities.User;
import org.springframework.data.jpa.repository.JpaRepository;
@ -7,10 +9,11 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
//if result could be null, should we wrapped in optional
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@Query("FROM User WHERE email = ?1")
User getByAuthority(@Param("authority") String authority);
Optional<User> getByAuthority(@Param("authority") String authority);
@Query("FROM User WHERE token = ?1")
User getByToken(@Param("token") String token);

View File

@ -1,6 +1,7 @@
package com.plannaplan.services;
import java.util.List;
import java.util.Optional;
import com.plannaplan.entities.Course;
import com.plannaplan.repositories.CourseRepository;
@ -13,7 +14,7 @@ public class CourseService {
@Autowired
private CourseRepository repo;
public Course getCourseByName(String name) {
public Optional<Course> getCourseByName(String name) {
return this.repo.findByName(name);
}
@ -21,15 +22,16 @@ public class CourseService {
return this.repo.findAll();
}
public void save(Course course) {
public Course save(Course course) {
this.repo.save(course);
return course;
}
public void delete(Course course){
public void delete(Course course) {
this.repo.delete(course);
}
public int getCoursesAmmount(){
return (int)this.repo.count();
public int getCoursesAmmount() {
return (int) this.repo.count();
}
}

View File

@ -1,6 +1,7 @@
package com.plannaplan.services;
import java.util.List;
import java.util.Optional;
import com.plannaplan.entities.Groups;
import com.plannaplan.repositories.GroupRepository;
@ -16,7 +17,7 @@ public class GroupService {
public GroupService() {
}
public Groups find(int time, int capacity, String room) {
public Optional<Groups> find(int time, int capacity, String room) {
return this.repo.find(time, room, capacity);
}
@ -24,15 +25,16 @@ public class GroupService {
return this.repo.getByCourse(id);
}
public void save(Groups group) {
public Groups save(Groups group) {
this.repo.save(group);
return group;
}
public void delete(Groups groups){
public void delete(Groups groups) {
this.repo.delete(groups);
}
public int getGroupsAmmount(){
return (int)this.repo.count();
public int getGroupsAmmount() {
return (int) this.repo.count();
}
}

View File

@ -1,5 +1,7 @@
package com.plannaplan.services;
import java.util.Optional;
import com.plannaplan.entities.Lecturer;
import com.plannaplan.repositories.LecturerRepository;
@ -11,19 +13,20 @@ public class LecturerService {
@Autowired
private LecturerRepository repo;
public Lecturer getLecturer(String title, String name, String surname) {
public Optional<Lecturer> getLecturer(String title, String name, String surname) {
return repo.find(title, name, surname);
}
public void save(Lecturer lecturer) {
public Lecturer save(Lecturer lecturer) {
repo.save(lecturer);
return lecturer;
}
public void delete(Lecturer lecturer) {
repo.delete(lecturer);
}
public int getLecturersAmmount(){
return (int)this.repo.count();
public int getLecturersAmmount() {
return (int) this.repo.count();
}
}

View File

@ -19,11 +19,11 @@ public class UserService extends EventWatcher {
super();
}
// this code is more idiomatic to java using java 8 stream API
public String login(String authority) throws UserNotFoundException {
User user = this.repo.getByAuthority(authority.replace("\n", "").trim());
if (user == null) {
throw new UserNotFoundException("Can not find user with given authority");
}
User user = this.repo.getByAuthority(authority.replace("\n", "").trim())
.orElseThrow(() -> new UserNotFoundException("Can not find user with given authority"));
String token = UUID.randomUUID().toString();
user.setToken(token);
this.repo.save(user);
@ -34,10 +34,14 @@ public class UserService extends EventWatcher {
this.repo.save(user);
}
public User getUserByEmail(String email) {
return this.repo.getByAuthority(email.replace("\n", "").trim());
// this code is more idiomatic to java
public User getUserByEmail(String email) throws UserNotFoundException {
return this.repo.getByAuthority(email.replace("\n", "").trim())
.orElseThrow(() -> new UserNotFoundException("Cannot find user with given authority"));
}
// why is not throwing exception?
public User getByToken(String token) {
return this.repo.getByToken(token);
}