package obrien.Util;

import obrien.App;
import obrien.dao.Dao;
import obrien.processing.Trends;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Clear out old entries in the DB.
 * <p>I don't really care about storing everything we persist, and we do have a hard limit with the cheaper DB
 * available on Heroku.</p>
 */
public class GarbageCollector {
    private static final Logger log = LoggerFactory.getLogger(GarbageCollector.class);
    private final Timer timer;
    private final TimerTask timerTask;

    private final SessionFactory sessionFactory;

    /**
     * While this instance exists, perform the task for the given frequency.
     * @param frequency how long in milliseconds between executions of the task.
     * @param sessionFactory the sessionFactory for hibernate.
     */
    public GarbageCollector(long frequency, SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;

        timer = new Timer();
        timerTask = new TimerTask () {
            @Override
            public void run () {
                log.info(" * * * ");
                clearOldEntries();
            }
        };
        timer.schedule(timerTask, 0, frequency);
    }

    /**
     * Delete entries older than 30 minutes.
     */
    private void clearOldEntries() {
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        Calendar calender = Calendar.getInstance();
        calender.add(Calendar.MINUTE, -30);

        Query query = session.createQuery("DELETE FROM TradeMessage where timePlaced < :date");
        query.setCalendarDate("date", calender);
        int deleted = query.executeUpdate();

        tx.commit();
        session.close();
        log.info("Garbage collection occurred, deleted: {} entries", deleted);
    }
}

@@ -19,83 +19,8 @@
 l;%0A%0A
-import obrien.App;%0Aimport obrien.dao.Dao;%0Aimport obrien.processing.Trends;%0A
 impo
@@ -1193,45 +1193,8 @@
 ) %7B%0A
-                log.info(%22 * * * %22);%0A
     
@@ -1530,24 +1530,136 @@
 alendar 
-calender
+future = Calendar.getInstance();%0A        future.add(Calendar.MINUTE, +10);   // aloow some leeway%0A%0A        Calendar past
  = Calen
@@ -1685,24 +1685,20 @@
         
-calender
+past
 .add(Cal
@@ -1806,19 +1806,44 @@
 aced %3C :
-dat
+past  OR timePlaced %3E :futur
 e%22);%0A   
@@ -1874,23 +1874,68 @@
 te(%22
-date%22, calender
+past%22, past);%0A        query.setCalendarDate(%22future%22, future
 );%0A 
@@ -2043,16 +2043,23 @@
 g.info(%22
+ * * * 
 Garbage 
@@ -2077,17 +2077,22 @@
 occurred
-,
+ * * *
  deleted

Delete messages from the future too