-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSendReminderAction.java
67 lines (58 loc) · 2.85 KB
/
SendReminderAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package edu.ncsu.csc.itrust.action;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import edu.ncsu.csc.itrust.beans.ApptBean;
import edu.ncsu.csc.itrust.beans.MessageBean;
import edu.ncsu.csc.itrust.beans.PersonnelBean;
import edu.ncsu.csc.itrust.dao.DAOFactory;
import edu.ncsu.csc.itrust.dao.mysql.ApptDAO;
import edu.ncsu.csc.itrust.dao.mysql.PersonnelDAO;
import edu.ncsu.csc.itrust.exception.DBException;
import edu.ncsu.csc.itrust.exception.FormValidationException;
import edu.ncsu.csc.itrust.exception.ITrustException;
import java.util.*;
public class SendReminderAction {
public final long systemReminderMID;
private long loggedInMID;
private ApptDAO apptDAO;
private SendMessageAction smAction;
public SendReminderAction(DAOFactory factory, long loggedInMID) throws DBException {
PersonnelDAO personnelDAO = factory.getPersonnelDAO();
List<PersonnelBean> personnels = personnelDAO.searchForPersonnelWithName("System", "Reminder");
this.systemReminderMID = personnels.get(0).getMID();
this.loggedInMID = loggedInMID;
this.apptDAO = factory.getApptDAO();
this.smAction = new SendMessageAction(factory, systemReminderMID);
}
public void sendReminder(ApptBean aBean) throws ITrustException, SQLException, FormValidationException {
LocalDateTime now = LocalDateTime.now();
LocalDateTime date = aBean.getDate().toLocalDateTime();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm, MMM d");
MessageBean message = new MessageBean();
message.setTo(aBean.getPatient());
message.setFrom(systemReminderMID);
message.setSubject(String.format("Reminder: upcoming appointment in %d day(s)", now.truncatedTo(ChronoUnit.DAYS).until(date.truncatedTo(ChronoUnit.DAYS), ChronoUnit.DAYS)));
message.setBody(String.format("You have an appointment on %s with Dr. %s", date.format(formatter), smAction.getPersonnelName(aBean.getHcp())));
message.setSentDate(Timestamp.valueOf(now));
smAction.sendMessage(message);
}
public int sendReminderForAppointments(int numDays) throws ITrustException {
List<ApptBean> appointments = null;
try {
appointments = apptDAO.getUpcomingAppts(numDays);
for (ApptBean appt : appointments) {
sendReminder(appt);
}
return appointments.size();
} catch (DBException e) {
throw new ITrustException("DB Error in sending reminders.");
} catch (SQLException e) {
throw new ITrustException("SQL Error in sending reminders.");
} catch (FormValidationException e) {
throw new ITrustException("Form Validation Error in sending reminders.");
}
}
}