Subversion Repositories general

Rev

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

package ak.hostcaptain.core.action;

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

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

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.core.model.User;
import ak.hostcaptain.core.model.UserManager;
import ak.hostcaptain.core.model.InetDomain;
import ak.hostcaptain.core.model.InetDomainManager;

public final class InetDomainAction
        extends Action
        implements ErrorHandlerX
{
        public void handleErrors(ActionMapping mapping, ActionForm form,
                         HttpServletRequest request, HttpServletResponse response)
                throws Exception
        {
                if("submit".equals(mapping.getParameter())) {
                        initUserList(request);
                }
        }

        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(InetDomainManager.getInstance().listInetDomains());
                        Collections.sort(list, InetDomainManager.NAME_COMPARATOR);
                        request.setAttribute("domains", list);

                        return mapping.findForward("default");
                }
                else if("edit".equals(mapping.getParameter())) {
                        DynaActionForm theForm  = (DynaActionForm)form;
                        Long           domainId = StringConverter.parseLong(theForm.get("id"));
                        DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
                                this, request, "ak.hostcaptain.core.form.InetDomainEditForm");

                        if(domainId == null) {

                        }
                        else {
                                InetDomain domain = InetDomainManager.getInstance().get(domainId);
                showForm.set("name", domain.getName());
                if(domain.getOwner() != null)
                        showForm.set("owner", StringConverter.toString(domain.getOwner().getId()));
                        }

                        initUserList(request);
                        return mapping.findForward("default");
                }
                else if("delete".equals(mapping.getParameter())) {
                        DynaActionForm theForm  = (DynaActionForm)form;
                        Long           domainId = StringConverter.parseLong(theForm.get("id"));
                        InetDomain     domain   = InetDomainManager.getInstance().get(domainId);

                        InetDomainManager.getInstance().delete(domain);
                        response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
                        return null;
                }
                else if("submit".equals(mapping.getParameter())) {
                        DynaActionForm theForm  = (DynaActionForm)form;
                        Long           domainId = StringConverter.parseLong(theForm.get("id"));
                        InetDomain     domain;

                        if(domainId == null) {
                                domain = InetDomainManager.getInstance().create();
                        }
                        else {
                                domain = InetDomainManager.getInstance().get(domainId);
                        }

            domain.setName((String)theForm.get("name"));
            domain.setOwner(UserManager.getInstance().get(
                StringConverter.parseLong(theForm.get("owner"))));

                        InetDomainManager.getInstance().save(domain);
                        response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
                        return null;
                }
                else {
                        throw new Exception("unknown mapping parameter");
                }
        }

        private void initUserList(HttpServletRequest request)
                throws Exception
        {
                List list = new ArrayList(UserManager.getInstance().listUsers());
                Collections.sort(list, UserManager.LOGIN_COMPARATOR);
                request.setAttribute("users", list);
        }
}