@RequestMapping(value = "/create", method = RequestMethod.POST) @RequireHardLogIn public String createUserGroup( @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.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 (bindingResult.hasErrors()) { GlobalMessages.addErrorMessage(model, "form.global.error"); model.addAttribute(userGroupForm); return createUserGroup(model); } if (b2bCommerceB2BUserGroupFacade.getUserGroupDataForUid(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 createUserGroup(model); } final B2BUserGroupData userGroupData = new B2BUserGroupData(); userGroupData.setUid(userGroupForm.getUid()); userGroupData.setName(userGroupForm.getName()); if (StringUtils.isNotBlank(userGroupForm.getParentUnit())) { userGroupData.setUnit(companyB2BCommerceFacade.getUnitForUid(userGroupForm.getParentUnit())); } try { b2bCommerceB2BUserGroupFacade.updateUserGroup(userGroupForm.getUid(), userGroupData); } catch (final DuplicateUidException e) { GlobalMessages.addErrorMessage(model, "form.global.error"); bindingResult.rejectValue("uid", "form.b2busergroup.notunique"); return createUserGroup(model); } GlobalMessages.addFlashMessage( redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER, "form.b2busergroup.success"); return String.format(REDIRECT_TO_USERGROUP_DETAILS, urlEncode(userGroupForm.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); } }