Subversion Repositories general

Rev

Rev 899 | Rev 904 | 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 InetDomainManager
{
        private static boolean registered = false;
        protected static void register()
        {
                synchronized(InetDomainManager.class) {
                        if(registered) return;

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

        static {
                register();
        }

        private InetDomainManager()
        {
        }

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

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

        public InetDomain findForName(String name)
                throws ModelException
        {
                try {
                        List list = HibernateUtil.currentSession().find(
                                "from InetDomain where name=?", name, Hibernate.STRING);

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

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

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

        public Collection listInetDomains()
                throws ModelException
        {
                try {
                        return HibernateUtil.currentSession().find("from InetDomain");
                }
                catch(HibernateException ex)
                {
                        throw new ModelException(ex);
                }
        }

        private static InetDomainManager inetDomainManager = null;

        public static InetDomainManager getInstance()
        {
                if(inetDomainManager == null)
                        inetDomainManager = new InetDomainManager();

                return inetDomainManager;
        }

        public static final Comparator NAME_COMPARATOR = new NameComparator();

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

                    InetDomain a1 = (InetDomain)o1;
                    InetDomain a2 = (InetDomain)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.getName().compareToIgnoreCase(a2.getName());
                }

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