Subversion Repositories general

Rev

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

package ak.webcontrol.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.backpath.BackPath;

import ak.webcontrol.util.StringConverter;
import ak.webcontrol.core.model.User;
import ak.webcontrol.core.model.SystemUser;
import ak.webcontrol.core.model.SystemUserManager;

public final class SystemUserAction
        extends Action
{
        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(SystemUserManager.getInstance().listSystemUsers());
                        Collections.sort(list, SystemUserManager.NAME_COMPARATOR);
                        request.setAttribute("users", list);

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

                        if(userId == null) {

                        }
                        else {
                                SystemUser u = SystemUserManager.getInstance().get(userId);
                showForm.set("uid",  StringConverter.toString(u.getUid()));
                showForm.set("name", u.getName());
                        }

                        return mapping.findForward("default");
                }
                else if("delete".equals(mapping.getParameter())) {
                        DynaActionForm theForm  = (DynaActionForm)form;
                        Long           userId   = StringConverter.parseLong(theForm.get("id"));
                        SystemUser     u        = SystemUserManager.getInstance().get(userId);

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

                        if(userId == null) {
                                u = SystemUserManager.getInstance().create();
                        }
                        else {
                                u = SystemUserManager.getInstance().get(userId);
                        }

            u.setUid(StringConverter.parseInteger(theForm.get("uid")));
            u.setName((String)theForm.get("name"));

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