Subversion Repositories general

Rev

Rev 1041 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package ak.hostadmiral.core.model;

import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Comparator;

import ak.hostadmiral.util.ConfigInit;
import ak.hostadmiral.util.CollectionInfo;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;
import ak.hostadmiral.core.model.store.MailAliasDestinationStore;

public class MailAliasDestinationManager
        implements
                ConfigInit
{
        // FIXME create, delete and modify listeners are not implemented, bacause
        //       all operations are done via MailAliasManager. Do we need them?

        private MailAliasDestinationStore store;

        public MailAliasDestinationManager()
                throws ModelException
        {
        }

        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 = store.get(id);

                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
        // FIXME: the mod_user is not set when changing a destination as element of collection

                store.save(mailAliasDestination);
        }

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

                store.delete(mailAliasDestination);
        }

        public Collection listMailAliasesDestination(MailAlias alias)
                throws ModelException
        {
                return store.listMailAliasesDestination(alias);
        }

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

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

        public void init(Map params)
                throws ModelException
        {
                try {
                mailAliasDestinationManager = this;

                        Class c = Class.forName(((String[])params.get("store"))[0]);
                        store = (MailAliasDestinationStore)c.newInstance();
        }
        catch(Exception ex) {
                throw new ModelException(ex);
        }
        }

        private static MailAliasDestinationManager mailAliasDestinationManager = null;

        public static MailAliasDestinationManager getInstance()
        {
                return mailAliasDestinationManager;
        }
}