1   package net.sf.flock.hibernate;
2   
3   import java.net.URL;
4   import java.util.Collection;
5   import java.util.List;
6   import java.util.Properties;
7   
8   import net.sf.flock.FeedFactoryI;
9   import net.sf.flock.FeedI;
10  import net.sf.flock.FilterI;
11  import net.sf.flock.FlockResourceException;
12  import net.sf.flock.SubscriptionI;
13  import net.sf.flock.SubscriptionInfoI;
14  import net.sf.flock.support.AbstractSubscriptionManager;
15  import net.sf.hibernate.Hibernate;
16  import net.sf.hibernate.HibernateException;
17  import net.sf.hibernate.MappingException;
18  import net.sf.hibernate.Session;
19  import net.sf.hibernate.SessionFactory;
20  import net.sf.hibernate.Transaction;
21  import net.sf.hibernate.cfg.Configuration;
22  import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
23  
24  public class HibernateSubscriptionManager extends AbstractSubscriptionManager implements FeedFactoryI {
25  	
26  	private final SessionFactory sessionFactory;
27  
28  	public HibernateSubscriptionManager(Properties properties) throws FlockResourceException {
29  
30  		try {
31  			
32  			properties.put("hibernate.query.imports", "net.sf.flock.hibernate");
33  			
34  			Configuration conf = new Configuration();
35  			conf.addClass( Subscription.class );
36  	
37  			conf.setProperties(properties);
38  			
39  			new SchemaExport(conf, properties).create(true, true);
40  			//new SchemaUpdater(ds, properties).execute(false);
41  
42  			this.sessionFactory = conf.buildSessionFactory();
43  		} catch (MappingException e) {
44  			throw new FlockResourceException(e);
45  		} catch (HibernateException e) {
46  			throw new FlockResourceException(e);
47  		}		
48  	}
49  
50  	protected void storeSubscription(Subscription subscription) throws Exception {
51  		Session s = this.sessionFactory.openSession();
52  		
53  		Transaction tx = null;
54  		try {
55  			tx = s.beginTransaction();
56  			//s.save(subscription.getFeed());
57  			s.saveOrUpdate(subscription);
58  			s.flush();
59  			tx.commit();
60  		} catch (Exception ex) {
61  			if (tx!=null) tx.rollback();
62  			throw ex;
63  		} finally {
64  			s.close();
65  		}
66  	}
67  
68  	protected Subscription loadSubscription(URL url) throws Exception {
69  		Session s = this.sessionFactory.openSession();
70  		
71  		try {
72  			List l = s.find("from sub in class Subscription where location=?", url, Hibernate.custom(URLType.class));
73  			return (Subscription)l.get(0);
74  		} catch (Exception ex) {
75  			throw ex;
76  		} finally {
77  			s.close();
78  		}
79  	}
80  
81  	protected SubscriptionI createSubscription(SubscriptionInfoI subscriptionInfo) throws FlockResourceException {
82  		Subscription sub = new Subscription();
83  		sub.setLocation(subscriptionInfo.getLocation());
84  		sub.setMetaData(subscriptionInfo.getMetaData());
85  		sub.setFeed(new Feed(subscriptionInfo.getFeedInfo()));		
86  		try {
87              storeSubscription(sub);
88          } catch (Exception e) {
89  			throw new FlockResourceException(e.getMessage(),e);
90          }
91  		return sub;
92  	}
93  
94  	protected FeedFactoryI getFeedFactory() {
95  		return this;
96  	}
97  
98  	public FeedI createFeed(SubscriptionInfoI subscriptionInfoI) {
99  		return new Feed(subscriptionInfoI.getFeedInfo());
100 	}
101 
102 	public void unsubscribe(URL url) throws FlockResourceException {
103 	}
104 
105 	public SubscriptionI getSubscription(URL url) throws FlockResourceException {
106 		return null;
107 	}
108 
109 	public Collection getSubscriptionInfos() throws FlockResourceException {
110 		return null;
111 	}
112 
113 	public void refresh(URL url) throws FlockResourceException {
114 	}
115 
116 	public Collection findItems(FilterI[] filters) throws FlockResourceException {
117 		return null;
118 	}
119 
120 
121 }
This page was automatically generated by Maven