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