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