From 220d13818500aec6ffc59069a667bfc542e4ee18 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Mon, 28 Nov 2022 19:31:08 +0100 Subject: [PATCH] fixed saving Task after editing --- .../ch/zhaw/gartenverwaltung/Settings.java | 2 +- .../backgroundtasks/Notifier.java | 27 ++++++++++++++++--- .../backgroundtasks/email/EMailSender.java | 7 +---- .../META-INF/javamail.default.address.map | 0 4 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 src/main/resources/META-INF/javamail.default.address.map diff --git a/src/main/java/ch/zhaw/gartenverwaltung/Settings.java b/src/main/java/ch/zhaw/gartenverwaltung/Settings.java index 63cf6e7..462237c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/Settings.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/Settings.java @@ -17,7 +17,7 @@ public class Settings { // E-Mail Inbox: https://www.mailinator.com/v4/public/inboxes.jsp?to=pm3.hs22.it21b.win.team1 private String mailNotificationReceivers = "pm3.hs22.it21b.win.team1@mailinator.com"; private String mailNotificationSubjectTemplate = "Task %s is due!"; // {0} = Task Name - private String mailNotificationTextTemplate = "Dear user\nYour gardentask %s is due since %tF. Don't forget to confirm in your application if the task is done.\nTask description:\n%s"; // {0} = Task Name, {1} = nextExecution, {2} = Task description + private String mailNotificationTextTemplate = "Dear user\nYour gardentask %s for plant %s is due at %tF. Don't forget to confirm in your application if the task is done.\nTask description:\n%s"; // {0} = Task Name, {1} = plantname, {2} = nextExecution, {3} = Task description static { instance = new Settings(); diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java index f36913f..c172e83 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/Notifier.java @@ -2,9 +2,11 @@ package ch.zhaw.gartenverwaltung.backgroundtasks; import ch.zhaw.gartenverwaltung.Settings; import ch.zhaw.gartenverwaltung.backgroundtasks.email.EMailSender; +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.types.Crop; import ch.zhaw.gartenverwaltung.types.Task; import javax.mail.MessagingException; @@ -23,11 +25,27 @@ public class Notifier { this.plantList = plantList; } - private void sendNotification(Task task) throws MessagingException { + private void sendNotification(Task task) { + String plantName = "unkown plant"; + try { + if(garden.getCrop(task.getCropId()).isPresent()){ + Crop crop = garden.getCrop(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(), task.getNextExecution(), task.getDescription()); - //TODO create Exception for email sent fail - eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText); + 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 { @@ -36,6 +54,7 @@ public class Notifier { if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) { sendNotification(task); task.setNextNotification(LocalDate.now().plusDays(1)); + taskList.saveTask(task); } } } diff --git a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java index 82f895a..1fa470c 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/backgroundtasks/email/EMailSender.java @@ -30,12 +30,7 @@ public class EMailSender { message.setText(text); message.setSentDate(new Date()); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients, false)); - try{ - Transport.send(message); - } catch (MessagingException e) { - e.printStackTrace(); - // TODO logger - } + Transport.send(message); } private void printMail(String receiver, String subject, String text){ diff --git a/src/main/resources/META-INF/javamail.default.address.map b/src/main/resources/META-INF/javamail.default.address.map new file mode 100644 index 0000000..e69de29