From c449bc22e18449f37732889307f0fc64f5f09b89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciek=20G=C5=82owacki?= Date: Mon, 21 Sep 2020 17:45:52 +0200 Subject: [PATCH] Major refactor of reading from excel to db --- .../java/com/plannaplan/Configurator.java | 17 +- .../plannaplan/abstracts/EventWatcher.java | 2 +- .../plannaplan/configutils/FileReader.java | 8 +- .../configutils/FileToDatabaseMigrator.java | 176 +++++++----------- .../java/com/plannaplan/entities/Course.java | 11 +- .../java/com/plannaplan/entities/Groups.java | 20 +- .../java/com/plannaplan/entities/User.java | 3 +- .../java/com/plannaplan/models/FileData.java | 21 ++- .../repositories/CourseRepository.java | 4 +- .../repositories/GroupRepository.java | 3 +- .../repositories/LecturerRepository.java | 4 +- .../repositories/UserRepository.java | 5 +- .../plannaplan/services/CourseService.java | 12 +- .../com/plannaplan/services/GroupService.java | 12 +- .../plannaplan/services/LecturerService.java | 11 +- .../com/plannaplan/services/UserService.java | 16 +- 16 files changed, 158 insertions(+), 167 deletions(-) diff --git a/buisnesslogic/src/main/java/com/plannaplan/Configurator.java b/buisnesslogic/src/main/java/com/plannaplan/Configurator.java index 21367d8..75fdd9d 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/Configurator.java +++ b/buisnesslogic/src/main/java/com/plannaplan/Configurator.java @@ -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 diff --git a/buisnesslogic/src/main/java/com/plannaplan/abstracts/EventWatcher.java b/buisnesslogic/src/main/java/com/plannaplan/abstracts/EventWatcher.java index ac8467a..7bb4a25 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/abstracts/EventWatcher.java +++ b/buisnesslogic/src/main/java/com/plannaplan/abstracts/EventWatcher.java @@ -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() { } } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/configutils/FileReader.java b/buisnesslogic/src/main/java/com/plannaplan/configutils/FileReader.java index 88e91f2..1fa253a 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/configutils/FileReader.java +++ b/buisnesslogic/src/main/java/com/plannaplan/configutils/FileReader.java @@ -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 cellIt = row.cellIterator(); - Hashtable keys = new Hashtable<>(); + HashMap keys = new HashMap<>(); int index = 0; while (cellIt.hasNext()) { Cell c = cellIt.next(); keys.put(c.toString(), index); - index+=1; + index += 1; } rowIt.remove(); diff --git a/buisnesslogic/src/main/java/com/plannaplan/configutils/FileToDatabaseMigrator.java b/buisnesslogic/src/main/java/com/plannaplan/configutils/FileToDatabaseMigrator.java index d2bf5ee..1eb6de9 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/configutils/FileToDatabaseMigrator.java +++ b/buisnesslogic/src/main/java/com/plannaplan/configutils/FileToDatabaseMigrator.java @@ -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 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; } } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Course.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Course.java index be2767e..529d6db 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Course.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Course.java @@ -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 = 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 getGroups(){ + public List getGroups() { return this.groups; } -} \ No newline at end of file +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/Groups.java b/buisnesslogic/src/main/java/com/plannaplan/entities/Groups.java index ec5aa38..ebfb4ec 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/Groups.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/Groups.java @@ -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); } -} \ No newline at end of file +} diff --git a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java index e675415..c08bab3 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/entities/User.java +++ b/buisnesslogic/src/main/java/com/plannaplan/entities/User.java @@ -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; } + } \ 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 a058206..5461ef3 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/models/FileData.java +++ b/buisnesslogic/src/main/java/com/plannaplan/models/FileData.java @@ -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 keys; + private HashMap keys; private Iterator rows; - public FileData(Dictionary keys, Iterator 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 keys, Iterator rows) { + this.keys = keys; + this.rows = rows; } + + public Iterator getRows() { return rows; } @@ -23,11 +28,11 @@ public class FileData { this.rows = rows; } - public Dictionary getKeys() { + public HashMap getKeys() { return keys; } - public void setKeys(Dictionary keys) { + public void setKeys(HashMap keys) { this.keys = keys; } diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/CourseRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/CourseRepository.java index 0bc99eb..78027e3 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/CourseRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/CourseRepository.java @@ -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 { @Query("FROM Course WHERE name = ?1") - Course findByName(@Param("name") String name); + Optional findByName(@Param("name") String name); } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java index 582b16f..b1abbeb 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/GroupRepository.java @@ -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 { @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 find(@Param("time") int time, @Param("room") String room, @Param("capacity") int capacity); @Query("FROM Groups WHERE course_id = ?1") List getByCourse(@Param("id") Long id); diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/LecturerRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/LecturerRepository.java index 1115ea9..d3bac97 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/LecturerRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/LecturerRepository.java @@ -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 { @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 find(@Param("title") String title, @Param("name") String name, @Param("surname") String surname); } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java b/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java index 8fbbbb4..82f883d 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java +++ b/buisnesslogic/src/main/java/com/plannaplan/repositories/UserRepository.java @@ -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 { @Query("FROM User WHERE email = ?1") - User getByAuthority(@Param("authority") String authority); + Optional getByAuthority(@Param("authority") String authority); @Query("FROM User WHERE token = ?1") User getByToken(@Param("token") String token); diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/CourseService.java b/buisnesslogic/src/main/java/com/plannaplan/services/CourseService.java index 15ae1aa..125abf8 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/CourseService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/CourseService.java @@ -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 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(); } } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java index 3142411..ff3c4d1 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/GroupService.java @@ -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 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(); } } \ 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 d02548a..9a2ee31 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/LecturerService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/LecturerService.java @@ -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 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(); } } \ No newline at end of file diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java index 0fec12f..d7fbe55 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/UserService.java @@ -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); }