Subversion Repositories general

Rev

Rev 1041 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package ak.hostadmiral.core.model.store.hibernate;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.type.Type;

import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.util.ModelStoreException;
import ak.hostadmiral.util.hibernate.HibernateUtil;
import ak.hostadmiral.core.model.User;
import ak.hostadmiral.core.model.InetDomain;
import ak.hostadmiral.core.model.InetDomainManager;
import ak.hostadmiral.core.model.store.InetDomainStore;

public class InetDomainHibernate
        implements InetDomainStore
{
        public InetDomainHibernate()
                throws ModelStoreException
        {
                initSortKeys();
                register();
        }

        public InetDomain get(Long id)
                throws ModelStoreException
        {
                try {
                        return (InetDomain)HibernateUtil.currentSession().load(
                                InetDomain.class, id);
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
        }

        public boolean nameExists(InetDomain domain, String name)
                throws ModelStoreException
        {
                try {
                        if(domain.getId() == null)
                                return ((Integer)HibernateUtil.currentSession().iterate(
                                        "select count(*) from InetDomain d where name = ?",
                                        name, Hibernate.STRING)
                                        .next()).intValue() > 0;
                        else
                                return ((Integer)HibernateUtil.currentSession().iterate(
                                        "select count(*) from InetDomain d where name = ? and d != ?",
                                        new Object[] { name, domain },
                                        new Type[] { Hibernate.STRING, Hibernate.entity(InetDomain.class) } )
                                        .next()).intValue() > 0;
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
        }

        public InetDomain findForName(String name)
                throws ModelStoreException
        {
                try {
                        List list = HibernateUtil.currentSession().find(
                                "select d from InetDomain d left join fetch d.owner where d.name=?",
                                name, Hibernate.STRING);

                        if(list.size() == 0)
                                return null;
                        else
                                return (InetDomain)list.get(0);
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
        }

        public void save(InetDomain domain)
                throws ModelStoreException
        {
                try {
                        HibernateUtil.currentSession().saveOrUpdate(domain);
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
        }

        public void delete(InetDomain domain)
                throws ModelStoreException
        {
                try {
                        HibernateUtil.currentSession().delete(domain);
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
        }

        public Collection listAllInetDomains(CollectionInfo info, int rowsPerPage, int pageNumber,
                        Integer[] sortingKeys)
                throws ModelStoreException
        {
                try {
                        if(info != null) {
                                info.init(((Integer)HibernateUtil.currentSession().iterate(
                                        "select count(*) from InetDomain").next()).intValue(),
                                        pageNumber, rowsPerPage);
                        }

                        return HibernateUtil.pageableList(rowsPerPage, pageNumber,
                                "select d from InetDomain d left join fetch d.owner"
                                + HibernateUtil.formOrderClause(sortingKeys, sortKeys), null, null);
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
        }

        public Collection listInetDomains(CollectionInfo info, int rowsPerPage, int pageNumber,
                        Integer[] sortingKeys, User user)
                throws ModelStoreException
        {
                try {
                        if(info != null) {
                                info.init(((Integer)HibernateUtil.currentSession().iterate(
                                        "select count(*) from InetDomain d where d.owner=?",
                                        user, Hibernate.entity(User.class)).next()).intValue(),
                                        pageNumber, rowsPerPage);
                        }

                        return HibernateUtil.pageableList(rowsPerPage, pageNumber,
                                "select d from InetDomain d where d.owner=?"
                                + HibernateUtil.formOrderClause(sortingKeys, sortKeys),
                                new Object[] { user }, new Type[] { Hibernate.entity(User.class) } );
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
        }

        public int countInetDomainsAvailable(User user)
                throws ModelStoreException
        {
                try {
                        return ((Integer)HibernateUtil.currentSession().iterate(
                                "select count(*) from InetDomain where owner=?",
                                user, Hibernate.entity(User.class)).next()).intValue();
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
        }

        public Collection listOwnInetDomains(User user)
                throws ModelStoreException
        {
                try {
                        return HibernateUtil.currentSession().find(
                                "select d from InetDomain d where d.owner = ?",
                                user, Hibernate.entity(User.class) );
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
        }

        protected static Map     sortKeys            = new HashMap();
        private   static boolean sortKeysInitialized = false;

        private static void initSortKeys()
        {
                if(!sortKeysInitialized) {
                        sortKeys.put(InetDomainManager.SORT_NAME, "d.name");
                        sortKeysInitialized = true;
                }
        }

        private static boolean registered = false;
        protected static void register()
                throws ModelStoreException
        {
                synchronized(InetDomainHibernate.class) {
                        if(registered) return;

                        registered = true;
                        try {
                                HibernateUtil.getConfiguration().addResource(
                                        "ak/hostadmiral/core/model/InetDomain.hbm.xml");
                        }
                        catch(Exception ex) {
                                throw new ModelStoreException(ex);
                        }
                }
        }
}