Subversion Repositories general

Rev

Rev 1058 | 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.InetDomainStore;

public class InetDomainManager
        implements
                ConfigInit,
                UserBeforeDeleteListener,
                UserDeletingListener
{
        private InetDomainStore store;

        private Collection validateListeners     = new ArrayList();
        private Collection createdListeners      = new ArrayList();
        private Collection modifiedListeners     = new ArrayList();
        private Collection beforeDeleteListeners = new ArrayList();
        private Collection deletingListeners     = new ArrayList();
        private Collection deletedListeners      = new ArrayList();

        public InetDomainManager()
                throws ModelException
        {
                UserManager.getInstance().addBeforeDeleteListener(this);
                UserManager.getInstance().addDeletingListener(this);
        }

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

                return new InetDomain();
        }

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

        public InetDomain get(User editor, Long id)
                throws ModelException
        {
                InetDomain domain = store.get(id);

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

                return domain;
        }

        public boolean nameExists(User editor, InetDomain domain, String name)
                throws ModelException
        {
                return store.nameExists(domain, name);
        }

        public InetDomain findForName(User editor, String name)
                throws ModelException
        {
                InetDomain domain = store.findForName(name);

                if(domain != null && !domain.viewableBy(editor))
                        throw new ModelSecurityException();

                return domain;
        }

        public void save(User editor, InetDomain domain)
                throws ModelException
        {
                // security check
                if(!domain.editableBy(editor))
                        throw new ModelSecurityException();

                //domain.setModUser(editor); // FIXME

        boolean isNew = domain.isNew();
        InetDomain oldDomain = domain.getOrigin();
        if(oldDomain == null) oldDomain = domain;

                // validate
        for(Iterator i = validateListeners.iterator(); i.hasNext(); ) {
                InetDomainValidateListener listener = (InetDomainValidateListener)i.next();
                        listener.inetDomainValidate(editor, domain, oldDomain);
        }

                store.save(domain);

        // inform listeners
        if(isNew) {
                for(Iterator i = createdListeners.iterator(); i.hasNext(); ) {
                        InetDomainCreatedListener listener = (InetDomainCreatedListener)i.next();
                        listener.inetDomainCreated(editor, domain);
                }
        }
        else {
                for(Iterator i = modifiedListeners.iterator(); i.hasNext(); ) {
                        InetDomainModifiedListener listener = (InetDomainModifiedListener)i.next();
                        listener.inetDomainModified(editor, domain, oldDomain);
                }
        }

        // reset backup
        domain.resetOrigin();
        }

    public void addValidateListener(InetDomainValidateListener listener)
    {
        validateListeners.add(listener);
    }

    public void removeValidateListener(InetDomainValidateListener listener)
    {
        validateListeners.remove(listener);
    }

    public void addCreatedListener(InetDomainCreatedListener listener)
    {
        createdListeners.add(listener);
    }

    public void removeCreatedListener(InetDomainCreatedListener listener)
    {
        createdListeners.remove(listener);
    }

    public void addModifiedListener(InetDomainModifiedListener listener)
    {
        modifiedListeners.add(listener);
    }

    public void removeModifiedListener(InetDomainModifiedListener listener)
    {
        modifiedListeners.remove(listener);
    }

    public void addBeforeDeleteListener(InetDomainBeforeDeleteListener listener)
    {
        beforeDeleteListeners.add(listener);
    }

    public void removeBeforeDeleteListener(InetDomainBeforeDeleteListener listener)
    {
        beforeDeleteListeners.remove(listener);
    }

    public void addDeletingListener(InetDomainDeletingListener listener)
    {
        deletingListeners.add(listener);
    }

    public void removeDeletingListener(InetDomainDeletingListener listener)
    {
        deletingListeners.remove(listener);
    }

    public void addDeletedListener(InetDomainDeletedListener listener)
    {
        deletedListeners.add(listener);
    }

    public void removeDeletedListener(InetDomainDeletedListener listener)
    {
        deletedListeners.remove(listener);
    }

    public Collection beforeDelete(User editor, InetDomain domain, Collection known)
                throws ModelException
    {
        Collection cascade = new ArrayList();

        for(Iterator i = beforeDeleteListeners.iterator(); i.hasNext(); ) {
                InetDomainBeforeDeleteListener listener = (InetDomainBeforeDeleteListener)i.next();
                        Collection subcascade = listener.inetDomainBeforeDelete(editor, domain, known);
                if(subcascade != null)
                        cascade.addAll(subcascade);
        }

        return cascade;
    }

        public void delete(User editor, InetDomain domain)
                throws ModelException
        {
            // check rights
                if(!domain.deleteableBy(editor))
                        throw new ModelSecurityException();

        // inform deleting listeners
        for(Iterator i = deletingListeners.iterator(); i.hasNext(); ) {
                InetDomainDeletingListener listener = (InetDomainDeletingListener)i.next();
                        listener.inetDomainDeleting(editor, domain);
        }

        // backup copy
        InetDomain oldDomain = new InetDomain(domain);

        // delete it
                store.delete(domain);

        // inform deleted listeners
        for(Iterator i = deletedListeners.iterator(); i.hasNext(); ) {
                InetDomainDeletedListener listener = (InetDomainDeletedListener)i.next();
                        listener.inetDomainDeleted(editor, oldDomain);
        }
        }

        public Collection listInetDomains(User editor)
                throws ModelException
        {
                return listInetDomains(null, 0, 0, null, editor);
        }

        public Collection listInetDomains(CollectionInfo info, int rowsPerPage, int pageNumber,
                        Integer[] sortingKeys, User editor)
                throws ModelException
        {
                if(editor.isSuperuser())
                        return store.listAllInetDomains(info, rowsPerPage, pageNumber, sortingKeys);
                else
                        return store.listInetDomains(info, rowsPerPage, pageNumber, sortingKeys, editor);
        }

        public boolean areInetDomainsAvailable(User editor)
                throws ModelException
        {
                if(editor.isSuperuser())
                        return true;
                else
                        return store.countInetDomainsAvailable(editor) > 0;
        }

        public Collection userBeforeDelete(User editor, User user, Collection known)
                throws ModelException
        {
        Collection domains = store.listOwnInetDomains(user);

        Collection cascade = new ArrayList();
                for(Iterator i = domains.iterator(); i.hasNext(); ) {
                        InetDomain d = (InetDomain)i.next();
                        if(known.contains(d)) continue;

                        known.add(d);
            if(d.viewableBy(editor)) {
                                if(d.deleteableBy(editor))
                                        cascade.add(new CascadeDeleteElement(d, CascadeDeleteElement.DELETE,
                                                this.beforeDelete(editor, d, known)));
                                else
                                        cascade.add(new CascadeDeleteElement(d, CascadeDeleteElement.FORBIDDEN, null));
                        }
                        else {
                                cascade.add(new CascadeDeleteElement(InetDomain.createLimitedCopy(d),
                                        CascadeDeleteElement.FORBIDDEN, null));
                        }
                }

        return cascade;
        }

        public void userDeleting(User editor, User user)
                throws ModelException
        {
        Collection domains = store.listOwnInetDomains(user);

                for(Iterator i = domains.iterator(); i.hasNext(); ) {
                        delete(editor, (InetDomain)i.next());
                }
        }

        public static final Integer SORT_NAME = new Integer(1);

        public static final Comparator NAME_COMPARATOR = new NameComparator();

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

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

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

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

        private static InetDomainManager inetDomainManager = null;

        public static InetDomainManager getInstance()
        {
                return inetDomainManager;
        }
}