Skip to content

Commit

Permalink
Hotfix substitutes
Browse files Browse the repository at this point in the history
  • Loading branch information
PAException committed Nov 27, 2023
1 parent 1a9fa51 commit cb22134
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

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

Expand All @@ -29,108 +32,116 @@
@AllArgsConstructor
public class SubstituteController {

private static long timestamp = 0;

private final SubstituteRepository substituteRepository;
private final NotificationService notificationService;

/**
* Create a {@link SubstituteModel} out of a {@link SubstituteDTO}.
*
* @param dto with information
* @return created substitute model
*/
private static SubstituteModel createSubstitute(SubstituteDTO dto) {
return new SubstituteModel(
-1,
dto.getDate(),
dto.getClassName(),
dto.getLesson(),
dto.getSubject(),
dto.getSubstituteTeacher() != null ? dto.getSubstituteTeacher().toUpperCase() : null,
dto.getTeacher() != null ? dto.getTeacher().toUpperCase() : null,
dto.getType(),
dto.getSubstituteOf(),
dto.getRoom(),
dto.getText()
);
}

/**
* Update substitutes.
* Only {@link SubstituteUpdateService} is supposed to call
* this function!
*
* @param fetchedDTOs with all crawled substitutes
* @param date of substitutes
*/
@Transactional
public void updateSubstitutes(List<SubstituteDTO> fetchedDTOs, Date date) {
timestamp = System.currentTimeMillis();

List<SubstituteDTO> 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<SubstituteDTO> updated = new ArrayList<>(), created = new ArrayList<>();
List<SubstituteModel> toSave = new ArrayList<>();
for (SubstituteDTO dto: fetchedDTOs) {
if (current.stream().anyMatch(substituteDTO -> substituteDTO.sameBase(dto))) {
if (!current.contains(dto)) updated.add(dto);
} else created.add(dto);

toSave.add(createSubstitute(dto));
}

this.substituteRepository.saveAll(toSave);

//Send notifications if lists are not empty
if (!updated.isEmpty()) this.notificationService.sendSubstituteNotifications(updated, false);
if (!created.isEmpty()) this.notificationService.sendSubstituteNotifications(created, true);
}

/**
* Get substitutes by specific filter.
* All parameters are optional.
*
* @param classNameFilter (optional) filter by className
* @param teacherFilter (optional) filter by teacher
* @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(","));
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);
}
if (!classes.isEmpty()) {
substitutes.addAll(this.substituteRepository.findAllByDateGreaterThanEqualAndClassNameIn(date, classes));
}
if (!teacher.isEmpty()) {
substitutes.addAll(
this.substituteRepository.findAllByDateGreaterThanEqualAndTeacherInOrDateGreaterThanEqualAndSubstituteTeacherIn(
date, teacher, date, teacher
)
);
}

//If no substitutes available return error
if (substitutes.isEmpty()) return Result.of(Error.NOT_FOUND, NAME_KEY);

//Map substitutes to response dtos and return them
List<SubstituteDTO> dtos = new ArrayList<>();
for (SubstituteModel substitute : substitutes) dtos.add(substitute.toResponseDTO());
return Result.of(new GetSubstitutesResponseDTO(dtos, timestamp));
}
private static long timestamp = 0;

private final SubstituteRepository substituteRepository;
private final NotificationService notificationService;

/**
* Create a {@link SubstituteModel} out of a {@link SubstituteDTO}.
*
* @param dto with information
* @return created substitute model
*/
private static SubstituteModel createSubstitute(SubstituteDTO dto) {
return new SubstituteModel(
-1,
dto.getDate(),
dto.getClassName(),
dto.getLesson(),
dto.getSubject(),
dto.getSubstituteTeacher() != null ? dto.getSubstituteTeacher().toUpperCase() : null,
dto.getTeacher() != null ? dto.getTeacher().toUpperCase() : null,
dto.getType(),
dto.getSubstituteOf(),
dto.getRoom(),
dto.getText()
);
}

/**
* Update substitutes.
* Only {@link SubstituteUpdateService} is supposed to call
* this function!
*
* @param fetchedDTOs with all crawled substitutes
* @param date of substitutes
*/
@Transactional
public void updateSubstitutes(List<SubstituteDTO> fetchedDTOs, Date date) {
timestamp = System.currentTimeMillis();

List<SubstituteDTO> 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<SubstituteDTO> updated = new ArrayList<>(), created = new ArrayList<>();
List<SubstituteModel> toSave = new ArrayList<>();
for (SubstituteDTO dto : fetchedDTOs) {
if (current.stream().anyMatch(substituteDTO -> substituteDTO.sameBase(dto))) {
if (!current.contains(dto)) updated.add(dto);
} else created.add(dto);

toSave.add(createSubstitute(dto));
}

this.substituteRepository.saveAll(toSave);

//Send notifications if lists are not empty
if (!updated.isEmpty()) this.notificationService.sendSubstituteNotifications(updated, false);
if (!created.isEmpty()) this.notificationService.sendSubstituteNotifications(created, true);
}

/**
* Get substitutes by specific filter.
* All parameters are optional.
*
* @param classNameFilter (optional) filter by className
* @param teacherFilter (optional) filter by teacher
* @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(","));
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);
} 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
)
);
}

//If no substitutes available return error
if (substitutes.isEmpty()) return Result.of(Error.NOT_FOUND, NAME_KEY);

//Map substitutes to response dtos and return them
List<SubstituteDTO> dtos = new ArrayList<>();
for (SubstituteModel substitute : substitutes) dtos.add(substitute.toResponseDTO());
return Result.of(new GetSubstitutesResponseDTO(dtos, timestamp));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,43 @@
import io.github.paexception.engelsburg.api.database.model.SubstituteModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

@Repository
public interface SubstituteRepository extends JpaRepository<SubstituteModel, Integer> {

/**
* 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) + "%";
}

List<SubstituteModel> findAllByDate(Date date);
/**
* 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) + "%";
}

Optional<SubstituteModel> findByDateAndLessonAndTeacher(Date date, int lesson, String teacher);
List<SubstituteModel> findAllByDate(Date date);

default Optional<SubstituteModel> findByDateAndLessonAndClassNameLike(Date date, int lesson, String className) {
return this.findByDateAndLessonAndClassNameIsLike(date, lesson, likeClassName(className));
}
List<SubstituteModel> findAllByDateGreaterThanEqual(Date date);

Optional<SubstituteModel> findByDateAndLessonAndClassNameIsLike(Date date, int lesson, String className);
List<SubstituteModel> findAllByDateGreaterThanEqualAndClassNameIsNull(Date date);

Optional<SubstituteModel> findByDateAndLessonAndSubject(Date date, int lesson, String subject);
default List<SubstituteModel> findAllByDateGreaterThanEqualAndClassNameVariations(Date date, String className) {
return this.findByDateGreaterThanEqualAndClassNameIsLike(date, likeClassName(className));
}

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

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

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

void deleteAllByDate(Date date);
void deleteAllByDate(Date date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public boolean sameBase(SubstituteDTO dto) {
if (lesson != dto.lesson) return false;
if (!Objects.equals(className, dto.className)) return false;

if (!Character.isDigit(className.charAt(0))) { //Only for E1 - Q4
if (className != null && !Character.isDigit(className.charAt(0))) { //Only for E1 - Q4
return Objects.equals(teacher, dto.teacher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static String getSubstituteTitle(SubstituteNotificationDTO substitute, b
} else if (DateUtils.isSameDay(substitute.getDate(), Date.from(Instant.now().plus(Duration.ofDays(1))))) {
relationalDay = "morgen";
} else {
relationalDay = "den " + DateTimeFormatter.ofPattern("dd.MM.").format(substitute.getDate().toInstant());
relationalDay = "den " + DateTimeFormatter.ofPattern("dd.MM.").format(substitute.getDate().toLocalDate());
}

return actuality + " Vertretung für " + relationalDay;
Expand Down

0 comments on commit cb22134

Please sign in to comment.