Subversion Repositories general

Rev

Rev 1051 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
924 dev 1
package ak.hostadmiral.core.action;
919 dev 2
 
3
import java.util.List;
1028 dev 4
import java.util.Collection;
919 dev 5
import java.util.Collections;
6
import java.util.ArrayList;
1012 dev 7
import java.util.HashSet;
919 dev 8
 
9
import javax.servlet.http.HttpServletRequest;
10
import javax.servlet.http.HttpServletResponse;
11
 
12
import org.apache.struts.action.Action;
13
import org.apache.struts.action.ActionMapping;
14
import org.apache.struts.action.ActionForm;
15
import org.apache.struts.action.DynaActionForm;
16
import org.apache.struts.action.ActionForward;
17
import org.apache.struts.action.ActionMessages;
18
import org.apache.struts.action.ActionErrors;
19
import org.apache.struts.action.ActionError;
20
 
21
import ak.strutsx.RequestUtilsX;
22
import ak.strutsx.ErrorHandlerX;
23
import ak.backpath.BackPath;
24
 
924 dev 25
import ak.hostadmiral.util.StringConverter;
26
import ak.hostadmiral.util.UserException;
1028 dev 27
import ak.hostadmiral.util.CollectionInfo;
1051 dev 28
import ak.hostadmiral.core.resources.CoreResources;
924 dev 29
import ak.hostadmiral.core.model.User;
30
import ak.hostadmiral.core.model.UserManager;
31
import ak.hostadmiral.core.model.InetDomain;
32
import ak.hostadmiral.core.model.InetDomainManager;
1056 dev 33
import ak.hostadmiral.core.servlet.SessionKeys;
919 dev 34
 
35
public final class InetDomainAction
36
	extends Action
37
	implements ErrorHandlerX
38
{
1028 dev 39
	public static final int PAGE_SIZE = 20;
40
 
919 dev 41
	public void handleErrors(ActionMapping mapping, ActionForm form,
42
			 HttpServletRequest request, HttpServletResponse response)
43
		throws Exception
44
	{
1014 dev 45
		ActionUtils.prepare(request, response);
919 dev 46
		if("submit".equals(mapping.getParameter())) {
1056 dev 47
			User user = (User)request.getSession().getAttribute(SessionKeys.USER);
919 dev 48
			initUserList(request, user);
49
		}
50
	}
51
 
52
	public ActionForward execute(ActionMapping mapping, ActionForm form,
53
			HttpServletRequest request, HttpServletResponse response)
54
		throws Exception
55
	{
1014 dev 56
		ActionUtils.prepare(request, response);
1056 dev 57
		User user = (User)request.getSession().getAttribute(SessionKeys.USER);
919 dev 58
 
59
		if("list".equals(mapping.getParameter())) {
1028 dev 60
			DynaActionForm theForm  = (DynaActionForm)form;
61
			Long           page     = StringConverter.parseLong(theForm.get("pg"));
62
			CollectionInfo listInfo = new CollectionInfo();
63
			Collection     list     = InetDomainManager.getInstance().listInetDomains(
64
				listInfo, PAGE_SIZE, (page == null) ? 0 : page.intValue(),
65
				new Integer[] { InetDomainManager.SORT_NAME }, user);
66
 
67
			request.setAttribute("domains",  list);
68
			request.setAttribute("listInfo", listInfo);
919 dev 69
			request.setAttribute("allowedToCreate",
951 dev 70
				Boolean.valueOf(InetDomainManager.getInstance().allowedToCreate(user)));
919 dev 71
 
72
			return mapping.findForward("default");
73
		}
74
		else if("edit".equals(mapping.getParameter())) {
75
			DynaActionForm theForm  = (DynaActionForm)form;
76
			Long           domainId = StringConverter.parseLong(theForm.get("id"));
923 dev 77
			InetDomain     domain;
919 dev 78
			DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
958 dev 79
				this, request, "InetDomainEditForm");
919 dev 80
 
81
			if(domainId == null) {
923 dev 82
				domain = InetDomainManager.getInstance().create(user);
951 dev 83
				showForm.set("enabled", Boolean.TRUE);
919 dev 84
			}
85
			else {
923 dev 86
				domain = InetDomainManager.getInstance().get(user, domainId);
919 dev 87
            	showForm.set("name", domain.getName());
88
            	if(domain.getOwner() != null)
89
            		showForm.set("owner", StringConverter.toString(domain.getOwner().getId()));
90
				showForm.set("enabled", domain.getEnabled());
91
				showForm.set("comment", domain.getComment());
92
			}
93
 
94
			initUserList(request, user);
923 dev 95
			request.setAttribute("domain", domain);
96
			if(domain.editableBy(user))
97
				return mapping.findForward("default");
98
			else
99
				return mapping.findForward("view");
919 dev 100
		}
1012 dev 101
		else if("deleting".equals(mapping.getParameter())) {
102
			DynaActionForm theForm  = (DynaActionForm)form;
103
			Long           domainId = StringConverter.parseLong(theForm.get("id"));
104
			InetDomain     domain   = InetDomainManager.getInstance().get(user, domainId);
105
 
106
			request.setAttribute("action", "/domain/delete.do");
107
			request.setAttribute("object", domain);
108
			request.setAttribute("cascade",
109
				InetDomainManager.getInstance().beforeDelete(user, domain, new HashSet()));
110
 
111
			return mapping.findForward("default");
112
		}
919 dev 113
		else if("delete".equals(mapping.getParameter())) {
114
			DynaActionForm theForm  = (DynaActionForm)form;
115
			Long           domainId = StringConverter.parseLong(theForm.get("id"));
116
			InetDomain     domain   = InetDomainManager.getInstance().get(user, domainId);
117
 
118
			InetDomainManager.getInstance().delete(user, domain);
119
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
120
			return null;
121
		}
122
		else if("submit".equals(mapping.getParameter())) {
123
			DynaActionForm theForm  = (DynaActionForm)form;
124
			Long           domainId = StringConverter.parseLong(theForm.get("id"));
125
			InetDomain     domain;
126
 
127
			if(domainId == null) {
128
				domain = InetDomainManager.getInstance().create(user);
129
			}
130
			else {
131
				domain = InetDomainManager.getInstance().get(user, domainId);
132
			}
133
 
923 dev 134
            String name = (String)theForm.get("name");
135
            if(InetDomainManager.getInstance().nameExists(user, domain, name)) {
136
				handleErrors(mapping, form, request, response);
137
            	throw new UserException(CoreResources.NONUNIQUE_DOMAIN_NAME);
138
            }
139
            domain.setName(user, name);
140
 
921 dev 141
            domain.setOwner(user, UserManager.getInstance().get(user,
919 dev 142
            	StringConverter.parseLong(theForm.get("owner"))));
143
 
921 dev 144
			domain.setEnabled(user, (Boolean)theForm.get("enabled"));
145
			domain.setComment(user, (String)theForm.get("comment"));
919 dev 146
 
147
			InetDomainManager.getInstance().save(user, domain);
148
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
149
			return null;
150
		}
151
		else {
152
			throw new Exception("unknown mapping parameter");
153
		}
154
	}
155
 
156
	private void initUserList(HttpServletRequest request, User user)
157
		throws Exception
158
	{
159
		List list = new ArrayList(UserManager.getInstance().listUsers(user));
160
		Collections.sort(list, UserManager.LOGIN_COMPARATOR);
161
		request.setAttribute("users", list);
162
	}
163
}