Search in sources :

Example 1 with CategoryExample

use of com.ganster.cms.core.pojo.CategoryExample in project Ganster-CMS by Gangster-trio.

the class TypeDirective method execute.

@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    String cateType = DirectiveUtil.getString(PARAM_CATEGORY_TYPE, params);
    String articleType = DirectiveUtil.getString(PARAM_ARTICLE_TYPE, params);
    String sort = DirectiveUtil.getString(PARAM_SORT, params);
    Integer size = DirectiveUtil.getInteger(PARAM_SIZE, params);
    Integer page = DirectiveUtil.getInteger(PARAM_PAGE, params);
    Site site = DirectiveUtil.getSite(env);
    if (site == null) {
        throw new TemplateException("site can't found", env);
    }
    // 异或非运算 有且仅有一个不为null
    if ((cateType == null) == (articleType == null)) {
        throw new TemplateException(PARAM_ARTICLE_TYPE + " and " + PARAM_CATEGORY_TYPE + " only one must be specified.", env);
    }
    if (size == null) {
        size = 0;
    }
    if (page == null) {
        page = 0;
    }
    if (sort == null) {
        if (cateType != null)
            sort = DEFAULT_CATE_SORT;
        if (articleType != null)
            sort = DEFAULT_ARTICLE_SORT;
    }
    List retList;
    if (cateType != null) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.or().andCategoryTypeEqualTo(cateType).andCategorySiteIdEqualTo(site.getSiteId());
        categoryExample.setOrderByClause(sort);
        retList = PageHelper.startPage(page, size).doSelectPage(() -> categoryService.selectByExample(categoryExample));
    } else {
        ArticleExample articleExample = new ArticleExample();
        articleExample.or().andArticleTypeEqualTo(articleType).andArticleSiteIdEqualTo(site.getSiteId());
        articleExample.setOrderByClause(sort);
        retList = PageHelper.startPage(page, size).doSelectPage(() -> articleService.selectByExample(articleExample));
    }
    DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.getVersion());
    env.setVariable(DirectiveUtil.getRetName(PARAM_RET, params), builder.build().wrap(retList));
    body.render(env.getOut());
}
Also used : Site(com.ganster.cms.core.pojo.Site) CategoryExample(com.ganster.cms.core.pojo.CategoryExample) List(java.util.List) ArticleExample(com.ganster.cms.core.pojo.ArticleExample)

Aggregations

ArticleExample (com.ganster.cms.core.pojo.ArticleExample)1 CategoryExample (com.ganster.cms.core.pojo.CategoryExample)1 Site (com.ganster.cms.core.pojo.Site)1 List (java.util.List)1