You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2006/04/09 00:36:28 UTC

svn commit: r392618 [1/5] - in /incubator/roller/branches/roller-newbackend/src/org/roller/business: ./ hibernate/ search/operations/

Author: agilliland
Date: Sat Apr  8 15:36:25 2006
New Revision: 392618

URL: http://svn.apache.org/viewcvs?rev=392618&view=rev
Log:
first batch of backend refactorings.

- removed unused PubTimeData and HitCountData classes
- removed unneeded code from abstract RollerImpl
- removed usage of PersistenceStrategy IF
- moved methods from all abstract managerImpls into their hibernate impls.
- added hibernate transaction management to all hibernate manager impls.
- removed unused Messages and messages.properties
- moved old HibernateStrategy to HibernatePersistenceStrategy


Added:
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java
Removed:
    incubator/roller/branches/roller-newbackend/src/org/roller/business/AutoPingManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/BookmarkManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/ConfigManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/HitCountData.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/PingQueueManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/PingTargetManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/PlanetManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/PropertiesManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/PubTimeData.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/RefererManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/UserManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/WeblogManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceSession.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateStrategy.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/Messages.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/messages.properties
Modified:
    incubator/roller/branches/roller-newbackend/src/org/roller/business/PersistenceStrategy.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/RollerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateAutoPingManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateBookmarkManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateConfigManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePingQueueManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePingTargetManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePlanetManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePropertiesManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateRefererManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateRollerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateUserManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateWeblogManagerImpl.java
    incubator/roller/branches/roller-newbackend/src/org/roller/business/search/operations/RebuildWebsiteIndexOperation.java

Modified: incubator/roller/branches/roller-newbackend/src/org/roller/business/PersistenceStrategy.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/PersistenceStrategy.java?rev=392618&r1=392617&r2=392618&view=diff
==============================================================================
--- incubator/roller/branches/roller-newbackend/src/org/roller/business/PersistenceStrategy.java (original)
+++ incubator/roller/branches/roller-newbackend/src/org/roller/business/PersistenceStrategy.java Sat Apr  8 15:36:25 2006
@@ -3,74 +3,52 @@
  */
 package org.roller.business;
 
-import java.io.Serializable;
-
 import org.roller.RollerException;
 import org.roller.pojos.PersistentObject;
-import org.roller.pojos.UserData;
+
 
 /**
- * Persistence strategy masks underlying persistence mechanism. 
- * State is held in thread local storage (TLS). 
+ * Persistence strategy masks underlying persistence mechanism.
+ * State is held in thread local storage (TLS).
  * When you call begin(), a PersistenceSession object is associated with your thread.
  * When you call release(), TLS is cleared.
- * 
+ *
  * @author Lance Lavandowska
  * @author Dave Johnson
  */
-public interface PersistenceStrategy extends Serializable
-{
-    /** 
-     * Save a persistent object to storage. This method is only needed when
-     * a new object is to be added to storage. 
-     */
-    public PersistentObject store(PersistentObject data)
-        throws RollerException;
-       
-    /** 
-     * Load an persistent object from storage. Object returned is a 
+public interface PersistenceStrategy {
+    
+    /**
+     * Load an persistent object from storage. Object returned is a
      * persistent instance, meaning that changes to it will be automatically
      * saved to storage the next time that commit() is called.
      */
-    public PersistentObject load(
-        String id, Class cls) throws RollerException;
-        
-    /** 
-     * Remove an object from storage.
-     */
-    public void remove(PersistentObject po)
-        throws RollerException;
-        
-    /** 
-     * Remove an object from storage.
-     */
-    public void remove(String id, Class cls)
-        throws RollerException;
-        
-    /** 
-     * Release existing resources and start new session and transaction.
-     */
-    public void begin(UserData user) throws RollerException;
+    public PersistentObject load(String id, Class cls) throws RollerException;
+    
     
     /**
-     * Associate user with thread.
+     * Save a persistent object to storage. This method is only needed when
+     * a new object is to be added to storage.
      */
-    public void setUser(UserData user) throws RollerException;
-    public UserData getUser() throws RollerException;
-
-    /** 
-     * Commit all changes made to persistent objects since last call to begin.
+    public void store(PersistentObject data) throws RollerException;
+    
+    
+    /**
+     * Remove an object from storage.
      */
-	public void commit() throws RollerException;
-
+    public void remove(PersistentObject po) throws RollerException;
+    
+    
     /**
-     * Rollback all changes since last call to begin.
+     * Remove an object from storage.
      */
-	public void rollback() throws RollerException;
+    public void remove(String id, Class cls) throws RollerException;
     
-    /** 
+    
+    /**
      * Release associated resources (database connection, session, etc.) and
      * clear thread local storage.
      */
     public void release() throws RollerException;
+    
 }

Modified: incubator/roller/branches/roller-newbackend/src/org/roller/business/RollerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/RollerImpl.java?rev=392618&r1=392617&r2=392618&view=diff
==============================================================================
--- incubator/roller/branches/roller-newbackend/src/org/roller/business/RollerImpl.java (original)
+++ incubator/roller/branches/roller-newbackend/src/org/roller/business/RollerImpl.java Sat Apr  8 15:36:25 2006
@@ -6,33 +6,19 @@
 package org.roller.business;
 
 import java.sql.Connection;
-
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-
 import org.roller.RollerException;
 import org.roller.business.referrers.ReferrerQueueManager;
 import org.roller.business.referrers.ReferrerQueueManagerImpl;
-
 import org.roller.business.utils.UpgradeDatabase;
-
-import org.roller.model.AutoPingManager;
-import org.roller.model.BookmarkManager;
-import org.roller.model.ConfigManager;
 import org.roller.model.FileManager;
 import org.roller.model.IndexManager;
 import org.roller.model.PagePluginManager;
-import org.roller.model.PingQueueManager;
-import org.roller.model.PingTargetManager;
-import org.roller.model.PlanetManager;
-import org.roller.model.PropertiesManager;
-import org.roller.model.RefererManager;
 import org.roller.model.Roller;
 import org.roller.model.ThemeManager;
 import org.roller.model.ThreadManager;
-import org.roller.pojos.UserData;
-import org.roller.model.UserManager;
-import org.roller.model.WeblogManager;
+
 
 /**
  * The abstract version of the Roller implementation.
@@ -43,26 +29,15 @@
  */
 public abstract class RollerImpl implements Roller {
     
-    private static Log mLogger =
-            LogFactory.getFactory().getInstance(RollerImpl.class);
+    private static Log mLogger = LogFactory.getLog(RollerImpl.class);
+    
+    private FileManager fileManager = null;
+    private IndexManager indexManager = null;
+    private ThreadManager threadManager = null;
+    private ThemeManager themeManager = null;
+    private PagePluginManager pluginManager = null;
     
-    protected FileManager   mFileManager = null;
-    protected IndexManager  mIndexManager = null;
-    protected ThreadManager mThreadManager = null;
-    protected ThemeManager  mThemeManager = null;
-    protected PagePluginManager mPluginManager = null;
-    protected BookmarkManager mBookmarkManager;   
-    protected PropertiesManager mPropsManager = null;
-    protected PlanetManager   mPlanetManager = null;
-    protected RefererManager  mRefererManager;
-    protected UserManager     mUserManager;
-    protected WeblogManager   mWeblogManager;
-    protected PingQueueManager mPingQueueManager;
-    protected AutoPingManager mAutoPingManager;
-    protected PingTargetManager mPingTargetManager;
-    protected PersistenceStrategy mStrategy = null;
     
-    /** Creates a new instance of RollerImpl */
     public RollerImpl() {
         // nothing to do here yet
     }
@@ -72,265 +47,92 @@
      * @see org.roller.model.Roller#getFileManager()
      */
     public FileManager getFileManager() throws RollerException {
-        if (mFileManager == null) {
-            mFileManager = new FileManagerImpl();
+        if (fileManager == null) {
+            fileManager = new FileManagerImpl();
         }
-        return mFileManager;
+        return fileManager;
     }
     
+    
     /**
      * @see org.roller.model.Roller#getThreadManager()
      */
     public ThreadManager getThreadManager() throws RollerException {
-        if (mThreadManager == null) {
-            mThreadManager = new ThreadManagerImpl();
+        if (threadManager == null) {
+            threadManager = new ThreadManagerImpl();
         }
-        return mThreadManager;
+        return threadManager;
     }
     
+    
     /**
      * @see org.roller.model.Roller#getIndexManager()
      */
     public IndexManager getIndexManager() throws RollerException {
-        if (mIndexManager == null) {
-            mIndexManager = new IndexManagerImpl();
+        if (indexManager == null) {
+            indexManager = new IndexManagerImpl();
         }
-        return mIndexManager;
+        return indexManager;
     }
     
+    
     /**
      * @see org.roller.model.Roller#getThemeManager()
      */
     public ThemeManager getThemeManager() throws RollerException {
-        if (mThemeManager == null) {
-            mThemeManager = new ThemeManagerImpl();
+        if (themeManager == null) {
+            themeManager = new ThemeManagerImpl();
         }
-        return mThemeManager;
+        return themeManager;
     }
     
+    
+    /**
+     * @see org.roller.business.referrers.ReferrerQueueManager
+     */
     public ReferrerQueueManager getReferrerQueueManager() {
         return ReferrerQueueManagerImpl.getInstance();
     }
     
+    
     /**
      * @see org.roller.model.Roller#getPluginManager()
      */
     public PagePluginManager getPagePluginManager() throws RollerException {
-        if (mPluginManager == null) {
-            mPluginManager = new PagePluginManagerImpl();
+        if (pluginManager == null) {
+            pluginManager = new PagePluginManagerImpl();
         }
-        return mPluginManager;
+        return pluginManager;
     }
     
     
-    public void release() {
-        if (mFileManager != null) mFileManager.release();
-        if (mThreadManager != null) mThreadManager.release();
-        if (mPluginManager != null) mPluginManager.release();
-        if (mBookmarkManager != null) mBookmarkManager.release();
-        if (mRefererManager != null) mRefererManager.release();
-        if (mUserManager != null) mUserManager.release();
-        if (mWeblogManager != null) mWeblogManager.release();
-        if (mPingTargetManager != null) mPingTargetManager.release();
-        if (mPingQueueManager != null) mPingQueueManager.release();
-        if (mAutoPingManager != null) mAutoPingManager.release();
-        
-        try
-        {
-            if (mStrategy != null) mStrategy.release(); 
-        }
-        catch (Throwable e)
-        {
-            mLogger.error(
-            "Exception with mSupport.release() [" + e + "]", e);
-        }
-    }
-    
-    public void begin() throws RollerException {
-        mStrategy.begin(UserData.ANONYMOUS_USER);
+    /**
+     * @see org.roller.model.Roller#upgradeDatabase(java.sql.Connection)
+     */
+    public void upgradeDatabase(Connection con) throws RollerException {
+        UpgradeDatabase.upgradeDatabase(con);
     }
     
-    public void begin(UserData user) throws RollerException {
-        mStrategy.begin(user);
-    }
     
-    public UserData getUser() throws RollerException {
-        return mStrategy.getUser();
-    }
-
-    public void setUser(UserData user) throws RollerException {
-        mStrategy.setUser(user);
-    }
-
-    public void commit() throws RollerException {
-        mStrategy.commit();
-    }
-
-    public void rollback() {
+    public void release() {
         try {
-            mStrategy.rollback();
-        } catch (Throwable e) {
-            mLogger.error(e);
-        }
-    }
-
-    /** */
-    protected UserManager createUserManager() {
-        throw new NullPointerException();
-    }
-
-    /** 
-     * @see org.roller.model.Roller#getUserManager()
-     */
-    public UserManager getUserManager() throws RollerException {
-        if (mUserManager == null) {
-            mUserManager = createUserManager();
-        }
-        return mUserManager;
-    }
-
-    /** */
-    protected BookmarkManager createBookmarkManager() {
-        throw new NullPointerException();
-    }
-
-    /** 
-     * @see org.roller.model.Roller#getBookmarkManager()
-     */
-    public BookmarkManager getBookmarkManager() throws RollerException {
-        if ( mBookmarkManager == null )  {
-            mBookmarkManager = createBookmarkManager();
+            if (fileManager != null) fileManager.release();
+            if (threadManager != null) threadManager.release();
+            if (pluginManager != null) pluginManager.release();
+        } catch(Throwable e) {
+            mLogger.error("Error calling Roller.release()", e);
         }
-        return mBookmarkManager;
-    }
-
-    /** */
-    protected WeblogManager createWeblogManager() {
-        throw new NullPointerException();
-    }
-
-    /** 
-     * @see org.roller.model.Roller#getWeblogManager()
-     */
-    public WeblogManager getWeblogManager() throws RollerException {
-        if ( mWeblogManager == null )  {
-            mWeblogManager = createWeblogManager();
-        }
-        return mWeblogManager;
-    }
-
-    /** */
-    protected RefererManager createRefererManager() {
-        throw new NullPointerException();
-    }
-
-    /** 
-     * @see org.roller.model.Roller#getRefererManager()
-     */
-    public RefererManager getRefererManager() throws RollerException {
-        if (mRefererManager == null) {
-            mRefererManager = createRefererManager();
-        }
-        return mRefererManager;
-    }
-
-    /**
-     * @see org.roller.model.Roller#getConfigManager()
-     */
-    public ConfigManager getConfigManager() throws RollerException 
-    {
-        throw new RollerException("getConfigManager is Deprecated.");
     }
     
-    /** */
-    protected PropertiesManager createPropertiesManager() {
-        throw new NullPointerException();
-    }
-
-    /**
-     * @see org.roller.model.Roller#getPropertiesManager()
-     */
-    public PropertiesManager getPropertiesManager() throws RollerException {
-        if (mPropsManager == null) {
-            mPropsManager = createPropertiesManager();
-        }
-        return mPropsManager;
-    }
-
-    /** */
-    protected PingQueueManager createPingQueueManager() {
-        throw new NullPointerException();
-    }
-
-    /**
-     * @see org.roller.model.Roller#getPingTargetManager()
-     */
-    public PingQueueManager getPingQueueManager() throws RollerException {
-        if (mPingQueueManager == null) {
-            mPingQueueManager = createPingQueueManager();
-        }
-        return mPingQueueManager;
-    }
-
-    /** */
-    protected PlanetManager createPlanetManager() {
-        throw new NullPointerException();
-    }
-
-    /**
-     * @see org.roller.model.Roller#getPlanetManager()
-     */
-    public PlanetManager getPlanetManager() throws RollerException {
-        if ( mPlanetManager == null ) {
-            mPlanetManager = createPlanetManager();
-        }
-        return mPlanetManager;
-    }
-
-    /** */
-    protected  AutoPingManager createAutoPingManager() {
-        throw new NullPointerException();
-    }
-
-    /**
-     * @see org.roller.model.Roller#getPingTargetManager()
-     */
-    public AutoPingManager getAutopingManager() throws RollerException {
-        if (mAutoPingManager == null) {
-            mAutoPingManager = createAutoPingManager();
-        }
-        return mAutoPingManager;
-    }
-
-    /** */
-    protected PingTargetManager createPingTargetManager() {
-        throw new NullPointerException();
-    }
-
-    /**
-     * @see org.roller.model.Roller#getPingTargetManager()
-     */
-    public PingTargetManager getPingTargetManager() throws RollerException {
-        if (mPingTargetManager == null) {
-            mPingTargetManager = createPingTargetManager();
-        }
-        return mPingTargetManager;
-    }
-
-    /**
-     * @see org.roller.model.Roller#upgradeDatabase(java.sql.Connection)
-     */
-    public void upgradeDatabase(Connection con) throws RollerException {
-        UpgradeDatabase.upgradeDatabase(con);
-    }
-
-     public void shutdown() {
+    
+    public void shutdown() {
         try {
-            if(getReferrerQueueManager() != null) getReferrerQueueManager().shutdown();
-            if (mIndexManager != null) mIndexManager.shutdown();
-            if (mThreadManager != null) mThreadManager.shutdown();
-        } catch(Exception e) {
-            mLogger.warn(e);
+            if (getReferrerQueueManager() != null) getReferrerQueueManager().shutdown();
+            if (indexManager != null) indexManager.shutdown();
+            if (threadManager != null) threadManager.shutdown();
+        } catch(Throwable e) {
+            mLogger.error("Error calling Roller.shutdown()", e);
         }
     }
+    
 }

Modified: incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateAutoPingManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateAutoPingManagerImpl.java?rev=392618&r1=392617&r2=392618&view=diff
==============================================================================
--- incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateAutoPingManagerImpl.java (original)
+++ incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateAutoPingManagerImpl.java Sat Apr  8 15:36:25 2006
@@ -13,123 +13,208 @@
 import org.hibernate.Session;
 import org.hibernate.criterion.Expression;
 import org.roller.RollerException;
-import org.roller.business.PersistenceStrategy;
-import org.roller.business.AutoPingManagerImpl;
 import org.roller.pojos.AutoPingData;
 import org.roller.pojos.PingTargetData;
 import org.roller.pojos.WeblogEntryData;
 import org.roller.pojos.WebsiteData;
-
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.roller.config.PingConfig;
+import org.roller.model.AutoPingManager;
+import org.roller.model.PingQueueManager;
+import org.roller.model.RollerFactory;
 
-public class HibernateAutoPingManagerImpl extends AutoPingManagerImpl
-{
-    static final long serialVersionUID = 5420615676256979199L;
 
-    public HibernateAutoPingManagerImpl(PersistenceStrategy persistenceStrategy)
-    {
-        super(persistenceStrategy);
-    }
-
-    public void removeAutoPing(PingTargetData pingTarget, WebsiteData website) throws RollerException
-    {
-        try
-        {
-            Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+/**
+ * Hibernate implementation of the AutoPingManager.
+ */
+public class HibernateAutoPingManagerImpl implements AutoPingManager {
+    
+    static final long serialVersionUID = 5420615676256979199L;
+    
+    private static Log log = LogFactory.getLog(HibernateAutoPingManagerImpl.class);
+    
+    private HibernatePersistenceStrategy strategy = null;
+    
+    
+    public HibernateAutoPingManagerImpl(HibernatePersistenceStrategy strat) {
+        this.strategy = strat;
+    }
+    
+    
+    public AutoPingData createAutoPing(PingTargetData pingTarget, WebsiteData website) 
+            throws RollerException {
+        return new AutoPingData(null, pingTarget, website);
+    }
+    
+    
+    public AutoPingData retrieveAutoPing(String id) throws RollerException {
+        return (AutoPingData) strategy.load(id, AutoPingData.class);
+    }
+    
+    
+    public void storeAutoPing(AutoPingData autoPing) throws RollerException {
+        strategy.storeAndCommit(autoPing);
+    }
+    
+    
+    public void removeAutoPing(String id) throws RollerException {
+        //TODO: first remove all related category restrictions (category restrictions are not yet implemented)
+        strategy.removeAndCommit(id, AutoPingData.class);
+    }
+    
+    
+    public void removeAutoPing(AutoPingData autoPing)  throws RollerException {
+        //TODO: first remove all related category restrictions (category restrictions are not yet implemented)
+        strategy.removeAndCommit(autoPing);
+    }
+    
+    
+    public void removeAutoPing(PingTargetData pingTarget, WebsiteData website) throws RollerException {
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = strategy.getSession();
             Criteria criteria = session.createCriteria(AutoPingData.class);
-            // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
-            // website.
+            
+            // Currently category restrictions are not yet implemented, so we 
+            // return all auto ping configs for the website.
             criteria.add(Expression.eq("pingTarget", pingTarget));
             criteria.add(Expression.eq("website", website));
             List matches = criteria.list();
+            
             // This should have at most one element, but we remove them all regardless.
-            for (Iterator i = matches.iterator(); i.hasNext(); ) {
-                ((AutoPingData) i.next()).remove();
+            this.removeAutoPings(matches);
+        } catch (HibernateException e) {
+            throw new RollerException(e);
+        }
+    }
+    
+    
+    public void removeAutoPings(Collection autopings) throws RollerException {
+        
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            // just go through the list and remove each auto ping
+            Iterator pings = autopings.iterator();
+            while (pings.hasNext()) {
+                    this.strategy.remove((AutoPingData) pings.next());
             }
+            
+            // commit changes
+            this.strategy.getSession().getTransaction().commit();
+        } catch (HibernateException ex) {
+            
+            try {
+                this.strategy.getSession().getTransaction().rollback();
+            } catch(HibernateException he) {
+                log.error("Error doing rollback", he);
+            }
+            
+            strategy.release();
+            
+            throw new RollerException(ex);
         }
-        catch (HibernateException e)
-        {
+    }
+    
+    
+    public void removeAllAutoPings() throws RollerException {
+        try {
+            Session session = ((HibernatePersistenceStrategy) strategy).getSession();
+            Criteria criteria = session.createCriteria(AutoPingData.class);
+            List allAutoPings = criteria.list();
+            this.removeAutoPings(allAutoPings);
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
-    public List getAutoPingsByWebsite(WebsiteData website) throws RollerException
-    {
-        try
-        {
-            Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+    
+    
+    public void queueApplicableAutoPings(WeblogEntryData changedWeblogEntry) throws RollerException {
+        if (PingConfig.getSuspendPingProcessing()) {
+            if (log.isDebugEnabled()) log.debug("Ping processing is suspended.  No auto pings will be queued.");
+            return;
+        }
+        
+        // TODO: new manager method for addQueueEntries(list)?
+        PingQueueManager pingQueueMgr = RollerFactory.getRoller().getPingQueueManager();
+        List applicableAutopings = getApplicableAutoPings(changedWeblogEntry);
+        for (Iterator i = applicableAutopings.iterator(); i.hasNext(); ) {
+            AutoPingData autoPing = (AutoPingData) i.next();
+            pingQueueMgr.addQueueEntry(autoPing);
+        }
+    }
+    
+    
+    public List getAutoPingsByWebsite(WebsiteData website) throws RollerException {
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy) strategy).getSession();
             Criteria criteria = session.createCriteria(AutoPingData.class);
             // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
             // website.
             criteria.add(Expression.eq("website", website));
             return criteria.list();
-        }
-        catch (HibernateException e)
-        {
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
-    public List getAutoPingsByTarget(PingTargetData pingTarget) throws RollerException
-    {
-        try
-        {
-            Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+    
+    
+    public List getAutoPingsByTarget(PingTargetData pingTarget) throws RollerException {
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy) strategy).getSession();
             Criteria criteria = session.createCriteria(AutoPingData.class);
             // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
             // website.
             criteria.add(Expression.eq("pingTarget", pingTarget));
             return criteria.list();
-        }
-        catch (HibernateException e)
-        {
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
-    public void removeAllAutoPings() throws RollerException
-    {
-        try
-        {
-            Session session = ((HibernateStrategy) persistenceStrategy).getSession();
+    
+    
+    public List getApplicableAutoPings(WeblogEntryData changedWeblogEntry) throws RollerException {
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy) strategy).getSession();
             Criteria criteria = session.createCriteria(AutoPingData.class);
-            List allAutoPings = criteria.list();
-            removeAutoPings(allAutoPings);
-        }
-        catch (HibernateException e)
-        {
+            // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
+            // website.
+            criteria.add(Expression.eq("website", changedWeblogEntry.getWebsite()));
+            return criteria.list();
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
-    public List getCategoryRestrictions(AutoPingData autoPing) throws RollerException
-    {
+    
+    
+    public List getCategoryRestrictions(AutoPingData autoPing) throws RollerException {
         return Collections.EMPTY_LIST;
     }
-
-    public void setCategoryRestrictions(AutoPingData autoPing, Collection newCategories)
-    {
+    
+    
+    public void setCategoryRestrictions(AutoPingData autoPing, Collection newCategories) {
         // NOT YET IMPLEMENTED
         return;
     }
-
-    public List getApplicableAutoPings(WeblogEntryData changedWeblogEntry) throws RollerException
-    {
-        try
-        {
-            Session session = ((HibernateStrategy) persistenceStrategy).getSession();
-            Criteria criteria = session.createCriteria(AutoPingData.class);
-            // Currently category restrictions are not yet implemented, so we return all auto ping configs for the
-            // website.
-            criteria.add(Expression.eq("website", changedWeblogEntry.getWebsite()));
-            return criteria.list();
-        }
-        catch (HibernateException e)
-        {
-            throw new RollerException(e);
-        }
-    }
+    
+    
+    public void release() {}
+    
 }

Modified: incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateBookmarkManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateBookmarkManagerImpl.java?rev=392618&r1=392617&r2=392618&view=diff
==============================================================================
--- incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateBookmarkManagerImpl.java (original)
+++ incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateBookmarkManagerImpl.java Sat Apr  8 15:36:25 2006
@@ -2,13 +2,14 @@
  * Created on Jun 18, 2004
  */
 package org.roller.business.hibernate;
+
+import java.io.IOException;
+import java.io.StringReader;
 import org.hibernate.Criteria;
 import org.hibernate.HibernateException;
 import org.hibernate.Session;
 import org.hibernate.criterion.Expression;
 import org.roller.RollerException;
-import org.roller.business.BookmarkManagerImpl;
-import org.roller.business.PersistenceStrategy;
 import org.roller.pojos.Assoc;
 import org.roller.pojos.BookmarkData;
 import org.roller.pojos.FolderAssoc;
@@ -19,57 +20,392 @@
 import java.util.List;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.jdom.Document;
+import org.jdom.Element;
+import org.jdom.JDOMException;
+import org.jdom.input.SAXBuilder;
+import org.roller.model.BookmarkManager;
+import org.roller.util.Utilities;
+
 
 /**
- * Hibernate queries.
- * 
- * @author David M Johnson
+ * Hibernate implementation of the BookmarkManager.
  */
-public class HibernateBookmarkManagerImpl extends BookmarkManagerImpl
-{
+public class HibernateBookmarkManagerImpl implements BookmarkManager {
+    
     static final long serialVersionUID = 5286654557062382772L;
-
-    private static Log mLogger =
-        LogFactory.getFactory().getInstance(HibernateBookmarkManagerImpl.class);
+    
+    private static Log log = LogFactory.getLog(HibernateBookmarkManagerImpl.class);
+    
+    private HibernatePersistenceStrategy strategy = null;
+    
     
     /**
      * @param pstrategy
      * @param roller
      */
-    public HibernateBookmarkManagerImpl(PersistenceStrategy pstrategy)
-    {
-        super(pstrategy);
-        mLogger.debug("Instantiating Bookmark Manager");
+    public HibernateBookmarkManagerImpl(HibernatePersistenceStrategy strat) {
+        log.debug("Instantiating Hibernate Bookmark Manager");
+        
+        this.strategy = strat;
     }
-
+    
+    
+    public BookmarkData createBookmark() {
+        BookmarkData bd = new BookmarkData();
+        return bd;
+    }
+    
+    public BookmarkData createBookmark(
+            FolderData parent,
+            String name,
+            String desc,
+            String url,
+            String feedUrl,
+            Integer weight,
+            Integer priority,
+            String image) {
+        BookmarkData bd = new BookmarkData(
+                parent, name, desc, url, feedUrl, weight, priority, image);
+        return bd;
+    }
+    
+    public BookmarkData retrieveBookmark(String id) throws RollerException {
+        BookmarkData bd = (BookmarkData)
+        strategy.load(id, BookmarkData.class);
+        // TODO: huh?  why do we do this?
+        if (bd != null) bd.setBookmarkManager(this);
+        return bd;
+    }
+    
+//	public void storeBookmark(BookmarkData data) throws RollerException
+//	{
+//		strategy.store(data);
+//	}
+    
+    public void removeBookmark(String id) throws RollerException {
+        strategy.removeAndCommit(id, BookmarkData.class);
+    }
+    
+    //------------------------------------------------------------ Folder CRUD
+    
+    /**
+     * @see org.roller.model.BookmarkManager#createFolder()
+     */
+    public FolderData createFolder() {
+        FolderData fd = new FolderData();
+        return fd;
+    }
+    
+    /**
+     * @see org.roller.model.BookmarkManager#createFolder()
+     */
+    public FolderData createFolder(
+            FolderData parent,
+            String name,
+            String desc,
+            WebsiteData website) {
+        FolderData fd = new FolderData(parent, name, desc, website);
+        return fd;
+    }
+    
+    /**
+     * Retrieve folder and lazy-load it's sub-folders and bookmarks.
+     */
+    public FolderData retrieveFolder(String id) throws RollerException {
+        return (FolderData)strategy.load(id, FolderData.class);
+    }
+    
+    //------------------------------------------------------------ Operations
+    
+    public void importBookmarks(WebsiteData website, String folderName, String opml)
+            throws RollerException {
+        
+        String msg = "importBookmarks";
+        try {
+            // Build JDOC document OPML string
+            SAXBuilder builder = new SAXBuilder();
+            StringReader reader = new StringReader( opml );
+            Document doc = builder.build( reader );
+            
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            FolderData newFolder = getFolder(website, folderName);
+            if (newFolder == null) {
+                newFolder = createFolder(
+                        getRootFolder(website), folderName, folderName, website);
+                this.strategy.store(newFolder);
+            }
+            
+            // Iterate through children of OPML body, importing each
+            Element body = doc.getRootElement().getChild("body");
+            Iterator iter = body.getChildren().iterator();
+            while (iter.hasNext()) {
+                Element elem = (Element)iter.next();
+                importOpmlElement( website, elem, newFolder );
+            }
+            
+            // commit changes
+            this.strategy.getSession().getTransaction().commit();
+        } catch (JDOMException ex) {
+            throw new RollerException(ex);
+        } catch (IOException ex) {
+            throw new RollerException(ex);
+        } catch (HibernateException ex) {
+            
+            try {
+                this.strategy.getSession().getTransaction().rollback();
+            } catch(HibernateException he) {
+                log.error("Error doing rollback", he);
+            }
+            
+            strategy.release();
+            
+            throw new RollerException(ex);
+        }
+    }
+    
+    // convenience method used when importing bookmarks
+    // NOTE: this method does not commit any changes, that is done by importBookmarks()
+    private void importOpmlElement(
+            WebsiteData website, Element elem, FolderData parent)
+            throws RollerException {
+        String text = elem.getAttributeValue("text");
+        String title = elem.getAttributeValue("title");
+        String desc = elem.getAttributeValue("description");
+        String url = elem.getAttributeValue("url");
+        //String type = elem.getAttributeValue("type");
+        String xmlUrl = elem.getAttributeValue("xmlUrl");
+        String htmlUrl = elem.getAttributeValue("htmlUrl");
+        
+        title =   null!=title ? title : text;
+        desc =    null!=desc ? desc : title;
+        xmlUrl =  null!=xmlUrl ? xmlUrl : url;
+        url =     null!=htmlUrl ? htmlUrl : url;
+        
+        if (elem.getChildren().size()==0) {
+            // Leaf element.  Store a bookmark
+            // Currently bookmarks must have at least a name and HTML url to be stored. Previous logic was
+            // trying to skip invalid ones, but was letting ones with an xml url and no html url through
+            // which could result in a db exception.
+            // TODO: Consider providing error feedback instead of silently skipping the invalid bookmarks here.
+            if (null != title && null != url) {
+                BookmarkData bd = createBookmark(parent,
+                        title,
+                        desc,
+                        url,
+                        xmlUrl,
+                        new Integer(0),
+                        new Integer(100),
+                        null);
+                parent.addBookmark(bd);
+                // TODO: maybe this should be saving the folder?
+                this.strategy.store(bd);
+            }
+        } else {
+            // Store a folder
+            FolderData fd = createFolder(
+                    parent,
+                    title,
+                    desc,
+                    parent.getWebsite());
+            this.strategy.store(fd);
+            
+            // Import folder's children
+            Iterator iter = elem.getChildren("outline").iterator();
+            while ( iter.hasNext() ) {
+                Element subelem = (Element)iter.next();
+                importOpmlElement( website, subelem, fd  );
+            }
+        }
+    }
+    
+    //----------------------------------------------------------------
+    public void moveFolderContents(FolderData src, FolderData dest)
+            throws RollerException {
+        
+        if (dest.descendentOf(src)) {
+            throw new RollerException(
+                    "ERROR cannot move parent folder into it's own child");
+        }
+        
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            // Add to destination folder
+            LinkedList deleteList = new LinkedList();
+            Iterator srcBookmarks = src.getBookmarks().iterator();
+            while (srcBookmarks.hasNext()) {
+                BookmarkData bd = (BookmarkData)srcBookmarks.next();
+                deleteList.add(bd);
+                
+                BookmarkData movedBd = new BookmarkData();
+                movedBd.setData(bd);
+                movedBd.setId(null);
+                
+                dest.addBookmark(movedBd);
+                this.strategy.store(movedBd);
+            }
+            
+            // Remove from source folder
+            Iterator deleteIter = deleteList.iterator();
+            while (deleteIter.hasNext()) {
+                BookmarkData bd = (BookmarkData)deleteIter.next();
+                src.removeBookmark(bd);
+                // TODO: this won't conflict with the bookmark we store above right?
+                this.strategy.remove(bd);
+            }
+            
+            // commit changes
+            this.strategy.getSession().getTransaction().commit();
+        } catch (HibernateException ex) {
+            
+            try {
+                this.strategy.getSession().getTransaction().rollback();
+            } catch(HibernateException he) {
+                log.error("Error doing rollback", he);
+            }
+            
+            strategy.release();
+            
+            throw new RollerException(ex);
+        }
+    }
+    
+    //----------------------------------------------------------------
+    public void deleteFolderContents(FolderData src) throws RollerException {
+        
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            // just go through the folder and remove each bookmark
+            Iterator srcBookmarks = src.getBookmarks().iterator();
+            while (srcBookmarks.hasNext()) {
+                BookmarkData bd = (BookmarkData)srcBookmarks.next();
+                this.strategy.remove(bd);
+            }
+            
+            // commit changes
+            this.strategy.getSession().getTransaction().commit();
+        } catch (HibernateException ex) {
+            
+            try {
+                this.strategy.getSession().getTransaction().rollback();
+            } catch(HibernateException he) {
+                log.error("Error doing rollback", he);
+            }
+            
+            strategy.release();
+            
+            throw new RollerException(ex);
+        }
+    }
+    
+    //---------------------------------------------------------------- Queries
+    
+    public FolderData getFolder(WebsiteData website, String folderPath)
+    throws RollerException {
+        return getFolderByPath(website, null, folderPath);
+    }
+    
+    public String getPath(FolderData folder) throws RollerException {
+        if (null == folder.getParent()) {
+            return "/";
+        } else {
+            String parentPath = getPath(folder.getParent());
+            parentPath = "/".equals(parentPath) ? "" : parentPath;
+            return parentPath + "/" + folder.getName();
+        }
+    }
+    
+    public FolderData getFolderByPath(
+            WebsiteData website, FolderData folder, String path)
+            throws RollerException {
+        final Iterator folders;
+        final String[] pathArray = Utilities.stringToStringArray(path, "/");
+        
+        if (folder == null && (null == path || "".equals(path.trim()))) {
+            throw new RollerException("Bad arguments.");
+        }
+        
+        if (path.trim().equals("/")) {
+            return getRootFolder(website);
+        } else if (folder == null || path.trim().startsWith("/")) {
+            folders = getRootFolder(website).getFolders().iterator();
+        } else {
+            folders = folder.getFolders().iterator();
+        }
+        
+        while (folders.hasNext()) {
+            FolderData possibleMatch = (FolderData)folders.next();
+            if (possibleMatch.getName().equals(pathArray[0])) {
+                if (pathArray.length == 1) {
+                    return possibleMatch;
+                } else {
+                    String[] subpath = new String[pathArray.length - 1];
+                    System.arraycopy(pathArray, 1, subpath, 0, subpath.length);
+                    
+                    String pathString= Utilities.stringArrayToString(subpath,"/");
+                    return getFolderByPath(website, possibleMatch, pathString);
+                }
+            }
+        }
+        
+        // The folder did not match and neither did any subfolders
+        return null;
+    }
+    
+    //----------------------------------------------- FolderAssoc CRUD
+    
+    public Assoc createFolderAssoc() {
+        return new FolderAssoc();
+    }
+    
+    public Assoc createFolderAssoc(
+            FolderData folder,
+            FolderData ancestor,
+            String relation) throws RollerException {
+        return new FolderAssoc(null, folder, ancestor, relation);
+    }
+    
+    public FolderAssoc retrieveFolderAssoc(String id) throws RollerException {
+        return (FolderAssoc)strategy.load(id, FolderAssoc.class);
+    }
+    
+    public void release() {
+        // TODO Auto-generated method stub
+        
+    }
+    
     /**
      * @see org.roller.model.BookmarkManager#retrieveBookmarks(
      *      org.roller.pojos.FolderData, boolean)
      */
     public List retrieveBookmarks(FolderData folder, boolean subfolders)
-                    throws RollerException
-    {
-        try
-        {
-            Session session = ((HibernateStrategy) mStrategy).getSession();
+    throws RollerException {
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy) strategy).getSession();
             List bookmarks = new LinkedList();
-            if (subfolders)
-            {
+            if (subfolders) {
                 // get bookmarks in subfolders
                 Criteria assocsQuery = session
-                                .createCriteria(FolderAssoc.class);
+                        .createCriteria(FolderAssoc.class);
                 assocsQuery.add(Expression.eq("ancestorFolder", folder));
                 Iterator assocs = assocsQuery.list().iterator();
-                while (assocs.hasNext())
-                {
+                while (assocs.hasNext()) {
                     FolderAssoc assoc = (FolderAssoc) assocs.next();
                     Criteria bookmarksQuery = session
-                                    .createCriteria(BookmarkData.class);
+                            .createCriteria(BookmarkData.class);
                     bookmarksQuery.add(Expression.eq("folder", assoc
-                                    .getFolder()));
+                            .getFolder()));
                     Iterator bookmarkIter = bookmarksQuery.list().iterator();
-                    while (bookmarkIter.hasNext())
-                    {
+                    while (bookmarkIter.hasNext()) {
                         BookmarkData entry = (BookmarkData) bookmarkIter.next();
                         bookmarks.add(entry);
                     }
@@ -77,104 +413,93 @@
             }
             // get bookmarks in folder
             Criteria bookmarksQuery = session
-                            .createCriteria(BookmarkData.class);
+                    .createCriteria(BookmarkData.class);
             bookmarksQuery.add(Expression.eq("folder", folder));
             Iterator bookmarkIter = bookmarksQuery.list().iterator();
-            while (bookmarkIter.hasNext())
-            {
+            while (bookmarkIter.hasNext()) {
                 BookmarkData bookmark = (BookmarkData) bookmarkIter.next();
                 bookmarks.add(bookmark);
             }
             return bookmarks;
-        }
-        catch (HibernateException e)
-        {
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
-    public FolderData getRootFolder(WebsiteData website) throws RollerException
-    {
+    
+    public FolderData getRootFolder(WebsiteData website) throws RollerException {
         if (website == null)
             throw new RollerException("website is null");
-        try
-        {
-            Session session = ((HibernateStrategy) mStrategy).getSession();
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy) strategy).getSession();
             Criteria criteria = session.createCriteria(FolderAssoc.class);
             criteria.createAlias("folder", "f");
             criteria.add(Expression.eq("f.website", website));
             criteria.add(Expression.isNull("ancestorFolder"));
             criteria.add(Expression.eq("relation", FolderAssoc.PARENT));
             List results = criteria.list();
-            if (results.size() > 1)
-            {
+            if (results.size() > 1) {
                 // Should not have more than one root
                 throw new RollerException(
-                                "More than one root folder found for website "
-                                                + website.getId());
-            }
-            else if (results.size() == 1)
-            {
+                        "More than one root folder found for website "
+                        + website.getId());
+            } else if (results.size() == 1) {
                 // Return root
                 return ((FolderAssoc) results.get(0)).getFolder();
             }
             return null;
-        }
-        catch (HibernateException e)
-        {
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
-    public List getAllFolders(WebsiteData website) throws RollerException
-    {
+    
+    public List getAllFolders(WebsiteData website) throws RollerException {
         if (website == null)
             throw new RollerException("Website is null");
         
-        try
-        {
-            Session session = ((HibernateStrategy) mStrategy).getSession();
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy) strategy).getSession();
             Criteria criteria = session.createCriteria(FolderData.class);
-            criteria.add(Expression.eq("website", website));            
+            criteria.add(Expression.eq("website", website));
             return criteria.list();
-        }
-        catch (HibernateException e)
-        {
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
-
+        
     }
-
-    /** 
+    
+    /**
      * @see org.roller.model.BookmarkManager#isDuplicateFolderName(org.roller.pojos.FolderData)
      */
-    public boolean isDuplicateFolderName(FolderData folder) throws RollerException
-    {
+    public boolean isDuplicateFolderName(FolderData folder) throws RollerException {
         // ensure that no sibling folders share the same name
         boolean isNewFolder = (folder.getId() == null);
         FolderData parent =
-            isNewFolder ? (FolderData)folder.getNewParent() : folder.getParent();
+                isNewFolder ? (FolderData)folder.getNewParent() : folder.getParent();
         
-        if (null != parent)
-        {
+        if (null != parent) {
             List sameNames;
-            try
-            {
-                Session session = ((HibernateStrategy) mStrategy).getSession();
+            try {
+                // begin transaction
+                this.strategy.getSession().beginTransaction();
+            
+                Session session = ((HibernatePersistenceStrategy) strategy).getSession();
                 Criteria criteria = session.createCriteria(FolderAssoc.class);
                 criteria.createAlias("folder", "f");
                 criteria.add(Expression.eq("f.name", folder.getName()));
                 criteria.add(Expression.eq("ancestorFolder", parent));
                 criteria.add(Expression.eq("relation", Assoc.PARENT));
                 sameNames = criteria.list();
-            }
-            catch (HibernateException e)
-            {
+            } catch (HibernateException e) {
                 throw new RollerException(e);
             }
             // If we got some matches
-            if (sameNames.size() > 0)
-            {
+            if (sameNames.size() > 0) {
                 // if we're saving a new folder, any matches are dups
                 if (isNewFolder) return true;
                 // otherwise it's a dup it isn't the same one (one match with the same id).
@@ -184,143 +509,133 @@
         }
         return false;
     }
-
-    /** 
+    
+    /**
      * @see org.roller.model.BookmarkManager#getFolderParentAssoc(
      * org.roller.pojos.FolderData)
      */
-    public Assoc getFolderParentAssoc(FolderData folder) throws RollerException
-    {
-        try
-        {
-            Session session = ((HibernateStrategy)mStrategy).getSession();
+    public Assoc getFolderParentAssoc(FolderData folder) throws RollerException {
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(FolderAssoc.class);
             criteria.add(Expression.eq("folder", folder));
             criteria.add(Expression.eq("relation", Assoc.PARENT));
             List parents = criteria.list();
-            if (parents.size() > 1)
-            {
+            if (parents.size() > 1) {
                 throw new RollerException("ERROR: more than one parent");
-            }
-            else if (parents.size() == 1)
-            {
+            } else if (parents.size() == 1) {
                 return (Assoc) parents.get(0);
-            }
-            else
-            {
+            } else {
                 return null;
             }
-        }
-        catch (HibernateException e)
-        {
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
-    /** 
+    
+    /**
      * @see org.roller.model.BookmarkManager#getFolderChildAssocs(
      * org.roller.pojos.FolderData)
      */
-    public List getFolderChildAssocs(FolderData folder) throws RollerException
-    {
-        try
-        {
-            Session session = ((HibernateStrategy)mStrategy).getSession();
+    public List getFolderChildAssocs(FolderData folder) throws RollerException {
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(FolderAssoc.class);
             criteria.add(Expression.eq("ancestorFolder", folder));
             criteria.add(Expression.eq("relation", Assoc.PARENT));
             return criteria.list();
-        }
-        catch (HibernateException e)
-        {
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
-    /** 
+    
+    /**
      * @see org.roller.model.BookmarkManager#getAllFolderDecscendentAssocs(
      * org.roller.pojos.FolderData)
      */
-    public List getAllFolderDecscendentAssocs(FolderData folder) throws RollerException
-    {
-        try
-        {
-            Session session = ((HibernateStrategy)mStrategy).getSession();
+    public List getAllFolderDecscendentAssocs(FolderData folder) throws RollerException {
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(FolderAssoc.class);
             criteria.add(Expression.eq("ancestorFolder", folder));
             return criteria.list();
-        }
-        catch (HibernateException e)
-        {
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
-    /** 
+    
+    /**
      * @see org.roller.model.BookmarkManager#getFolderAncestorAssocs(
      * org.roller.pojos.FolderData)
      */
-    public List getFolderAncestorAssocs(FolderData folder) throws RollerException
-    {
-        try
-        {
-            Session session = ((HibernateStrategy)mStrategy).getSession();
+    public List getFolderAncestorAssocs(FolderData folder) throws RollerException {
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(FolderAssoc.class);
             criteria.add(Expression.eq("folder", folder));
             return criteria.list();
-        }
-        catch (HibernateException e)
-        {
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
+    
     /**
      * @see org.roller.model.BookmarkManager#isFolderInUse(org.roller.pojos.FolderData)
      */
-    public boolean isFolderInUse(FolderData folder) throws RollerException
-    {
-        try
-        {
+    public boolean isFolderInUse(FolderData folder) throws RollerException {
+        try {
             // We consider a folder to be "in use" if it contains any bookmarks or has
             // any children.
-
+            
             // We first determine the number of bookmark entries.
             // NOTE: This seems to be an attempt to optimize, rather than just use getBookmarks(),
             // but I'm not sure that this optimization is really worthwhile, and it ignores
             // caching in the case that the (lazy) getBookmarks has been done already. --agangolli
             // TODO: condider changing to just use getBookmarks().size()
-            Session session = ((HibernateStrategy)mStrategy).getSession();
+            
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(BookmarkData.class);
             criteria.add(Expression.eq("folder", folder));
             criteria.setMaxResults(1);
             int entryCount = criteria.list().size();
-
+            
             // Return true if we have bookmarks or (, failing that, then checking) if we have children
             return (entryCount > 0 || folder.getFolders().size() > 0);
-        }
-        catch (HibernateException e)
-        {
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
+    
     public boolean isDescendentOf(FolderData child, FolderData ancestor)
-        throws RollerException
-    {
+    throws RollerException {
         boolean ret = false;
-        try
-        {
-            Session session = ((HibernateStrategy)mStrategy).getSession();
+        try {
+            // begin transaction
+            this.strategy.getSession().beginTransaction();
+            
+            Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(FolderAssoc.class);
             criteria.add(Expression.eq("folder", child));
             criteria.add(Expression.eq("ancestorFolder", ancestor));
             ret = criteria.list().size() > 0;
-        }
-        catch (HibernateException e)
-        {
-            throw new RollerException(e);        
+        } catch (HibernateException e) {
+            throw new RollerException(e);
         }
         return ret;
-    }    
+    }
 }

Modified: incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateConfigManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateConfigManagerImpl.java?rev=392618&r1=392617&r2=392618&view=diff
==============================================================================
--- incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateConfigManagerImpl.java (original)
+++ incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateConfigManagerImpl.java Sat Apr  8 15:36:25 2006
@@ -6,62 +6,85 @@
 import org.hibernate.Criteria;
 import org.hibernate.HibernateException;
 import org.hibernate.Session;
-
 import org.roller.RollerException;
-import org.roller.business.ConfigManagerImpl;
-import org.roller.business.PersistenceStrategy;
 import org.roller.pojos.RollerConfigData;
-
 import java.util.List;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.roller.model.ConfigManager;
+
 
 /**
- * @author David M Johnson
+ * The *OLD* Roller configuration mechanism.
+ *
+ * This has been replaced by the PropertiesManager and the roller.properties
+ * file.
  */
-public class HibernateConfigManagerImpl extends ConfigManagerImpl
-{
+public class HibernateConfigManagerImpl implements ConfigManager {
+    
     static final long serialVersionUID = -3674252864091781177L;
     
-    private static Log mLogger =
-        LogFactory.getFactory().getInstance(HibernateConfigManagerImpl.class);
+    private static Log log = LogFactory.getLog(HibernateConfigManagerImpl.class);
+    
+    private HibernatePersistenceStrategy strategy = null;
+    
+    
+    public HibernateConfigManagerImpl(HibernatePersistenceStrategy strategy) {
+        log.debug("Instantiating Hibernate Config Manager");
+        
+        this.strategy = strategy;
+    }
+    
     
     /**
-     * @param strategy
-     * @param roller
+     * @see org.roller.model.ConfigManager#storeRollerConfig(org.roller.pojos.RollerConfig)
      */
-    public HibernateConfigManagerImpl(PersistenceStrategy strategy)
-    {
-        super(strategy);
-        mLogger.debug("Instantiating Config Manager");
+    public void storeRollerConfig(RollerConfigData data) throws RollerException {
+        // no longer supported
     }
-
+    
+    
+    /**
+     * This isn't part of the ConfigManager Interface, because really
+     * we shouldn't ever delete the RollerConfig.  This is mostly here
+     * to assist with unit testing.
+     */
+    public void removeRollerConfig(String id) throws RollerException {
+        // no longer supported
+    }
+    
+    
     /**
      * Fetch all RollerConfigs and return the first one, if any.
      * Note: there should only be one!
      * @see org.roller.model.ConfigManager#getRollerConfig()
      */
-    public RollerConfigData getRollerConfig() throws RollerException
-    {
-        mLogger.error("Someone is trying to use the old config!!\n"+
+    public RollerConfigData getRollerConfig() throws RollerException {
+        
+        log.error("Someone is trying to use the old config!!\n"+
                 "This configuration mechanism has been deprecated\n"+
                 "You should see this message only once when you first upgrade\n"+
                 "your installation to roller 1.2\n\n"+
                 "If you continue to see this message please shoot us an email\n"+
-                "at roller-development@lists.sourceforge.net with some output\n"+
+                "at roller-dev@incubator.apache.org with some output\n"+
                 "from your log files.\n");
-        try
-        {
-            Session session = ((HibernateStrategy)mStrategy).getSession();
+        
+        try {
+            Session session = this.strategy.getSession();
             Criteria criteria = session.createCriteria(RollerConfigData.class);
             criteria.setMaxResults(1);
-            List list = criteria.list();
-            return list.size()!=0 ? (RollerConfigData)list.get(0) : null;
-        }
-        catch (HibernateException e)
-        {
+            return (RollerConfigData) criteria.uniqueResult();
+        } catch (HibernateException e) {
             throw new RollerException(e);
         }
     }
-
+    
+    
+    public RollerConfigData readFromFile(String file) {
+        return null;
+    }
+    
+    
+    public void release() {}
+    
 }

Added: incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java?rev=392618&view=auto
==============================================================================
--- incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java (added)
+++ incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java Sat Apr  8 15:36:25 2006
@@ -0,0 +1,428 @@
+/*
+ * Created on Mar 7, 2003
+ */
+package org.roller.business.hibernate;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hibernate.HibernateException;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
+import org.hibernate.cfg.Configuration;
+import org.roller.RollerException;
+import org.roller.pojos.Assoc;
+import org.roller.pojos.HierarchicalPersistentObject;
+import org.roller.pojos.PersistentObject;
+
+
+/**
+ * Base class for Hibernate persistence strategy.
+ *
+ * This class serves as a helper/util class for all of the Hibernate
+ * manager implementations by providing a set of basic persistence methods
+ * that can be easily reused.
+ *
+ * IMPORTANT: some of the persistence methods in this class do not contain any
+ * transaction logic.  they do not attempt to start/join a transaction at
+ * the beginning of an operation and they do not attempt to commit or rollback
+ * at the end of an operation.  this is done purposely so that these methods
+ * can be called repeatedly by the manager impls to make up a transaction.
+ *
+ * an example of this would be if the properties manager impl wanted to store
+ * a list of properties all in one transaction then it could do so by starting
+ * a transaction, then calling the store() method of this class XX times, and
+ * then finally doing a commit.
+ *
+ */
+public class HibernatePersistenceStrategy {
+    
+    static final long serialVersionUID = 2561090040518169098L;
+    
+    private static SessionFactory sessionFactory = null;
+    
+    private static Log mLogger = LogFactory.getLog(HibernatePersistenceStrategy.class);
+    
+    
+    /**
+     * Construct using Hibernate Session Factory.
+     */
+    public HibernatePersistenceStrategy() throws Exception {
+        
+        mLogger.debug("Initializing Hibernate SessionFactory");
+        
+        Configuration config = new Configuration();
+        config.configure("/hibernate.cfg.xml");
+        this.sessionFactory = config.buildSessionFactory();
+        
+    }
+
+    
+    /**
+     * Get persistence session on current thread.
+     *
+     * This will open a new Session if one is not already open, otherwise
+     * it will return the already open Session.
+     */
+    protected Session getSession() {
+        
+        mLogger.debug("Getting hibernate session");
+        
+        return sessionFactory.getCurrentSession();
+    }
+    
+    
+    /**
+     * Release database session, rolls back any uncommitted changes.
+     */
+    protected void release() throws HibernateException {
+        
+        Session session = this.getSession();
+        
+        if(session != null) {
+            if(session.isOpen()) {
+                
+                mLogger.debug("Closing open hibernate session");
+                
+                // TODO: should we do a rollback() first?
+                //session.getTransaction().rollback();
+                
+                session.close();
+            }
+        }
+    }
+
+    
+    /**
+     * Retrieve object.  We return null if the object is not found.
+     */
+    protected PersistentObject load(String id, Class clazz) throws RollerException {
+        
+        if(id == null || clazz == null) {
+            throw new RollerException("Cannot load objects when value is null");
+        }
+        
+        // begin transaction
+        getSession().beginTransaction();
+            
+        return (PersistentObject) getSession().get(clazz, id);
+    }
+    
+    
+    /**
+     * Store object.
+     */
+    protected void store(PersistentObject obj) throws HibernateException {
+        
+        if(obj == null) {
+            throw new HibernateException("Cannot save null object");
+        }
+        
+        Session session = getSession();
+        
+        // TODO: this is wacky, we should double check logic here
+        
+        
+        // TODO: better to use session.saveOrUpdate() here, if possible
+        if ( obj.getId() == null || obj.getId().trim().equals("") ) {
+            // Object has never been written to database, so save it.
+            // This makes obj into a persistent instance.
+            session.save(obj);
+        }
+        
+        if ( !session.contains(obj) ) {
+            // Object has been written to database, but instance passed in
+            // is not a persistent instance, so must be loaded into session.
+            PersistentObject vo =
+                    (PersistentObject)session.load(obj.getClass(),obj.getId());
+            vo.setData(obj);
+            obj = vo;
+            
+            // TODO: do we need to call session.save() here?
+        }
+        
+    }
+    
+    
+    /**
+     * Store object and commit it.
+     * 
+     * If the commit fails then we rollback and release the session.
+     */
+    protected void storeAndCommit(PersistentObject obj) throws RollerException {
+        
+        try {
+            // begin transaction
+            getSession().beginTransaction();
+            
+            this.store(obj);
+            
+            // commit changes
+            getSession().getTransaction().commit();
+        } catch (HibernateException ex) {
+            
+            try {
+                getSession().getTransaction().rollback();
+            } catch(HibernateException he) {
+                mLogger.error("Error doing rollback", he);
+            }
+            
+            release();
+            
+            throw new RollerException(ex);
+        }
+    }
+    
+    
+    /**
+     * Remove object.
+     */
+    protected void remove(String id, Class clazz) throws HibernateException {
+        
+        if(id == null || clazz == null) {
+            throw new HibernateException("Cannot remove object when values are null");
+        }
+        
+        Session session = getSession();
+        
+        PersistentObject obj = (PersistentObject) session.load(clazz,id);
+        
+        session.delete(obj);
+    }
+    
+    
+    /**
+     * Remove object and commit.  If the commit fails then we rollback and release.
+     */
+    protected void removeAndCommit(String id, Class clazz) throws RollerException {
+        
+        if(id == null || clazz == null) {
+            throw new RollerException("Cannot remove object when values are null");
+        }
+        
+        try {
+            // begin transaction
+            getSession().beginTransaction();
+            
+            this.remove(id, clazz);
+            
+            // commit changes
+            getSession().getTransaction().commit();
+        } catch (HibernateException ex) {
+            
+            try {
+                getSession().getTransaction().rollback();
+            } catch(HibernateException he) {
+                mLogger.error("Error doing rollback", he);
+            }
+            
+            release();
+            
+            throw new RollerException(ex);
+        }
+    }
+    
+    
+    /**
+     * Remove object.
+     */
+    protected void remove(PersistentObject obj) throws HibernateException {
+        
+        if(obj == null) {
+            throw new HibernateException("Cannot remove null object");
+        }
+        
+        // can't remove transient objects
+        if (obj.getId() != null) {
+            
+            getSession().delete(obj);
+        }
+    }
+    
+    
+    /**
+     * Remove object and commit.  If the commit fails then we rollback and release.
+     */
+    protected void removeAndCommit(PersistentObject obj) throws RollerException {
+        
+        if(obj == null) {
+            throw new RollerException("Cannot remove null object");
+        }
+        
+        try {
+            // begin transaction
+            getSession().beginTransaction();
+            
+            this.remove(obj);
+            
+            // commit changes
+            getSession().getTransaction().commit();
+        } catch (HibernateException ex) {
+            
+            try {
+                getSession().getTransaction().rollback();
+            } catch(HibernateException he) {
+                mLogger.error("Error doing rollback", he);
+            }
+            
+            release();
+            
+            throw new RollerException(ex);
+        }
+    }
+    
+    
+    /**
+     * Store hierarchical object.
+     *
+     * TODO: this looks complicated.  needs some checking.
+     * NOTE: if the object has proper cascade setting then is all this necessary?
+     */
+    protected void store(HierarchicalPersistentObject obj) 
+            throws HibernateException, RollerException {
+        
+        if(obj == null) {
+            throw new HibernateException("Cannot save null object");
+        }
+        
+        Session session = getSession();
+        
+        HierarchicalPersistentObject mNewParent = obj.getNewParent();
+        boolean fresh = (obj.getId() == null || "".equals(obj.getId()));
+        
+        // TODO: saveOrUpdate()?  check if transient?
+        session.save(obj);
+        
+        if (fresh) {
+            // Every fresh cat needs a parent assoc
+            Assoc parentAssoc = obj.createAssoc(
+                    obj, mNewParent, Assoc.PARENT);
+            this.store(parentAssoc);
+        } else if (null != mNewParent) {
+            // New parent must be added to parentAssoc
+            Assoc parentAssoc = obj.getParentAssoc();
+            parentAssoc.setAncestor(mNewParent);
+            this.store(parentAssoc);
+        }
+        
+        // Clear out existing grandparent associations
+        Iterator ancestors = obj.getAncestorAssocs().iterator();
+        while (ancestors.hasNext()) {
+            Assoc assoc = (Assoc)ancestors.next();
+            if (assoc.getRelation().equals(Assoc.GRANDPARENT)) {
+                this.remove(assoc);
+            }
+        }
+        
+        // Walk parent assocations, creating new grandparent associations
+        int count = 0;
+        Assoc currentAssoc = obj.getParentAssoc();
+        while (null != currentAssoc.getAncestor()) {
+            if (count > 0) {
+                Assoc assoc = obj.createAssoc(obj,
+                        currentAssoc.getAncestor(),
+                        Assoc.GRANDPARENT);
+                this.store(assoc);
+            }
+            currentAssoc = currentAssoc.getAncestor().getParentAssoc();
+            count++;
+        }
+        
+        Iterator children = obj.getChildAssocs().iterator();
+        while (children.hasNext()) {
+            Assoc assoc = (Assoc) children.next();
+            
+            // resetting parent will cause reset of ancestors links
+            assoc.getObject().setParent(obj);
+            
+            // recursively...
+            this.store(assoc.getObject());
+        }
+        
+        // Clear new parent now that new parent has been saved
+        mNewParent = null;
+
+    }
+    
+    
+    /**
+     * Store assoc.
+     */
+    protected void store(Assoc assoc) throws HibernateException {
+        
+        if(assoc == null) {
+            throw new HibernateException("Cannot save null object");
+        }
+        
+        // TODO: double check that this is okay
+        getSession().saveOrUpdate(assoc);
+        
+    }
+    
+    
+    /**
+     * Remove hierarchical object.
+     *
+     * TODO: this is complicated.  needs some checking.
+     * NOTE: if the object has proper cascade setting then is all this necessary?
+     */
+    protected void remove(HierarchicalPersistentObject obj) throws RollerException {
+        
+        if(obj == null) {
+            throw new RollerException("Cannot remove null object");
+        }
+        
+        // loop to remove all descendents and associations
+        List toRemove = new LinkedList();
+        List assocs = obj.getAllDescendentAssocs();
+        for (int i=assocs.size()-1; i>=0; i--) {
+            Assoc assoc = (Assoc)assocs.get(i);
+            HierarchicalPersistentObject hpo = assoc.getObject();
+            
+            // remove my descendent's parent and grandparent associations
+            Iterator ancestors = hpo.getAncestorAssocs().iterator();
+            while (ancestors.hasNext()) {
+                Assoc dassoc = (Assoc)ancestors.next();
+                this.remove(dassoc);
+            }
+            
+            // remove decendent association and descendents
+            //assoc.remove();
+            toRemove.add(hpo);
+        }
+        Iterator removeIterator = toRemove.iterator();
+        while (removeIterator.hasNext()) {
+            PersistentObject po = (PersistentObject) removeIterator.next();
+            getSession().delete(po);
+        }
+        
+        // loop to remove my own parent and grandparent associations
+        Iterator ancestors = obj.getAncestorAssocs().iterator();
+        while (ancestors.hasNext()) {
+            Assoc assoc = (Assoc)ancestors.next();
+            this.remove(assoc);
+        }
+        
+        // remove myself
+        getSession().delete(obj);
+    }
+    
+    
+    /**
+     * Remove assoc.
+     */
+    protected void remove(Assoc assoc) throws HibernateException {
+        
+        if(assoc == null) {
+            throw new HibernateException("Cannot save null object");
+        }
+        
+        // TODO: double check that this is okay
+        getSession().delete(assoc);
+        
+    }
+    
+}