Subversion Repositories general

Rev

Rev 903 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package ak.webcontrol.core.model;

import java.util.*;
import net.sf.hibernate.*;
import ak.webcontrol.util.HibernateUtil;
import ak.webcontrol.util.ModelException;

public class MailAliasDestinationManager
{
        private static boolean registered = false;
        protected static void register()
        {
                synchronized(MailAliasDestinationManager.class) {
                        if(registered) return;

                        registered = true;
                        try {
                                HibernateUtil.getConfiguration().addResource(
                                        "/ak/webcontrol/core/model/MailAliasDestination.hbm.xml");
                        }
                        catch(Exception ex) {
                                ex.printStackTrace();
                                throw new RuntimeException(ex.getMessage());
                        }
                }
        }

        static {
                register();
        }

        private MailAliasDestinationManager()
        {
        }

        public MailAliasDestination create()
        {
                return new MailAliasDestination();
        }

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

        public void save(MailAliasDestination mailAliasDestination)
                throws ModelException
        {
                try {
                        HibernateUtil.currentSession().saveOrUpdate(mailAliasDestination);
                }
                catch(HibernateException ex)
                {
                        throw new ModelException(ex);
                }
        }

        public void delete(MailAliasDestination mailAliasDestination)
                throws ModelException
        {
                try {
                        HibernateUtil.currentSession().delete(mailAliasDestination);
                }
                catch(HibernateException ex)
                {
                        throw new ModelException(ex);
                }
        }

        public Collection listMailAliasesDestination()
        {
                return null;
        }

        public Collection listMailAliasesDestination(MailAlias alias)
                throws ModelException
        {
                try {
                        return HibernateUtil.currentSession().find(
                                "from MailAliasDestination where alias=?",
                                alias, Hibernate.entity(MailAlias.class));
                }
                catch(HibernateException ex)
                {
                        throw new ModelException(ex);
                }
        }

        private static MailAliasDestinationManager mailAliasDestinationManager = null;

        public static MailAliasDestinationManager getInstance()
        {
                if(mailAliasDestinationManager == null)
                        mailAliasDestinationManager = new MailAliasDestinationManager();

                return mailAliasDestinationManager;
        }

        public static final Comparator EMAIL_COMPARATOR = new EmailComparator();

        private static class EmailComparator
                implements Comparator
        {
                public int compare(Object o1, Object o2)
                {
                        if(!(o1 instanceof MailAliasDestination) || !(o2 instanceof MailAliasDestination))
                                throw new ClassCastException("not a MailAliasDestination");

                    MailAliasDestination a1 = (MailAliasDestination)o1;
                    MailAliasDestination a2 = (MailAliasDestination)o2;

                    if(a1 == null && a2 == null)
                        return 0;
                    else if(a1 == null && a2 != null)
                        return -1;
                    else if(a1 != null && a2 == null)
                        return 1;
                    else
                        return a1.getEmail().compareToIgnoreCase(a2.getEmail());
                }

                public boolean equals(Object obj)
                {
                        return (obj instanceof EmailComparator);
                }
        }
}