From d8069ba320f15cd7c77cbe6bbdc083d8bb3ef2b7 Mon Sep 17 00:00:00 2001 From: Paul Huerkamp <35733278+PAException@users.noreply.github.com> Date: Wed, 1 Nov 2023 01:38:28 +0100 Subject: [PATCH 1/2] Fixed substitutes --- .../NotificationSettingsController.java | 9 +- .../reserved/SubstituteController.java | 40 ++-- .../repository/SubstituteRepository.java | 10 + .../api/endpoint/dto/SubstituteDTO.java | 22 +++ .../scheduled/SubstituteParseTest.java | 173 ++++++++++++++++++ 5 files changed, 224 insertions(+), 30 deletions(-) diff --git a/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/NotificationSettingsController.java b/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/NotificationSettingsController.java index 3fb0a92..7825baf 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/NotificationSettingsController.java +++ b/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/NotificationSettingsController.java @@ -102,8 +102,13 @@ public List getTimetableTokens(SubstituteNotificationDTO dto) { //Add all possible combinations List combinations = new ArrayList<>(); for (int i = lower; i <= upper; i++) { - combinations.add(prefix + "." + day + "." + i + "." + teacher); - combinations.add(prefix + "." + day + "." + i + "." + className); + if (teacher != null && !teacher.isEmpty()) { + combinations.add(prefix + "." + day + "." + i + "." + teacher); + } + + if (className != null && !className.isEmpty()) { + combinations.add(prefix + "." + day + "." + i + "." + className); + } } //Return all tokens that matched the combinations diff --git a/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/SubstituteController.java b/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/SubstituteController.java index 23b1553..10b1807 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/SubstituteController.java +++ b/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/SubstituteController.java @@ -19,7 +19,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Optional; + import static io.github.paexception.engelsburg.api.util.Constants.Substitute.NAME_KEY; /** @@ -75,38 +75,22 @@ private static SubstituteModel createSubstitute(int substituteId, SubstituteDTO */ @Transactional public void updateSubstitutes(List fetchedDTOs, Date date) { - //Get all substitutes by date, remove all which are also in fetched dtos - for (SubstituteModel substitute : this.substituteRepository.findAllByDate(date)) - fetchedDTOs.remove(substitute.toResponseDTO()); + List current = new ArrayList<>(); + for (SubstituteModel substitute : this.substituteRepository.findAllByDate(date)) { + current.add(substitute.toResponseDTO()); + } + this.substituteRepository.deleteAllByDate(date); //Check if substitutes have been updated or newly created List updated = new ArrayList<>(), created = new ArrayList<>(); List toSave = new ArrayList<>(); - for (SubstituteDTO dto : fetchedDTOs) { - //Get optional substitute based on dto information - Optional optionalSubstitute; - if (Character.isDigit(dto.getClassName().charAt(0))) { //5a-10e - optionalSubstitute = this.substituteRepository - .findByDateAndLessonAndClassNameLike(date, dto.getLesson(), dto.getClassName()); - } else if (notBlank(dto.getTeacher())) { //E1-Q4 with teacher - optionalSubstitute = this.substituteRepository - .findByDateAndLessonAndTeacher(date, dto.getLesson(), dto.getTeacher()); - } else { //E1-Q4 without teacher - optionalSubstitute = this.substituteRepository - .findByDateAndLessonAndSubject(date, dto.getLesson(), dto.getSubject()); - } - - //Check if the substitute was newly created or updated - if (optionalSubstitute.isPresent()) { - //Update substitute and add to updated list - toSave.add(createSubstitute(optionalSubstitute.get().getSubstituteId(), dto)); - updated.add(dto); - } else { - //Save newly created substitute and add to created list - toSave.add(createSubstitute(-1, dto)); - created.add(dto); - } + for (SubstituteDTO dto: fetchedDTOs) { + if (current.contains(dto)) updated.add(dto); + else created.add(dto); + + toSave.add(createSubstitute(-1, dto)); } + this.substituteRepository.saveAll(toSave); //Send notifications if lists are not empty diff --git a/src/main/java/io/github/paexception/engelsburg/api/database/repository/SubstituteRepository.java b/src/main/java/io/github/paexception/engelsburg/api/database/repository/SubstituteRepository.java index fa9793f..946d39f 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/database/repository/SubstituteRepository.java +++ b/src/main/java/io/github/paexception/engelsburg/api/database/repository/SubstituteRepository.java @@ -14,6 +14,14 @@ @Repository public interface SubstituteRepository extends JpaRepository { + /** + * Converts the class to a like parameter. + * 9c --> 9%c% + * 10c --> 10%c% + * should not be used vor E1 - Q4 + * @param className to convert + * @return parsed parameter + */ static String likeClassName(String className) { if (className.length() == 2) return className.charAt(0) + "%" + className.charAt(1) + "%"; else return className.substring(0, 2) + "%" + className.charAt(2) + "%"; @@ -37,4 +45,6 @@ default Optional findByDateAndLessonAndClassNameLike(Date date, List findAllByDateGreaterThanEqualAndTeacherInOrDateGreaterThanEqualAndSubstituteTeacherIn( Date date, List teacher, Date date2, List substituteTeacher); + + void deleteAllByDate(Date date); } diff --git a/src/main/java/io/github/paexception/engelsburg/api/endpoint/dto/SubstituteDTO.java b/src/main/java/io/github/paexception/engelsburg/api/endpoint/dto/SubstituteDTO.java index 0418cd4..d03bfc2 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/endpoint/dto/SubstituteDTO.java +++ b/src/main/java/io/github/paexception/engelsburg/api/endpoint/dto/SubstituteDTO.java @@ -9,6 +9,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import java.sql.Date; +import java.util.Objects; @Data @NoArgsConstructor @@ -42,4 +43,25 @@ public SubstituteDTO appendText(String text) { return this; } + @Override + public boolean equals(Object other) { + if (this == other) return true; + if (other == null || getClass() != other.getClass()) return false; + + SubstituteDTO dto = (SubstituteDTO) other; + if (!Objects.equals(date, dto.date)) return false; + if (lesson != dto.lesson) return false; + if (!Objects.equals(className, dto.className)) return false; + + if (!Character.isDigit(className.charAt(0))) { //Only for E1 - Q4 + return Objects.equals(teacher, dto.teacher); + } + + return true; + } + + @Override + public int hashCode() { + return Objects.hash(date, className, lesson, teacher); + } } diff --git a/src/test/java/io/github/paexception/engelsburg/api/test/service/scheduled/SubstituteParseTest.java b/src/test/java/io/github/paexception/engelsburg/api/test/service/scheduled/SubstituteParseTest.java index ae4715b..16503a8 100644 --- a/src/test/java/io/github/paexception/engelsburg/api/test/service/scheduled/SubstituteParseTest.java +++ b/src/test/java/io/github/paexception/engelsburg/api/test/service/scheduled/SubstituteParseTest.java @@ -2,6 +2,9 @@ import io.github.paexception.engelsburg.api.service.scheduled.SubstituteUpdateService; import org.junit.jupiter.api.Test; +import io.github.paexception.engelsburg.api.endpoint.dto.SubstituteDTO; +import java.sql.Date; +import java.util.Objects; public class SubstituteParseTest { @@ -10,4 +13,174 @@ public void testSubstituteParse() { new SubstituteUpdateService(null, null, null).updateSubstitutes(); } + @Test + public void substituteDTOEqualTest() { + //Correct 5a - 10e + SubstituteDTO dto1 = new SubstituteDTO( + new Date(169879761), + "10c", + 2, + "M", + "BSU", + "GAR", + "Vertretung", + "Mo-21.2. / 4", + "H301", + "Aufg. vorhanden" + ); + SubstituteDTO dto2 = new SubstituteDTO( + new Date(169879761), + "10c", + 2, + "D", + "GRB", + "KLE", + "Betreuung", + "asg", + "dshssdssh", + "dshgdhsen" + ); + + assert Objects.equals(dto1, dto2); + + //Different lesson 5a - 10e + SubstituteDTO dto3 = new SubstituteDTO( + new Date(169879761), + "10a", + 2, + "M", + "BSU", + "GAR", + "Vertretung", + "Mo-21.2. / 4", + "H301", + "Aufg. vorhanden" + ); + SubstituteDTO dto4 = new SubstituteDTO( + new Date(169879761), + "10c", + 3, + "D", + "GRB", + "KLE", + "Betreuung", + "asg", + "dshssdssh", + "dshgdhsen" + ); + + assert !Objects.equals(dto3, dto4); + + //Different class + SubstituteDTO dto5 = new SubstituteDTO( + new Date(169879761), + "Q3", + 2, + "M", + "BSU", + "GAR", + "Vertretung", + "Mo-21.2. / 4", + "H301", + "Aufg. vorhanden" + ); + SubstituteDTO dto6 = new SubstituteDTO( + new Date(169879761), + "10c", + 2, + "D", + "GRB", + "KLE", + "Betreuung", + "asg", + "dshssdssh", + "dshgdhsen" + ); + + assert !Objects.equals(dto5, dto6); + + //Different date + SubstituteDTO dto7 = new SubstituteDTO( + new Date(169761), + "10c", + 2, + "M", + "BSU", + "GAR", + "Vertretung", + "Mo-21.2. / 4", + "H301", + "Aufg. vorhanden" + ); + SubstituteDTO dto8 = new SubstituteDTO( + new Date(169879761), + "10c", + 2, + "D", + "GRB", + "KLE", + "Betreuung", + "asg", + "dshssdssh", + "dshgdhsen" + ); + + assert !Objects.equals(dto7, dto8); + + //Different teacher E1 - Q4 + SubstituteDTO dto9 = new SubstituteDTO( + new Date(169761), + "Q3", + 2, + "M", + "BSU", + "GAR", + "Vertretung", + "Mo-21.2. / 4", + "H301", + "Aufg. vorhanden" + ); + SubstituteDTO dto10 = new SubstituteDTO( + new Date(169879761), + "Q3", + 2, + "D", + "GRB", + "KLE", + "Betreuung", + "asg", + "dshssdssh", + "dshgdhsen" + ); + + assert !Objects.equals(dto9, dto10); + + //Correct teacher E1 - Q4 + SubstituteDTO dto11 = new SubstituteDTO( + new Date(169761), + "Q3", + 2, + "M", + "BSU", + "GAR", + "Vertretung", + "Mo-21.2. / 4", + "H301", + "Aufg. vorhanden" + ); + SubstituteDTO dto12 = new SubstituteDTO( + new Date(169879761), + "Q3", + 2, + "D", + "BSU", + "KLE", + "Betreuung", + "asg", + "dshssdssh", + "dshgdhsen" + ); + + assert !Objects.equals(dto11, dto12); + } } From 4336fe509bddb93edfcb3acbc229eca25f63299e Mon Sep 17 00:00:00 2001 From: Paul Huerkamp <35733278+PAException@users.noreply.github.com> Date: Wed, 1 Nov 2023 01:58:27 +0100 Subject: [PATCH 2/2] Bugfix notification service, added timestamp for substitutes --- .../reserved/NotificationSettingsController.java | 2 +- .../api/controller/reserved/SubstituteController.java | 7 ++++++- .../endpoint/dto/response/GetSubstitutesResponseDTO.java | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/NotificationSettingsController.java b/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/NotificationSettingsController.java index 7825baf..02b381f 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/NotificationSettingsController.java +++ b/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/NotificationSettingsController.java @@ -118,6 +118,6 @@ public List getTimetableTokens(SubstituteNotificationDTO dto) { @Transactional public void deleteInvalidTokens(List invalidTokens) { //Delete all invalid tokens - this.tokenRepository.deleteAllByTokenIn(invalidTokens); + if (!invalidTokens.isEmpty()) this.tokenRepository.deleteAllByTokenIn(invalidTokens); } } diff --git a/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/SubstituteController.java b/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/SubstituteController.java index 10b1807..4294f88 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/SubstituteController.java +++ b/src/main/java/io/github/paexception/engelsburg/api/controller/reserved/SubstituteController.java @@ -29,9 +29,12 @@ @AllArgsConstructor public class SubstituteController { + private static long timestamp = 0; + private final SubstituteRepository substituteRepository; private final NotificationService notificationService; + /** * Checks if a string is not blank, empty or null. * @@ -75,6 +78,8 @@ private static SubstituteModel createSubstitute(int substituteId, SubstituteDTO */ @Transactional public void updateSubstitutes(List fetchedDTOs, Date date) { + timestamp = System.currentTimeMillis(); + List current = new ArrayList<>(); for (SubstituteModel substitute : this.substituteRepository.findAllByDate(date)) { current.add(substitute.toResponseDTO()); @@ -137,6 +142,6 @@ public Result getSubstitutes(String classNameFilter, //Map substitutes to response dtos and return them List dtos = new ArrayList<>(); for (SubstituteModel substitute : substitutes) dtos.add(substitute.toResponseDTO()); - return Result.of(new GetSubstitutesResponseDTO(dtos)); + return Result.of(new GetSubstitutesResponseDTO(dtos, timestamp)); } } diff --git a/src/main/java/io/github/paexception/engelsburg/api/endpoint/dto/response/GetSubstitutesResponseDTO.java b/src/main/java/io/github/paexception/engelsburg/api/endpoint/dto/response/GetSubstitutesResponseDTO.java index ffdac4a..7ec53c8 100644 --- a/src/main/java/io/github/paexception/engelsburg/api/endpoint/dto/response/GetSubstitutesResponseDTO.java +++ b/src/main/java/io/github/paexception/engelsburg/api/endpoint/dto/response/GetSubstitutesResponseDTO.java @@ -16,5 +16,6 @@ public class GetSubstitutesResponseDTO { private List substitutes; + private long timestamp; }