1 18 21 package org.apache.roller.business.hibernate; 22 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.hibernate.HibernateException; 29 import org.hibernate.Session; 30 import org.hibernate.SessionFactory; 31 import org.hibernate.Transaction; 32 import org.hibernate.cfg.Configuration; 33 import org.apache.roller.RollerException; 34 import org.apache.roller.pojos.Assoc; 35 import org.apache.roller.pojos.HierarchicalPersistentObject; 36 import org.apache.roller.pojos.PersistentObject; 37 38 39 47 public class HibernatePersistenceStrategy { 48 49 static final long serialVersionUID = 2561090040518169098L; 50 51 private static SessionFactory sessionFactory = null; 52 53 private static Log log = LogFactory.getLog(HibernatePersistenceStrategy.class); 54 55 56 59 public HibernatePersistenceStrategy() throws Exception { 60 61 log.debug("Initializing Hibernate SessionFactory"); 62 63 Configuration config = new Configuration(); 64 config.configure("/hibernate.cfg.xml"); 65 this.sessionFactory = config.buildSessionFactory(); 66 67 } 68 69 70 76 protected Session getSession() { 77 78 log.debug("Opening Hibernate Session"); 79 80 Session session = sessionFactory.getCurrentSession(); 83 session.beginTransaction(); 84 85 return session; 86 } 87 88 89 protected void flush() throws RollerException { 90 91 Session session = getSession(); 92 try { 93 session.getTransaction().commit(); 94 } catch(Throwable t) { 95 release(); 97 } 98 } 99 100 101 114 protected void release() { 115 116 try { 117 Session session = sessionFactory.getCurrentSession(); 118 119 if(session != null && session.isOpen()) { 120 121 log.debug("Closing Hibernate Session"); 122 123 try { 124 Transaction tx = session.getTransaction(); 125 126 if(tx != null && tx.isActive()) { 127 log.debug("Forcing rollback on active transaction"); 128 tx.rollback(); 129 } 130 } catch(Throwable t) { 131 log.error("ERROR doing Hibernate rollback", t); 132 } finally { 133 if(session.isOpen()) { 134 session.close(); 135 } 136 } 137 } 138 } catch(Throwable t) { 139 log.error("ERROR closing Hibernate Session", t); 140 } 141 } 142 143 144 147 protected PersistentObject load(String id, Class clazz) throws RollerException { 148 149 if(id == null || clazz == null) { 150 throw new RollerException("Cannot load objects when value is null"); 151 } 152 153 return (PersistentObject) getSession().get(clazz, id); 154 } 155 156 157 160 protected void store(PersistentObject obj) throws HibernateException { 161 162 if(obj == null) { 163 throw new HibernateException("Cannot save null object"); 164 } 165 166 Session session = getSession(); 167 168 170 if ( obj.getId() == null || obj.getId().trim().equals("") ) { 172 session.save(obj); 175 } 176 177 190 if ( !session.contains(obj) ) { 191 192 log.debug("storing detached object: "+obj.toString()); 193 194 PersistentObject vo = 197 (PersistentObject)session.load(obj.getClass(),obj.getId()); 198 vo.setData(obj); 199 obj = vo; 200 } 201 202 } 203 204 205 210 protected void remove(String id, Class clazz) throws HibernateException { 211 212 if(id == null || clazz == null) { 213 throw new HibernateException("Cannot remove object when values are null"); 214 } 215 216 Session session = getSession(); 217 218 PersistentObject obj = (PersistentObject) session.load(clazz,id); 219 session.delete(obj); 220 } 221 222 223 226 protected void remove(PersistentObject obj) throws HibernateException { 227 228 if(obj == null) { 229 throw new HibernateException("Cannot remove null object"); 230 } 231 232 if (obj.getId() != null) { 236 237 getSession().delete(obj); 238 } 239 } 240 241 242 247 protected void store(HierarchicalPersistentObject obj) 248 throws HibernateException, RollerException { 249 250 if(obj == null) { 251 throw new HibernateException("Cannot save null object"); 252 } 253 254 log.debug("Storing hierarchical object "+obj); 255 256 Session session = getSession(); 257 258 HierarchicalPersistentObject mNewParent = obj.getNewParent(); 259 boolean fresh = (obj.getId() == null || "".equals(obj.getId())); 260 261 if (fresh) { 262 session.save(obj); 265 } 266 267 if(!session.contains(obj)) { 268 269 HierarchicalPersistentObject vo = 272 (HierarchicalPersistentObject)session.load(obj.getClass(),obj.getId()); 273 vo.setData(obj); 274 obj = vo; 275 } 276 277 if (fresh) { 278 Assoc parentAssoc = obj.createAssoc( 280 obj, mNewParent, Assoc.PARENT); 281 this.store(parentAssoc); 282 } else if (null != mNewParent) { 283 Assoc parentAssoc = obj.getParentAssoc(); 285 if(parentAssoc == null) 286 log.error("parent assoc is null"); 287 parentAssoc.setAncestor(mNewParent); 288 this.store(parentAssoc); 289 } 290 291 Iterator ancestors = obj.getAncestorAssocs().iterator(); 293 while (ancestors.hasNext()) { 294 Assoc assoc = (Assoc)ancestors.next(); 295 if (assoc.getRelation().equals(Assoc.GRANDPARENT)) { 296 this.remove(assoc); 297 } 298 } 299 300 int count = 0; 302 Assoc currentAssoc = obj.getParentAssoc(); 303 while (null != currentAssoc.getAncestor()) { 304 if (count > 0) { 305 Assoc assoc = obj.createAssoc(obj, 306 currentAssoc.getAncestor(), 307 Assoc.GRANDPARENT); 308 this.store(assoc); 309 } 310 currentAssoc = currentAssoc.getAncestor().getParentAssoc(); 311 count++; 312 } 313 314 Iterator children = obj.getChildAssocs().iterator(); 315 while (children.hasNext()) { 316 Assoc assoc = (Assoc) children.next(); 317 318 assoc.getObject().setParent(obj); 320 321 this.store(assoc.getObject()); 323 } 324 325 mNewParent = null; 327 } 328 329 330 333 protected void store(Assoc assoc) throws HibernateException { 334 335 if(assoc == null) { 336 throw new HibernateException("Cannot save null object"); 337 } 338 339 getSession().saveOrUpdate(assoc); 340 } 341 342 343 348 protected void remove(HierarchicalPersistentObject obj) throws RollerException { 349 350 if(obj == null) { 351 throw new RollerException("Cannot remove null object"); 352 } 353 354 log.debug("Removing hierarchical object "+obj.getId()); 355 356 List toRemove = new LinkedList (); 358 List assocs = obj.getAllDescendentAssocs(); 359 for (int i=assocs.size()-1; i>=0; i--) { 360 Assoc assoc = (Assoc)assocs.get(i); 361 HierarchicalPersistentObject hpo = assoc.getObject(); 362 363 Iterator ancestors = hpo.getAncestorAssocs().iterator(); 365 while (ancestors.hasNext()) { 366 Assoc dassoc = (Assoc)ancestors.next(); 367 this.remove(dassoc); 368 } 369 370 toRemove.add(hpo); 373 } 374 Iterator removeIterator = toRemove.iterator(); 375 while (removeIterator.hasNext()) { 376 PersistentObject po = (PersistentObject) removeIterator.next(); 377 getSession().delete(po); 378 } 379 380 Iterator ancestors = obj.getAncestorAssocs().iterator(); 382 while (ancestors.hasNext()) { 383 Assoc assoc = (Assoc)ancestors.next(); 384 this.remove(assoc); 385 } 386 387 getSession().delete(obj); 388 } 389 390 391 394 protected void remove(Assoc assoc) throws HibernateException { 395 396 if(assoc == null) { 397 throw new HibernateException("Cannot save null object"); 398 } 399 400 getSession().delete(assoc); 401 } 402 403 } 404 | Popular Tags |