@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);
  }
Exemplo n.º 2
0
 /** 生成索引 */
 @RequestMapping(value = "/build", method = RequestMethod.POST)
 public @ResponseBody Map<String, Object> build(
     BuildType buildType, Boolean isPurge, Integer first, Integer count) {
   long startTime = System.currentTimeMillis();
   if (first == null || first < 0) {
     first = 0;
   }
   if (count == null || count <= 0) {
     count = 50;
   }
   int buildCount = 0;
   boolean isCompleted = true;
   if (buildType == BuildType.article) {
     if (first == 0 && isPurge != null && isPurge) {
       searchService.purge(Article.class);
     }
     List<Article> articles = articleService.findList(null, null, null, first, count);
     for (Article article : articles) {
       searchService.index(article);
       buildCount++;
     }
     first += articles.size();
     if (articles.size() == count) {
       isCompleted = false;
     }
   } else if (buildType == BuildType.product) {
     if (first == 0 && isPurge != null && isPurge) {
       searchService.purge(Product.class);
     }
     List<Product> products = productService.findList(null, null, null, first, count);
     for (Product product : products) {
       searchService.index(product);
       buildCount++;
     }
     first += products.size();
     if (products.size() == count) {
       isCompleted = false;
     }
   }
   long endTime = System.currentTimeMillis();
   Map<String, Object> map = new HashMap<String, Object>();
   map.put("first", first);
   map.put("buildCount", buildCount);
   map.put("buildTime", endTime - startTime);
   map.put("isCompleted", isCompleted);
   return map;
 }