Search in sources :

Example 16 with AdminTask

use of web.AdminTask in project common by zenlunatics.

the class EventProvider method sendReminders.

// --------------------------------------------------------------------------
@AdminTask({ "nightly" })
public void sendReminders(LocalDateTime now, boolean nightly, Site site, DBConnection db) {
    Mail mail = new Mail(site);
    ArrayList<Integer> delete_ids = new ArrayList<Integer>();
    String reminders_table = m_events_table + "_reminders";
    try {
        StringBuilder columns = new StringBuilder();
        columns.append(m_events_table).append(".id,").append(reminders_table).append(".id AS reminder_id,").append(m_events_table).append("_id,last_sent,note,email,num,unit,before,date,notes");
        if (m_events_have_event)
            columns.append(",event");
        if (m_events_can_repeat)
            columns.append(",repeat,end_date,repeat_days");
        if (m_events_have_start_time)
            columns.append(",start_time,end_time");
        if (m_events_are_owned)
            columns.append(",").append(m_events_table).append("._owner_");
        Select query = new Select(columns.toString()).from(m_events_table).joinOn(reminders_table, "(" + m_events_table + ".id=" + reminders_table + "." + m_events_table + "_id OR " + reminders_table + "." + m_events_table + "_id IS NULL)").where(nightly ? "(unit='day' OR unit='week')" : "(unit='hour' OR unit='minute')");
        andFilters(query);
        ResultSet rs = db.select(query);
        Calendar calendar = site.newCalendar();
        while (rs.next()) {
            int num = rs.getInt("num");
            String unit = rs.getString("unit");
            boolean before = rs.getBoolean("before");
            int num_days = numDays(num, unit, before);
            LocalDate reminder_date = site.newLocalDate().minusDays(num_days);
            Event event = new Event(this, calendar, null);
            event.load(rs, site);
            if (event.hasEndedBy(reminder_date)) {
                int id = rs.getInt(3);
                if (!rs.wasNull())
                    delete_ids.add(id);
            } else {
                boolean occurs_on = false;
                boolean last_event = false;
                if (m_events_can_repeat) {
                    String repeat_days = rs.getString("repeat_days");
                    if ("first event".equals(repeat_days))
                        occurs_on = reminder_date.isEqual(rs.getDate("date").toLocalDate());
                    else if ("last event".equals(repeat_days)) {
                        occurs_on = reminder_date.isEqual(rs.getDate("end_date").toLocalDate());
                        last_event = true;
                    } else
                        occurs_on = event.occursOn(reminder_date);
                } else
                    occurs_on = event.occursOn(reminder_date);
                if (occurs_on) {
                    Date last_sent = rs.getDate("last_sent");
                    if (last_sent == null || !last_sent.toLocalDate().equals(now.toLocalDate())) {
                        if (m_events_have_start_time && (unit.equals("hour") || unit.equals("minute"))) {
                            LocalTime time = before ? rs.getTime("start_time").toLocalTime() : rs.getTime("end_time").toLocalTime();
                            if (unit.equals("hour"))
                                if (before)
                                    time = time.minusHours(num);
                                else
                                    time = time.plusHours(num);
                            else if (before)
                                time = time.minusMinutes(num);
                            else
                                time = time.plusMinutes(num);
                            if (now.toLocalTime().isBefore(time))
                                continue;
                        }
                        String recipient = rs.getString("email");
                        sendReminder(mail, recipient, event, rs.getString("note"), reminder_date, num_days, last_event, site, db);
                        if (!event.isRepeating()) {
                            int id = rs.getInt(3);
                            if (!rs.wasNull()) {
                                delete_ids.add(id);
                                continue;
                            }
                        }
                        db.update(reminders_table, new NameValuePairs().setDate("last_sent", now.toLocalDate()), rs.getInt("reminder_id"));
                    }
                }
            }
        }
        rs.getStatement().close();
    } catch (Exception e) {
        System.out.println("EventProvider.sendReminders");
        e.printStackTrace(System.out);
    }
    for (Integer id : delete_ids) db.deleteById(reminders_table, id);
}
Also used : FilePathStringBuilder(web.FilePathStringBuilder) LocalTime(java.time.LocalTime) NameValuePairs(db.NameValuePairs) Calendar(java.util.Calendar) ICalendar(biweekly.ICalendar) ArrayList(java.util.ArrayList) LocalDate(java.time.LocalDate) LocalDate(java.time.LocalDate) Date(java.sql.Date) SQLException(java.sql.SQLException) IOException(java.io.IOException) Mail(email.Mail) Select(db.Select) ResultSet(java.sql.ResultSet) VEvent(biweekly.component.VEvent) AdminTask(web.AdminTask)

Aggregations

AdminTask (web.AdminTask)16 IOException (java.io.IOException)9 File (java.io.File)7 SQLException (java.sql.SQLException)7 Select (db.Select)6 ResultSet (java.sql.ResultSet)5 Session (javax.mail.Session)4 FilePathStringBuilder (web.FilePathStringBuilder)4 LocalDate (java.time.LocalDate)3 Properties (java.util.Properties)3 MessagingException (javax.mail.MessagingException)3 AddressException (javax.mail.internet.AddressException)3 MimeMessage (javax.mail.internet.MimeMessage)3 ICalendar (biweekly.ICalendar)2 VEvent (biweekly.component.VEvent)2 MailConnectException (com.sun.mail.util.MailConnectException)2 DBConnection (db.DBConnection)2 NameValuePairs (db.NameValuePairs)2 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2