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        
027        public void init() {
028                backgroundThread = new Thread(new BackgroundThread());
029        backgroundThread.setDaemon(true);
030        backgroundThread.start();       
031        }
032
033    public void cleanup() {
034        LOG.info("Cleaning up events.");
035    }
036    
037    public void updateEvents(){
038                LOG.info("Refreshing Events...");
039        List<Event> events = new ArrayList<Event>();
040                LOG.info("Refreshing Events...");
041                for (Category c : getDao().getCategories()) {
042                        events.addAll(getDao().loadEventsForCategory(null, c.getCategoryId()));
043                }
044                getDao().setEvents(events);
045    }
046    
047    
048    
049    private class BackgroundThread implements Runnable {
050        
051        public void run() {    
052                        LOG.info("Initializing events...");
053                        getDao().initData(null); 
054                        LOG.info("Finished initializing events.");
055                while (true) {
056                        try {
057                                LOG.info("Events sleeping...");
058                        try {
059                            Thread.sleep(1000 * 60 * 15);
060                        } catch (InterruptedException e) {
061                            LOG.error(e.getMessage(), e);
062                        }
063                        List<Event> events = new ArrayList<Event>();
064                                LOG.info("Refreshing Events...");
065                                for (Category c : getDao().getCategories()) {
066                                        events.addAll(getDao().loadEventsForCategory(null, c.getCategoryId()));
067                                }
068                                getDao().setEvents(events);
069                                LOG.info("Finished refreshing Events.");                        
070                        } catch (Exception e) {
071                    LOG.error(e.getMessage(), e);
072                        }
073                }
074        }
075        
076    }
077
078}