Skip to content

Commit

Permalink
Merge pull request #119 from PAException/dev
Browse files Browse the repository at this point in the history
Bugfix substitute classNames
  • Loading branch information
PAException authored Nov 28, 2023
2 parents 062480e + 5e9b3e0 commit 645b565
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 75 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docker-push-dev.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Build and push development Docker Image

on:
workflow_dispatch:
push:
branches: [ "dev" ]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Set;

import static io.github.paexception.engelsburg.api.util.Constants.Substitute.NAME_KEY;

Expand Down Expand Up @@ -104,36 +104,32 @@ public void updateSubstitutes(List<SubstituteDTO> fetchedDTOs, Date date) {
* @return substitutes with specific filters
*/
public Result<GetSubstitutesResponseDTO> getSubstitutes(String classNameFilter, String teacherFilter) {
List<String> classes = classNameFilter == null || classNameFilter.isBlank()
? new ArrayList<>()
: Arrays.asList(classNameFilter.split(","));
List<String> teacher = teacherFilter == null || teacherFilter.isBlank()
? new ArrayList<>()
: Arrays.asList(teacherFilter.split(","));
Set<String> classes = classNameFilter == null || classNameFilter.isBlank()
? new HashSet<>()
: new HashSet<>(Arrays.asList(classNameFilter.split(",")));
Set<String> teachers = teacherFilter == null || teacherFilter.isBlank()
? new HashSet<>()
: new HashSet<>(Arrays.asList(teacherFilter.split(",")));
final Date date = new Date(System.currentTimeMillis());

//Get all substitute based on optional parameters
List<SubstituteModel> substitutes = new ArrayList<>();
if (teacher.isEmpty() && classes.isEmpty()) {
substitutes = this.substituteRepository.findAllByDateGreaterThanEqual(date);
final List<SubstituteModel> substitutes = new ArrayList<>();
if (classes.isEmpty() && teachers.isEmpty()) {
substitutes.addAll(this.substituteRepository.findAllByDateGreaterThanEqual(date));
} else {
substitutes = this.substituteRepository.findAllByDateGreaterThanEqualAndClassNameIsNull(date);
}

if (!classes.isEmpty()) {
substitutes.addAll(this.substituteRepository.findAllByDateGreaterThanEqualAndClassNameIn(date, classes));

substitutes.addAll(
classes.stream().filter(className -> className.length() >= 3 && !(Character.isDigit(className.charAt(1)) && className.length() == 3)).map(
className -> this.substituteRepository.findAllByDateGreaterThanEqualAndClassNameVariations(date, className)
).flatMap(Collection::stream).collect(Collectors.toList()));
}
if (!teacher.isEmpty()) {
substitutes.addAll(
this.substituteRepository.findAllByDateGreaterThanEqualAndTeacherInOrDateGreaterThanEqualAndSubstituteTeacherIn(
date, teacher, date, teacher
)
);
for (SubstituteModel substitute : this.substituteRepository.findAllByDateGreaterThanEqual(date)) {
List<String> classNames = substitute.getClassName() == null
? new ArrayList<>()
: SubstituteModel.splitClasses(substitute.getClassName());

String teacher = substitute.getTeacher();
String substituteTeacher = substitute.getSubstituteTeacher();

if (classNames.isEmpty()) substitutes.add(substitute);
else if (classes.stream().anyMatch(classNames::contains)) substitutes.add(substitute);
else if (teacher != null && (teachers.contains(teacher) || teachers.contains(substituteTeacher))) {
substitutes.add(substitute);
}
}
}

//If no substitutes available return error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
Expand All @@ -18,6 +19,8 @@
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
Expand Down Expand Up @@ -45,6 +48,49 @@ public class SubstituteModel {
private String room;
private String text;

/**
* Splits possible className merges like 10ab to 10a, 10b to send notifications to specific topics.
*
* @param className to split
* @return list of classNames
*/
public static List<String> splitClasses(String className) {
if (className.length() <= 2 || (Character.isDigit(className.charAt(1)) && className.length() == 3)) {
return List.of(className);
} else { //5ab or 5ab6ab or E2Q2Q4
List<String> strings = new ArrayList<>();
StringBuilder curr = new StringBuilder();
char c;
boolean write = false, adv = false;
for (int i = 0; i < className.length(); i++) {
c = className.charAt(i);
if (Character.isDigit(c)) {
if (!adv || write) {
if (!write) {
write = true;
curr = new StringBuilder();
}
curr.append(c);
} else {
strings.add(curr.toString() + c);
curr = new StringBuilder();
}
} else {
if (Character.isLowerCase(c)) {
write = false;
strings.add(curr.toString() + c);
} else {
curr = new StringBuilder();
adv = true;
curr.append(c);
}
}
}

return strings;
}
}

public SubstituteDTO toResponseDTO() {
return new SubstituteDTO(
this.date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.Collection;
import java.util.List;

@Repository
Expand Down Expand Up @@ -40,10 +41,10 @@ default List<SubstituteModel> findAllByDateGreaterThanEqualAndClassNameVariation

List<SubstituteModel> findByDateGreaterThanEqualAndClassNameIsLike(Date date, String className);

List<SubstituteModel> findAllByDateGreaterThanEqualAndClassNameIn(Date date, List<String> classes);
List<SubstituteModel> findAllByDateGreaterThanEqualAndClassNameIn(Date date, Collection<String> classes);

List<SubstituteModel> findAllByDateGreaterThanEqualAndTeacherInOrDateGreaterThanEqualAndSubstituteTeacherIn(
Date date, List<String> teacher, Date date2, List<String> substituteTeacher);
Date date, Collection<String> teacher, Date date2, Collection<String> substituteTeacher);

void deleteAllByDate(Date date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.google.firebase.messaging.FirebaseMessagingException;
import io.github.paexception.engelsburg.api.controller.reserved.NotificationSettingsController;
import io.github.paexception.engelsburg.api.database.model.SubstituteModel;
import io.github.paexception.engelsburg.api.endpoint.dto.ArticleDTO;
import io.github.paexception.engelsburg.api.endpoint.dto.SubstituteDTO;
import io.github.paexception.engelsburg.api.endpoint.dto.response.SubstituteNotificationDTO;
Expand All @@ -16,6 +17,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.time.Duration;
import java.time.Instant;
Expand All @@ -37,49 +39,6 @@ public class NotificationService implements LoggingComponent {

private final NotificationSettingsController notificationSettingsController;

/**
* Splits possible className merges like 10ab to 10a, 10b to send notifications to specific topics.
*
* @param className to split
* @return list of classNames
*/
private static List<String> splitClasses(String className) {
if (className.length() <= 2 || (Character.isDigit(className.charAt(1)) && className.length() == 3)) {
return List.of(className);
} else { //5ab or 5ab6ab or E2Q2Q4
List<String> strings = new ArrayList<>();
StringBuilder curr = new StringBuilder();
char c;
boolean write = false, adv = false;
for (int i = 0; i < className.length(); i++) {
c = className.charAt(i);
if (Character.isDigit(c)) {
if (!adv || write) {
if (!write) {
write = true;
curr = new StringBuilder();
}
curr.append(c);
} else {
strings.add(curr.toString() + c);
curr = new StringBuilder();
}
} else {
if (Character.isLowerCase(c)) {
write = false;
strings.add(curr.toString() + c);
} else {
curr = new StringBuilder();
adv = true;
curr.append(c);
}
}
}

return strings;
}
}

/**
* Return a formatted text to display substitutes.
*
Expand Down Expand Up @@ -229,7 +188,7 @@ public void sendSubstituteNotifications(List<SubstituteDTO> dtos, boolean create

//Classes
if (dto.getClassName() != null) {
for (String className : splitClasses(dto.getClassName().toUpperCase()))
for (String className : SubstituteModel.splitClasses(dto.getClassName().toUpperCase()))
tokens.addAll(this.notificationSettingsController.getTokensOf("substitute.class." + className));
}

Expand Down

0 comments on commit 645b565

Please sign in to comment.