Package net.sf.jportlet.portlets.bookmark.impl

Source Code of net.sf.jportlet.portlets.bookmark.impl.BookmarkManagerImpl

/*
* Created on May 7, 2003
*/
package net.sf.jportlet.portlets.bookmark.impl;

import java.net.URL;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import cirrus.hibernate.Hibernate;
import cirrus.hibernate.HibernateException;
import cirrus.hibernate.ObjectDeletedException;
import cirrus.hibernate.Session;
import cirrus.hibernate.SessionFactory;
import cirrus.hibernate.Transaction;

import net.sf.jportlet.portlet.PortletContext;
import net.sf.jportlet.portlet.PortletException;
import net.sf.jportlet.portlet.User;
import net.sf.jportlet.portlets.ObjectNotFoundException;
import net.sf.jportlet.portlets.PersistenceException;
import net.sf.jportlet.portlets.bookmark.Bookmark;
import net.sf.jportlet.portlets.bookmark.BookmarkManager;
import net.sf.jportlet.service.hibernate.HibernateService;


/**
* Implementation of {@link Bookmark}
* @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
*/
public class BookmarkManagerImpl
    implements BookmarkManager
{
    //~ Instance fields --------------------------------------------------------

    private SessionFactory _sessionFactory;

    //~ Constructors -----------------------------------------------------------

    /**
     *
     */
    public BookmarkManagerImpl( PortletContext context )
        throws PortletException
    {
        HibernateService hib = ( HibernateService ) context.getService( HibernateService.NAME );
        _sessionFactory = hib.getSessionFactory(  );
    }

    //~ Methods ----------------------------------------------------------------

    /**
     * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#createBookmark(java.lang.String, java.net.URL, java.lang.String, net.sf.jportlet.portlet.User)
     */
    public String createBookmark( String title,
                                  URL    url,
                                  String description,
                                  User   user )
    {
        Session     session = null;
        Transaction tx = null;

        try
        {
            session = _sessionFactory.openSession(  );

            BookmarkImpl bk = new BookmarkImpl( null, title, url.toString(), description, user );

            tx = session.beginTransaction(  );
            session.save( bk );
            tx.commit(  );

            return bk.getId(  );
        }
        catch ( HibernateException e )
        {
            rollback( tx );
            throw new PersistenceException( e );
        }
        catch ( SQLException e )
        {
            rollback( tx );
            throw new PersistenceException( e );
        }
        finally
        {
            close( session );
        }
    }

    /**
     * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#updateBookmark(java.lang.String, java.lang.String, java.net.URL, java.lang.String)
     */
    public void updateBookmark( String id,
                                String title,
                                URL    url,
                                String description )
        throws ObjectNotFoundException
    {
        Session     session = null;
        Transaction tx = null;

        try
        {
            session = _sessionFactory.openSession(  );

            BookmarkImpl bk = ( BookmarkImpl ) session.load( BookmarkImpl.class, id );
            bk.setTitle( title );
            bk.setUrl( url.toString() );
            bk.setDescription( description );

            tx = session.beginTransaction(  );
            session.update( bk );
            tx.commit(  );
        }
        catch( cirrus.hibernate.ObjectNotFoundException o )
        {
            throw new ObjectNotFoundException( "Bookmark#" + id + " not found" );
        }
        catch ( HibernateException e )
        {
            rollback( tx );
            throw new PersistenceException( e );
        }
        catch ( SQLException e )
        {
            rollback( tx );
            throw new PersistenceException( e );
        }
        finally
        {
            close( session );
        }
    }

    /**
     * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#deleteBookmarks(java.lang.String[])
     */
    public void deleteBookmarks( String ids[] )
    {
        Session     session = null;
        Transaction tx = null;

        try
        {
            session = _sessionFactory.openSession(  );

            tx = session.beginTransaction(  );
            for ( int i = 0; i < ids.length; i++ )
            {
                BookmarkImpl bk = ( BookmarkImpl ) session.load( BookmarkImpl.class, ids[ i ] );
                try
                {
                  session.delete( bk );
                }
                catch( ObjectDeletedException o )
                {
                  o.printStackTrace();
                }
            }

            tx.commit(  );
        }
        catch ( HibernateException e )
        {
            rollback( tx );
            throw new PersistenceException( e );
        }
        catch ( SQLException e )
        {
            rollback( tx );
            throw new PersistenceException( e );
        }
        finally
        {
            close( session );
        }
    }

    /**
     * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#findBookmark(java.lang.String)
     */
    public Bookmark findBookmark( String id )
        throws ObjectNotFoundException
    {
        Session session = null;

        try
        {
            session = _sessionFactory.openSession(  );
            BookmarkImpl bookmark = ( BookmarkImpl ) session.load( BookmarkImpl.class, id );
           
            /*
             * Call a function so that the proxy get effectively loaded.
             * This will avoir LazyInstantiation error
             */
            bookmark.getTitle();
           
            return bookmark;
        }
        catch( cirrus.hibernate.ObjectNotFoundException o )
        {
            throw new ObjectNotFoundException( "Bookmark#" + id + " not found" );
        }
        catch ( HibernateException e )
        {
            throw new PersistenceException( e );
        }
        catch ( SQLException e )
        {
            throw new PersistenceException( e );
        }
        finally
        {
            close( session );
        }
    }

    /**
     * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#findBookmarks(net.sf.jportlet.portlet.User, java.lang.String)
     */
    public Collection findBookmarks( User   user )
    {
        return findBookmarks( user, -1 );
    }

    /**
     * @see net.sf.jportlet.portlets.bookmark.BookmarkManager#findBookmarks(net.sf.jportlet.portlet.User, int, java.lang.String)
     */
    public Collection findBookmarks( User   user,
                                     int    maxsize )
    {
        Session session = null;

        try
        {
            session = _sessionFactory.openSession(  );

            String     oql;
            Collection col;
            if ( user != null )
            {
                oql = "FROM b IN CLASS " + BookmarkImpl.class.getName(  ) + " WHERE b.userId=? ORDER BY b.title";
                col = session.find( oql, user.getId(  ), Hibernate.STRING );
            }
            else
            {
                oql = "FROM b IN CLASS " + BookmarkImpl.class.getName(  ) + " WHERE b.userId IS NULL ORDER BY b.title";
                col = session.find( oql );
            }

            if ( maxsize >= 0 )
            {
                ArrayList lst = new ArrayList(  );
                Iterator  it = col.iterator(  );
                for ( int i = 0; ( i < maxsize ) && it.hasNext(  ); i++ )
                {
                    lst.add( it.next(  ) );
                }

                return lst;
            }
            else
            {
                return col;
            }
        }
        catch ( HibernateException e )
        {
            throw new PersistenceException( e );
        }
        catch ( SQLException e )
        {
            throw new PersistenceException( e );
        }
        finally
        {
            close( session );
        }
    }

    private void close( Session session )
        throws PersistenceException
    {
        if ( session != null )
        {
            try
            {
                session.close(  );
            }
            catch ( Exception e )
            {
                throw new PersistenceException( "Error while closing the session", e );
            }
        }
    }

    private void rollback( Transaction tx )
        throws PersistenceException
    {
        if ( tx != null )
        {
            try
            {
                tx.rollback(  );
            }
            catch ( Exception ee )
            {
                throw new PersistenceException( "Error while roll-backing the transaction", ee );
            }
        }
    }
}
TOP

Related Classes of net.sf.jportlet.portlets.bookmark.impl.BookmarkManagerImpl

TOP
Copyright © 2015 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.