001package org.kuali.mobility.events.dao;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.kuali.mobility.events.entity.Category;
007import org.kuali.mobility.events.entity.Event;
008
009
010
011public class EventInitBean {
012        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EventInitBean.class);
013           
014        private EventsDaoUMImpl dao;
015        
016        public EventsDaoUMImpl getDao() {
017                return dao;
018        }
019
020        public void setDao(EventsDaoUMImpl dao) {
021                this.dao = dao;
022        }
023
024        private static Thread backgroundThread = null;
025        
026        public void init() {
027                backgroundThread = new Thread(new BackgroundThread());
028        backgroundThread.setDaemon(true);
029        backgroundThread.start();       
030        }
031
032    public void cleanup() {
033        LOG.info("Cleaning up events.");
034    }
035    private class BackgroundThread implements Runnable {
036        
037        public void run() {    
038                        LOG.info("Initializing events...");
039                        getDao().initData(null); 
040                        LOG.info("Finished initializing events.");
041                while (true) {
042                        try {
043                                LOG.info("Events sleeping...");
044                        try {
045                            Thread.sleep(1000 * 60 * 15);
046                        } catch (InterruptedException e) {
047                            LOG.error(e.getMessage(), e);
048                        }
049                        List<Event> events = new ArrayList<Event>();
050                                LOG.info("Refreshing Events...");
051                                for (Category c : getDao().getCategories()) {
052                                        events.addAll(getDao().loadEventsForCategory(null, c.getCategoryId()));
053                                }
054                                getDao().setEvents(events);
055                                LOG.info("Finished refreshing Events.");                        
056                        } catch (Exception e) {
057                    LOG.error(e.getMessage(), e);
058                        }
059                }
060        }
061        
062    }
063
064}