Go to most recent revision | 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; |
||
20 | import ak.strutsx.ErrorHandlerX; |
||
21 | import ak.backpath.BackPath; |
||
22 | |||
904 | dev | 23 | import ak.hostcaptain.util.StringConverter; |
24 | import ak.hostcaptain.core.model.User; |
||
25 | import ak.hostcaptain.core.model.UserManager; |
||
26 | import ak.hostcaptain.core.model.InetDomain; |
||
27 | import ak.hostcaptain.core.model.InetDomainManager; |
||
899 | dev | 28 | |
29 | public final class InetDomainAction |
||
30 | extends Action |
||
31 | implements ErrorHandlerX |
||
32 | { |
||
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 | |||
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(InetDomainManager.getInstance().listInetDomains()); |
||
50 | Collections.sort(list, InetDomainManager.NAME_COMPARATOR); |
||
51 | request.setAttribute("domains", list); |
||
52 | |||
53 | return mapping.findForward("default"); |
||
54 | } |
||
55 | else if("edit".equals(mapping.getParameter())) { |
||
56 | DynaActionForm theForm = (DynaActionForm)form; |
||
57 | Long domainId = StringConverter.parseLong(theForm.get("id")); |
||
58 | DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm( |
||
904 | dev | 59 | this, request, "ak.hostcaptain.core.form.InetDomainEditForm"); |
899 | dev | 60 | |
61 | if(domainId == null) { |
||
62 | |||
63 | } |
||
64 | else { |
||
65 | InetDomain domain = InetDomainManager.getInstance().get(domainId); |
||
66 | showForm.set("name", domain.getName()); |
||
67 | if(domain.getOwner() != null) |
||
68 | showForm.set("owner", StringConverter.toString(domain.getOwner().getId())); |
||
69 | } |
||
70 | |||
71 | initUserList(request); |
||
72 | return mapping.findForward("default"); |
||
73 | } |
||
74 | else if("delete".equals(mapping.getParameter())) { |
||
75 | DynaActionForm theForm = (DynaActionForm)form; |
||
76 | Long domainId = StringConverter.parseLong(theForm.get("id")); |
||
77 | InetDomain domain = InetDomainManager.getInstance().get(domainId); |
||
78 | |||
79 | InetDomainManager.getInstance().delete(domain); |
||
80 | response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl()); |
||
81 | return null; |
||
82 | } |
||
83 | else if("submit".equals(mapping.getParameter())) { |
||
84 | DynaActionForm theForm = (DynaActionForm)form; |
||
85 | Long domainId = StringConverter.parseLong(theForm.get("id")); |
||
86 | InetDomain domain; |
||
87 | |||
88 | if(domainId == null) { |
||
89 | domain = InetDomainManager.getInstance().create(); |
||
90 | } |
||
91 | else { |
||
92 | domain = InetDomainManager.getInstance().get(domainId); |
||
93 | } |
||
94 | |||
95 | domain.setName((String)theForm.get("name")); |
||
96 | domain.setOwner(UserManager.getInstance().get( |
||
97 | StringConverter.parseLong(theForm.get("owner")))); |
||
98 | |||
99 | InetDomainManager.getInstance().save(domain); |
||
100 | response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl()); |
||
101 | return null; |
||
102 | } |
||
103 | else { |
||
104 | throw new Exception("unknown mapping parameter"); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | private void initUserList(HttpServletRequest request) |
||
109 | throws Exception |
||
110 | { |
||
111 | List list = new ArrayList(UserManager.getInstance().listUsers()); |
||
112 | Collections.sort(list, UserManager.LOGIN_COMPARATOR); |
||
113 | request.setAttribute("users", list); |
||
114 | } |
||
115 | } |