
package org.synyx.urlaubsverwaltung.core.cron;

import org.joda.time.DateMidnight;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Service;

import org.synyx.urlaubsverwaltung.core.application.domain.Application;
import org.synyx.urlaubsverwaltung.core.application.domain.ApplicationStatus;
import org.synyx.urlaubsverwaltung.core.application.service.ApplicationService;
import org.synyx.urlaubsverwaltung.core.mail.MailService;
import org.synyx.urlaubsverwaltung.core.settings.SettingsService;
import org.synyx.urlaubsverwaltung.core.sicknote.SickNote;
import org.synyx.urlaubsverwaltung.core.sicknote.SickNoteService;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;


/**
 * Service to send emails at a particular time. (Cronjob)
 *
 * @author  Aljona Murygina - murygina@synyx.de
 */
@Service
public class CronMailService {

    private final ApplicationService applicationService;
    private final SettingsService settingsService;
    private final SickNoteService sickNoteService;
    private final MailService mailService;

    @Autowired
    public CronMailService(ApplicationService applicationService,
                           SettingsService settingsService,
                           SickNoteService sickNoteService,
                           MailService mailService) {

        this.applicationService = applicationService;
        this.settingsService = settingsService;
        this.sickNoteService = sickNoteService;
        this.mailService = mailService;
    }

    @Scheduled(cron = "${uv.cron.endOfSickPayNotification}")
    public void sendEndOfSickPayNotification() {

        List<SickNote> sickNotes = sickNoteService.getSickNotesReachingEndOfSickPay();

        for (SickNote sickNote : sickNotes) {
            mailService.sendEndOfSickPayNotification(sickNote);
        }
    }

    @Scheduled(cron = "${uv.cron.daysBeforeWaitingApplicationsReminderNotification}")
    public void sendWaitingApplicationsReminderNotification() {

        boolean isRemindForWaitingApplicationsActive =
                settingsService.getSettings().getAbsenceSettings().getRemindForWaitingApplications();

        if (isRemindForWaitingApplicationsActive) {
            List<Application> allWaitingApplications =
                    applicationService.getApplicationsForACertainState(ApplicationStatus.WAITING);

            List<Application> longWaitingApplications = allWaitingApplications.stream()
                    .filter(isLongWaitingApplications())
                    .collect(Collectors.toList());

            mailService.sendRemindForWaitingApplicationsReminderNotification(longWaitingApplications);

            for (Application longWaitingApplication : longWaitingApplications) {
                longWaitingApplication.setRemindDate(DateMidnight.now());
                applicationService.save(longWaitingApplication);
            }

        }

    }

    private Predicate<Application> isLongWaitingApplications() {
        return application -> {

            DateMidnight remindDate = application.getRemindDate();
            if (remindDate == null) {
                Integer daysBeforeRemindForWaitingApplications =
                        settingsService.getSettings().getAbsenceSettings().getDaysBeforeRemindForWaitingApplications();

                // never reminded before
                DateMidnight minDateForNotification = application.getApplicationDate()
                        .plusDays(daysBeforeRemindForWaitingApplications);

                // true -> remind!
                // false -> to early for notification
                return minDateForNotification.isBeforeNow();
            } else {
                // true -> not reminded today
                // false -> allready remined today
                return !remindDate.isEqual(DateMidnight.now());
            }
        };
    }
}

@@ -76,16 +76,49 @@
 idnight;
+%0A%0Aimport org.apache.log4j.Logger;
 %0Aimport 
@@ -1074,24 +1074,104 @@
 lService %7B%0A%0A
+    private static final Logger LOG = Logger.getLogger(CronMailService.class);%0A%0A
     private 
@@ -2009,24 +2009,113 @@
 SickPay();%0A%0A
+        LOG.info(%22Found %22 + sickNotes.size() + %22 sick notes reaching end of sick pay%22);%0A%0A
         for 
@@ -2890,16 +2890,16 @@
 ions())%0A
-
         
@@ -2938,24 +2938,141 @@
 toList());%0A%0A
+            LOG.info(%22Found %22 + longWaitingApplications.size() + %22 applications for leave waiting longer already%22);%0A%0A
             

Add logging to cron mail service