An add, delete or update will redirect to the list page when complete, so a browser "refresh" will not try to repeat the operation.

This commit is contained in:
j2blake 2011-06-16 17:25:35 +00:00
parent 0909c97b36
commit ad60dc6908
2 changed files with 102 additions and 58 deletions

View file

@ -57,9 +57,9 @@ public class UserAccountsAdminController extends FreemarkerHttpServlet {
if (page.isSubmit() && page.isValid()) { if (page.isSubmit() && page.isValid()) {
page.createNewAccount(); page.createNewAccount();
UserAccountsListPage listPage = new UserAccountsListPage(vreq); UserAccountsListPage.Message.showNewAccount(vreq,
return listPage.showPageWithNewAccount(page.getAddedAccount(), page.getAddedAccount(), page.wasPasswordEmailSent());
page.wasPasswordEmailSent()); return redirectToList();
} else { } else {
return page.showPage(); return page.showPage();
} }
@ -71,9 +71,10 @@ public class UserAccountsAdminController extends FreemarkerHttpServlet {
return showHomePage(vreq, page.getBogusMessage()); return showHomePage(vreq, page.getBogusMessage());
} else if (page.isSubmit() && page.isValid()) { } else if (page.isSubmit() && page.isValid()) {
page.updateAccount(); page.updateAccount();
UserAccountsListPage listPage = new UserAccountsListPage(vreq);
return listPage.showPageWithUpdatedAccount( UserAccountsListPage.Message.showUpdatedAccount(vreq,
page.getUpdatedAccount(), page.wasPasswordEmailSent()); page.getUpdatedAccount(), page.wasPasswordEmailSent());
return redirectToList();
} else { } else {
return page.showPage(); return page.showPage();
} }
@ -83,8 +84,8 @@ public class UserAccountsAdminController extends FreemarkerHttpServlet {
UserAccountsDeleter deleter = new UserAccountsDeleter(vreq); UserAccountsDeleter deleter = new UserAccountsDeleter(vreq);
Collection<String> deletedUris = deleter.delete(); Collection<String> deletedUris = deleter.delete();
return new UserAccountsListPage(vreq) UserAccountsListPage.Message.showDeletions(vreq, deletedUris);
.showPageWithDeletions(deletedUris); return redirectToList();
} }
private ResponseValues handleListRequest(VitroRequest vreq) { private ResponseValues handleListRequest(VitroRequest vreq) {
@ -92,6 +93,14 @@ public class UserAccountsAdminController extends FreemarkerHttpServlet {
return page.showPage(); 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) { private ResponseValues showHomePage(VitroRequest vreq, String message) {
DisplayMessage.setMessage(vreq, message); DisplayMessage.setMessage(vreq, message);
return new RedirectResponseValues("/"); return new RedirectResponseValues("/");

View file

@ -11,6 +11,9 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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.Log;
import org.apache.commons.logging.LogFactory; 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.beans.UserAccount.Status;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; 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;
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.UserAccountsPage;
import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsSelection; 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.UserAccountsSelectionCriteria;
import edu.cornell.mannlib.vitro.webapp.controller.accounts.UserAccountsSelector; 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.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
@ -91,55 +94,7 @@ public class UserAccountsListPage extends UserAccountsPage {
UserAccountsSelection selection = UserAccountsSelector.select( UserAccountsSelection selection = UserAccountsSelector.select(
userAccountsModel, criteria); userAccountsModel, criteria);
Map<String, Object> body = buildTemplateBodyMap(selection); Map<String, Object> body = buildTemplateBodyMap(selection);
return new TemplateResponseValues(TEMPLATE_NAME, body); Message.applyToBodyMap(vreq, 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<String, Object> body = buildTemplateBodyMap(selection);
body.put("newUserAccount", new UserAccountWrapper(userAccount,
Collections.<String> 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<String, Object> body = buildTemplateBodyMap(selection);
body.put("updatedUserAccount", new UserAccountWrapper(userAccount,
Collections.<String> 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<String> deletedUris) {
UserAccountsSelection selection = UserAccountsSelector.select(
userAccountsModel, criteria);
Map<String, Object> body = buildTemplateBodyMap(selection);
body.put("deletedAccountCount", deletedUris.size());
return new TemplateResponseValues(TEMPLATE_NAME, 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<String> 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<String> 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<String, Object> 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<String> deletedUris;
public Message(Type type, UserAccount userAccount,
boolean emailWasSent, Collection<String> deletedUris) {
this.type = type;
this.userAccount = userAccount;
this.emailWasSent = emailWasSent;
this.deletedUris = deletedUris;
}
private void applyToBodyMap(Map<String, Object> body) {
if (type == Type.NEW_ACCOUNT) {
body.put("newUserAccount", new UserAccountWrapper(userAccount,
Collections.<String> emptyList()));
if (emailWasSent) {
body.put("emailWasSent", Boolean.TRUE);
}
} else if (type == Type.UPDATED_ACCOUNT) {
body.put("updatedUserAccount", new UserAccountWrapper(
userAccount, Collections.<String> emptyList()));
if (emailWasSent) {
body.put("emailWasSent", Boolean.TRUE);
}
} else {
body.put("deletedAccountCount", deletedUris.size());
}
}
}
} }