/**
* Most of the code in the Qalingo project is copyrighted Hoteia and licensed
* under the Apache License Version 2.0 (release version 0.8.0)
* http://www.apache.org/licenses/LICENSE-2.0
*
* Copyright (c) Hoteia, 2012-2014
* http://www.hoteia.com - http://twitter.com/hoteia - contact@hoteia.com
*
*/
package org.hoteia.qalingo.web.mvc.controller.catalog;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.hoteia.qalingo.core.ModelConstants;
import org.hoteia.qalingo.core.RequestConstants;
import org.hoteia.qalingo.core.domain.CatalogCategoryVirtualProductSkuPk;
import org.hoteia.qalingo.core.domain.Localization;
import org.hoteia.qalingo.core.domain.ProductBrand;
import org.hoteia.qalingo.core.domain.ProductBrand_;
import org.hoteia.qalingo.core.domain.ProductMarketing;
import org.hoteia.qalingo.core.domain.ProductMarketing_;
import org.hoteia.qalingo.core.domain.ProductSkuStorePrice_;
import org.hoteia.qalingo.core.domain.ProductSku_;
import org.hoteia.qalingo.core.domain.CatalogCategoryVirtualProductSkuRel_;
import org.hoteia.qalingo.core.domain.CatalogCategoryVirtualProductSkuPk_;
import org.hoteia.qalingo.core.domain.enumtype.FoUrls;
import org.hoteia.qalingo.core.fetchplan.FetchPlan;
import org.hoteia.qalingo.core.fetchplan.SpecificFetchMode;
import org.hoteia.qalingo.core.i18n.enumtype.ScopeWebMessage;
import org.hoteia.qalingo.core.service.ProductService;
import org.hoteia.qalingo.core.web.mvc.viewbean.BreadcrumbViewBean;
import org.hoteia.qalingo.core.web.mvc.viewbean.MenuViewBean;
import org.hoteia.qalingo.core.web.mvc.viewbean.ProductBrandViewBean;
import org.hoteia.qalingo.core.web.mvc.viewbean.ProductMarketingViewBean;
import org.hoteia.qalingo.core.web.resolver.RequestData;
import org.hoteia.qalingo.core.web.servlet.ModelAndViewThemeDevice;
import org.hoteia.qalingo.core.web.servlet.view.RedirectView;
import org.hoteia.qalingo.web.mvc.controller.AbstractMCommerceController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
*
*/
@Controller("brandController")
public class BrandController extends AbstractMCommerceController {
@Autowired
protected ProductService productService;
protected List<SpecificFetchMode> productBrandFetchPlans = new ArrayList<SpecificFetchMode>();
protected List<SpecificFetchMode> productMarketingFetchPlans = new ArrayList<SpecificFetchMode>();
public BrandController() {
productBrandFetchPlans.add(new SpecificFetchMode(ProductBrand_.attributes.getName()));
productBrandFetchPlans.add(new SpecificFetchMode(ProductBrand_.assets.getName()));
productMarketingFetchPlans.add(new SpecificFetchMode(ProductMarketing_.productBrand.getName()));
productMarketingFetchPlans.add(new SpecificFetchMode(ProductMarketing_.productMarketingType.getName()));
productMarketingFetchPlans.add(new SpecificFetchMode(ProductMarketing_.attributes.getName()));
productMarketingFetchPlans.add(new SpecificFetchMode(ProductMarketing_.productSkus.getName()));
productMarketingFetchPlans.add(new SpecificFetchMode(ProductMarketing_.productSkus.getName() + "." + ProductSku_.prices.getName()));
productMarketingFetchPlans.add(new SpecificFetchMode(ProductMarketing_.productSkus.getName() + "." + ProductSku_.prices.getName() + "." + ProductSkuStorePrice_.currency.getName()));
productMarketingFetchPlans.add(new SpecificFetchMode(ProductMarketing_.productSkus.getName() + "." + ProductSku_.catalogCategoryVirtualProductSkuRels.getName() + "." + CatalogCategoryVirtualProductSkuRel_.pk.getName() + "." + CatalogCategoryVirtualProductSkuPk_.catalogCategoryVirtual.getName()));
productMarketingFetchPlans.add(new SpecificFetchMode(ProductMarketing_.assets.getName()));
}
@RequestMapping(FoUrls.BRAND_ALL_URL)
public ModelAndView allBrand(final HttpServletRequest request, final Model model) throws Exception {
ModelAndViewThemeDevice modelAndView = new ModelAndViewThemeDevice(getCurrentVelocityPath(request), FoUrls.BRAND_ALL.getVelocityPage());
final RequestData requestData = requestUtil.getRequestData(request);
overrideDefaultPageTitle(request, modelAndView, FoUrls.BRAND_ALL.getKey());
final List<Long> productBrandIds = productService.findAllProductBrandIdsEnabled();
final List<ProductBrandViewBean> productBrandViewBeans = new ArrayList<ProductBrandViewBean>();
for (Long productBrandId : productBrandIds) {
ProductBrand productBrand = productService.getProductBrandById(productBrandId, new FetchPlan(productBrandFetchPlans));
ProductBrandViewBean productBrandViewBean = frontofficeViewBeanFactory.buildViewBeanProductBrand(requestData, productBrand);
productBrandViewBeans.add(productBrandViewBean);
}
model.addAttribute(ModelConstants.PRODUCT_BRANDS_VIEW_BEAN, productBrandViewBeans);
// BREADCRUMB
model.addAttribute(ModelConstants.BREADCRUMB_VIEW_BEAN, buildBreadcrumbAllBrandViewBean(requestData));
return modelAndView;
}
protected BreadcrumbViewBean buildBreadcrumbAllBrandViewBean(final RequestData requestData) {
final Localization localization = requestData.getMarketAreaLocalization();
final String localizationCode = localization.getCode();
final Locale locale = requestData.getLocale();
BreadcrumbViewBean breadcrumbViewBean = new BreadcrumbViewBean();
breadcrumbViewBean.setName(getSpecificMessage(ScopeWebMessage.HEADER_MENU, FoUrls.BRAND_ALL.getKey(), locale));
List<MenuViewBean> menuViewBeans = breadcrumbViewBean.getMenus();
MenuViewBean menu = new MenuViewBean();
menu.setKey(FoUrls.HOME.getKey());
menu.setName(getSpecificMessage(ScopeWebMessage.HEADER_MENU, FoUrls.HOME.getMessageKey(), locale));
menu.setUrl(urlService.generateUrl(FoUrls.HOME, requestData));
menuViewBeans.add(menu);
menu = new MenuViewBean();
menu.setKey(FoUrls.BRAND_ALL.getKey());
menu.setName(getSpecificMessage(ScopeWebMessage.HEADER_MENU, FoUrls.BRAND_ALL.getKey(), locale));
menu.setUrl(urlService.generateUrl(FoUrls.BRAND_ALL, requestData));
menu.setActive(true);
menuViewBeans.add(menu);
return breadcrumbViewBean;
}
@RequestMapping(FoUrls.BRAND_DETAILS_OLD_URL)
public ModelAndView brandDetailsOld(final HttpServletRequest request, final Model model, @PathVariable(RequestConstants.URL_PATTERN_BRAND_CODE) final String brandCode) throws Exception {
ModelAndViewThemeDevice modelAndView = new ModelAndViewThemeDevice(getCurrentVelocityPath(request), FoUrls.BRAND_DETAILS.getVelocityPage());
final RequestData requestData = requestUtil.getRequestData(request);
final ProductBrand productBrand = productService.getProductBrandByCode(brandCode, new FetchPlan(productBrandFetchPlans));
if(productBrand != null
&& productBrand.isEnabled()){
final String urlRedirect = urlService.generateRedirectUrl(FoUrls.BRAND_DETAILS, requestUtil.getRequestData(request));
return new ModelAndView(new RedirectView(urlRedirect));
}
final String urlRedirect = urlService.generateRedirectUrl(FoUrls.BRAND_ALL, requestUtil.getRequestData(request));
return new ModelAndView(new RedirectView(urlRedirect));
}
@RequestMapping(FoUrls.BRAND_DETAILS_URL)
public ModelAndView brandDetails(final HttpServletRequest request, final Model model, @PathVariable(RequestConstants.URL_PATTERN_BRAND_CODE) final String brandCode) throws Exception {
ModelAndViewThemeDevice modelAndView = new ModelAndViewThemeDevice(getCurrentVelocityPath(request), FoUrls.BRAND_DETAILS.getVelocityPage());
final RequestData requestData = requestUtil.getRequestData(request);
final ProductBrand productBrand = productService.getProductBrandByCode(brandCode, new FetchPlan(productBrandFetchPlans));
if(productBrand != null
&& productBrand.isEnabled()){
final ProductBrandViewBean productBrandViewBean = frontofficeViewBeanFactory.buildViewBeanProductBrand(requestUtil.getRequestData(request), productBrand);
model.addAttribute(ModelConstants.PRODUCT_BRAND_VIEW_BEAN, productBrandViewBean);
List<ProductMarketing> productMarketings = productService.findProductMarketingsByBrandCode(brandCode, new FetchPlan(productMarketingFetchPlans));
List<ProductMarketingViewBean> productMarketingViewBeans = frontofficeViewBeanFactory.buildListViewBeanProductMarketing(requestData, productMarketings);
model.addAttribute(ModelConstants.PRODUCT_MARKETINGS_VIEW_BEAN, productMarketingViewBeans);
overrideDefaultPageTitle(request, modelAndView, FoUrls.BRAND_DETAILS.getKey());
model.addAttribute(ModelConstants.BREADCRUMB_VIEW_BEAN, buildBreadcrumbBrandDetailsViewBean(requestData, productBrand));
List<ProductBrand> productBrands = productService.findAllProductBrandsEnabled();
List<ProductBrandViewBean> productBrandViewBeans = frontofficeViewBeanFactory.buildListViewBeanProductBrand(requestUtil.getRequestData(request), productBrands);
model.addAttribute(ModelConstants.BREADCRUMB_VIEW_BEAN, productBrands);
return modelAndView;
}
final String urlRedirect = urlService.generateRedirectUrl(FoUrls.BRAND_ALL, requestUtil.getRequestData(request));
return new ModelAndView(new RedirectView(urlRedirect));
}
protected BreadcrumbViewBean buildBreadcrumbBrandDetailsViewBean(final RequestData requestData, ProductBrand productBrand) {
final Localization localization = requestData.getMarketAreaLocalization();
final String localizationCode = localization.getCode();
final Locale locale = requestData.getLocale();
Object[] params = { productBrand.getI18nName(localizationCode) };
// BREADCRUMB
BreadcrumbViewBean breadcrumbViewBean = new BreadcrumbViewBean();
breadcrumbViewBean.setName(getSpecificMessage(ScopeWebMessage.HEADER_MENU, FoUrls.BRAND_DETAILS.getKey(), params, locale));
List<MenuViewBean> menuViewBeans = breadcrumbViewBean.getMenus();
MenuViewBean menu = new MenuViewBean();
menu.setName(getSpecificMessage(ScopeWebMessage.HEADER_MENU, FoUrls.HOME.getMessageKey(), locale));
menu.setUrl(urlService.generateUrl(FoUrls.HOME, requestData));
menuViewBeans.add(menu);
menu = new MenuViewBean();
menu.setName(getSpecificMessage(ScopeWebMessage.HEADER_MENU, FoUrls.BRAND_ALL.getKey(), locale));
menu.setUrl(urlService.generateUrl(FoUrls.BRAND_ALL, requestData));
menuViewBeans.add(menu);
menu = new MenuViewBean();
menu.setName(getSpecificMessage(ScopeWebMessage.HEADER_MENU, FoUrls.BRAND_DETAILS.getKey(), params, locale));
menu.setUrl(urlService.generateUrl(FoUrls.BRAND_DETAILS, requestData, productBrand));
menu.setActive(true);
menuViewBeans.add(menu);
return breadcrumbViewBean;
}
}