Subversion Repositories general

Rev

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

package ak.hostadmiral.core.action;

import java.util.List;
import java.util.Iterator;
import java.util.Collections;
import java.util.ArrayList;
import java.util.HashSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;

import ak.strutsx.RequestUtilsX;
import ak.strutsx.ErrorHandlerX;
import ak.backpath.BackPath;

import ak.hostadmiral.util.StringConverter;
import ak.hostadmiral.util.UserException;
import ak.hostadmiral.core.CoreResources;
import ak.hostadmiral.core.model.User;
import ak.hostadmiral.core.model.UserManager;
import ak.hostadmiral.core.model.Mailbox;
import ak.hostadmiral.core.model.MailboxManager;
import ak.hostadmiral.core.model.MailAlias;
import ak.hostadmiral.core.model.MailAliasManager;
import ak.hostadmiral.core.model.MailAliasDestination;
import ak.hostadmiral.core.model.MailAliasDestinationManager;
import ak.hostadmiral.core.model.InetDomainManager;
import ak.hostadmiral.core.form.MailAliasDestBean;

public final class MailAliasAction
        extends Action
        implements ErrorHandlerX
{
        public void handleErrors(ActionMapping mapping, ActionForm form,
                         HttpServletRequest request, HttpServletResponse response)
                throws Exception
        {
                ActionUtils.prepare(request, response);
                if("submit".equals(mapping.getParameter())) {
                        User user = (User)request.getSession().getAttribute("user");
                        initLists(request, user);
                }
        }

        public ActionForward execute(ActionMapping mapping, ActionForm form,
                        HttpServletRequest request, HttpServletResponse response)
                throws Exception
        {
                ActionUtils.prepare(request, response);
                User user = (User)request.getSession().getAttribute("user");
                if("list".equals(mapping.getParameter())) {
                        List list = new ArrayList(MailAliasManager.getInstance().listMailAliases(user));
                        Collections.sort(list, MailAliasManager.ADDRESS_COMPARATOR);
                        request.setAttribute("aliases", list);
                        request.setAttribute("allowedToCreate",
                                Boolean.valueOf(MailAliasManager.getInstance().allowedToCreate(user)));

                        return mapping.findForward("default");
                }
                else if("edit".equals(mapping.getParameter())) {
                        DynaActionForm theForm = (DynaActionForm)form;
                        MailAlias      alias;
                        Long           aliasId = StringConverter.parseLong(theForm.get("id"));
                        List           dests;

                        DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
                                this, request, "MailAliasEditForm");

                        if(aliasId == null) {
                                alias = MailAliasManager.getInstance().create(user);
                                dests = new ArrayList();
                                showForm.set("enabled", Boolean.TRUE);
                        }
                        else {
                                alias = MailAliasManager.getInstance().get(user, aliasId);
                                dests = new ArrayList(MailAliasDestinationManager.getInstance()
                                      .listMailAliasesDestination(alias));
                                MailAliasDestBean[] d = new MailAliasDestBean[dests.size()];

                                // FIXME: sort dests here

                                for(int i = 0; i < dests.size(); i++) {
                                        d[i] = new MailAliasDestBean((MailAliasDestination)dests.get(i));
                                }
                                showForm.set("dests", d);

                                showForm.set("address", alias.getAddress());
                                if(alias.getDomain() != null)
                                        showForm.set("domain", StringConverter.toString(alias.getDomain().getId()));
                                if(alias.getOwner() != null)
                                        showForm.set("owner", StringConverter.toString(alias.getOwner().getId()));
                                showForm.set("enabled", alias.getEnabled());
                                showForm.set("comment", alias.getComment());
                        }

                        initLists(request, user);
                        request.setAttribute("alias", alias);
                        request.setAttribute("dests", dests);
                        if(alias.editableBy(user))
                                return mapping.findForward("default");
                        else if(alias.mayChangeDestinations(user))
                                return mapping.findForward("editdests");
                        else
                                return mapping.findForward("view");
                }
                else if("deleting".equals(mapping.getParameter())) {
                        DynaActionForm theForm = (DynaActionForm)form;
                        Long           aliasId = StringConverter.parseLong(theForm.get("id"));
                        MailAlias      alias   = MailAliasManager.getInstance().get(user, aliasId);

                        request.setAttribute("action", "/mail/alias/delete.do");
                        request.setAttribute("object", alias);
                        request.setAttribute("cascade",
                                MailAliasManager.getInstance().beforeDelete(user, alias, new HashSet()));

                        return mapping.findForward("default");
                }
                else if("delete".equals(mapping.getParameter())) {
                        DynaActionForm theForm = (DynaActionForm)form;
                        Long           aliasId = StringConverter.parseLong(theForm.get("id"));
                        MailAlias      alias   = MailAliasManager.getInstance().get(user, aliasId);

                        MailAliasManager.getInstance().delete(user, alias);
                        response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
                        return null;
                }
                else if("submit".equals(mapping.getParameter())) {
                        DynaActionForm      theForm = (DynaActionForm)form;
                        Long                aliasId = StringConverter.parseLong(theForm.get("id"));
                        MailAlias           alias   = (aliasId == null)
                                                    ? MailAliasManager.getInstance().create(user)
                                                                                : MailAliasManager.getInstance().get(user, aliasId);
                        MailAliasDestBean[] dests   = (MailAliasDestBean[])theForm.get("dests");

                        // submit
                        if(request.getParameter("submit") != null) {
                                // FIXME: if empty element of select box is active, it will be changed
                                //        by submit

                                // validate required fields, because it cannot be done in general case
                                if(StringConverter.isEmpty(theForm.get("address"))) {
                                        handleErrors(mapping, form, request, response);
                                        throw new UserException("ak.hostadmiral.core.mail.alias.edit.address.empty");
                                }
                                if(StringConverter.isEmpty(theForm.get("domain"))) {
                                        handleErrors(mapping, form, request, response);
                                        throw new UserException("ak.hostadmiral.core.mail.alias.edit.domain.wrong");
                                }
                                if(StringConverter.isEmpty(theForm.get("owner"))) {
                                        handleErrors(mapping, form, request, response);
                                        throw new UserException("ak.hostadmiral.core.mail.alias.edit.owner.wrong");
                                }

                                alias.setDomain(user, InetDomainManager.getInstance().get(user,
                                        StringConverter.parseLong(theForm.get("domain"))));

                                String address = (String)theForm.get("address");
                    if(MailAliasManager.getInstance().addressExists(user, alias, address)) {
                                        handleErrors(mapping, form, request, response);
                        throw new UserException(CoreResources.NONUNIQUE_MAIL_ALIAS_ADDRESS);
                    }
                                alias.setAddress(user, address);

                                alias.setOwner(user, UserManager.getInstance().get(user,
                                        StringConverter.parseLong(theForm.get("owner"))));
                                alias.setEnabled(user, (Boolean)theForm.get("enabled"));
                                alias.setComment(user, (String)theForm.get("comment"));

                                alias.getDestinations(user).clear();
                                for(int i = 0; i < dests.length; i++) {
                                        // FIXME: validate dest id, mailbox id, email

                                        // skip empty rows
                                        if((dests[i].getMailbox() == null)
                                                        && (dests[i].getEmail() == null || dests[i].getEmail().equals("")))
                                                continue;

                                        // get bean
                                        Long destId    = StringConverter.parseLong(dests[i].getId());
                                        Long mailboxId = StringConverter.parseLong(dests[i].getMailbox());
                                        MailAliasDestination dest;
                                        if(destId == null)
                                                dest = MailAliasDestinationManager.getInstance().create(user);
                                        else
                                                dest = MailAliasDestinationManager.getInstance().get(user, destId);

                                        // connect
                                        dest.setAlias(user, alias);
                                        alias.getDestinations(user).add(dest);

                                        // set mailbox or email
                                        if(mailboxId != null) {
                                                dest.setMailbox(user, MailboxManager.getInstance().get(user, mailboxId));
                                                dest.setEmail(user, null);
                                        }
                                        else if(dests[i].getEmail() != null && !dests[i].getEmail().equals("")) {
                                                dest.setMailbox(user, null);
                                                dest.setEmail(user, dests[i].getEmail());
                                        }

                                        dest.setEnabled(user, dests[i].getEnabled());
                                        dest.setComment(user, dests[i].getComment());
                                }

                                // update alias
                                MailAliasManager.getInstance().save(user, alias);

                                response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
                                return null;
                        }

                        // add
                        else if(request.getParameter("add") != null) {
                                // FIXME: if called when no entries defined two rows are created

                                MailAliasDestBean[] newDests = new MailAliasDestBean[dests.length+1];
                                if(dests.length > 0)
                                        System.arraycopy(dests, 0, newDests, 0, dests.length);
                                newDests[dests.length] = new MailAliasDestBean();
                                newDests[dests.length].setEnabled(Boolean.TRUE);
                                theForm.set("dests", newDests);

                                initLists(request, user);
                                request.setAttribute("alias", alias);
                                request.setAttribute("dests", newDests);
                                if(alias.editableBy(user))
                                        return mapping.findForward("default");
                                else if(alias.mayChangeDestinations(user))
                                        return mapping.findForward("editdests");
                                else
                                        return mapping.findForward("view");
                        }

                        // delete
                        else {
                                Iterator iter = request.getParameterMap().keySet().iterator();
                                while(iter.hasNext()) {
                                        String name = (String)iter.next();
                                        if(name.startsWith("delete.dests[")) {
                                                int p = name.indexOf("]");
                                                if(p > 0) {
                                                        String index = name.substring("delete.dests[".length(), p);
                                                        try {
                                                                int n = Integer.parseInt(index);
                                                                if(n < 0 || n >= dests.length) break;

                                                                MailAliasDestBean[] newDests;
                                                                newDests = new MailAliasDestBean[dests.length-1];
                                                                if(n > 0)
                                                                        System.arraycopy(dests, 0, newDests, 0, n);
                                                                if(n < dests.length-1)
                                                                        System.arraycopy(dests, n+1, newDests,
                                                                                n, dests.length-n-1);
                                                                theForm.set("dests", newDests);
                                                                request.setAttribute("dests", newDests);
                                                                break;
                                                        }
                                                        catch(NumberFormatException ex) {
                                                        }
                                                }
                                        }
                                }

                                initLists(request, user);
                                request.setAttribute("alias", alias);
                                if(alias.editableBy(user))
                                        return mapping.findForward("default");
                                else if(alias.mayChangeDestinations(user))
                                        return mapping.findForward("editdests");
                                else
                                        return mapping.findForward("view");
                        }
                }
                else {
                        throw new Exception("unknown mapping parameter");
                }
        }

        private void initLists(HttpServletRequest request, User user)
                throws Exception
        {
                // list of mailboxes to redirect to
                List mailboxes = new ArrayList(MailboxManager.getInstance().listMailboxes(user));
                Collections.sort(mailboxes, MailboxManager.LOGIN_COMPARATOR);
                request.setAttribute("mailboxes", mailboxes);

                List users = new ArrayList(UserManager.getInstance().listUsers(user));
                Collections.sort(users, UserManager.LOGIN_COMPARATOR);
                request.setAttribute("users", users);

                List domains = new ArrayList(InetDomainManager.getInstance().listInetDomains(user));
                Collections.sort(domains, InetDomainManager.NAME_COMPARATOR);
                request.setAttribute("domains", domains);
        }
}