Subversion Repositories general

Rev

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