protected String updateCostCenterDetails(
      final B2BCostCenterForm b2BCostCenterForm,
      final BindingResult bindingResult,
      final Model model,
      final RedirectAttributes redirectModel)
      throws CMSItemNotFoundException {
    if (bindingResult.hasErrors()) {
      model.addAttribute(b2BCostCenterForm);
      return editCostCenterDetails(b2BCostCenterForm.getOriginalCode(), model);
    }
    final B2BCostCenterData b2BCostCenterData =
        populateB2BCostCenterDataFromForm(b2BCostCenterForm);

    try {
      b2bCostCenterFacade.updateCostCenter(b2BCostCenterData);
    } catch (final Exception e) {
      LOG.warn("Exception while saving the cost center details " + e);
      model.addAttribute(b2BCostCenterForm);
      GlobalMessages.addErrorMessage(model, "form.global.error");
      return editCostCenterDetails(b2BCostCenterData.getOriginalCode(), model);
    }
    storeCmsPageInModel(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    setUpMetaDataForContentPage(
        model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    GlobalMessages.addFlashMessage(
        redirectModel,
        GlobalMessages.CONF_MESSAGES_HOLDER,
        "text.company.costCenter.update.success");
    return String.format(REDIRECT_TO_COSTCENTER_DETAILS, urlEncode(b2BCostCenterData.getCode()));
  }
 protected String editCostCenterDetails(final String costCenterCode, final Model model)
     throws CMSItemNotFoundException {
   if (!model.containsAttribute("b2BCostCenterForm")) {
     final B2BCostCenterData b2BCostCenterData =
         b2bCostCenterFacade.getCostCenterDataForCode(costCenterCode);
     final B2BCostCenterForm b2BCostCenterform = new B2BCostCenterForm();
     b2BCostCenterform.setOriginalCode(costCenterCode);
     b2BCostCenterform.setCode(b2BCostCenterData.getCode());
     b2BCostCenterform.setCurrency(b2BCostCenterData.getCurrency().getIsocode());
     b2BCostCenterform.setName(b2BCostCenterData.getName());
     b2BCostCenterform.setParentB2BUnit(b2BCostCenterData.getUnit().getUid());
     model.addAttribute(b2BCostCenterform);
   }
   storeCmsPageInModel(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
   setUpMetaDataForContentPage(
       model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
   final List<Breadcrumb> breadcrumbs =
       myCompanyBreadcrumbBuilder.createManageCostCenterBreadCrumbs();
   myCompanyBreadcrumbBuilder.addViewCostCenterBreadCrumbs(breadcrumbs, costCenterCode);
   breadcrumbs.add(
       new Breadcrumb(
           String.format(MANAGE_COSTCENTERS_EDIT_URL, costCenterCode),
           getMessageSource()
               .getMessage(
                   "text.company.costCenter.editPage.breadcrumb",
                   new Object[] {costCenterCode},
                   getI18nService().getCurrentLocale()),
           null));
   model.addAttribute("breadcrumbs", breadcrumbs);
   model.addAttribute("metaRobots", "no-index,no-follow");
   return ControllerConstants.Views.Pages.MyCompany.MyCompanyCostCenterEditPage;
 }
 protected String viewCostCenterDetails(final String costCenterCode, final Model model)
     throws CMSItemNotFoundException {
   model.addAttribute(
       "b2bCostCenter", b2bCostCenterFacade.getCostCenterDataForCode(costCenterCode));
   storeCmsPageInModel(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
   setUpMetaDataForContentPage(
       model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
   final List<Breadcrumb> breadcrumbs =
       myCompanyBreadcrumbBuilder.createManageCostCenterBreadCrumbs();
   myCompanyBreadcrumbBuilder.addViewCostCenterBreadCrumbs(breadcrumbs, costCenterCode);
   model.addAttribute("breadcrumbs", breadcrumbs);
   model.addAttribute("metaRobots", "no-index,no-follow");
   return ControllerConstants.Views.Pages.MyCompany.MyCompanyCostCenterViewPage;
 }
 protected String saveCostCenter(
     @Valid final B2BCostCenterForm b2BCostCenterForm,
     final BindingResult bindingResult,
     final Model model,
     final RedirectAttributes redirectModel)
     throws CMSItemNotFoundException {
   if (bindingResult.hasFieldErrors()) {
     model.addAttribute(b2BCostCenterForm);
     return addCostCenter(model);
   }
   final B2BCostCenterData b2BCostCenterData =
       populateB2BCostCenterDataFromForm(b2BCostCenterForm);
   try {
     b2bCostCenterFacade.addCostCenter(b2BCostCenterData);
   } catch (final Exception e) {
     LOG.warn("Exception while saving the cost center details " + e);
     model.addAttribute(b2BCostCenterForm);
     bindingResult.rejectValue("code", "text.company.costCenter.code.exists.error.title");
     GlobalMessages.addErrorMessage(model, "form.global.error");
     return addCostCenter(model);
   }
   storeCmsPageInModel(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
   setUpMetaDataForContentPage(
       model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
   final List<Breadcrumb> breadcrumbs =
       myCompanyBreadcrumbBuilder.createManageCostCenterBreadCrumbs();
   breadcrumbs.add(
       new Breadcrumb(
           ADD_COSTCENTER_URL,
           getMessageSource()
               .getMessage(
                   "text.company.costCenter.addPage", null, getI18nService().getCurrentLocale()),
           null));
   model.addAttribute("breadcrumbs", breadcrumbs);
   GlobalMessages.addFlashMessage(
       redirectModel,
       GlobalMessages.CONF_MESSAGES_HOLDER,
       "text.company.costCenter.create.success");
   return String.format(REDIRECT_TO_COSTCENTER_DETAILS, urlEncode(b2BCostCenterData.getCode()));
 }
 @ModelAttribute("costCenters")
 public List<? extends B2BCostCenterData> getVisibleActiveCostCenters() {
   final List<? extends B2BCostCenterData> costCenterData =
       costCenterFacade.getActiveCostCenters();
   return costCenterData == null ? Collections.<B2BCostCenterData>emptyList() : costCenterData;
 }