Subversion Repositories general

Rev

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

package ak.hostadmiral.core.model;

import java.util.Iterator;
import java.util.Collection;
import java.util.HashSet;

import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ModelSecurityException;

/**
 *
 * @hibernate.class table="mailboxes"
 */
public class Mailbox
        extends GeneralModelObject
{
        private String     login;
        private Collection passwords; // Collection(PasswordStore)
        private InetDomain domain;
        private User       owner;
        private Boolean    virusCheck;
        private Boolean    spamCheck;
        private SystemUser systemUser;
        private Mailbox    origin;  // save original object state before any changes

        protected Mailbox()
        {
        }

        protected Mailbox(Mailbox origin)
        {
                super(origin);
                this.login      = origin.login;

                if(origin.passwords == null)
                this.passwords = null;
                else
                this.passwords = new HashSet(origin.passwords);

                this.domain     = origin.domain;
                this.owner      = origin.owner;
                this.virusCheck = origin.virusCheck;
                this.spamCheck  = origin.spamCheck;
                this.systemUser = origin.systemUser;
        }

    protected Mailbox getOrigin()
    {
        return origin;
    }

    protected void backupMe()
    {
        if(origin == null)
            origin = new Mailbox(this);
    }

        /**
         *
         * @hibernate.property
         */
        public String getLogin()
        {
                return login;
        }

        protected void setLogin(String login)
        {
                this.login = login;
        }

        public void setLogin(User editor, String login)
                throws ModelException
        {
                if(!editableBy(editor))
                        throw new ModelSecurityException();

        backupMe();
                this.login = login;
        }

        protected void addPasswordStore(PasswordStore ps)
        {
                if(passwords == null) passwords = new HashSet();
                passwords.add(ps);
        }

        public void setPassword(User editor, String password)
                throws ModelException
        {
                if(!editableBy(editor))
                        throw new ModelSecurityException();

                if(password == null)
                        throw new NullPointerException("Null password");

        backupMe();

                for(Iterator i = passwords.iterator(); i.hasNext(); ) {
                        Object o = i.next();
                        if(!(o instanceof PasswordStore))
                                throw new ModelException("It's not a password store");
                        ((PasswordStore)o).setNewPassword(password);
                }
        }

        /**
         *
         * @hibernate.set                    cascade="all" lazy="true"
         * @hibernate.collection-key         column="obj"
         * @hibernate.collection-one-to-many class="ak.hostadmiral.core.model.PasswordStoreAbstract"
         */
        protected Collection getPasswords()
        {
                return passwords;
        }

        protected void setPasswords(Collection passwords)
        {
                this.passwords = passwords;
        }

        /**
         *
         * @hibernate.many-to-one
         */
        public InetDomain getDomain()
        {
                return domain;
        }

        protected void setDomain(InetDomain domain)
        {
                this.domain = domain;
        }

        public void setDomain(User editor, InetDomain domain)
                throws ModelException
        {
                if(!editableBy(editor))
                        throw new ModelSecurityException();

        backupMe();
                this.domain = domain;
        }

        /**
         *
         * @hibernate.many-to-one
         */
        public User getOwner()
        {
                return owner;
        }

        protected void setOwner(User owner)
        {
                this.owner = owner;
        }

        public void setOwner(User editor, User owner)
                throws ModelException
        {
                if(!editableBy(editor))
                        throw new ModelSecurityException();

        backupMe();
                this.owner = owner;
        }

        /**
         *
         * @hibernate.property
         */
        public Boolean getVirusCheck()
        {
                return virusCheck;
        }

        protected void setVirusCheck(Boolean virusCheck)
        {
                this.virusCheck = virusCheck;
        }

        public void setVirusCheck(User editor, Boolean virusCheck)
                throws ModelException
        {
                if(!editableBy(editor))
                        throw new ModelSecurityException();

        backupMe();
                this.virusCheck = virusCheck;
        }

        /**
         *
         * @hibernate.property
         */
        public Boolean getSpamCheck()
        {
                return spamCheck;
        }

        protected void setSpamCheck(Boolean spamCheck)
        {
                this.spamCheck = spamCheck;
        }

        public void setSpamCheck(User editor, Boolean spamCheck)
                throws ModelException
        {
                if(!editableBy(editor))
                        throw new ModelSecurityException();

        backupMe();
                this.spamCheck = spamCheck;
        }

        /**
         *
         * @hibernate.many-to-one
         */
        public SystemUser getSystemUser()
        {
                return systemUser;
        }

        protected void setSystemUser(SystemUser systemUser)
        {
                this.systemUser = systemUser;
        }

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

        backupMe();
                this.systemUser = systemUser;
        }

        public String getTypeKey()
        {
                return ak.hostadmiral.core.CoreResources.TYPE_MAILBOX;
        }

        public String getIdentKey()
        {
                return ak.hostadmiral.core.CoreResources.IDENT_MAILBOX;
        }

        public Object[] getIdentParams()
        {
                return new Object[] { getLogin(), getDomain().getName() };
        }

        public boolean viewableBy(User user)
        {
                return user.isSuperuser() || user.equals(domain.getOwner()) || user.equals(owner);
        }

        public boolean editableBy(User user)
        {
                return user.isSuperuser()
                        || (domain == null) // just created
                        || user.equals(domain.getOwner());
        }

        public boolean deleteableBy(User user)
        {
                return user.isSuperuser() || user.equals(domain.getOwner());
        }

        protected static boolean allowedToCreate(MailboxManager manager, User editor)
                throws ModelException
        {
                return editor.isSuperuser()
                        || InetDomainManager.getInstance().areInetDomainsAvailable(editor);
        }

        protected static Mailbox createLimitedCopy(Mailbox origin)
        {
                Mailbox m = new Mailbox();
                m.setLogin(origin.getLogin());
                m.setDomain(origin.getDomain());
                m.setOwner(origin.getOwner());
                return m;
        }
}