@RequestMapping(value = "/create", method = RequestMethod.GET)
  @RequireHardLogIn
  public String createUserGroup(final Model model) throws CMSItemNotFoundException {
    storeCmsPageInModel(model, getContentPageForLabelOrId(MANAGE_USERGROUPS_CMS_PAGE));
    setUpMetaDataForContentPage(model, getContentPageForLabelOrId(MANAGE_USERGROUPS_CMS_PAGE));
    final List<Breadcrumb> breadcrumbs =
        myCompanyBreadcrumbBuilder.createManageUserGroupBreadCrumbs();
    breadcrumbs.add(
        new Breadcrumb(
            "/my-company/organization-management/manage-usergroups/create",
            getMessageSource()
                .getMessage(
                    "text.company.manageUsergroups.createUsergroup.breadcrumb",
                    null,
                    "Create Usergroup ",
                    getI18nService().getCurrentLocale()),
            null));
    model.addAttribute("breadcrumbs", breadcrumbs);

    if (!model.containsAttribute("b2BUserGroupForm")) {
      final B2BUnitData unitData = companyB2BCommerceFacade.getParentUnit();
      final B2BUserGroupForm b2BUserGroupForm = new B2BUserGroupForm();
      b2BUserGroupForm.setParentUnit(unitData.getUid());
      model.addAttribute(b2BUserGroupForm);
    }

    model.addAttribute(
        "branchSelectOptions",
        getBranchSelectOptions(this.companyB2BCommerceFacade.getBranchNodes()));
    model.addAttribute("unit", companyB2BCommerceFacade.getParentUnit());
    model.addAttribute("metaRobots", "no-index,no-follow");
    return ControllerConstants.Views.Pages.MyCompany.MyCompanyManageUsergroupCreatePage;
  }
  protected String unitDetails(
      final String unit, final Model model, final HttpServletRequest request)
      throws CMSItemNotFoundException {
    storeCmsPageInModel(model, getContentPageForLabelOrId(MANAGE_UNITS_CMS_PAGE));
    setUpMetaDataForContentPage(model, getContentPageForLabelOrId(MANAGE_UNITS_CMS_PAGE));
    final List<Breadcrumb> breadcrumbs =
        myCompanyBreadcrumbBuilder.createManageUnitsDetailsBreadcrumbs(unit);
    model.addAttribute("breadcrumbs", breadcrumbs);

    B2BUnitData unitData = companyB2BCommerceFacade.getUnitForUid(unit);
    if (unitData == null) {
      unitData = new B2BUnitData();
      GlobalMessages.addErrorMessage(model, "b2bunit.notfound");
    } else if (!unitData.isActive()) {
      GlobalMessages.addInfoMessage(model, "b2bunit.disabled.infomsg");
    }

    model.addAttribute("unit", unitData);
    model.addAttribute("user", customerFacade.getCurrentCustomer());
    model.addAttribute("metaRobots", "no-index,no-follow");
    return ControllerConstants.Views.Pages.MyCompany.MyCompanyManageUnitDetailsPage;
  }
  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()));
  }
  @RequestMapping(value = "/edit", method = RequestMethod.POST)
  @RequireHardLogIn
  public String editUserGroup(
      @RequestParam("usergroup") final String usergroup,
      @Valid final B2BUserGroupForm userGroupForm,
      final BindingResult bindingResult,
      final Model model,
      final RedirectAttributes redirectModel)
      throws CMSItemNotFoundException {
    storeCmsPageInModel(model, getContentPageForLabelOrId(MANAGE_USERGROUPS_CMS_PAGE));
    setUpMetaDataForContentPage(model, getContentPageForLabelOrId(MANAGE_USERGROUPS_CMS_PAGE));
    final List<Breadcrumb> breadcrumbs =
        myCompanyBreadcrumbBuilder.createManageUserGroupDetailsBreadCrumbs(usergroup);
    breadcrumbs.add(
        new Breadcrumb(
            String.format(
                "/my-company/organization-management/manage-usergroups/edit?usergroup=%s",
                urlEncode(usergroup)),
            getMessageSource()
                .getMessage(
                    "text.company.manageUsergroups.editUsergroup.breadcrumb",
                    new Object[] {usergroup},
                    "Edit {0} Usergroup ",
                    getI18nService().getCurrentLocale()),
            null));
    model.addAttribute("breadcrumbs", breadcrumbs);

    if (bindingResult.hasErrors()) {
      GlobalMessages.addErrorMessage(model, "form.global.error");
      model.addAttribute(userGroupForm);
      return editUserGroup(usergroup, model);
    }
    if (!userGroupForm.getUid().equals(usergroup)
        && b2bCommerceB2BUserGroupFacade.getB2BUserGroup(userGroupForm.getUid()) != null) {
      // a unit uid is not unique
      GlobalMessages.addErrorMessage(model, "form.global.error");
      bindingResult.rejectValue("uid", "form.b2busergroup.notunique");
      model.addAttribute(userGroupForm);
      return editUserGroup(usergroup, model);
    }

    final B2BUserGroupData userGroupData = b2bCommerceB2BUserGroupFacade.getB2BUserGroup(usergroup);
    if (userGroupData != null) {
      boolean userGroupUpdated = false;

      userGroupData.setUid(userGroupForm.getUid());
      userGroupData.setName(userGroupForm.getName());
      if (StringUtils.isNotBlank(userGroupForm.getParentUnit())) {
        final B2BUnitData newUserGroup =
            companyB2BCommerceFacade.getUnitForUid(userGroupForm.getParentUnit());
        if (!newUserGroup.getUid().equals(userGroupData.getUnit().getUid())) {
          userGroupUpdated = true;
        }

        userGroupData.setUnit(newUserGroup);
      }

      try {
        b2bCommerceB2BUserGroupFacade.updateUserGroup(
            userGroupForm.getOriginalUid(), userGroupData);
      } catch (final DuplicateUidException e) {
        GlobalMessages.addErrorMessage(model, "form.global.error");
        bindingResult.rejectValue("uid", "form.b2busergroup.notunique");
        return editUserGroup(usergroup, model);
      }

      if (userGroupUpdated) {
        GlobalMessages.addFlashMessage(
            redirectModel,
            GlobalMessages.INFO_MESSAGES_HOLDER,
            "form.b2busergroup.parentunit.updated");
      } else {
        GlobalMessages.addFlashMessage(
            redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER, "form.b2busergroup.success");
      }

      return String.format(REDIRECT_TO_USERGROUP_DETAILS, urlEncode(userGroupForm.getUid()));

    } else {
      // user has no permissions to edit the group.
      GlobalMessages.addErrorMessage(model, "form.b2busergroup.noeditpermissions");
      model.addAttribute(userGroupForm);
      return editUserGroup(usergroup, model);
    }
  }