Subversion Repositories general

Rev

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) {
913 dev 72
				showForm.set("enabled", new Boolean(true));
899 dev 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)
913 dev 89
            		showForm.set("domain", StringConverter.toString(alias.getDomain().getId()));
899 dev 90
            	if(alias.getOwner() != null)
913 dev 91
	            	showForm.set("owner", StringConverter.toString(alias.getOwner().getId()));
92
				showForm.set("enabled", alias.getEnabled());
93
				showForm.set("comment", alias.getComment());
899 dev 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"))));
913 dev 158
				alias.setEnabled((Boolean)theForm.get("enabled"));
159
				alias.setComment((String)theForm.get("comment"));
899 dev 160
 
161
				// update alias
162
				MailAliasManager.getInstance().save(alias);
163
 
164
				response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
165
				return null;
166
			}
167
 
168
			// add
169
			else if(request.getParameter("add") != null) {
170
				MailAliasDestBean[] newDests = new MailAliasDestBean[dests.length+1];
171
				System.arraycopy(dests, 0, newDests, 0, dests.length);
172
				newDests[dests.length] = new MailAliasDestBean();
173
				theForm.set("dests", newDests);
174
 
175
				initLists(request, user);
176
				return mapping.findForward("back");
177
			}
178
 
179
			// delete
180
			else {
181
				Iterator iter = request.getParameterMap().keySet().iterator();
182
				while(iter.hasNext()) {
183
					String name = (String)iter.next();
184
					if(name.startsWith("delete.dests[")) {
185
						int p = name.indexOf("]");
186
						if(p > 0) {
187
							String index = name.substring("delete.dests[".length(), p);
188
							try {
189
								int n = Integer.parseInt(index);
190
								if(n < 0 || n >= dests.length) break;
191
 
192
								MailAliasDestBean[] newDests;
193
								if(dests.length <= 1) {
194
									newDests = new MailAliasDestBean[1];
195
							                newDests[0] = new MailAliasDestBean();
196
								}
197
								else {
198
									newDests = new MailAliasDestBean[dests.length-1];
199
							                if(n > 0)
200
							                	System.arraycopy(dests, 0, newDests, 0, n);
201
							                if(n < dests.length-1)
202
							                	System.arraycopy(dests, n+1, newDests,
203
							                		n, dests.length-n-1);
204
								}
205
								theForm.set("dests", newDests);
206
 
207
								break;
208
							}
209
							catch(NumberFormatException ex) {
210
							}
211
						}
212
					}
213
				}
214
 
215
				initLists(request, user);
216
				return mapping.findForward("back");
217
			}
218
		}
219
		else {
220
			throw new Exception("unknown mapping parameter");
221
		}
222
	}
223
 
224
	private void initLists(HttpServletRequest request, User user)
225
		throws Exception
226
	{
227
        // list of mailboxes to redirect to
228
		List mailboxes = new ArrayList(MailboxManager.getInstance().listMailboxes(user));
229
		Collections.sort(mailboxes, MailboxManager.LOGIN_COMPARATOR);
230
		request.setAttribute("mailboxes", mailboxes);
231
 
232
		List users = new ArrayList(UserManager.getInstance().listUsers());
233
		Collections.sort(users, UserManager.LOGIN_COMPARATOR);
234
		request.setAttribute("users", users);
235
 
236
		List domains = new ArrayList(InetDomainManager.getInstance().listInetDomains());
237
		Collections.sort(domains, InetDomainManager.NAME_COMPARATOR);
238
		request.setAttribute("domains", domains);
239
	}
240
}