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