diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ManageProxiesController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ManageProxiesController.java index 9c8d247a5..ffcaf5afa 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ManageProxiesController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ManageProxiesController.java @@ -22,6 +22,7 @@ public class ManageProxiesController extends FreemarkerHttpServlet { private static final Log log = LogFactory .getLog(ManageProxiesController.class); + private static final String ACTION_CREATE = "/create"; private static final String ACTION_EDIT = "/edit"; @Override @@ -38,18 +39,21 @@ public class ManageProxiesController extends FreemarkerHttpServlet { String action = vreq.getPathInfo(); log.debug("action = '" + action + "'"); - if (ACTION_EDIT.equals(action)) { + + if (ACTION_CREATE.equals(action)) { + return handleCreateRequest(vreq); + } else if (ACTION_EDIT.equals(action)) { return handleEditRequest(vreq); } else { return handleListRequest(vreq); } } - private ResponseValues handleEditRequest(VitroRequest vreq) { - ManageProxiesEditPage page = new ManageProxiesEditPage(vreq); + private ResponseValues handleCreateRequest(VitroRequest vreq) { + ManageProxiesCreatePage page = new ManageProxiesCreatePage(vreq); if (page.isValid()) { - page.applyEdits(); + page.createRelationships(); Message.setMessage(vreq, new SuccessMessage()); } else { Message.setMessage(vreq, new FailureMessage()); @@ -58,6 +62,19 @@ public class ManageProxiesController extends FreemarkerHttpServlet { return redirectToList(); } + private ResponseValues handleEditRequest(VitroRequest vreq) { + ManageProxiesEditPage page = new ManageProxiesEditPage(vreq); + + if (page.isValid()) { + page.applyEdits(); + Message.setMessage(vreq, new SuccessMessage()); + } else { + Message.setMessage(vreq, new FailureMessage()); + } + + return redirectToList(); + } + private ResponseValues handleListRequest(VitroRequest vreq) { ManageProxiesListPage page = new ManageProxiesListPage(vreq); return page.showPage(); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ManageProxiesCreatePage.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ManageProxiesCreatePage.java new file mode 100644 index 000000000..f2b655efe --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/accounts/manageproxies/ManageProxiesCreatePage.java @@ -0,0 +1,122 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.controller.accounts.manageproxies; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.beans.SelfEditingConfiguration; +import edu.cornell.mannlib.vitro.webapp.beans.UserAccount; +import edu.cornell.mannlib.vitro.webapp.controller.AbstractPageHandler; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; + +/** + * Create a new relationship, or set of relationships, between zero or more + * proxies and zero or more profiles. + * + * Note that this is additive, so if a proxy already has some profiles, they + * will be preserved, even if they are not listed here. + * + * It is possible that one or more profiles might be the "self" pages of one or + * more proxies, so as we do each proxy, we exclude any profile which is the + * "self" for that proxy. + * + * If there are zero proxies here, or zero profiles, it doesn't hurt anything, + * it just doesn't accomplish anything either. + * + * This is not really a page, in that it doesn't display anything. It's just a + * way to separate out some of the logic of the ManageProxies list page. + */ +public class ManageProxiesCreatePage extends AbstractPageHandler { + private static final Log log = LogFactory + .getLog(ManageProxiesCreatePage.class); + + private static final String PARAMETER_PROXY_URI = "proxyUri"; + private static final String PARAMETER_PROFILE_URI = "profileUri"; + + private final SelfEditingConfiguration selfEditingConfiguration; + private List proxyUris; + private List profileUris; + private List proxyAccounts; + + private boolean valid = true; + + public ManageProxiesCreatePage(VitroRequest vreq) { + super(vreq); + this.selfEditingConfiguration = SelfEditingConfiguration.getBean(vreq); + + parseParameters(); + } + + private void parseParameters() { + try { + proxyUris = getStringParameters(PARAMETER_PROXY_URI); + profileUris = getStringParameters(PARAMETER_PROFILE_URI); + + proxyAccounts = findProxyAccounts(); + validateProfileUris(); + } catch (InvalidParametersException e) { + log.error(e.getMessage()); + valid = false; + } + + } + + private List findProxyAccounts() + throws InvalidParametersException { + List accounts = new ArrayList(); + for (String proxyUri : proxyUris) { + UserAccount proxy = userAccountsDao.getUserAccountByUri(proxyUri); + if (proxy == null) { + throw new InvalidParametersException( + "Found no User Account for proxyUri='" + proxyUri + "'"); + } + accounts.add(proxy); + } + return accounts; + } + + private void validateProfileUris() throws InvalidParametersException { + for (String profileUri : profileUris) { + Individual ind = indDao.getIndividualByURI(profileUri); + if (ind == null) { + throw new InvalidParametersException( + "Found no Individual for profileUri='" + profileUri + + "'"); + } + } + } + + public boolean isValid() { + return valid; + } + + /** We don't remove any existing relationships, we just add new ones. */ + public void createRelationships() { + for (UserAccount proxyAccount : proxyAccounts) { + Set profiles = new HashSet(); + profiles.addAll(proxyAccount.getProxiedIndividualUris()); + profiles.addAll(figureNonSelfProfileUris(proxyAccount)); + + proxyAccount.setProxiedIndividualUris(profiles); + userAccountsDao.updateUserAccount(proxyAccount); + } + } + + private Collection figureNonSelfProfileUris(UserAccount proxyAccount) { + List mySelves = selfEditingConfiguration + .getAssociatedIndividuals(indDao, proxyAccount); + + List myProfiles = new ArrayList(profileUris); + myProfiles.removeAll(mySelves); + return myProfiles; + } + +}