@SuppressWarnings({"unchecked", "rawtypes"})
  public void execute(
      Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
      throws TemplateException, IOException {
    Long articleCategoryId =
        FreemarkerUtils.getParameter(ARTICLE_CATEGORY_ID_PARAMETER_NAME, Long.class, params);
    Long[] tagIds = FreemarkerUtils.getParameter(TAG_IDS_PARAMETER_NAME, Long[].class, params);

    ArticleCategory articleCategory = articleCategoryService.find(articleCategoryId);
    List<Tag> tags = tagService.findList(tagIds);

    List<Article> articles;
    if ((articleCategoryId != null && articleCategory == null)
        || (tagIds != null && tags.isEmpty())) {
      articles = new ArrayList<Article>();
    } else {
      boolean useCache = useCache(env, params);
      String cacheRegion = getCacheRegion(env, params);
      Integer count = getCount(params);
      List<Filter> filters = getFilters(params, Article.class);
      List<Order> orders = getOrders(params);
      if (useCache) {
        articles =
            articleService.findList(articleCategory, tags, count, filters, orders, cacheRegion);
      } else {
        articles = articleService.findList(articleCategory, tags, count, filters, orders);
      }
    }
    setLocalVariable(VARIABLE_NAME, articles, env, body);
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  public void execute(
      Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
      throws TemplateException, IOException {
    Long articleCategoryId =
        FreemarkerUtils.getParameter(ARTICLE_CATEGORY_ID_PARAMETER_NAME, Long.class, params);

    ArticleCategory articleCategory = articleCategoryService.find(articleCategoryId);

    List<ArticleCategory> articleCategories;
    if (articleCategoryId != null && articleCategory == null) {
      articleCategories = new ArrayList<ArticleCategory>();
    } else {
      boolean useCache = useCache(env, params);
      String cacheRegion = getCacheRegion(env, params);
      Integer count = getCount(params);
      if (useCache) {
        articleCategories =
            articleCategoryService.findChildren(articleCategory, count, cacheRegion);
      } else {
        articleCategories = articleCategoryService.findChildren(articleCategory, count);
      }
    }
    setLocalVariable(VARIABLE_NAME, articleCategories, env, body);
  }
 @Transactional(readOnly = true)
 public int buildSitemap() {
   int buildCount = 0;
   Template sitemapIndexTemplate = templateService.get("sitemapIndex");
   Template sitemapTemplate = templateService.get("sitemap");
   Map<String, Object> model = new HashMap<String, Object>();
   List<String> staticPaths = new ArrayList<String>();
   for (int step = 0, index = 0, first = 0, count = SITEMAP_MAX_SIZE; ; ) {
     try {
       model.put("index", index);
       String templatePath = sitemapTemplate.getTemplatePath();
       String staticPath = FreemarkerUtils.process(sitemapTemplate.getStaticPath(), model);
       if (step == 0) {
         List<Article> articles = articleDao.findList(first, count, null, null);
         model.put("articles", articles);
         if (articles.size() < count) {
           step++;
           first = 0;
           count -= articles.size();
         } else {
           buildCount += build(templatePath, staticPath, model);
           articleDao.clear();
           articleDao.flush();
           staticPaths.add(staticPath);
           model.clear();
           index++;
           first += articles.size();
           count = SITEMAP_MAX_SIZE;
         }
       } else if (step == 1) {
         List<Product> products = productDao.findList(first, count, null, null);
         model.put("products", products);
         if (products.size() < count) {
           step++;
           first = 0;
           count -= products.size();
         } else {
           buildCount += build(templatePath, staticPath, model);
           productDao.clear();
           productDao.flush();
           staticPaths.add(staticPath);
           model.clear();
           index++;
           first += products.size();
           count = SITEMAP_MAX_SIZE;
         }
       } else if (step == 2) {
         List<Brand> brands = brandDao.findList(first, count, null, null);
         model.put("brands", brands);
         if (brands.size() < count) {
           step++;
           first = 0;
           count -= brands.size();
         } else {
           buildCount += build(templatePath, staticPath, model);
           brandDao.clear();
           brandDao.flush();
           staticPaths.add(staticPath);
           model.clear();
           index++;
           first += brands.size();
           count = SITEMAP_MAX_SIZE;
         }
       } else if (step == 3) {
         List<Promotion> promotions = promotionDao.findList(first, count, null, null);
         model.put("promotions", promotions);
         buildCount += build(templatePath, staticPath, model);
         promotionDao.clear();
         promotionDao.flush();
         staticPaths.add(staticPath);
         if (promotions.size() < count) {
           model.put("staticPaths", staticPaths);
           buildCount +=
               build(
                   sitemapIndexTemplate.getTemplatePath(),
                   sitemapIndexTemplate.getStaticPath(),
                   model);
           break;
         } else {
           model.clear();
           index++;
           first += promotions.size();
           count = SITEMAP_MAX_SIZE;
         }
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   return buildCount;
 }