Subversion Repositories general

Rev

Rev 1051 | 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;
1020 dev 4
import java.util.Collection;
919 dev 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.struts.action.Action;
13
import org.apache.struts.action.ActionMapping;
14
import org.apache.struts.action.ActionForm;
15
import org.apache.struts.action.DynaActionForm;
16
import org.apache.struts.action.ActionForward;
17
import org.apache.struts.action.ActionMessages;
18
import org.apache.struts.action.ActionErrors;
19
import org.apache.struts.action.ActionError;
20
 
21
import ak.strutsx.RequestUtilsX;
22
import ak.strutsx.ErrorHandlerX;
23
import ak.backpath.BackPath;
24
 
924 dev 25
import ak.hostadmiral.util.StringConverter;
26
import ak.hostadmiral.util.UserException;
1020 dev 27
import ak.hostadmiral.util.CollectionInfo;
1051 dev 28
import ak.hostadmiral.core.resources.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.SystemUserManager;
34
import ak.hostadmiral.core.model.InetDomainManager;
1056 dev 35
import ak.hostadmiral.core.servlet.SessionKeys;
919 dev 36
 
37
public final class MailboxAction
38
	extends Action
39
	implements ErrorHandlerX
40
{
1020 dev 41
	public static final int PAGE_SIZE = 20;
42
 
919 dev 43
	public void handleErrors(ActionMapping mapping, ActionForm form,
44
			 HttpServletRequest request, HttpServletResponse response)
45
		throws Exception
46
	{
1014 dev 47
		ActionUtils.prepare(request, response);
919 dev 48
		if("submit".equals(mapping.getParameter())) {
1056 dev 49
			User user = (User)request.getSession().getAttribute(SessionKeys.USER);
919 dev 50
			initLists(request, user);
51
		}
52
	}
53
 
54
	public ActionForward execute(ActionMapping mapping, ActionForm form,
55
			HttpServletRequest request, HttpServletResponse response)
56
		throws Exception
57
	{
1014 dev 58
		ActionUtils.prepare(request, response);
1056 dev 59
		User user = (User)request.getSession().getAttribute(SessionKeys.USER);
919 dev 60
		if("list".equals(mapping.getParameter())) {
1020 dev 61
			DynaActionForm theForm  = (DynaActionForm)form;
1021 dev 62
			Long           page     = StringConverter.parseLong(theForm.get("pg"));
1020 dev 63
			CollectionInfo listInfo = new CollectionInfo();
64
			Collection     list     = MailboxManager.getInstance().listMailboxes(
65
				listInfo, PAGE_SIZE, (page == null) ? 0 : page.intValue(),
66
				new Integer[] { MailboxManager.SORT_DOMAIN, MailboxManager.SORT_LOGIN }, user);
67
 
919 dev 68
			request.setAttribute("mailboxes", list);
1020 dev 69
			request.setAttribute("listInfo", listInfo);
919 dev 70
			request.setAttribute("allowedToCreate",
951 dev 71
				Boolean.valueOf(MailboxManager.getInstance().allowedToCreate(user)));
919 dev 72
 
73
			return mapping.findForward("default");
74
		}
75
		else if("edit".equals(mapping.getParameter())) {
76
			DynaActionForm theForm  = (DynaActionForm)form;
77
			Long           boxId    = StringConverter.parseLong(theForm.get("id"));
923 dev 78
			Mailbox        mailbox;
919 dev 79
			DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
958 dev 80
				this, request, "MailboxEditForm");
919 dev 81
 
82
			if(boxId == null) {
923 dev 83
				mailbox = MailboxManager.getInstance().create(user);
951 dev 84
				showForm.set("enabled",    Boolean.TRUE);
85
            	showForm.set("viruscheck", Boolean.TRUE);
86
            	showForm.set("spamcheck",  Boolean.TRUE);
919 dev 87
			}
88
			else {
923 dev 89
				mailbox = MailboxManager.getInstance().get(user, boxId);
919 dev 90
            	showForm.set("login", mailbox.getLogin());
91
            	if(mailbox.getDomain() != null)
92
            		showForm.set("domain", StringConverter.toString(mailbox.getDomain().getId()));
93
            	if(mailbox.getOwner() != null)
94
	            	showForm.set("owner", StringConverter.toString(mailbox.getOwner().getId()));
95
            	showForm.set("viruscheck", mailbox.getVirusCheck());
96
            	showForm.set("spamcheck", mailbox.getSpamCheck());
97
            	if(mailbox.getSystemUser() != null)
98
	            	showForm.set("systemuser", StringConverter.toString(mailbox.getSystemUser().getId()));
99
				showForm.set("enabled", mailbox.getEnabled());
100
				showForm.set("comment", mailbox.getComment());
101
			}
102
 
103
			initLists(request, user);
923 dev 104
			request.setAttribute("mailbox", mailbox);
105
			if(mailbox.editableBy(user))
106
				return mapping.findForward("default");
107
			else
108
				return mapping.findForward("view");
919 dev 109
		}
1012 dev 110
		else if("deleting".equals(mapping.getParameter())) {
111
			DynaActionForm theForm  = (DynaActionForm)form;
112
			Long           boxId    = StringConverter.parseLong(theForm.get("id"));
113
			Mailbox        mailbox  = MailboxManager.getInstance().get(user, boxId);
114
 
115
			request.setAttribute("action", "/mail/box/delete.do");
116
			request.setAttribute("object", mailbox);
117
			request.setAttribute("cascade",
118
				MailboxManager.getInstance().beforeDelete(user, mailbox, new HashSet()));
119
 
120
			return mapping.findForward("default");
121
		}
919 dev 122
		else if("delete".equals(mapping.getParameter())) {
123
			DynaActionForm theForm  = (DynaActionForm)form;
124
			Long           boxId    = StringConverter.parseLong(theForm.get("id"));
125
			Mailbox        mailbox  = MailboxManager.getInstance().get(user, boxId);
126
 
127
			MailboxManager.getInstance().delete(user, mailbox);
128
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
129
			return null;
130
		}
131
		else if("submit".equals(mapping.getParameter())) {
132
			DynaActionForm theForm  = (DynaActionForm)form;
133
			Long           boxId    = StringConverter.parseLong(theForm.get("id"));
134
			Mailbox        mailbox;
135
            String         password = (String)theForm.get("password");
136
 
137
			if(boxId == null) {
923 dev 138
	            if(password == null || password.equals("")) {
139
					handleErrors(mapping, form, request, response);
919 dev 140
	            	throw new UserException(CoreResources.PASSWORD_REQUIRED);
923 dev 141
	            }
919 dev 142
 
143
				mailbox = MailboxManager.getInstance().create(user);
144
 
926 dev 145
				// FIXME: create an user as owner of the new mailbox here,
146
				//        create a mail alias with the same name
919 dev 147
			}
148
			else {
149
				mailbox = MailboxManager.getInstance().get(user, boxId);
150
			}
151
 
921 dev 152
            mailbox.setDomain(user, InetDomainManager.getInstance().get(user,
919 dev 153
            	StringConverter.parseLong(theForm.get("domain"))));
926 dev 154
 
155
			String login = (String)theForm.get("login");
1024 dev 156
            if(MailboxManager.getInstance().loginExists(user, mailbox, login)) {
157
				handleErrors(mapping, form, request, response);
158
            	throw new UserException(CoreResources.NONUNIQUE_MAILBOX_LOGIN);
159
            }
926 dev 160
            mailbox.setLogin(user, login);
161
 
921 dev 162
            mailbox.setOwner(user, UserManager.getInstance().get(user,
919 dev 163
            	StringConverter.parseLong(theForm.get("owner"))));
921 dev 164
            mailbox.setVirusCheck(user, (Boolean)theForm.get("viruscheck"));
165
            mailbox.setSpamCheck(user, (Boolean)theForm.get("spamcheck"));
919 dev 166
 
167
            Long systemUserId = StringConverter.parseLong(theForm.get("systemuser"));
168
			if(systemUserId == null) {
921 dev 169
            	mailbox.setSystemUser(user, null);
919 dev 170
			}
171
			else {
921 dev 172
            	mailbox.setSystemUser(user, SystemUserManager.getInstance().get(user, systemUserId));
919 dev 173
            }
174
 
175
            if(password != null && !password.equals(""))
921 dev 176
            	mailbox.setPassword(user, password);
919 dev 177
 
921 dev 178
			mailbox.setEnabled(user, (Boolean)theForm.get("enabled"));
179
			mailbox.setComment(user, (String)theForm.get("comment"));
919 dev 180
 
181
			MailboxManager.getInstance().save(user, mailbox);
182
			response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
183
			return null;
184
		}
185
		else {
186
			throw new Exception("unknown mapping parameter");
187
		}
188
	}
189
 
190
	private void initLists(HttpServletRequest request, User user)
191
		throws Exception
192
	{
193
		List users = new ArrayList(UserManager.getInstance().listUsers(user));
194
		Collections.sort(users, UserManager.LOGIN_COMPARATOR);
195
		request.setAttribute("users", users);
196
 
197
		List systemUsers = new ArrayList(SystemUserManager.getInstance().listSystemUsers(user));
198
		Collections.sort(systemUsers, SystemUserManager.UID_COMPARATOR);
199
		request.setAttribute("systemusers", systemUsers);
200
 
201
		List domains = new ArrayList(InetDomainManager.getInstance().listInetDomains(user));
202
		Collections.sort(domains, InetDomainManager.NAME_COMPARATOR);
203
		request.setAttribute("domains", domains);
204
	}
205
}