Subversion Repositories general

Rev

Rev 1042 | 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.UserLogin;
import ak.hostadmiral.core.model.UserManager;
import ak.hostadmiral.core.model.store.UserStore;

public class UserHibernate
        implements UserStore
{
        public UserHibernate()
                throws ModelStoreException
        {
                initSortKeys();
                register();
        }

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

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

        public User findForLogin(String login)
                throws ModelStoreException
        {
                try {
                        List list = HibernateUtil.currentSession().find(
                                "select u from User u left join fetch u.boss where u.login = ? and u.enabled = ?",
                                new Object[] { login, Boolean.TRUE },
                                new Type[] { Hibernate.STRING, Hibernate.BOOLEAN } );

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

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

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

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

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

        public Collection listUsers(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 User u where u = ? or u.boss = ?",
                                        new Object[] { user, user},
                                        new Type[] { Hibernate.entity(User.class), Hibernate.entity(User.class) }
                                        ).next()).intValue(),
                                        pageNumber, rowsPerPage);
                        }

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

        public void saveUserLogin(UserLogin userLogin)
                throws ModelStoreException
        {
                try {
                        HibernateUtil.currentSession().saveOrUpdate(userLogin);
                }
                catch(HibernateException ex) {
                        throw new ModelStoreException(ex);
                }
        }

        public Collection listFailedLogins()
                throws ModelStoreException
        {
                try {
                        return HibernateUtil.currentSession().find(
                                "select l from UserLogin l left join fetch l.user where l.success = ?",
                                Boolean.FALSE, Hibernate.BOOLEAN);
                }
                catch(HibernateException ex) {
            throw new ModelStoreException(ex);
                }
        }

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

        public Collection listUserLogins(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 UserLogin where usr = ?",
                                        new Object[] { user },
                                        new Type[] { Hibernate.entity(User.class) }
                                        ).next()).intValue(),
                                        pageNumber, rowsPerPage);
                        }

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

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

        private static void initSortKeys()
        {
                if(!sortKeysInitialized) {
                        sortKeys.put(UserManager.SORT_LOGIN, "u.login");
                        sortKeysLogins.put(UserManager.SORT_LOGINS_TIME,         "l.loginTime");
                        sortKeysLogins.put(UserManager.SORT_LOGINS_TIME_REVERSE, "l.loginTime desc");
                        sortKeysInitialized = true;
                }
        }

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

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