From 2f5440696fb93f5ad1443a3bc3ff426a34137f0a Mon Sep 17 00:00:00 2001 From: schrom01 Date: Thu, 1 Dec 2022 23:45:53 +0100 Subject: [PATCH] Cleanup of Class BackgroundTasks, added Logger --- .../java/ch/zhaw/gartenverwaltung/Main.java | 1 + .../backgroundtasks/BackgroundTasks.java | 38 ++++++++++++++----- .../backgroundtasks/Notifier.java | 24 ++++-------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/main/java/ch/zhaw/gartenverwaltung/Main.java b/src/main/java/ch/zhaw/gartenverwaltung/Main.java index 9005ec0..d31860d 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/Main.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/Main.java @@ -22,6 +22,7 @@ public class Main extends Application { AppLoader appLoader = new AppLoader(); backgroundTasks = new BackgroundTasks((TaskList) appLoader.getAppDependency(TaskList.class),(CropList) appLoader.getAppDependency(CropList.class), (PlantList) appLoader.getAppDependency(PlantList.class)); + // TODO reduce period backGroundTaskTimer.scheduleAtFixedRate(backgroundTasks, 0, 1000); appLoader.loadSceneToStage("MainFXML.fxml", stage); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java index 33c58f6..96eed67 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/BackgroundTasks.java @@ -1,44 +1,62 @@ package ch.zhaw.gartenverwaltung.backgroundtasks; +import ch.zhaw.gartenverwaltung.CropDetailController; import ch.zhaw.gartenverwaltung.backgroundtasks.weather.WeatherGradenTaskPlanner; import ch.zhaw.gartenverwaltung.io.CropList; import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException; import ch.zhaw.gartenverwaltung.io.PlantList; import ch.zhaw.gartenverwaltung.io.TaskList; -import ch.zhaw.gartenverwaltung.models.Garden; import ch.zhaw.gartenverwaltung.models.PlantNotFoundException; +import ch.zhaw.gartenverwaltung.types.Task; import javax.mail.MessagingException; import java.io.IOException; +import java.time.LocalDate; +import java.util.List; import java.util.TimerTask; +import java.util.logging.Level; +import java.util.logging.Logger; public class BackgroundTasks extends TimerTask { + private static final Logger LOG = Logger.getLogger(CropDetailController.class.getName()); + private final TaskList taskList; private final Notifier notifier; private final WeatherGradenTaskPlanner weatherGardenTaskPlaner; + private void movePastTasks() throws IOException { + List taskList = this.taskList.getTaskList(LocalDate.MIN, LocalDate.now().minusDays(1)); + taskList.forEach(task -> { + if (!task.isDone()) { + task.setNextExecution(LocalDate.now()); + } + }); + } + public BackgroundTasks(TaskList taskList, CropList cropList, PlantList plantList) { + this.taskList = taskList; notifier = new Notifier(taskList, plantList, cropList); weatherGardenTaskPlaner = new WeatherGradenTaskPlanner(taskList, plantList, cropList); } @Override public void run() { + try { + movePastTasks(); + } catch (IOException e) { + e.printStackTrace(); + LOG.log(Level.SEVERE, "Could not execute Background Task: move past Tasks ", e.getCause()); + } try { weatherGardenTaskPlaner.refreshTasks(); } catch (IOException | HardinessZoneNotSetException | PlantNotFoundException e) { e.printStackTrace(); - // TODO logger + LOG.log(Level.SEVERE, "Could not execute Background Task: Refresh Tasks by WeatherGardenTaskPlaner ", e.getCause()); } try { - try { - notifier.sendNotifications(); - } catch (MessagingException e) { - e.printStackTrace(); - // TODO logger - } - } catch (IOException e) { + notifier.sendNotifications(); + } catch (IOException | MessagingException | HardinessZoneNotSetException e) { e.printStackTrace(); - // TODO logger + LOG.log(Level.SEVERE, "Could not execute Background Task: send Notification for due Tasks", e.getCause()); } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java index a77ae06..072cd25 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java @@ -25,30 +25,20 @@ public class Notifier { this.plantList = plantList; } - private void sendNotification(Task task) { + private void sendNotification(Task task) throws IOException, MessagingException, HardinessZoneNotSetException { String plantName = "unkown plant"; - try { - if(cropList.getCropById((task.getCropId())).isPresent()){ - Crop crop = cropList.getCropById(task.getCropId()).get(); - if(plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).isPresent()) { - plantName = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get().name(); - } + if(cropList.getCropById((task.getCropId())).isPresent()){ + Crop crop = cropList.getCropById(task.getCropId()).get(); + if(plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).isPresent()) { + plantName = plantList.getPlantById(Settings.getInstance().getCurrentHardinessZone(), crop.getPlantId()).get().name(); } - } catch (IOException | HardinessZoneNotSetException e) { - e.printStackTrace(); - // TODO logger } String messageSubject = String.format(Settings.getInstance().getMailNotificationSubjectTemplate(), task.getName()); String messageText = String.format(Settings.getInstance().getMailNotificationTextTemplate(), task.getName(), plantName, task.getNextExecution(), task.getDescription()); - try { - eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText); - } catch (MessagingException e) { - e.printStackTrace(); - // TODO Logger - } + eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText); } - public void sendNotifications() throws IOException, MessagingException { + public void sendNotifications() throws IOException, MessagingException, HardinessZoneNotSetException { for (Task task : taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)) { if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) { sendNotification(task);