KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > workflow > hibernate > InfoglueHibernatePropertySetDAOImpl


1 package org.infoglue.cms.util.workflow.hibernate;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 import net.sf.hibernate.HibernateException;
9 import net.sf.hibernate.Session;
10 import net.sf.hibernate.SessionFactory;
11
12 import com.opensymphony.module.propertyset.PropertyException;
13 import com.opensymphony.module.propertyset.hibernate.HibernatePropertySetDAO;
14 import com.opensymphony.module.propertyset.hibernate.PropertySetItem;
15
16
17 /**
18  * Quickfix
19  */

20 public class InfoglueHibernatePropertySetDAOImpl implements HibernatePropertySetDAO {
21     /**
22      *
23      */

24     private SessionFactory sessionFactory;
25
26     /**
27      *
28      */

29     public InfoglueHibernatePropertySetDAOImpl(SessionFactory sessionFactory) {
30         this.sessionFactory = sessionFactory;
31     }
32
33     /**
34      *
35      */

36     public void setImpl(PropertySetItem item, boolean isUpdate) {
37         Session session = null;
38
39         try {
40             session = this.sessionFactory.openSession();
41
42             if (isUpdate) {
43                 session.update(item);
44             } else {
45                 session.save(item);
46             }
47
48             session.flush();
49         } catch (HibernateException he) {
50             throw new PropertyException("Could not save key '" + item.getKey() + "':" + he.getMessage());
51         } finally {
52             try {
53                 if (session != null) {
54                     if (!session.connection().getAutoCommit()) {
55                         session.connection().commit();
56                     }
57
58                     session.close();
59                 }
60             } catch (Exception JavaDoc e) {
61             }
62         }
63     }
64
65     public Collection JavaDoc getKeys(String JavaDoc entityName, Long JavaDoc entityId, String JavaDoc prefix, int type) {
66         Session session = null;
67         List JavaDoc list = null;
68
69         try {
70             session = this.sessionFactory.openSession();
71             list = InfoglueHibernatePropertySetDAOUtils.getKeysImpl(session, entityName, entityId, prefix, type);
72         } catch (HibernateException e) {
73             list = Collections.EMPTY_LIST;
74         } finally {
75             try {
76                 if (session != null) {
77                     session.flush();
78                     session.close();
79                 }
80             } catch (Exception JavaDoc e) {
81             }
82         }
83
84         return list;
85     }
86
87     public PropertySetItem create(String JavaDoc entityName, long entityId, String JavaDoc key) {
88         return new InfogluePropertySetItemImpl(entityName, entityId, key);
89     }
90
91     public PropertySetItem findByKey(String JavaDoc entityName, Long JavaDoc entityId, String JavaDoc key) {
92         Session session = null;
93         PropertySetItem item = null;
94
95         try {
96             session = this.sessionFactory.openSession();
97             item = InfoglueHibernatePropertySetDAOUtils.getItem(session, entityName, entityId, key);
98             session.flush();
99         } catch (HibernateException e) {
100             return null;
101         } finally {
102             try {
103                 if (session != null) {
104                     session.close();
105                 }
106             } catch (Exception JavaDoc e) {
107             }
108         }
109
110         return item;
111     }
112
113     public void remove(String JavaDoc entityName, Long JavaDoc entityId) {
114         Session session = null;
115
116         try {
117             session = this.sessionFactory.openSession();
118
119             //hani: todo this needs to be optimised rather badly, but I have no idea how
120
Collection JavaDoc keys = getKeys(entityName, entityId, null, 0);
121             Iterator JavaDoc iter = keys.iterator();
122
123             while (iter.hasNext()) {
124                 String JavaDoc key = (String JavaDoc) iter.next();
125                 session.delete(InfoglueHibernatePropertySetDAOUtils.getItem(session, entityName, entityId, key));
126             }
127
128             session.flush();
129         } catch (HibernateException e) {
130             throw new PropertyException("Could not remove all keys: " + e.getMessage());
131         } finally {
132             try {
133                 if (session != null) {
134                     if (!session.connection().getAutoCommit()) {
135                         session.connection().commit();
136                     }
137
138                     session.close();
139                 }
140             } catch (Exception JavaDoc e) {
141             }
142         }
143     }
144
145     public void remove(String JavaDoc entityName, Long JavaDoc entityId, String JavaDoc key) {
146         Session session = null;
147
148         try {
149             session = this.sessionFactory.openSession();
150             session.delete(InfoglueHibernatePropertySetDAOUtils.getItem(session, entityName, entityId, key));
151             session.flush();
152         } catch (HibernateException e) {
153             throw new PropertyException("Could not remove key '" + key + "': " + e.getMessage());
154         } finally {
155             try {
156                 if (session != null) {
157                     if (!session.connection().getAutoCommit()) {
158                         session.connection().commit();
159                     }
160
161                     session.close();
162                 }
163             } catch (Exception JavaDoc e) {
164             }
165         }
166     }
167 }
168
Popular Tags