protected String createUser(
      final B2BCustomerForm b2BCustomerForm,
      final BindingResult bindingResult,
      final Model model,
      final RedirectAttributes redirectModel)
      throws CMSItemNotFoundException {

    if (bindingResult.hasErrors()) {
      GlobalMessages.addErrorMessage(model, "form.global.error");
      model.addAttribute(b2BCustomerForm);
      return editUser(b2BCustomerForm.getUid(), model);
    }

    final CustomerData b2bCustomerData = new CustomerData();
    b2bCustomerData.setTitleCode(b2BCustomerForm.getTitleCode());
    b2bCustomerData.setFirstName(b2BCustomerForm.getFirstName());
    b2bCustomerData.setLastName(b2BCustomerForm.getLastName());
    b2bCustomerData.setEmail(b2BCustomerForm.getEmail());
    b2bCustomerData.setDisplayUid(b2BCustomerForm.getEmail());
    b2bCustomerData.setUnit(
        companyB2BCommerceFacade.getUnitForUid(b2BCustomerForm.getParentB2BUnit()));
    b2bCustomerData.setRoles(b2BCustomerForm.getRoles());
    model.addAttribute(b2BCustomerForm);
    model.addAttribute("titleData", getUserFacade().getTitles());
    model.addAttribute("roles", populateRolesCheckBoxes(companyB2BCommerceFacade.getUserGroups()));

    storeCmsPageInModel(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    setUpMetaDataForContentPage(
        model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    final List<Breadcrumb> breadcrumbs = myCompanyBreadcrumbBuilder.getBreadcrumbs(null);
    breadcrumbs.add(
        new Breadcrumb(
            "/my-company/organization-management/",
            getMessageSource()
                .getMessage(
                    "text.company.organizationManagement",
                    null,
                    getI18nService().getCurrentLocale()),
            null));
    breadcrumbs.add(
        new Breadcrumb(
            "/my-company/organization-management/manage-user",
            getMessageSource()
                .getMessage("text.company.manageUsers", null, getI18nService().getCurrentLocale()),
            null));
    model.addAttribute("breadcrumbs", breadcrumbs);

    try {
      b2bCommerceUserFacade.updateCustomer(b2bCustomerData);
      b2bCustomerData.setUid(b2BCustomerForm.getEmail().toLowerCase());
      GlobalMessages.addFlashMessage(
          redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER, "text.confirmation.user.added");
    } catch (final DuplicateUidException e) {
      bindingResult.rejectValue("email", "text.manageuser.error.email.exists.title");
      GlobalMessages.addErrorMessage(model, "form.global.error");
      model.addAttribute("b2BCustomerForm", b2BCustomerForm);
      return editUser(b2BCustomerForm.getUid(), model);
    }
    return String.format(REDIRECT_TO_USER_DETAILS, urlEncode(b2bCustomerData.getUid()));
  }
  @RequestMapping(value = "/update-profile", method = RequestMethod.POST)
  @RequireHardLogIn
  public String updateProfile(
      final UpdateProfileForm updateProfileForm,
      final BindingResult bindingResult,
      final Model model,
      final RedirectAttributes redirectAttributes)
      throws CMSItemNotFoundException {
    getProfileValidator().validate(updateProfileForm, bindingResult);

    String returnAction = ControllerConstants.Views.Pages.Account.AccountProfileEditPage;
    final CustomerData currentCustomerData = customerFacade.getCurrentCustomer();
    final CustomerData customerData = new CustomerData();
    customerData.setTitleCode(updateProfileForm.getTitleCode());
    customerData.setFirstName(updateProfileForm.getFirstName());
    customerData.setLastName(updateProfileForm.getLastName());
    customerData.setUid(currentCustomerData.getUid());
    customerData.setDisplayUid(currentCustomerData.getDisplayUid());

    model.addAttribute("titleData", userFacade.getTitles());

    if (bindingResult.hasErrors()) {
      GlobalMessages.addErrorMessage(model, "form.global.error");
    } else {
      try {
        customerFacade.updateProfile(customerData);
        GlobalMessages.addFlashMessage(
            redirectAttributes,
            GlobalMessages.CONF_MESSAGES_HOLDER,
            "text.account.profile.confirmationUpdated",
            null);
        returnAction = REDIRECT_TO_PROFILE_PAGE;
      } catch (final DuplicateUidException e) {
        bindingResult.rejectValue("email", "registration.error.account.exists.title");
        GlobalMessages.addErrorMessage(model, "form.global.error");
      }
    }

    storeCmsPageInModel(model, getContentPageForLabelOrId(PROFILE_CMS_PAGE));
    setUpMetaDataForContentPage(model, getContentPageForLabelOrId(PROFILE_CMS_PAGE));
    model.addAttribute(
        "breadcrumbs", accountBreadcrumbBuilder.getBreadcrumbs("text.account.profile"));
    return returnAction;
  }
  protected String editUser(
      final String user,
      final B2BCustomerForm b2BCustomerForm,
      final BindingResult bindingResult,
      final Model model,
      final RedirectAttributes redirectModel)
      throws CMSItemNotFoundException {
    if (bindingResult.hasErrors()) {
      GlobalMessages.addErrorMessage(model, "form.global.error");
      model.addAttribute(b2BCustomerForm);
      return editUser(b2BCustomerForm.getUid(), model);
    }

    // A B2B Admin should not be able to downgrade their roles, they must at lest belong to B2B
    // Administrator role
    if (customerFacade.getCurrentCustomer().getUid().equals(b2BCustomerForm.getUid())) {
      final Collection<String> roles =
          b2BCustomerForm.getRoles() != null ? b2BCustomerForm.getRoles() : new ArrayList<String>();
      if (!roles.contains(B2BConstants.B2BADMINGROUP)) {
        GlobalMessages.addErrorMessage(model, "form.b2bcustomer.adminrole.error");
        roles.add(B2BConstants.B2BADMINGROUP);
        b2BCustomerForm.setRoles(roles);
        model.addAttribute(b2BCustomerForm);
        return editUser(b2BCustomerForm.getUid(), model);
      } else {
        // A session user can't modify their own parent unit.
        final B2BUnitData parentUnit = companyB2BCommerceFacade.getParentUnit();
        if (!parentUnit.getUid().equals(b2BCustomerForm.getParentB2BUnit())) {
          GlobalMessages.addErrorMessage(model, "form.b2bcustomer.parentunit.error");
          b2BCustomerForm.setParentB2BUnit(parentUnit.getUid());
          model.addAttribute(b2BCustomerForm);
          return editUser(b2BCustomerForm.getUid(), model);
        }
      }
    }

    final CustomerData b2bCustomerData = new CustomerData();
    b2bCustomerData.setUid(b2BCustomerForm.getUid());
    b2bCustomerData.setTitleCode(b2BCustomerForm.getTitleCode());
    b2bCustomerData.setFirstName(b2BCustomerForm.getFirstName());
    b2bCustomerData.setLastName(b2BCustomerForm.getLastName());
    b2bCustomerData.setEmail(b2BCustomerForm.getEmail());
    b2bCustomerData.setDisplayUid(b2BCustomerForm.getEmail());
    b2bCustomerData.setUnit(
        companyB2BCommerceFacade.getUnitForUid(b2BCustomerForm.getParentB2BUnit()));
    b2bCustomerData.setRoles(
        b2BCustomerForm.getRoles() != null
            ? b2BCustomerForm.getRoles()
            : Collections.<String>emptyList());
    model.addAttribute(b2BCustomerForm);
    model.addAttribute("titleData", getUserFacade().getTitles());
    model.addAttribute("roles", populateRolesCheckBoxes(companyB2BCommerceFacade.getUserGroups()));

    storeCmsPageInModel(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    setUpMetaDataForContentPage(
        model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    final List<Breadcrumb> breadcrumbs =
        myCompanyBreadcrumbBuilder.createManageUserDetailsBreadcrumb(user);
    model.addAttribute("breadcrumbs", breadcrumbs);

    try {
      b2bCommerceUserFacade.updateCustomer(b2bCustomerData);
      b2bCustomerData.setUid(b2BCustomerForm.getEmail().toLowerCase());
      GlobalMessages.addFlashMessage(
          redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER, "text.confirmation.user.edited");
    } catch (final DuplicateUidException e) {
      bindingResult.rejectValue("email", "text.manageuser.error.email.exists.title");
      GlobalMessages.addErrorMessage(model, "form.global.error");
      model.addAttribute("b2BCustomerForm", b2BCustomerForm);
      return editUser(b2BCustomerForm.getUid(), model);
    }
    return String.format(REDIRECT_TO_USER_DETAILS, urlEncode(b2bCustomerData.getUid()));
  }