fixed saving Task after editing
This commit is contained in:
parent
fd184e1248
commit
220d138185
|
@ -17,7 +17,7 @@ public class Settings {
|
||||||
// E-Mail Inbox: https://www.mailinator.com/v4/public/inboxes.jsp?to=pm3.hs22.it21b.win.team1
|
// 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 mailNotificationReceivers = "pm3.hs22.it21b.win.team1@mailinator.com";
|
||||||
private String mailNotificationSubjectTemplate = "Task %s is due!"; // {0} = Task Name
|
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 {
|
static {
|
||||||
instance = new Settings();
|
instance = new Settings();
|
||||||
|
|
|
@ -2,9 +2,11 @@ package ch.zhaw.gartenverwaltung.backgroundtasks;
|
||||||
|
|
||||||
import ch.zhaw.gartenverwaltung.Settings;
|
import ch.zhaw.gartenverwaltung.Settings;
|
||||||
import ch.zhaw.gartenverwaltung.backgroundtasks.email.EMailSender;
|
import ch.zhaw.gartenverwaltung.backgroundtasks.email.EMailSender;
|
||||||
|
import ch.zhaw.gartenverwaltung.io.HardinessZoneNotSetException;
|
||||||
import ch.zhaw.gartenverwaltung.io.PlantList;
|
import ch.zhaw.gartenverwaltung.io.PlantList;
|
||||||
import ch.zhaw.gartenverwaltung.io.TaskList;
|
import ch.zhaw.gartenverwaltung.io.TaskList;
|
||||||
import ch.zhaw.gartenverwaltung.models.Garden;
|
import ch.zhaw.gartenverwaltung.models.Garden;
|
||||||
|
import ch.zhaw.gartenverwaltung.types.Crop;
|
||||||
import ch.zhaw.gartenverwaltung.types.Task;
|
import ch.zhaw.gartenverwaltung.types.Task;
|
||||||
|
|
||||||
import javax.mail.MessagingException;
|
import javax.mail.MessagingException;
|
||||||
|
@ -23,11 +25,27 @@ public class Notifier {
|
||||||
this.plantList = plantList;
|
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 messageSubject = String.format(Settings.getInstance().getMailNotificationSubjectTemplate(), task.getName());
|
||||||
String messageText = String.format(Settings.getInstance().getMailNotificationTextTemplate(), task.getName(), task.getNextExecution(), task.getDescription());
|
String messageText = String.format(Settings.getInstance().getMailNotificationTextTemplate(), task.getName(), plantName, task.getNextExecution(), task.getDescription());
|
||||||
//TODO create Exception for email sent fail
|
try {
|
||||||
eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText);
|
eMailSender.sendMails(Settings.getInstance().getMailNotificationReceivers(), messageSubject, messageText);
|
||||||
|
} catch (MessagingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
// TODO Logger
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendNotifications() throws IOException, MessagingException {
|
public void sendNotifications() throws IOException, MessagingException {
|
||||||
|
@ -36,6 +54,7 @@ public class Notifier {
|
||||||
if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) {
|
if (task.getNextNotification() != null && task.getNextNotification().isBefore(LocalDate.now().minusDays(1))) {
|
||||||
sendNotification(task);
|
sendNotification(task);
|
||||||
task.setNextNotification(LocalDate.now().plusDays(1));
|
task.setNextNotification(LocalDate.now().plusDays(1));
|
||||||
|
taskList.saveTask(task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,12 +30,7 @@ public class EMailSender {
|
||||||
message.setText(text);
|
message.setText(text);
|
||||||
message.setSentDate(new Date());
|
message.setSentDate(new Date());
|
||||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients, false));
|
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients, false));
|
||||||
try{
|
Transport.send(message);
|
||||||
Transport.send(message);
|
|
||||||
} catch (MessagingException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
// TODO logger
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void printMail(String receiver, String subject, String text){
|
private void printMail(String receiver, String subject, String text){
|
||||||
|
|
Loading…
Reference in New Issue