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())) {
49
			List list = new ArrayList(SystemUserManager.getInstance().listSystemUsers());
50
			Collections.sort(list, SystemUserManager.NAME_COMPARATOR);
51
			request.setAttribute("users", list);
52
 
53
			return mapping.findForward("default");
54
		}
55
		else if("edit".equals(mapping.getParameter())) {
56
			DynaActionForm theForm  = (DynaActionForm)form;
57
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
58
			DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
904 dev 59
				this, request, "ak.hostcaptain.core.form.SystemUserEditForm");
899 dev 60
 
61
			if(userId == null) {
913 dev 62
				showForm.set("enabled", new Boolean(true));
899 dev 63
			}
64
			else {
65
				SystemUser u = SystemUserManager.getInstance().get(userId);
911 dev 66
            	showForm.set("uid",   StringConverter.toString(u.getUid()));
67
            	showForm.set("name",  u.getName());
68
                if(u.getOwner() != null)
69
            	    showForm.set("owner", StringConverter.toString(u.getOwner().getId()));
913 dev 70
				showForm.set("enabled", u.getEnabled());
71
				showForm.set("comment", u.getComment());
899 dev 72
			}
73
 
911 dev 74
			initUserList(request);
899 dev 75
			return mapping.findForward("default");
76
		}
77
		else if("delete".equals(mapping.getParameter())) {
78
			DynaActionForm theForm  = (DynaActionForm)form;
79
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
80
			SystemUser     u        = SystemUserManager.getInstance().get(userId);
81
 
82
			SystemUserManager.getInstance().delete(u);
83
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
84
			return null;
85
		}
86
		else if("submit".equals(mapping.getParameter())) {
87
			DynaActionForm theForm  = (DynaActionForm)form;
88
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
89
			SystemUser     u;
90
 
91
			if(userId == null) {
92
				u = SystemUserManager.getInstance().create();
93
			}
94
			else {
95
				u = SystemUserManager.getInstance().get(userId);
96
			}
97
 
98
            u.setUid(StringConverter.parseInteger(theForm.get("uid")));
99
            u.setName((String)theForm.get("name"));
913 dev 100
 
911 dev 101
            Long ownerId = StringConverter.parseLong(theForm.get("owner"));
102
            if(ownerId == null)
103
                u.setOwner(null);
104
            else
105
                u.setOwner(UserManager.getInstance().get(ownerId));
899 dev 106
 
913 dev 107
			u.setEnabled((Boolean)theForm.get("enabled"));
108
			u.setComment((String)theForm.get("comment"));
109
 
899 dev 110
			SystemUserManager.getInstance().save(u);
111
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
112
			return null;
113
		}
114
		else {
115
			throw new Exception("unknown mapping parameter");
116
		}
117
	}
911 dev 118
 
119
	private void initUserList(HttpServletRequest request)
120
		throws Exception
121
	{
122
		List list = new ArrayList(UserManager.getInstance().listUsers());
123
		Collections.sort(list, UserManager.LOGIN_COMPARATOR);
124
		request.setAttribute("users", list);
125
	}
899 dev 126
}