Subversion Repositories general

Rev

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