From ad60dc6908f36a01dd14b0b8fdf49203696c675e Mon Sep 17 00:00:00 2001 From: j2blake Date: Thu, 16 Jun 2011 17:25:35 +0000 Subject: [PATCH] An add, delete or update will redirect to the list page when complete, so a browser "refresh" will not try to repeat the operation. --- .../admin/UserAccountsAdminController.java | 23 ++- .../accounts/admin/UserAccountsListPage.java | 137 +++++++++++------- 2 files changed, 102 insertions(+), 58 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsAdminController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsAdminController.java index b17a01c32..f21d4c340 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsAdminController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsAdminController.java @@ -57,9 +57,9 @@ public class UserAccountsAdminController extends FreemarkerHttpServlet { if (page.isSubmit() && page.isValid()) { page.createNewAccount(); - UserAccountsListPage listPage = new UserAccountsListPage(vreq); - return listPage.showPageWithNewAccount(page.getAddedAccount(), - page.wasPasswordEmailSent()); + UserAccountsListPage.Message.showNewAccount(vreq, + page.getAddedAccount(), page.wasPasswordEmailSent()); + return redirectToList(); } else { return page.showPage(); } @@ -71,9 +71,10 @@ public class UserAccountsAdminController extends FreemarkerHttpServlet { return showHomePage(vreq, page.getBogusMessage()); } else if (page.isSubmit() && page.isValid()) { page.updateAccount(); - UserAccountsListPage listPage = new UserAccountsListPage(vreq); - return listPage.showPageWithUpdatedAccount( + + UserAccountsListPage.Message.showUpdatedAccount(vreq, page.getUpdatedAccount(), page.wasPasswordEmailSent()); + return redirectToList(); } else { return page.showPage(); } @@ -83,8 +84,8 @@ public class UserAccountsAdminController extends FreemarkerHttpServlet { UserAccountsDeleter deleter = new UserAccountsDeleter(vreq); Collection deletedUris = deleter.delete(); - return new UserAccountsListPage(vreq) - .showPageWithDeletions(deletedUris); + UserAccountsListPage.Message.showDeletions(vreq, deletedUris); + return redirectToList(); } private ResponseValues handleListRequest(VitroRequest vreq) { @@ -92,6 +93,14 @@ public class UserAccountsAdminController extends FreemarkerHttpServlet { return page.showPage(); } + /** + * After an successful change, redirect to the list instead of forwarding. + * That way, a browser "refresh" won't try to repeat the operation. + */ + private ResponseValues redirectToList() { + return new RedirectResponseValues("/accountsAdmin/list"); + } + private ResponseValues showHomePage(VitroRequest vreq, String message) { DisplayMessage.setMessage(vreq, message); return new RedirectResponseValues("/"); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsListPage.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsListPage.java index dd2dd427b..3da82e412 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsListPage.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/admin/UserAccountsListPage.java @@ -11,6 +11,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -19,12 +22,12 @@ import edu.cornell.mannlib.vitro.webapp.beans.UserAccount; import edu.cornell.mannlib.vitro.webapp.beans.UserAccount.Status; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsOrdering; +import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsOrdering.Direction; +import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsOrdering.Field; import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsPage; import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsSelection; import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsSelectionCriteria; import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsSelector; -import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsOrdering.Direction; -import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsOrdering.Field; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues; @@ -91,55 +94,7 @@ public class UserAccountsListPage extends UserAccountsPage { UserAccountsSelection selection = UserAccountsSelector.select( userAccountsModel, criteria); Map body = buildTemplateBodyMap(selection); - return new TemplateResponseValues(TEMPLATE_NAME, body); - } - - /** - * We just came from adding a new account. Show the list with a message. - */ - public ResponseValues showPageWithNewAccount(UserAccount userAccount, - boolean emailWasSent) { - UserAccountsSelection selection = UserAccountsSelector.select( - userAccountsModel, criteria); - Map body = buildTemplateBodyMap(selection); - - body.put("newUserAccount", new UserAccountWrapper(userAccount, - Collections. emptyList())); - if (emailWasSent) { - body.put("emailWasSent", Boolean.TRUE); - } - - return new TemplateResponseValues(TEMPLATE_NAME, body); - } - - /** - * We just came from editing an account. Show the list with a message. - */ - public ResponseValues showPageWithUpdatedAccount(UserAccount userAccount, - boolean emailWasSent) { - UserAccountsSelection selection = UserAccountsSelector.select( - userAccountsModel, criteria); - Map body = buildTemplateBodyMap(selection); - - body.put("updatedUserAccount", new UserAccountWrapper(userAccount, - Collections. emptyList())); - if (emailWasSent) { - body.put("emailWasSent", Boolean.TRUE); - } - - return new TemplateResponseValues(TEMPLATE_NAME, body); - } - - /** - * We just came from deleting accounts. Show the list with a message. - */ - public ResponseValues showPageWithDeletions(Collection deletedUris) { - UserAccountsSelection selection = UserAccountsSelector.select( - userAccountsModel, criteria); - Map body = buildTemplateBodyMap(selection); - - body.put("deletedAccountCount", deletedUris.size()); - + Message.applyToBodyMap(vreq, body); return new TemplateResponseValues(TEMPLATE_NAME, body); } @@ -306,4 +261,84 @@ public class UserAccountsListPage extends UserAccountsPage { } + /** + * Message info that lives in the session. Another request can store this, + * and it will be displayed (once) by the list page. + */ + public static class Message { + private static final String ATTRIBUTE = Message.class.getName(); + private static final Collection EMPTY = Collections.emptySet(); + + public static void showNewAccount(HttpServletRequest req, + UserAccount userAccount, boolean emailWasSent) { + Message message = new Message(Type.NEW_ACCOUNT, userAccount, + emailWasSent, EMPTY); + setMessage(req, message); + } + + public static void showUpdatedAccount(HttpServletRequest req, + UserAccount userAccount, boolean emailWasSent) { + Message message = new Message(Type.UPDATED_ACCOUNT, userAccount, + emailWasSent, EMPTY); + setMessage(req, message); + } + + public static void showDeletions(HttpServletRequest req, + Collection deletedUris) { + Message message = new Message(Type.DELETIONS, null, false, + deletedUris); + setMessage(req, message); + } + + private static void setMessage(HttpServletRequest req, Message message) { + req.getSession().setAttribute(ATTRIBUTE, message); + } + + public static void applyToBodyMap(HttpServletRequest req, + Map body) { + HttpSession session = req.getSession(); + Object o = session.getAttribute(ATTRIBUTE); + session.removeAttribute(ATTRIBUTE); + + if (o instanceof Message) { + ((Message) o).applyToBodyMap(body); + } + } + + enum Type { + NEW_ACCOUNT, UPDATED_ACCOUNT, DELETIONS + } + + private final Type type; + private final UserAccount userAccount; + private final boolean emailWasSent; + private final Collection deletedUris; + + public Message(Type type, UserAccount userAccount, + boolean emailWasSent, Collection deletedUris) { + this.type = type; + this.userAccount = userAccount; + this.emailWasSent = emailWasSent; + this.deletedUris = deletedUris; + } + + private void applyToBodyMap(Map body) { + if (type == Type.NEW_ACCOUNT) { + body.put("newUserAccount", new UserAccountWrapper(userAccount, + Collections. emptyList())); + if (emailWasSent) { + body.put("emailWasSent", Boolean.TRUE); + } + } else if (type == Type.UPDATED_ACCOUNT) { + body.put("updatedUserAccount", new UserAccountWrapper( + userAccount, Collections. emptyList())); + if (emailWasSent) { + body.put("emailWasSent", Boolean.TRUE); + } + } else { + body.put("deletedAccountCount", deletedUris.size()); + } + } + } + }