Subversion Repositories general

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
904 dev 1
package ak.hostcaptain.core.action;
899 dev 2
 
3
import java.util.List;
4
import java.util.Collections;
5
import java.util.ArrayList;
6
 
7
import javax.servlet.http.HttpServletRequest;
8
import javax.servlet.http.HttpServletResponse;
9
 
10
import org.apache.struts.action.Action;
11
import org.apache.struts.action.ActionMapping;
12
import org.apache.struts.action.ActionForm;
13
import org.apache.struts.action.DynaActionForm;
14
import org.apache.struts.action.ActionForward;
15
import org.apache.struts.action.ActionMessages;
16
import org.apache.struts.action.ActionErrors;
17
import org.apache.struts.action.ActionError;
18
 
19
import ak.strutsx.RequestUtilsX;
911 dev 20
import ak.strutsx.ErrorHandlerX;
899 dev 21
import ak.backpath.BackPath;
22
 
904 dev 23
import ak.hostcaptain.util.StringConverter;
24
import ak.hostcaptain.core.model.User;
911 dev 25
import ak.hostcaptain.core.model.UserManager;
904 dev 26
import ak.hostcaptain.core.model.SystemUser;
27
import ak.hostcaptain.core.model.SystemUserManager;
899 dev 28
 
29
public final class SystemUserAction
30
	extends Action
911 dev 31
	implements ErrorHandlerX
899 dev 32
{
911 dev 33
	public void handleErrors(ActionMapping mapping, ActionForm form,
34
			 HttpServletRequest request, HttpServletResponse response)
35
		throws Exception
36
	{
37
		if("submit".equals(mapping.getParameter())) {
38
			initUserList(request);
39
		}
40
	}
41
 
899 dev 42
	public ActionForward execute(ActionMapping mapping, ActionForm form,
43
			HttpServletRequest request, HttpServletResponse response)
44
		throws Exception
45
	{
46
		User user = (User)request.getSession().getAttribute("user");
47
 
48
		if("list".equals(mapping.getParameter())) {
914 dev 49
			List list = new ArrayList(SystemUserManager.getInstance().listSystemUsers(user));
899 dev 50
			Collections.sort(list, SystemUserManager.NAME_COMPARATOR);
51
			request.setAttribute("users", list);
914 dev 52
			request.setAttribute("allowedToCreate",
53
				new Boolean(SystemUserManager.getInstance().allowedToCreate(user)));
899 dev 54
 
55
			return mapping.findForward("default");
56
		}
57
		else if("edit".equals(mapping.getParameter())) {
58
			DynaActionForm theForm  = (DynaActionForm)form;
59
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
60
			DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
904 dev 61
				this, request, "ak.hostcaptain.core.form.SystemUserEditForm");
899 dev 62
 
63
			if(userId == null) {
913 dev 64
				showForm.set("enabled", new Boolean(true));
899 dev 65
			}
66
			else {
914 dev 67
				SystemUser u = SystemUserManager.getInstance().get(user, userId);
911 dev 68
            	showForm.set("uid",   StringConverter.toString(u.getUid()));
69
            	showForm.set("name",  u.getName());
70
                if(u.getOwner() != null)
71
            	    showForm.set("owner", StringConverter.toString(u.getOwner().getId()));
913 dev 72
				showForm.set("enabled", u.getEnabled());
73
				showForm.set("comment", u.getComment());
899 dev 74
			}
75
 
911 dev 76
			initUserList(request);
899 dev 77
			return mapping.findForward("default");
78
		}
79
		else if("delete".equals(mapping.getParameter())) {
80
			DynaActionForm theForm  = (DynaActionForm)form;
81
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
914 dev 82
			SystemUser     u        = SystemUserManager.getInstance().get(user, userId);
899 dev 83
 
914 dev 84
			SystemUserManager.getInstance().delete(user, u);
899 dev 85
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
86
			return null;
87
		}
88
		else if("submit".equals(mapping.getParameter())) {
89
			DynaActionForm theForm  = (DynaActionForm)form;
90
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
91
			SystemUser     u;
92
 
93
			if(userId == null) {
914 dev 94
				u = SystemUserManager.getInstance().create(user);
899 dev 95
			}
96
			else {
914 dev 97
				u = SystemUserManager.getInstance().get(user, userId);
899 dev 98
			}
99
 
100
            u.setUid(StringConverter.parseInteger(theForm.get("uid")));
101
            u.setName((String)theForm.get("name"));
913 dev 102
 
911 dev 103
            Long ownerId = StringConverter.parseLong(theForm.get("owner"));
104
            if(ownerId == null)
105
                u.setOwner(null);
106
            else
107
                u.setOwner(UserManager.getInstance().get(ownerId));
899 dev 108
 
913 dev 109
			u.setEnabled((Boolean)theForm.get("enabled"));
110
			u.setComment((String)theForm.get("comment"));
111
 
914 dev 112
			SystemUserManager.getInstance().save(user, u);
899 dev 113
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
114
			return null;
115
		}
116
		else {
117
			throw new Exception("unknown mapping parameter");
118
		}
119
	}
911 dev 120
 
121
	private void initUserList(HttpServletRequest request)
122
		throws Exception
123
	{
124
		List list = new ArrayList(UserManager.getInstance().listUsers());
125
		Collections.sort(list, UserManager.LOGIN_COMPARATOR);
126
		request.setAttribute("users", list);
127
	}
899 dev 128
}