Compare commits

..

No commits in common. "564cecd4ffb6721e8f251a12143b3777ddf402e4" and "5e42cae2902f30fa80c82e7268900fcccd8c06db" have entirely different histories.

3 changed files with 27 additions and 36 deletions

View File

@ -22,7 +22,6 @@ 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);

View File

@ -1,62 +1,44 @@
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<Task> 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();
LOG.log(Level.SEVERE, "Could not execute Background Task: Refresh Tasks by WeatherGardenTaskPlaner ", e.getCause());
// TODO logger
}
try {
try {
notifier.sendNotifications();
} catch (IOException | MessagingException | HardinessZoneNotSetException e) {
} catch (MessagingException e) {
e.printStackTrace();
LOG.log(Level.SEVERE, "Could not execute Background Task: send Notification for due Tasks", e.getCause());
// TODO logger
}
} catch (IOException e) {
e.printStackTrace();
// TODO logger
}
}
}

View File

@ -25,20 +25,30 @@ public class Notifier {
this.plantList = plantList;
}
private void sendNotification(Task task) throws IOException, MessagingException, HardinessZoneNotSetException {
private void sendNotification(Task task) {
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();
}
}
} 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
}
}
public void sendNotifications() throws IOException, MessagingException, HardinessZoneNotSetException {
public void sendNotifications() throws IOException, MessagingException {
for (Task task : taskList.getTaskList(LocalDate.MIN, LocalDate.MAX)) {
if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) {
sendNotification(task);