Rev 1012 | Rev 1024 | 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; |
||
4 | import java.util.Iterator; |
||
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.commons.beanutils.BeanUtils; |
||
13 | |||
14 | import org.apache.struts.action.Action; |
||
15 | import org.apache.struts.action.ActionMapping; |
||
16 | import org.apache.struts.action.ActionForm; |
||
17 | import org.apache.struts.action.DynaActionForm; |
||
18 | import org.apache.struts.action.ActionForward; |
||
19 | import org.apache.struts.action.ActionMessages; |
||
20 | import org.apache.struts.action.ActionErrors; |
||
21 | import org.apache.struts.action.ActionError; |
||
22 | |||
23 | import ak.strutsx.RequestUtilsX; |
||
24 | import ak.strutsx.ErrorHandlerX; |
||
25 | import ak.backpath.BackPath; |
||
26 | |||
924 | dev | 27 | import ak.hostadmiral.util.StringConverter; |
28 | import ak.hostadmiral.util.UserException; |
||
926 | dev | 29 | import ak.hostadmiral.core.CoreResources; |
924 | dev | 30 | import ak.hostadmiral.core.model.User; |
31 | import ak.hostadmiral.core.model.UserManager; |
||
32 | import ak.hostadmiral.core.model.Mailbox; |
||
33 | import ak.hostadmiral.core.model.MailboxManager; |
||
34 | import ak.hostadmiral.core.model.MailAlias; |
||
35 | import ak.hostadmiral.core.model.MailAliasManager; |
||
36 | import ak.hostadmiral.core.model.MailAliasDestination; |
||
37 | import ak.hostadmiral.core.model.MailAliasDestinationManager; |
||
38 | import ak.hostadmiral.core.model.InetDomainManager; |
||
39 | import ak.hostadmiral.core.form.MailAliasDestBean; |
||
919 | dev | 40 | |
41 | public final class MailAliasAction |
||
42 | extends Action |
||
43 | implements ErrorHandlerX |
||
44 | { |
||
45 | public void handleErrors(ActionMapping mapping, ActionForm form, |
||
46 | HttpServletRequest request, HttpServletResponse response) |
||
47 | throws Exception |
||
48 | { |
||
1014 | dev | 49 | ActionUtils.prepare(request, response); |
919 | dev | 50 | if("submit".equals(mapping.getParameter())) { |
51 | User user = (User)request.getSession().getAttribute("user"); |
||
52 | initLists(request, user); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | public ActionForward execute(ActionMapping mapping, ActionForm form, |
||
57 | HttpServletRequest request, HttpServletResponse response) |
||
58 | throws Exception |
||
59 | { |
||
1014 | dev | 60 | ActionUtils.prepare(request, response); |
919 | dev | 61 | User user = (User)request.getSession().getAttribute("user"); |
62 | if("list".equals(mapping.getParameter())) { |
||
63 | List list = new ArrayList(MailAliasManager.getInstance().listMailAliases(user)); |
||
64 | Collections.sort(list, MailAliasManager.ADDRESS_COMPARATOR); |
||
65 | request.setAttribute("aliases", list); |
||
66 | request.setAttribute("allowedToCreate", |
||
951 | dev | 67 | Boolean.valueOf(MailAliasManager.getInstance().allowedToCreate(user))); |
919 | dev | 68 | |
69 | return mapping.findForward("default"); |
||
70 | } |
||
71 | else if("edit".equals(mapping.getParameter())) { |
||
72 | DynaActionForm theForm = (DynaActionForm)form; |
||
923 | dev | 73 | MailAlias alias; |
919 | dev | 74 | Long aliasId = StringConverter.parseLong(theForm.get("id")); |
923 | dev | 75 | List dests; |
919 | dev | 76 | |
77 | DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm( |
||
958 | dev | 78 | this, request, "MailAliasEditForm"); |
919 | dev | 79 | |
80 | if(aliasId == null) { |
||
923 | dev | 81 | alias = MailAliasManager.getInstance().create(user); |
82 | dests = new ArrayList(); |
||
951 | dev | 83 | showForm.set("enabled", Boolean.TRUE); |
919 | dev | 84 | } |
85 | else { |
||
923 | dev | 86 | alias = MailAliasManager.getInstance().get(user, aliasId); |
87 | dests = new ArrayList(MailAliasDestinationManager.getInstance() |
||
88 | .listMailAliasesDestination(alias)); |
||
919 | dev | 89 | MailAliasDestBean[] d = new MailAliasDestBean[dests.size()]; |
90 | |||
91 | // FIXME: sort dests here |
||
92 | |||
93 | for(int i = 0; i < dests.size(); i++) { |
||
94 | d[i] = new MailAliasDestBean((MailAliasDestination)dests.get(i)); |
||
95 | } |
||
96 | showForm.set("dests", d); |
||
97 | |||
98 | showForm.set("address", alias.getAddress()); |
||
99 | if(alias.getDomain() != null) |
||
100 | showForm.set("domain", StringConverter.toString(alias.getDomain().getId())); |
||
101 | if(alias.getOwner() != null) |
||
102 | showForm.set("owner", StringConverter.toString(alias.getOwner().getId())); |
||
103 | showForm.set("enabled", alias.getEnabled()); |
||
104 | showForm.set("comment", alias.getComment()); |
||
105 | } |
||
106 | |||
107 | initLists(request, user); |
||
923 | dev | 108 | request.setAttribute("alias", alias); |
109 | request.setAttribute("dests", dests); |
||
110 | if(alias.editableBy(user)) |
||
111 | return mapping.findForward("default"); |
||
112 | else if(alias.mayChangeDestinations(user)) |
||
113 | return mapping.findForward("editdests"); |
||
114 | else |
||
115 | return mapping.findForward("view"); |
||
919 | dev | 116 | } |
1012 | dev | 117 | else if("deleting".equals(mapping.getParameter())) { |
118 | DynaActionForm theForm = (DynaActionForm)form; |
||
119 | Long aliasId = StringConverter.parseLong(theForm.get("id")); |
||
120 | MailAlias alias = MailAliasManager.getInstance().get(user, aliasId); |
||
121 | |||
122 | request.setAttribute("action", "/mail/alias/delete.do"); |
||
123 | request.setAttribute("object", alias); |
||
124 | request.setAttribute("cascade", |
||
125 | MailAliasManager.getInstance().beforeDelete(user, alias, new HashSet())); |
||
126 | |||
127 | return mapping.findForward("default"); |
||
128 | } |
||
919 | dev | 129 | else if("delete".equals(mapping.getParameter())) { |
130 | DynaActionForm theForm = (DynaActionForm)form; |
||
131 | Long aliasId = StringConverter.parseLong(theForm.get("id")); |
||
132 | MailAlias alias = MailAliasManager.getInstance().get(user, aliasId); |
||
133 | |||
134 | MailAliasManager.getInstance().delete(user, alias); |
||
135 | response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl()); |
||
136 | return null; |
||
137 | } |
||
138 | else if("submit".equals(mapping.getParameter())) { |
||
139 | DynaActionForm theForm = (DynaActionForm)form; |
||
140 | Long aliasId = StringConverter.parseLong(theForm.get("id")); |
||
923 | dev | 141 | MailAlias alias = (aliasId == null) |
142 | ? MailAliasManager.getInstance().create(user) |
||
919 | dev | 143 | : MailAliasManager.getInstance().get(user, aliasId); |
144 | MailAliasDestBean[] dests = (MailAliasDestBean[])theForm.get("dests"); |
||
145 | |||
146 | // submit |
||
147 | if(request.getParameter("submit") != null) { |
||
148 | // FIXME: if empty element of select box is active, it will be changed |
||
149 | // by submit |
||
150 | |||
151 | // validate required fields, because it cannot be done in general case |
||
923 | dev | 152 | if(StringConverter.isEmpty(theForm.get("address"))) { |
153 | handleErrors(mapping, form, request, response); |
||
924 | dev | 154 | throw new UserException("ak.hostadmiral.core.mail.alias.edit.address.empty"); |
923 | dev | 155 | } |
156 | if(StringConverter.isEmpty(theForm.get("domain"))) { |
||
157 | handleErrors(mapping, form, request, response); |
||
924 | dev | 158 | throw new UserException("ak.hostadmiral.core.mail.alias.edit.domain.wrong"); |
923 | dev | 159 | } |
160 | if(StringConverter.isEmpty(theForm.get("owner"))) { |
||
161 | handleErrors(mapping, form, request, response); |
||
924 | dev | 162 | throw new UserException("ak.hostadmiral.core.mail.alias.edit.owner.wrong"); |
923 | dev | 163 | } |
919 | dev | 164 | |
1010 | dev | 165 | alias.setDomain(user, InetDomainManager.getInstance().get(user, |
166 | StringConverter.parseLong(theForm.get("domain")))); |
||
167 | |||
168 | String address = (String)theForm.get("address"); |
||
169 | if(MailAliasManager.getInstance().addressExists(user, alias, address)) { |
||
170 | handleErrors(mapping, form, request, response); |
||
171 | throw new UserException(CoreResources.NONUNIQUE_MAIL_ALIAS_ADDRESS); |
||
172 | } |
||
173 | alias.setAddress(user, address); |
||
174 | |||
175 | alias.setOwner(user, UserManager.getInstance().get(user, |
||
176 | StringConverter.parseLong(theForm.get("owner")))); |
||
177 | alias.setEnabled(user, (Boolean)theForm.get("enabled")); |
||
178 | alias.setComment(user, (String)theForm.get("comment")); |
||
179 | |||
921 | dev | 180 | alias.getDestinations(user).clear(); |
919 | dev | 181 | for(int i = 0; i < dests.length; i++) { |
182 | // FIXME: validate dest id, mailbox id, email |
||
183 | |||
184 | // skip empty rows |
||
185 | if((dests[i].getMailbox() == null) |
||
186 | && (dests[i].getEmail() == null || dests[i].getEmail().equals(""))) |
||
187 | continue; |
||
188 | |||
189 | // get bean |
||
190 | Long destId = StringConverter.parseLong(dests[i].getId()); |
||
191 | Long mailboxId = StringConverter.parseLong(dests[i].getMailbox()); |
||
192 | MailAliasDestination dest; |
||
193 | if(destId == null) |
||
194 | dest = MailAliasDestinationManager.getInstance().create(user); |
||
195 | else |
||
196 | dest = MailAliasDestinationManager.getInstance().get(user, destId); |
||
197 | |||
923 | dev | 198 | // connect |
199 | dest.setAlias(user, alias); |
||
200 | alias.getDestinations(user).add(dest); |
||
201 | |||
919 | dev | 202 | // set mailbox or email |
203 | if(mailboxId != null) { |
||
922 | dev | 204 | dest.setMailbox(user, MailboxManager.getInstance().get(user, mailboxId)); |
205 | dest.setEmail(user, null); |
||
919 | dev | 206 | } |
207 | else if(dests[i].getEmail() != null && !dests[i].getEmail().equals("")) { |
||
922 | dev | 208 | dest.setMailbox(user, null); |
209 | dest.setEmail(user, dests[i].getEmail()); |
||
919 | dev | 210 | } |
211 | |||
921 | dev | 212 | dest.setEnabled(user, dests[i].getEnabled()); |
213 | dest.setComment(user, dests[i].getComment()); |
||
919 | dev | 214 | } |
215 | |||
216 | // update alias |
||
217 | MailAliasManager.getInstance().save(user, alias); |
||
218 | |||
219 | response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl()); |
||
220 | return null; |
||
221 | } |
||
222 | |||
223 | // add |
||
224 | else if(request.getParameter("add") != null) { |
||
225 | // FIXME: if called when no entries defined two rows are created |
||
226 | |||
227 | MailAliasDestBean[] newDests = new MailAliasDestBean[dests.length+1]; |
||
228 | if(dests.length > 0) |
||
229 | System.arraycopy(dests, 0, newDests, 0, dests.length); |
||
230 | newDests[dests.length] = new MailAliasDestBean(); |
||
951 | dev | 231 | newDests[dests.length].setEnabled(Boolean.TRUE); |
919 | dev | 232 | theForm.set("dests", newDests); |
233 | |||
234 | initLists(request, user); |
||
923 | dev | 235 | request.setAttribute("alias", alias); |
236 | request.setAttribute("dests", newDests); |
||
237 | if(alias.editableBy(user)) |
||
238 | return mapping.findForward("default"); |
||
239 | else if(alias.mayChangeDestinations(user)) |
||
240 | return mapping.findForward("editdests"); |
||
241 | else |
||
242 | return mapping.findForward("view"); |
||
919 | dev | 243 | } |
244 | |||
245 | // delete |
||
246 | else { |
||
247 | Iterator iter = request.getParameterMap().keySet().iterator(); |
||
248 | while(iter.hasNext()) { |
||
249 | String name = (String)iter.next(); |
||
250 | if(name.startsWith("delete.dests[")) { |
||
251 | int p = name.indexOf("]"); |
||
252 | if(p > 0) { |
||
253 | String index = name.substring("delete.dests[".length(), p); |
||
254 | try { |
||
255 | int n = Integer.parseInt(index); |
||
256 | if(n < 0 || n >= dests.length) break; |
||
257 | |||
258 | MailAliasDestBean[] newDests; |
||
259 | newDests = new MailAliasDestBean[dests.length-1]; |
||
260 | if(n > 0) |
||
261 | System.arraycopy(dests, 0, newDests, 0, n); |
||
262 | if(n < dests.length-1) |
||
263 | System.arraycopy(dests, n+1, newDests, |
||
264 | n, dests.length-n-1); |
||
265 | theForm.set("dests", newDests); |
||
923 | dev | 266 | request.setAttribute("dests", newDests); |
919 | dev | 267 | break; |
268 | } |
||
269 | catch(NumberFormatException ex) { |
||
270 | } |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | |||
275 | initLists(request, user); |
||
923 | dev | 276 | request.setAttribute("alias", alias); |
277 | if(alias.editableBy(user)) |
||
278 | return mapping.findForward("default"); |
||
279 | else if(alias.mayChangeDestinations(user)) |
||
280 | return mapping.findForward("editdests"); |
||
281 | else |
||
282 | return mapping.findForward("view"); |
||
919 | dev | 283 | } |
284 | } |
||
285 | else { |
||
286 | throw new Exception("unknown mapping parameter"); |
||
287 | } |
||
288 | } |
||
289 | |||
290 | private void initLists(HttpServletRequest request, User user) |
||
291 | throws Exception |
||
292 | { |
||
293 | // list of mailboxes to redirect to |
||
294 | List mailboxes = new ArrayList(MailboxManager.getInstance().listMailboxes(user)); |
||
295 | Collections.sort(mailboxes, MailboxManager.LOGIN_COMPARATOR); |
||
296 | request.setAttribute("mailboxes", mailboxes); |
||
297 | |||
298 | List users = new ArrayList(UserManager.getInstance().listUsers(user)); |
||
299 | Collections.sort(users, UserManager.LOGIN_COMPARATOR); |
||
300 | request.setAttribute("users", users); |
||
301 | |||
302 | List domains = new ArrayList(InetDomainManager.getInstance().listInetDomains(user)); |
||
303 | Collections.sort(domains, InetDomainManager.NAME_COMPARATOR); |
||
304 | request.setAttribute("domains", domains); |
||
305 | } |
||
306 | } |