001    
002    package de.jw.cloud42.core.eventing.storage;
003    
004    
005    import java.util.Iterator;
006    import java.util.List;
007    
008    
009    
010    import org.hibernate.Query;
011    import org.hibernate.Session;
012    
013    import de.jw.cloud42.core.eventing.subscription.Subscription;
014    import de.jw.cloud42.core.hibernate.HibernateUtil;
015    
016    
017    /**
018     * Default implementation of a subscriber store.
019     * Uses a database with Hibernate to save Subscriptions.
020     * 
021     * 
022     * @author fbitzer
023     *
024     */
025    public class DefaultSubscriberStore implements SubscriberStore {
026    
027            
028            
029        /**
030            * Get a subscriber.
031            * @return The Subscription or null if no subscription with given id exists.
032            */
033        public Subscription retrieve(String id) {
034           
035            
036            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
037    
038            session.beginTransaction();
039    
040            Subscription s;
041                    try {
042                            s = (Subscription) session.load(Subscription.class, id);
043                              
044                    } catch (Exception ex) {
045                            s = null;
046                    }
047              
048                session.getTransaction().commit();
049                
050                return s;
051                
052       
053        }
054       
055    
056        /**
057         * Save a subscriber.
058         * 
059         * @param s the Subscription to store.
060         * @param topic the topic for the subscription.
061         */
062        public void store(Subscription s) {
063            
064            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
065    
066            session.beginTransaction();
067    
068            session.save(s);
069            
070            session.getTransaction().commit();
071               
072            
073            
074        }
075    
076        /**
077         * Delete a subscriber.
078         */
079        public void delete(String subscriberID) {
080            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
081    
082            session.beginTransaction();
083            
084                    try {
085                       Object s = session.load(Subscription.class, subscriberID);
086                       session.delete(s);
087                              
088                    } catch (Exception ex) {
089                            
090                    }
091            session.getTransaction().commit();
092               
093        }
094    
095        /**
096         * Get an iterator over all present subscribers for a specific topic.
097         * 
098         * @param The topic.
099         */
100        public Iterator retrieveAllSubscribers(String topic) {
101    
102            
103            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
104    
105            session.beginTransaction();
106    
107            Query q = session.createQuery("SELECT s FROM Subscription s WHERE s.topic = :topic");
108                q.setParameter("topic", topic);
109    
110                List<Subscription> resultset = q.list();
111                
112            session.getTransaction().commit();
113            
114            return resultset.iterator();
115    
116    
117            
118        }
119    
120    }