Subversion Repositories general

Rev

Rev 914 | Blame | Last modification | View Log | RSS feed

package ak.hostcaptain.core.action;

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

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.hostcaptain.util.StringConverter;
import ak.hostcaptain.util.UserException;
import ak.hostcaptain.core.model.User;
import ak.hostcaptain.core.model.UserManager;
import ak.hostcaptain.core.model.Mailbox;
import ak.hostcaptain.core.model.MailboxManager;
import ak.hostcaptain.core.model.MailAlias;
import ak.hostcaptain.core.model.MailAliasManager;
import ak.hostcaptain.core.model.MailAliasDestination;
import ak.hostcaptain.core.model.MailAliasDestinationManager;
import ak.hostcaptain.core.model.InetDomainManager;
import ak.hostcaptain.core.form.MailAliasDestBean;

public final class MailAliasAction
        extends Action
        implements ErrorHandlerX
{
        public void handleErrors(ActionMapping mapping, ActionForm form,
                         HttpServletRequest request, HttpServletResponse response)
                throws Exception
        {
                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
        {
                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",
                                new Boolean(MailAliasManager.getInstance().allowedToCreate(user)));

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

                        DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
                                this, request, "ak.hostcaptain.core.form.MailAliasEditForm");

                        if(aliasId == null) {
                                showForm.set("enabled", new Boolean(true));
                        }
                        else {
                                MailAlias alias = MailAliasManager.getInstance().get(user, aliasId);
                                List      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);
                        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) ? null
                                                                                : 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")))
                                        throw new UserException("ak.hostcaptain.core.mail.alias.edit.address.empty");
                                if(StringConverter.isEmpty(theForm.get("domain")))
                                        throw new UserException("ak.hostcaptain.core.mail.alias.edit.domain.wrong");
                                if(StringConverter.isEmpty(theForm.get("owner")))
                                        throw new UserException("ak.hostcaptain.core.mail.alias.edit.owner.wrong");

                                if(alias == null)
                                        alias = MailAliasManager.getInstance().create(user);

                                alias.getDestinations().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);

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

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

                                        // connect
                                        dest.setAlias(alias);
                                        alias.getDestinations().add(dest);
                                }

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

                                // 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(new Boolean(true));
                                theForm.set("dests", newDests);

                                initLists(request, user);
                                return mapping.findForward("back");
                        }

                        // 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);
                                                                break;
                                                        }
                                                        catch(NumberFormatException ex) {
                                                        }
                                                }
                                        }
                                }

                                initLists(request, user);
                                return mapping.findForward("back");
                        }
                }
                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);
        }
}