Rev 915 | Blame | Last modification | View Log | RSS feed
package ak.hostcaptain.core.action;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import ak.strutsx.RequestUtilsX;
import ak.strutsx.ErrorHandlerX;
import ak.backpath.BackPath;
import ak.hostcaptain.util.StringConverter;
import ak.hostcaptain.util.UserException;
import ak.hostcaptain.core.CoreResources;
import ak.hostcaptain.core.model.User;
import ak.hostcaptain.core.model.UserManager;
import ak.hostcaptain.core.model.Mailbox;
import ak.hostcaptain.core.model.MailboxManager;
import ak.hostcaptain.core.model.SystemUserManager;
import ak.hostcaptain.core.model.InetDomainManager;
public final class MailboxAction
extends Action
implements ErrorHandlerX
{
public void handleErrors(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
if("submit".equals(mapping.getParameter())) {
User user = (User)request.getSession().getAttribute("user");
initLists(request, user);
}
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
User user = (User)request.getSession().getAttribute("user");
if("list".equals(mapping.getParameter())) {
List list = new ArrayList(MailboxManager.getInstance().listMailboxes(user));
Collections.sort(list, MailboxManager.LOGIN_COMPARATOR);
request.setAttribute("mailboxes", list);
request.setAttribute("allowedToCreate",
new Boolean(MailboxManager.getInstance().allowedToCreate(user)));
return mapping.findForward("default");
}
else if("edit".equals(mapping.getParameter())) {
DynaActionForm theForm = (DynaActionForm)form;
Long boxId = StringConverter.parseLong(theForm.get("id"));
DynaActionForm showForm = (DynaActionForm)RequestUtilsX.populateActionForm(
this, request, "ak.hostcaptain.core.form.MailboxEditForm");
if(boxId == null) {
showForm.set("enabled", new Boolean(true));
showForm.set("viruscheck", new Boolean(true));
showForm.set("spamcheck", new Boolean(true));
}
else {
Mailbox mailbox = MailboxManager.getInstance().get(user, boxId);
showForm.set("login", mailbox.getLogin());
if(mailbox.getDomain() != null)
showForm.set("domain", StringConverter.toString(mailbox.getDomain().getId()));
if(mailbox.getOwner() != null)
showForm.set("owner", StringConverter.toString(mailbox.getOwner().getId()));
showForm.set("viruscheck", mailbox.getVirusCheck());
showForm.set("spamcheck", mailbox.getSpamCheck());
if(mailbox.getSystemUser() != null)
showForm.set("systemuser", StringConverter.toString(mailbox.getSystemUser().getId()));
showForm.set("enabled", mailbox.getEnabled());
showForm.set("comment", mailbox.getComment());
}
initLists(request, user);
return mapping.findForward("default");
}
else if("delete".equals(mapping.getParameter())) {
DynaActionForm theForm = (DynaActionForm)form;
Long boxId = StringConverter.parseLong(theForm.get("id"));
Mailbox mailbox = MailboxManager.getInstance().get(user, boxId);
MailboxManager.getInstance().delete(user, mailbox);
response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
return null;
}
else if("submit".equals(mapping.getParameter())) {
DynaActionForm theForm = (DynaActionForm)form;
Long boxId = StringConverter.parseLong(theForm.get("id"));
Mailbox mailbox;
String password = (String)theForm.get("password");
if(boxId == null) {
if(password == null || password.equals(""))
throw new UserException(CoreResources.PASSWORD_REQUIRED);
mailbox = MailboxManager.getInstance().create(user);
// FIXME: create an user as owner of the new mailbox here
}
else {
mailbox = MailboxManager.getInstance().get(user, boxId);
}
mailbox.setLogin((String)theForm.get("login"));
mailbox.setDomain(InetDomainManager.getInstance().get(user,
StringConverter.parseLong(theForm.get("domain"))));
mailbox.setOwner(UserManager.getInstance().get(user,
StringConverter.parseLong(theForm.get("owner"))));
mailbox.setVirusCheck((Boolean)theForm.get("viruscheck"));
mailbox.setSpamCheck((Boolean)theForm.get("spamcheck"));
Long systemUserId = StringConverter.parseLong(theForm.get("systemuser"));
if(systemUserId == null) {
mailbox.setSystemUser(null);
}
else {
mailbox.setSystemUser(SystemUserManager.getInstance().get(user, systemUserId));
}
if(password != null && !password.equals(""))
mailbox.setNewPassword(password);
mailbox.setEnabled((Boolean)theForm.get("enabled"));
mailbox.setComment((String)theForm.get("comment"));
MailboxManager.getInstance().save(user, mailbox);
response.sendRedirect(BackPath.findBackPath(request).getBackwardUrl());
return null;
}
else {
throw new Exception("unknown mapping parameter");
}
}
private void initLists(HttpServletRequest request, User user)
throws Exception
{
List users = new ArrayList(UserManager.getInstance().listUsers(user));
Collections.sort(users, UserManager.LOGIN_COMPARATOR);
request.setAttribute("users", users);
List systemUsers = new ArrayList(SystemUserManager.getInstance().listSystemUsers(user));
Collections.sort(systemUsers, SystemUserManager.UID_COMPARATOR);
request.setAttribute("systemusers", systemUsers);
List domains = new ArrayList(InetDomainManager.getInstance().listInetDomains(user));
Collections.sort(domains, InetDomainManager.NAME_COMPARATOR);
request.setAttribute("domains", domains);
}
}