View Javadoc

1   package gov.bnl.gums.db;
2   
3   import java.text.DateFormat;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.Date;
7   import java.util.Iterator;
8   
9   import gov.bnl.gums.persistence.HibernatePersistenceFactory;
10  
11  import org.hibernate.Query;
12  import org.hibernate.Session;
13  import org.hibernate.Transaction;
14  
15  import org.apache.log4j.Logger;
16  
17  public class HibernateConfigurationDB implements ConfigurationDB {
18  	private Logger log = Logger.getLogger(LDAPUserGroupDB.class);
19  	private HibernatePersistenceFactory persistenceFactory;
20  
21  	public HibernateConfigurationDB(HibernatePersistenceFactory persistenceFactory) {
22  		this.persistenceFactory = persistenceFactory;
23  		log.trace("LDAPConfigurationDB object create: factory " + persistenceFactory);
24  	}
25  
26  	public boolean deleteBackupConfiguration(String name) {
27  		Session session = null;
28  		Transaction tx = null;
29  		try {
30  			session = persistenceFactory.retrieveSessionFactory().openSession();
31  			tx = session.beginTransaction();		
32  			Query q = session.createQuery("FROM HibernateConfig c where c.name = ?");
33  			q.setString(0, name);
34  			if (q.list().size() == 1) {
35  				HibernateConfig config = (HibernateConfig)q.list().get(0);
36  				session.delete(config);
37  				tx.commit();
38  				return true;
39  			}
40  			else {
41  				log.error("None or more than one configuration is stored for name " + name);
42  				throw new RuntimeException("None or more than one configuration is stored name " + name);
43  			}        	
44  			// Handles when transaction goes wrong...
45  		} catch (Exception e) {
46  			log.error("Couldn't retrieve backup configuration", e);
47  			if (tx != null) {
48  				try {
49  					tx.rollback();
50  				} catch (Exception e1) {
51  					log.error("Hibernate error: rollback failed", e1);
52  					throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
53  				}
54  			}
55  			throw new RuntimeException("Database error: " + e.getMessage(), e);
56  		} finally {
57  			if (session != null) {
58  				try {
59  					session.close();
60  				} catch (Exception e1) {
61  					log.error("Hibernate error: couldn't close session", e1);
62  					throw new RuntimeException("Database error: " + e1.getMessage(), e1);
63  				}
64  			}
65  		}		
66  	}
67  
68  	public Collection getBackupNames(DateFormat format) {
69  		Session session = null;
70  		Transaction tx = null;
71  		ArrayList list = new ArrayList();
72  		try {
73  			// Checks whether the value is present in the database
74  			session = persistenceFactory.retrieveSessionFactory().openSession();
75  			tx = session.beginTransaction();			
76  			Query q;
77  			q = session.createQuery("FROM HibernateConfig c WHERE c.current = FALSE AND c.name != null");
78  			Iterator it = q.list().iterator();
79  			if (q.list().size() > 0) {
80  				while (it.hasNext()) {
81  					HibernateConfig config = (HibernateConfig)it.next();
82  					if (config.getName() != null)
83  						list.add(config.getName());
84  				}
85  			}
86  			tx.commit();
87  			return list;
88  			// Handles when transaction goes wrong...
89  		} catch (Exception e) {
90  			log.error("Couldn't retrive backup configuration dates", e);
91  			if (tx != null) {
92  				try {
93  					tx.rollback();
94  				} catch (Exception e1) {
95  					log.error("Hibernate error: rollback failed", e1);
96  					throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
97  				}
98  			}
99  			throw new RuntimeException("Database error: " + e.getMessage(), e);
100 		} finally {
101 			if (session != null) {
102 				try {
103 					session.close();
104 				} catch (Exception e1) {
105 					log.error("Hibernate error: couldn't close session", e1);
106 					throw new RuntimeException("Database error: " + e1.getMessage(), e1);
107 				}
108 			}
109 		}
110 	}
111 
112 	public Date getLastModification() {
113 		Session session = null;
114 		Transaction tx = null;
115 		try {
116 			// Checks whether the value is present in the database
117 			session = persistenceFactory.retrieveSessionFactory().openSession();
118 			tx = session.beginTransaction();			
119 			Query q;
120 			q = session.createQuery("select c.timestamp from HibernateConfig c where c.current = TRUE");
121 			if (q.list().size() == 0)
122 				return new Date(0);
123 			else {
124 				Date date = (Date)((Date)q.list().get(0)).clone();
125 				return date;
126 			}
127 			// Handles when transaction goes wrong...
128 		} catch (Exception e) {
129 			log.error("Couldn't retrieve current configuration", e);
130 			if (tx != null) {
131 				try {
132 					tx.rollback();
133 				} catch (Exception e1) {
134 					log.error("Hibernate error: rollback failed", e1);
135 					throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
136 				}
137 			}
138 			throw new RuntimeException("Database error: " + e.getMessage(), e);
139 		} finally {
140 			if (tx != null)
141 				tx.commit();
142 			if (session != null)
143 				session.close();
144 		}
145 	}
146 
147 	public boolean isActive() {
148 		return true;
149 	}
150 
151 	public String restoreConfiguration(String name) {
152 		Session session = null;
153 		Transaction tx = null;
154 		try {
155 			// Checks whether the value is present in the database
156 			session = persistenceFactory.retrieveSessionFactory().openSession();
157 			tx = session.beginTransaction();			
158 			Query q;
159 			q = session.createQuery("FROM HibernateConfig c WHERE c.name = ?");
160 			q.setString(0, name);
161 			if (q.list().size() == 1) {
162 				String xml = ((HibernateConfig)q.list().get(0)).getXml();
163 				tx.commit();
164 				return xml;
165 			}
166 			else {
167 				throw new RuntimeException("None or more than one configuration is stored name " + name);
168 			}
169 			// Handles when transaction goes wrong...
170 		} catch (Exception e) {
171 			log.error("Couldn't restore configuration", e);
172 			if (tx != null) {
173 				try {
174 					tx.rollback();
175 				} catch (Exception e1) {
176 					log.error("Hibernate error: rollback failed", e1);
177 					throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
178 				}
179 			}
180 			throw new RuntimeException("Database error: " + e.getMessage(), e);
181 		} finally {
182 			if (session != null) {
183 				try {
184 					session.close();
185 				} catch (Exception e1) {
186 					log.error("Hibernate error: couldn't close session", e1);
187 					throw new RuntimeException("Database error: " + e1.getMessage(), e1);
188 				}
189 			}
190 		}		
191 	}
192 
193 	public String retrieveCurrentConfiguration() {
194 		Session session = null;
195 		Transaction tx = null;
196 		try {
197 			// Checks whether the value is present in the database
198 			session = persistenceFactory.retrieveSessionFactory().openSession();
199 			tx = session.beginTransaction();			
200 			Query q;
201 			q = session.createQuery("FROM HibernateConfig c WHERE c.current = TRUE");
202 			if (q.list().size() != 1) {
203 				log.error("None or more than one configuration is set to current in the database");
204 				throw new RuntimeException("None or more than one configuration is set to current in the database");
205 			}
206 			tx.commit();
207 			return new String(((HibernateConfig)q.list().get(0)).getXml());
208 			// Handles when transaction goes wrong...
209 		} catch (Exception e) {
210 			log.error("Couldn't retrive current configuration", e);
211 			if (tx != null) {
212 				try {
213 					tx.rollback();
214 				} catch (Exception e1) {
215 					log.error("Hibernate error: rollback failed", e1);
216 					throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
217 				}
218 			}
219 			throw new RuntimeException("Database error: " + e.getMessage(), e);
220 		} finally {
221 			if (session != null) {
222 				try {
223 					session.close();
224 				} catch (Exception e1) {
225 					log.error("Hibernate error: couldn't close session", e1);
226 					throw new RuntimeException("Database error: " + e1.getMessage(), e1);
227 				}
228 			}
229 		}		
230 	}
231 
232 	public void setConfiguration(String text, boolean backupCopy, String name, Date date) {
233 		Session session = null;
234 		Transaction tx = null;
235 		
236 		try {
237 			// Checks whether the value is present in the database
238 			session = persistenceFactory.retrieveSessionFactory().openSession();
239 			tx = session.beginTransaction();
240 
241 			if (backupCopy) {
242 				// delete any configurations with the same name
243 				Query q = session.createQuery("FROM HibernateConfig c WHERE c.name = ?");
244 				q.setString(0, name);
245 				Iterator it = q.list().iterator();
246 				while (it.hasNext()) {
247 					HibernateConfig hibernateConfig = (HibernateConfig)it.next();
248 					if (hibernateConfig.getTimestamp().compareTo(date)==0)
249 						session.delete(hibernateConfig);
250 				}
251 			}
252 			else
253 			{
254 				// delete current configuration
255 				Query q = session.createQuery("DELETE FROM HibernateConfig WHERE current = TRUE");
256 				q.executeUpdate();
257 			}
258 
259 			// add new configuration
260 			HibernateConfig config = new HibernateConfig();
261 			config.setXml(text);
262 			config.setTimestamp(date);
263 			config.setCurrent(new Boolean(!backupCopy));
264 			config.setName(name);
265 			session.save(config);
266 
267 			tx.commit();
268 			// Handles when transaction goes wrong...
269 		} catch (Exception e) {
270 			log.error("Couldn't set configuration", e);
271 			if (tx != null) {
272 				try {
273 					tx.rollback();
274 				} catch (Exception e1) {
275 					log.error("Hibernate error: rollback failed", e1);
276 					throw new RuntimeException("Database errors: " + e.getMessage() + " - " + e1.getMessage(), e);
277 				}
278 			}
279 			throw new RuntimeException("Database error: " + e.getMessage(), e);
280 		} finally {
281 			if (session != null) {
282 				try {
283 					session.close();
284 				} catch (Exception e1) {
285 					log.error("Hibernate error: couldn't close session", e1);
286 					throw new RuntimeException("Database error: " + e1.getMessage(), e1);
287 				}
288 			}
289 		}			
290 	}    
291 
292 }