final String catalogCategoryId = catalogCategoryForm.getId();
final String parentCatalogCategoryId = catalogCategoryForm.getDefaultParentCategoryId();
if(StringUtils.isNotEmpty(catalogCategoryId)){
final CatalogCategoryVirtual catalogCategory = catalogCategoryService.getVirtualCatalogCategoryById(catalogCategoryId, new FetchPlan(categoryVirtualFetchPlans));
// SANITY CHECK
if(catalogCategory == null){
final String url = backofficeUrlService.generateUrl(BoUrls.MASTER_CATALOG, requestData);
return new ModelAndView(new RedirectView(url));
}
model.addAttribute(ModelConstants.URL_BACK, backofficeUrlService.generateUrl(BoUrls.VIRTUAL_CATEGORY_DETAILS, requestData, catalogCategory));
// UPDATE CATEORY
if (result.hasErrors()) {
if(StringUtils.isNotEmpty(parentCatalogCategoryId)){
// CHIELD CATEGORY
final CatalogCategoryVirtual parentCatalogCategory = catalogCategoryService.getVirtualCatalogCategoryById(parentCatalogCategoryId, new FetchPlan(categoryVirtualFetchPlans));
initProductVirtualCategoryModelAndView(request, modelAndView, parentCatalogCategory);
return modelAndView;
} else {
// ROOT CATEGORY
initProductVirtualCategoryModelAndView(request, modelAndView, catalogCategory);
return modelAndView;
}
}
// UPDATE CATEGORY
try {
// CatalogCategoryVirtual catalogCategoryVirtual = new CatalogCategoryVirtual();
CatalogCategoryVirtual catalogCategoryVirtual = webBackofficeService.updateCatalogCategory(requestData, currentMarketArea, currentRetailer, currentLocalization, catalogCategory, catalogCategoryForm);
addSuccessMessage(request, coreMessageSource.getMessage("business.catalog.category.edit.success.message", locale));
final String urlRedirect = backofficeUrlService.generateUrl(BoUrls.VIRTUAL_CATEGORY_DETAILS, requestData, catalogCategoryVirtual);
return new ModelAndView(new RedirectView(urlRedirect));
} catch (UniqueConstraintCodeCategoryException e) {
addErrorMessage(request, coreMessageSource.getMessage("business.catalog.category.edit.error.message", locale));
if(StringUtils.isNotEmpty(parentCatalogCategoryId)){
// CHIELD CATEGORY
final CatalogCategoryVirtual parentCatalogCategory = catalogCategoryService.getVirtualCatalogCategoryById(parentCatalogCategoryId, new FetchPlan(categoryVirtualFetchPlans));
initProductVirtualCategoryModelAndView(request, modelAndView, parentCatalogCategory);
return modelAndView;
} else {
// ROOT CATEGORY
initProductVirtualCategoryModelAndView(request, modelAndView, catalogCategory);
return modelAndView;
}
}
} else {
CatalogCategoryVirtual parentCatalogCategory = null;
if(StringUtils.isNotEmpty(parentCatalogCategoryId)){
parentCatalogCategory = catalogCategoryService.getVirtualCatalogCategoryById(parentCatalogCategoryId, new FetchPlan(categoryVirtualFetchPlans));
}
// CREATE A NEW CATEORY
if (result.hasErrors()) {
if(StringUtils.isNotEmpty(parentCatalogCategoryId)){
// CHIELD CATEGORY
initProductVirtualCategoryModelAndView(request, modelAndView, null);
return modelAndView;
} else {
// ROOT CATEGORY
initProductVirtualCategoryModelAndView(request, modelAndView, null);
return modelAndView;
}
}
// SAVE CATEGORY
try {
CatalogCategoryVirtual catalogCategoryVirtual = new CatalogCategoryVirtual();
CatalogCategoryVirtual savedCatalogCategory = webBackofficeService.createCatalogCategory(requestData, currentMarketArea, currentLocalization, parentCatalogCategory, catalogCategoryVirtual, catalogCategoryForm);
addSuccessMessage(request, coreMessageSource.getMessage("business.catalog.category.add.success.message", locale));
final String urlRedirect = backofficeUrlService.generateUrl(BoUrls.VIRTUAL_CATEGORY_DETAILS, requestData, savedCatalogCategory);
return new ModelAndView(new RedirectView(urlRedirect));