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.Iterator; |
||
5 | import java.util.Collections; |
||
6 | import java.util.ArrayList; |
||
7 | |||
8 | import javax.servlet.http.HttpServletRequest; |
||
9 | import javax.servlet.http.HttpServletResponse; |
||
10 | |||
11 | import org.apache.commons.beanutils.BeanUtils; |
||
12 | |||
13 | import org.apache.struts.action.Action; |
||
14 | import org.apache.struts.action.ActionMapping; |
||
15 | import org.apache.struts.action.ActionForm; |
||
16 | import org.apache.struts.action.DynaActionForm; |
||
17 | import org.apache.struts.action.ActionForward; |
||
18 | import org.apache.struts.action.ActionMessages; |
||
19 | import org.apache.struts.action.ActionErrors; |
||
20 | import org.apache.struts.action.ActionError; |
||
21 | |||
22 | import ak.strutsx.RequestUtilsX; |
||
23 | import ak.strutsx.ErrorHandlerX; |
||
24 | import ak.backpath.BackPath; |
||
25 | |||
904 | dev | 26 | import ak.hostcaptain.util.StringConverter; |
27 | import ak.hostcaptain.core.model.User; |
||
28 | import ak.hostcaptain.core.model.UserManager; |
||
29 | import ak.hostcaptain.core.model.Mailbox; |
||
30 | import ak.hostcaptain.core.model.MailboxManager; |
||
31 | import ak.hostcaptain.core.model.MailAlias; |
||
32 | import ak.hostcaptain.core.model.MailAliasManager; |
||
33 | import ak.hostcaptain.core.model.MailAliasDestination; |
||
34 | import ak.hostcaptain.core.model.MailAliasDestinationManager; |
||
35 | import ak.hostcaptain.core.model.InetDomainManager; |
||
36 | import ak.hostcaptain.core.form.MailAliasDestBean; |
||
899 | dev | 37 | |
38 | public final class MailAliasAction |
||
39 | extends Action |
||
40 | implements ErrorHandlerX |
||
41 | { |
||
42 | public void handleErrors(ActionMapping mapping, ActionForm form, |
||
43 | HttpServletRequest request, HttpServletResponse response) |
||
44 | throws Exception |
||
45 | { |
||
46 | if("submit".equals(mapping.getParameter())) { |
||
47 | User user = (User)request.getSession().getAttribute("user"); |
||
48 | initLists(request, user); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | public ActionForward execute(ActionMapping mapping, ActionForm form, |
||
53 | HttpServletRequest request, HttpServletResponse response) |
||
54 | throws Exception |
||
55 | { |
||
56 | User user = (User)request.getSession().getAttribute("user"); |
||
57 | if("list".equals(mapping.getParameter())) { |
||
58 | List list = new ArrayList(MailAliasManager.getInstance().listMailAliases()); |
||
59 | Collections.sort(list, MailAliasManager.ADDRESS_COMPARATOR); |
||
60 | request.setAttribute("aliases", list); |
||
61 | |||
62 | return mapping.findForward("default"); |
||
63 | } |
||
64 | else if("edit".equals(mapping.getParameter())) { |
||
65 | DynaActionForm theForm = (DynaActionForm)form; |
||
66 | Long aliasId = StringConverter.parseLong(theForm.get("id")); |
||
67 | |||
68 | DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm( |
||
904 | dev | 69 | this, request, "ak.hostcaptain.core.form.MailAliasEditForm"); |
899 | dev | 70 | |
71 | if(aliasId == null) { |
||
72 | |||
73 | } |
||
74 | else { |
||
75 | MailAlias alias = MailAliasManager.getInstance().get(aliasId); |
||
76 | List dests = new ArrayList(MailAliasDestinationManager.getInstance() |
||
77 | .listMailAliasesDestination(alias)); |
||
78 | MailAliasDestBean[] d = new MailAliasDestBean[dests.size()]; |
||
79 | |||
80 | // FIXME: sort dests here |
||
81 | |||
82 | for(int i = 0; i < dests.size(); i++) { |
||
83 | d[i] = new MailAliasDestBean((MailAliasDestination)dests.get(i)); |
||
84 | } |
||
85 | showForm.set("dests", d); |
||
86 | |||
87 | showForm.set("address", alias.getAddress()); |
||
88 | if(alias.getDomain() != null) |
||
89 | showForm.set("domain", |
||
90 | StringConverter.toString(alias.getDomain().getId())); |
||
91 | if(alias.getOwner() != null) |
||
92 | showForm.set("owner", |
||
93 | StringConverter.toString(alias.getOwner().getId())); |
||
94 | } |
||
95 | |||
96 | initLists(request, user); |
||
97 | return mapping.findForward("default"); |
||
98 | } |
||
99 | else if("delete".equals(mapping.getParameter())) { |
||
100 | DynaActionForm theForm = (DynaActionForm)form; |
||
101 | Long aliasId = StringConverter.parseLong(theForm.get("id")); |
||
102 | MailAlias alias = MailAliasManager.getInstance().get(aliasId); |
||
103 | |||
104 | MailAliasManager.getInstance().delete(alias); |
||
105 | response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl()); |
||
106 | return null; |
||
107 | } |
||
108 | else if("submit".equals(mapping.getParameter())) { |
||
109 | DynaActionForm theForm = (DynaActionForm)form; |
||
110 | Long aliasId = StringConverter.parseLong(theForm.get("id")); |
||
111 | MailAlias alias = (aliasId == null) ? null |
||
112 | : MailAliasManager.getInstance().get(aliasId); |
||
113 | MailAliasDestBean[] dests = (MailAliasDestBean[])theForm.get("dests"); |
||
114 | |||
115 | // submit |
||
116 | if(request.getParameter("submit") != null) { |
||
117 | if(alias == null) |
||
118 | alias = MailAliasManager.getInstance().create(); |
||
119 | |||
120 | alias.getDestinations().clear(); |
||
121 | for(int i = 0; i < dests.length; i++) { |
||
122 | // FIXME: validate dest id, mailbox id, email |
||
123 | |||
124 | // skip empty rows |
||
125 | if((dests[i].getMailbox() == null) |
||
126 | && (dests[i].getEmail() == null || dests[i].getEmail().equals(""))) |
||
127 | continue; |
||
128 | |||
129 | // get bean |
||
130 | Long destId = StringConverter.parseLong(dests[i].getId()); |
||
131 | Long mailboxId = StringConverter.parseLong(dests[i].getMailbox()); |
||
132 | MailAliasDestination dest; |
||
133 | if(destId == null) |
||
134 | dest = MailAliasDestinationManager.getInstance().create(); |
||
135 | else |
||
136 | dest = MailAliasDestinationManager.getInstance().get(destId); |
||
137 | |||
138 | // set mailbox or email |
||
139 | if(mailboxId != null) { |
||
140 | dest.setMailbox(MailboxManager.getInstance().get(mailboxId)); |
||
141 | dest.setEmail(null); |
||
142 | } |
||
143 | else if(dests[i].getEmail() != null && !dests[i].getEmail().equals("")) { |
||
144 | dest.setMailbox(null); |
||
145 | dest.setEmail(dests[i].getEmail()); |
||
146 | } |
||
147 | |||
148 | // connect |
||
149 | dest.setAlias(alias); |
||
150 | alias.getDestinations().add(dest); |
||
151 | } |
||
152 | |||
153 | alias.setAddress((String)theForm.get("address")); |
||
154 | alias.setDomain(InetDomainManager.getInstance().get( |
||
155 | StringConverter.parseLong(theForm.get("domain")))); |
||
156 | alias.setOwner(UserManager.getInstance().get( |
||
157 | StringConverter.parseLong(theForm.get("owner")))); |
||
158 | |||
159 | // update alias |
||
160 | MailAliasManager.getInstance().save(alias); |
||
161 | |||
162 | response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl()); |
||
163 | return null; |
||
164 | } |
||
165 | |||
166 | // add |
||
167 | else if(request.getParameter("add") != null) { |
||
168 | MailAliasDestBean[] newDests = new MailAliasDestBean[dests.length+1]; |
||
169 | System.arraycopy(dests, 0, newDests, 0, dests.length); |
||
170 | newDests[dests.length] = new MailAliasDestBean(); |
||
171 | theForm.set("dests", newDests); |
||
172 | |||
173 | initLists(request, user); |
||
174 | return mapping.findForward("back"); |
||
175 | } |
||
176 | |||
177 | // delete |
||
178 | else { |
||
179 | Iterator iter = request.getParameterMap().keySet().iterator(); |
||
180 | while(iter.hasNext()) { |
||
181 | String name = (String)iter.next(); |
||
182 | if(name.startsWith("delete.dests[")) { |
||
183 | int p = name.indexOf("]"); |
||
184 | if(p > 0) { |
||
185 | String index = name.substring("delete.dests[".length(), p); |
||
186 | try { |
||
187 | int n = Integer.parseInt(index); |
||
188 | if(n < 0 || n >= dests.length) break; |
||
189 | |||
190 | MailAliasDestBean[] newDests; |
||
191 | if(dests.length <= 1) { |
||
192 | newDests = new MailAliasDestBean[1]; |
||
193 | newDests[0] = new MailAliasDestBean(); |
||
194 | } |
||
195 | else { |
||
196 | newDests = new MailAliasDestBean[dests.length-1]; |
||
197 | if(n > 0) |
||
198 | System.arraycopy(dests, 0, newDests, 0, n); |
||
199 | if(n < dests.length-1) |
||
200 | System.arraycopy(dests, n+1, newDests, |
||
201 | n, dests.length-n-1); |
||
202 | } |
||
203 | theForm.set("dests", newDests); |
||
204 | |||
205 | break; |
||
206 | } |
||
207 | catch(NumberFormatException ex) { |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | initLists(request, user); |
||
214 | return mapping.findForward("back"); |
||
215 | } |
||
216 | } |
||
217 | else { |
||
218 | throw new Exception("unknown mapping parameter"); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | private void initLists(HttpServletRequest request, User user) |
||
223 | throws Exception |
||
224 | { |
||
225 | // list of mailboxes to redirect to |
||
226 | List mailboxes = new ArrayList(MailboxManager.getInstance().listMailboxes(user)); |
||
227 | Collections.sort(mailboxes, MailboxManager.LOGIN_COMPARATOR); |
||
228 | request.setAttribute("mailboxes", mailboxes); |
||
229 | |||
230 | List users = new ArrayList(UserManager.getInstance().listUsers()); |
||
231 | Collections.sort(users, UserManager.LOGIN_COMPARATOR); |
||
232 | request.setAttribute("users", users); |
||
233 | |||
234 | List domains = new ArrayList(InetDomainManager.getInstance().listInetDomains()); |
||
235 | Collections.sort(domains, InetDomainManager.NAME_COMPARATOR); |
||
236 | request.setAttribute("domains", domains); |
||
237 | } |
||
238 | } |