Subversion Repositories general

Rev

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

package ak.hostcaptain.core.model;

import java.util.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.Type;
import ak.hostcaptain.util.HibernateUtil;
import ak.hostcaptain.util.ModelException;
import ak.hostcaptain.util.ModelSecurityException;

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

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

        static {
                register();
        }

        private SystemUserManager()
        {
        }

        public SystemUser create(User editor)
                throws ModelException
        {
                if(!allowedToCreate(editor)) throw new ModelSecurityException();

                return new SystemUser();
        }

        public boolean allowedToCreate(User editor)
                throws ModelException
        {
                return SystemUser.allowedToCreate(this, editor);
        }

        public SystemUser get(User editor, Long id)
                throws ModelException
        {
                SystemUser user;

                try {
                        user = (SystemUser)HibernateUtil.currentSession().load(SystemUser.class, id);
                }
                catch(HibernateException ex)
                {
                        throw new ModelException(ex);
                }

                if(!user.viewableBy(editor))
                        throw new ModelSecurityException();

                return user;
        }

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

        public boolean uidExists(User editor, SystemUser user, Integer uid)
                throws ModelException
        {
                try {
                        if(user.getId() == null)
                                return ((Integer)HibernateUtil.currentSession().iterate(
                                        "select count(*) from SystemUser u where uid = ?",
                                        uid, Hibernate.INTEGER)
                                        .next()).intValue() > 0;
                        else
                                return ((Integer)HibernateUtil.currentSession().iterate(
                                        "select count(*) from SystemUser u where uid = ? and u != ?",
                                        new Object[] { uid, user },
                                        new Type[] { Hibernate.INTEGER, Hibernate.entity(SystemUser.class) } )
                                        .next()).intValue() > 0;
                }
                catch(HibernateException ex)
                {
                        throw new ModelException(ex);
                }
        }

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

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

        protected SystemUser findForUid(Integer uid)
                throws ModelException
        {
                try {
                        List list = HibernateUtil.currentSession().find(
                                "from SystemUser where uid=?", uid, Hibernate.INTEGER);

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

        public void save(User editor, SystemUser systemUser)
                throws ModelException
        {
                if(!systemUser.editableBy(editor))
                        throw new ModelSecurityException();

                systemUser.setModUser(editor);

                try {
                        HibernateUtil.currentSession().saveOrUpdate(systemUser);
                }
                catch(HibernateException ex)
                {
                        throw new ModelException(ex);
                }
        }

        public void delete(User editor, SystemUser systemUser)
                throws ModelException
        {
                if(!systemUser.deleteableBy(editor))
                        throw new ModelSecurityException();

                try {
                        HibernateUtil.currentSession().delete(systemUser);
                }
                catch(HibernateException ex)
                {
                        throw new ModelException(ex);
                }
        }

        public Collection listSystemUsers(User editor)
                throws ModelException
        {
                try {
                        if(editor.isSuperuser()) {
                                return HibernateUtil.currentSession().find("from SystemUser");
                        }
                        else {
                                return HibernateUtil.currentSession().find(
                                        "select u from SystemUser u left join u.owner o where o is null or o=?",
                                        editor, Hibernate.entity(User.class));
                        }
                }
                catch(HibernateException ex)
                {
                        throw new ModelException(ex);
                }
        }

        public boolean areSystemUsersAvailable(User editor)
                throws ModelException
        {
                try {
                        if(editor.isSuperuser())
                                return true;
                        else
                                return ((Integer)HibernateUtil.currentSession().iterate(
                                        "select count(*) from SystemUser u left join u.owner o where o is null or o=?",
                                        editor, Hibernate.entity(User.class)).next()).intValue() > 0;
                }
                catch(HibernateException ex)
                {
                        throw new ModelException(ex);
                }
        }

        private static SystemUserManager systemUserManager = null;

        public static SystemUserManager getInstance()
        {
                if(systemUserManager == null)
                        systemUserManager = new SystemUserManager();

                return systemUserManager;
        }

        public static final Comparator UID_COMPARATOR  = new UidComparator();
        public static final Comparator NAME_COMPARATOR = new NameComparator();

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

                    SystemUser a1 = (SystemUser)o1;
                    SystemUser a2 = (SystemUser)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.getUid().compareTo(a2.getUid());
                }

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

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

                    SystemUser a1 = (SystemUser)o1;
                    SystemUser a2 = (SystemUser)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);
                }
        }
}