use of com.ganster.cms.core.pojo.ArticleExample in project Ganster-CMS by Gangster-trio.
the class ArticleServiceTest method PageHelperTest.
@Test
@Transactional
public void PageHelperTest() {
final String TEST_AUTHOR = "PageHelperTest.-@#$%^&*&^%";
// insert Data
List<Article> articles = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Article article = new Article();
article.setArticleAuthor(TEST_AUTHOR);
articles.add(article);
}
for (Article article : articles) {
articleService.insert(article);
}
ArticleExample articleExample = new ArticleExample();
articleExample.or().andArticleAuthorEqualTo(TEST_AUTHOR);
PageHelper.startPage(1, 2);
articles = articleService.selectByExample(articleExample);
PageInfo<Article> pageInfo = new PageInfo<>(articles);
// PageInfo page = PageHelper.startPage(1,5).doSelectPageInfo(() -> articleService.selectByExample(articleExample));
LOGGER.info(pageInfo.toString());
for (Article article : articles) {
LOGGER.info(article.toString());
}
Assert.assertEquals(articles.size(), 2);
// articleService.deleteByExample(articleExample);
}
use of com.ganster.cms.core.pojo.ArticleExample in project Ganster-CMS by Gangster-trio.
the class ArticleDirective method execute.
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException {
Integer id = DirectiveUtil.getInteger(PARAM_ID, params);
Boolean blob = DirectiveUtil.getBoolean(PARAM_BLOB, params);
if (blob == null) {
blob = false;
}
if (id == null) {
throw new TemplateException("Must special id", env);
}
Article article = null;
ArticleExample example = new ArticleExample();
example.or().andArticleIdEqualTo(id);
if (blob) {
List<Article> articleList = articleService.selectByExampleWithBLOBs(example);
if (!articleList.isEmpty()) {
article = articleList.get(0);
}
} else {
List<Article> articles = articleService.selectByExample(example);
if (!articles.isEmpty()) {
article = articles.get(0);
}
}
DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.getVersion());
env.setVariable(DirectiveUtil.getRetName(PARAM_RET, params, PARAM_RET), builder.build().wrap(article));
}
use of com.ganster.cms.core.pojo.ArticleExample 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());
}
Aggregations