Search in sources :

Example 1 with RenderedArticle

use of org.summerb.minicms.api.dto.consuming.RenderedArticle in project summerb by skarpushin.

the class ArticleRendererImpl method renderArticle.

@Override
public RenderedArticle renderArticle(String key, Locale locale) {
    Preconditions.checkArgument(StringUtils.hasText(key));
    Preconditions.checkArgument(locale != null);
    try {
        Article article = articleService.findArticleByKeyAndLocale(key, locale);
        if (article == null) {
            throw new RuntimeException("Article not found: " + key + ", locale: " + locale);
        }
        RenderedArticle renderedArticle = buildRenderedArticleTemplate(article);
        ArticleRenderingContext articleRenderingContext = new ArticleRenderingContext(locale, renderedArticle, articleService, attachmentService, articleAbsoluteUrlBuilder);
        StringTemplate annotationTemplate = stringTemplateCompiler.compile(article.getAnnotation());
        renderedArticle.setAnnotation(annotationTemplate.applyTo(articleRenderingContext));
        StringTemplate contentTemplate = stringTemplateCompiler.compile(article.getContent());
        renderedArticle.setContent(contentTemplate.applyTo(articleRenderingContext));
        return renderedArticle;
    } catch (Throwable t) {
        throw new RuntimeException("Failed to render article", t);
    }
}
Also used : Article(org.summerb.minicms.api.dto.Article) RenderedArticle(org.summerb.minicms.api.dto.consuming.RenderedArticle) StringTemplate(org.summerb.stringtemplate.api.StringTemplate) RenderedArticle(org.summerb.minicms.api.dto.consuming.RenderedArticle)

Example 2 with RenderedArticle

use of org.summerb.minicms.api.dto.consuming.RenderedArticle in project summerb by skarpushin.

the class ArticleRendererImpl method buildRenderedArticleTemplate.

private RenderedArticle buildRenderedArticleTemplate(Article article) {
    RenderedArticle renderedArticle = new RenderedArticle();
    renderedArticle.setId(article.getId());
    renderedArticle.setArticleKey(article.getArticleKey());
    renderedArticle.setLang(article.getLang());
    renderedArticle.setCreatedAt(article.getCreatedAt());
    renderedArticle.setCreatedBy(article.getCreatedBy());
    renderedArticle.setModifiedAt(article.getModifiedAt());
    renderedArticle.setModifiedBy(article.getModifiedBy());
    renderedArticle.setTitle(article.getTitle());
    renderedArticle.setArticleGroup(article.getArticleGroup());
    return renderedArticle;
}
Also used : RenderedArticle(org.summerb.minicms.api.dto.consuming.RenderedArticle)

Example 3 with RenderedArticle

use of org.summerb.minicms.api.dto.consuming.RenderedArticle in project summerb by skarpushin.

the class ArticleRendererCachingImplTest method testRenderArticle_expectArticleCacheInvalidatedOnAttachmentChange.

@Test
public void testRenderArticle_expectArticleCacheInvalidatedOnAttachmentChange() throws Exception {
    ArticleRendererCachingImpl fixture = new ArticleRendererCachingImpl();
    fixture.setEventBus(mock(EventBus.class));
    ArticleRenderer renderer = mock(ArticleRenderer.class);
    fixture.setArticleRenderer(renderer);
    fixture.afterPropertiesSet();
    Locale locale = new Locale("ru");
    mockTest(renderer, locale);
    // force cache
    fixture.renderArticle("a1", locale);
    fixture.renderArticle("a2", locale);
    fixture.renderArticle("a3", locale);
    RenderedArticle ra2v2 = new RenderedArticle();
    ra2v2.setId(1L);
    ra2v2.setLang("ru");
    ra2v2.setTitle("t2v2");
    ra2v2.setArticleKey("a2");
    ra2v2.setContent("c");
    ra2v2.setArticleReferences(Arrays.asList(3L));
    when(renderer.renderArticle("a2", locale)).thenReturn(ra2v2);
    Attachment att3 = new Attachment();
    att3.setArticleId(3L);
    fixture.onArticleAttachmentChanged(EntityChangedEvent.removedObject(att3));
    ra2v2 = fixture.renderArticle("a2", locale);
    assertEquals("t2v2", ra2v2.getTitle());
}
Also used : Locale(java.util.Locale) ArticleRenderer(org.summerb.minicms.api.ArticleRenderer) Attachment(org.summerb.minicms.api.dto.Attachment) EventBus(com.google.common.eventbus.EventBus) RenderedArticle(org.summerb.minicms.api.dto.consuming.RenderedArticle) Test(org.junit.Test)

Example 4 with RenderedArticle

use of org.summerb.minicms.api.dto.consuming.RenderedArticle in project summerb by skarpushin.

the class ArticleRendererCachingImplTest method mockTest.

private void mockTest(ArticleRenderer renderer, Locale locale) {
    RenderedArticle ra1 = new RenderedArticle();
    ra1.setId(1L);
    ra1.setLang("ru");
    ra1.setTitle("t1v1");
    ra1.setArticleKey("a1");
    ra1.setContent("c");
    ra1.setArticleReferences(Arrays.asList(2L, 3L));
    when(renderer.renderArticle("a1", locale)).thenReturn(ra1);
    RenderedArticle ra2 = new RenderedArticle();
    ra2.setId(2L);
    ra2.setLang("ru");
    ra2.setTitle("t2v1");
    ra2.setArticleKey("a2");
    ra2.setContent("c");
    ra2.setArticleReferences(Arrays.asList(3L));
    when(renderer.renderArticle("a2", locale)).thenReturn(ra2);
    RenderedArticle ra3 = new RenderedArticle();
    ra3.setId(3L);
    ra3.setLang("ru");
    ra3.setTitle("t3v1");
    ra3.setArticleKey("a3");
    ra3.setContent("c");
    when(renderer.renderArticle("a3", locale)).thenReturn(ra3);
}
Also used : RenderedArticle(org.summerb.minicms.api.dto.consuming.RenderedArticle)

Example 5 with RenderedArticle

use of org.summerb.minicms.api.dto.consuming.RenderedArticle in project summerb by skarpushin.

the class ArticleController method get.

@RequestMapping(method = RequestMethod.GET, value = ArticleAbsoluteUrlBuilderImpl.DEFAULT_PATH_ARTICLES + "/{articleKey}")
public String get(Model model, @PathVariable("articleKey") String articleKey, HttpServletResponse respons, Locale locale) {
    try {
        RenderedArticle article = articleRenderer.renderArticle(articleKey, locale);
        model.addAttribute(ATTR_ARTICLE, article);
    } catch (Throwable t) {
        log.debug("Failed to get article", t);
        String msg = exceptionTranslator.buildUserMessage(t, CurrentRequestUtils.getLocale());
        addPageMessage(model.asMap(), new PageMessage(msg, MessageSeverity.Danger));
    }
    return viewNameArticle;
}
Also used : PageMessage(org.summerb.webappboilerplate.model.PageMessage) RenderedArticle(org.summerb.minicms.api.dto.consuming.RenderedArticle) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RenderedArticle (org.summerb.minicms.api.dto.consuming.RenderedArticle)9 Locale (java.util.Locale)4 Test (org.junit.Test)4 Article (org.summerb.minicms.api.dto.Article)4 EventBus (com.google.common.eventbus.EventBus)3 ArticleRenderer (org.summerb.minicms.api.ArticleRenderer)3 Attachment (org.summerb.minicms.api.dto.Attachment)2 UrlBuilderTestImpl (integr.org.summerb.minicms.impl.UrlBuilderTestImpl)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ArticleAbsoluteUrlBuilder (org.summerb.minicms.api.ArticleAbsoluteUrlBuilder)1 ArticleService (org.summerb.minicms.api.ArticleService)1 AttachmentService (org.summerb.minicms.api.AttachmentService)1 ArticleRendererImpl (org.summerb.minicms.impl.ArticleRendererImpl)1 StringTemplate (org.summerb.stringtemplate.api.StringTemplate)1 StringTemplateCompilerlImpl (org.summerb.stringtemplate.impl.StringTemplateCompilerlImpl)1 PageMessage (org.summerb.webappboilerplate.model.PageMessage)1