Subversion Repositories general

Rev

Rev 1042 | Go to most recent revision | 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.MailAlias;
import ak.hostadmiral.core.model.MailAliasManager;
import ak.hostadmiral.core.model.store.MailAliasStore;

public class MailAliasHibernate
        implements MailAliasStore
{
        public MailAliasHibernate()
                throws ModelStoreException
        {
                initSortKeys();
                register();
        }

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

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

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

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

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

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

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

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

        public Collection listMailAliases(CollectionInfo info, int rowsPerPage, int pageNumber,
                        Integer[] sortingKeys, User user)
                throws ModelStoreException
        {
                try {
                        if(info != null) {
                                List countlist = HibernateUtil.sqlQuery(
                                        "select count(*) from ("
                                        + " select a.id from mailaliases a"
                                        + "   where a.owner=?"
                                        + " union"
                                        + " select a.id from mailaliases a"
                                        + "   left join domains as d on a.domain = d.id"
                                        + "   where d.owner=?"
                                        + ") as count_table",
                                        new Object[] { user.getId(), user.getId() });

                                info.init(((Long)countlist.get(0)).intValue(),
                                        pageNumber, rowsPerPage);
                        }

                        return HibernateUtil.pageableListSql(rowsPerPage, pageNumber,
                                "(select {a.*}, {d.*}, {o.*}"
                                + "   from      mailaliases as a"
                                + "   left join domains     as d on a.domain = d.id"
                                + "   left join users       as o on a.owner  = o.id"
                                + " where a.owner=?)"
                                + " union "
                                + "(select {a.*}, {d.*}, {o.*}"
                                + "   from      mailaliases as a"
                                + "   left join domains     as d on a.domain = d.id"
                                + "   left join users       as o on a.owner  = o.id"
                                + " where d.owner=?)"
                                + HibernateUtil.formOrderClause(sortingKeys, sortKeysSql),
                                new String[] { "a", "d", "o" },
                                new Class[] { MailAlias.class, InetDomain.class, User.class },
                                new Object[] { user, user },
                                new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) });
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
        }

        public int countMailAliasesAvailable(User user)
                throws ModelStoreException
        {
                try {
                        List countlist = HibernateUtil.sqlQuery(
                                "select count(*) from ("
                                + " select a.id from mailaliases a"
                                + "   where a.owner=?"
                                + " union"
                                + " select a.id from mailaliases a"
                                + "   left join domains as d on a.domain = d.id"
                                + "   where d.owner=?"
                                + ") as count_table",
                                new Object[] { user.getId(), user.getId() });

                        return ((Long)countlist.get(0)).intValue();
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
        }

        public Collection listOwnMailAliases(User user)
                throws ModelStoreException
        {
                try {
                        return HibernateUtil.currentSession().find(
                                "select a from MailAlias a left join fetch a.domain where a.owner = ?",
                                user, Hibernate.entity(User.class) );
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
    }

        public Collection listMailAliasesForDomain(InetDomain domain)
                throws ModelStoreException
        {
                try {
                        return HibernateUtil.currentSession().find(
                                "select a from MailAlias a left join fetch a.owner where a.domain = ?",
                                domain, Hibernate.entity(InetDomain.class) );
                }
                catch(HibernateException ex)
                {
                        throw new ModelStoreException(ex);
                }
    }

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

        private static void initSortKeys()
        {
                if(!sortKeysInitialized) {
                        sortKeys.put(MailAliasManager.SORT_ADDRESS, "a.address");
                        sortKeys.put(MailAliasManager.SORT_DOMAIN,  "d.name");
                        sortKeysSql.put(MailAliasManager.SORT_ADDRESS, "address0_");
                        sortKeysSql.put(MailAliasManager.SORT_DOMAIN,  "name1_");
                        sortKeysInitialized = true;
                }
        }

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

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