Subversion Repositories general

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
919 dev 1
package ak.hostcaptain.core.action;
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;
919 dev 21
import ak.backpath.BackPath;
22
 
23
import ak.hostcaptain.util.StringConverter;
923 dev 24
import ak.hostcaptain.util.UserException;
25
import ak.hostcaptain.core.CoreResources;
919 dev 26
import ak.hostcaptain.core.model.User;
27
import ak.hostcaptain.core.model.UserManager;
28
import ak.hostcaptain.core.model.SystemUser;
29
import ak.hostcaptain.core.model.SystemUserManager;
30
 
31
public final class SystemUserAction
32
	extends Action
911 dev 33
	implements ErrorHandlerX
919 dev 34
{
911 dev 35
	public void handleErrors(ActionMapping mapping, ActionForm form,
36
			 HttpServletRequest request, HttpServletResponse response)
37
		throws Exception
38
	{
39
		if("submit".equals(mapping.getParameter())) {
919 dev 40
			User user = (User)request.getSession().getAttribute("user");
915 dev 41
			initUserList(request, user);
911 dev 42
		}
43
	}
44
 
919 dev 45
	public ActionForward execute(ActionMapping mapping, ActionForm form,
46
			HttpServletRequest request, HttpServletResponse response)
47
		throws Exception
48
	{
49
		User user = (User)request.getSession().getAttribute("user");
50
 
51
		if("list".equals(mapping.getParameter())) {
52
			List list = new ArrayList(SystemUserManager.getInstance().listSystemUsers(user));
53
			Collections.sort(list, SystemUserManager.NAME_COMPARATOR);
54
			request.setAttribute("users", list);
55
			request.setAttribute("allowedToCreate",
56
				new Boolean(SystemUserManager.getInstance().allowedToCreate(user)));
57
 
58
			return mapping.findForward("default");
59
		}
60
		else if("edit".equals(mapping.getParameter())) {
61
			DynaActionForm theForm  = (DynaActionForm)form;
62
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
923 dev 63
			SystemUser     u;
919 dev 64
			DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
65
				this, request, "ak.hostcaptain.core.form.SystemUserEditForm");
66
 
67
			if(userId == null) {
923 dev 68
				u = SystemUserManager.getInstance().create(user);
919 dev 69
				showForm.set("enabled", new Boolean(true));
70
			}
71
			else {
923 dev 72
				u = SystemUserManager.getInstance().get(user, userId);
919 dev 73
            	showForm.set("uid",   StringConverter.toString(u.getUid()));
74
            	showForm.set("name",  u.getName());
911 dev 75
                if(u.getOwner() != null)
919 dev 76
            	    showForm.set("owner", StringConverter.toString(u.getOwner().getId()));
77
				showForm.set("enabled", u.getEnabled());
78
				showForm.set("comment", u.getComment());
79
			}
80
 
915 dev 81
			initUserList(request, user);
923 dev 82
			request.setAttribute("u", u);
83
			if(u.editableBy(user))
84
				return mapping.findForward("default");
85
			else
86
				return mapping.findForward("view");
919 dev 87
		}
88
		else if("delete".equals(mapping.getParameter())) {
89
			DynaActionForm theForm  = (DynaActionForm)form;
90
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
91
			SystemUser     u        = SystemUserManager.getInstance().get(user, userId);
92
 
93
			SystemUserManager.getInstance().delete(user, u);
94
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
95
			return null;
96
		}
97
		else if("submit".equals(mapping.getParameter())) {
98
			DynaActionForm theForm  = (DynaActionForm)form;
99
			Long           userId   = StringConverter.parseLong(theForm.get("id"));
100
			SystemUser     u;
101
 
102
			if(userId == null) {
103
				u = SystemUserManager.getInstance().create(user);
104
			}
105
			else {
106
				u = SystemUserManager.getInstance().get(user, userId);
107
			}
108
 
923 dev 109
            Integer uid = StringConverter.parseInteger(theForm.get("uid"));
110
            if(SystemUserManager.getInstance().uidExists(user, u, uid)) {
111
				handleErrors(mapping, form, request, response);
112
            	throw new UserException(CoreResources.NONUNIQUE_SYSTEM_USER_UID);
113
            }
114
            u.setUid(user, uid);
919 dev 115
 
923 dev 116
            String name = (String)theForm.get("name");
117
            if(SystemUserManager.getInstance().nameExists(user, u, name)) {
118
				handleErrors(mapping, form, request, response);
119
            	throw new UserException(CoreResources.NONUNIQUE_SYSTEM_USER_NAME);
120
            }
121
            u.setName(user, name);
122
 
911 dev 123
            Long ownerId = StringConverter.parseLong(theForm.get("owner"));
124
            if(ownerId == null)
921 dev 125
                u.setOwner(user, null);
911 dev 126
            else
921 dev 127
                u.setOwner(user, UserManager.getInstance().get(user, ownerId));
911 dev 128
 
921 dev 129
			u.setEnabled(user, (Boolean)theForm.get("enabled"));
130
			u.setComment(user, (String)theForm.get("comment"));
919 dev 131
 
132
			SystemUserManager.getInstance().save(user, u);
133
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
134
			return null;
135
		}
136
		else {
137
			throw new Exception("unknown mapping parameter");
138
		}
139
	}
140
 
915 dev 141
	private void initUserList(HttpServletRequest request, User user)
911 dev 142
		throws Exception
143
	{
915 dev 144
		List list = new ArrayList(UserManager.getInstance().listUsers(user));
911 dev 145
		Collections.sort(list, UserManager.LOGIN_COMPARATOR);
146
		request.setAttribute("users", list);
147
	}
919 dev 148
}