Subversion Repositories general

Rev

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

package ak.hostadmiral.core.model;

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

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/hostadmiral/core/model/MailAliasDestination.hbm.xml");
                        }
                        catch(Exception ex) {
                                ex.printStackTrace();
                                throw new RuntimeException(ex.getMessage());
                        }
                }
        }

        static {
                register();
        }

        private MailAliasDestinationManager()
        {
        }

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

                return new MailAliasDestination();
        }

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

        public MailAliasDestination get(User editor, Long id)
                throws ModelException
        {
                MailAliasDestination dest;

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

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

                return dest;
        }

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

                mailAliasDestination.setModUser(editor);
        // FIXME: the mod_user is not set when changing a destination as element of collection

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

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

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

        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);
                }
        }

        public boolean areMailAliasesDestinationsAvailable(User editor)
                throws ModelException
        {
                return true;
        }

        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);
                }
        }
}