Subversion Repositories general

Rev

Rev 1014 | Rev 1051 | 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.Collection;
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.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.util.CollectionInfo;
import ak.hostadmiral.core.CoreResources;
import ak.hostadmiral.core.model.User;
import ak.hostadmiral.core.model.UserManager;
import ak.hostadmiral.core.model.InetDomain;
import ak.hostadmiral.core.model.InetDomainManager;

public final class InetDomainAction
        extends Action
        implements ErrorHandlerX
{
        public static final int PAGE_SIZE = 20;

        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");
                        initUserList(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())) {
                        DynaActionForm theForm  = (DynaActionForm)form;
                        Long           page     = StringConverter.parseLong(theForm.get("pg"));
                        CollectionInfo listInfo = new CollectionInfo();
                        Collection     list     = InetDomainManager.getInstance().listInetDomains(
                                listInfo, PAGE_SIZE, (page == null) ? 0 : page.intValue(),
                                new Integer[] { InetDomainManager.SORT_NAME }, user);

                        request.setAttribute("domains",  list);
                        request.setAttribute("listInfo", listInfo);
                        request.setAttribute("allowedToCreate",
                                Boolean.valueOf(InetDomainManager.getInstance().allowedToCreate(user)));

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

                        if(domainId == null) {
                                domain = InetDomainManager.getInstance().create(user);
                                showForm.set("enabled", Boolean.TRUE);
                        }
                        else {
                                domain = InetDomainManager.getInstance().get(user, domainId);
                showForm.set("name", domain.getName());
                if(domain.getOwner() != null)
                        showForm.set("owner", StringConverter.toString(domain.getOwner().getId()));
                                showForm.set("enabled", domain.getEnabled());
                                showForm.set("comment", domain.getComment());
                        }

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

                        request.setAttribute("action", "/domain/delete.do");
                        request.setAttribute("object", domain);
                        request.setAttribute("cascade",
                                InetDomainManager.getInstance().beforeDelete(user, domain, new HashSet()));

                        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(user, domainId);

                        InetDomainManager.getInstance().delete(user, 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(user);
                        }
                        else {
                                domain = InetDomainManager.getInstance().get(user, domainId);
                        }

            String name = (String)theForm.get("name");
            if(InetDomainManager.getInstance().nameExists(user, domain, name)) {
                                handleErrors(mapping, form, request, response);
                throw new UserException(CoreResources.NONUNIQUE_DOMAIN_NAME);
            }
            domain.setName(user, name);

            domain.setOwner(user, UserManager.getInstance().get(user,
                StringConverter.parseLong(theForm.get("owner"))));

                        domain.setEnabled(user, (Boolean)theForm.get("enabled"));
                        domain.setComment(user, (String)theForm.get("comment"));

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

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