Search in sources :

Example 1 with Article

use of org.summerb.microservices.articles.api.dto.Article 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, 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 : RenderedArticle(org.summerb.microservices.articles.api.dto.consuming.RenderedArticle) Article(org.summerb.microservices.articles.api.dto.Article) StringTemplate(org.summerb.utils.stringtemplate.api.StringTemplate) RenderedArticle(org.summerb.microservices.articles.api.dto.consuming.RenderedArticle)

Example 2 with Article

use of org.summerb.microservices.articles.api.dto.Article in project summerb by skarpushin.

the class ArticleRenderingContext method getReferencedArticle.

protected Article getReferencedArticle(String articleKey) throws GenericEntityNotFoundException {
    if (referencedArticles.containsKey(articleKey)) {
        return referencedArticles.get(articleKey);
    }
    try {
        Article ret = articleService.findArticleByKeyAndLocale(articleKey, locale);
        if (ret == null) {
            throw new GenericEntityNotFoundException("article", articleKey);
        }
        referencedArticles.put(articleKey, ret);
        registerReferencedArticle(ret.getId());
        return ret;
    } catch (Throwable e) {
        Throwables.throwIfInstanceOf(e, GenericEntityNotFoundException.class);
        throw new RuntimeException("Failed to find effective referenced article permutation " + articleKey, e);
    }
}
Also used : RenderedArticle(org.summerb.microservices.articles.api.dto.consuming.RenderedArticle) Article(org.summerb.microservices.articles.api.dto.Article) GenericEntityNotFoundException(org.summerb.approaches.jdbccrud.api.exceptions.GenericEntityNotFoundException)

Example 3 with Article

use of org.summerb.microservices.articles.api.dto.Article in project summerb by skarpushin.

the class ArticleServiceImpl method findArticleLocalizations.

@Override
public Map<Locale, Article> findArticleLocalizations(String articleKey) {
    try {
        PaginatedList<Article> articleOptions = query(PagerParams.ALL, Query.n().eq(Article.FN_KEY, articleKey));
        Map<Locale, Article> ret = new HashMap<Locale, Article>(articleOptions.getItems().size());
        for (Article a : articleOptions.getItems()) {
            ret.put(new Locale(a.getLang()), a);
        }
        return ret;
    } catch (Throwable t) {
        throw new RuntimeException("Failed to find all article localizations", t);
    }
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) Article(org.summerb.microservices.articles.api.dto.Article)

Example 4 with Article

use of org.summerb.microservices.articles.api.dto.Article in project summerb by skarpushin.

the class ArticleRenderingServiceImplTest method testRenderArticle_smokeTest.

@Test
public void testRenderArticle_smokeTest() throws Exception {
    ArticleAbsoluteUrlBuilder articleAbsoluteUrlBuilder = new UrlBuilderTestImpl();
    ArticleService articleService = Mockito.mock(ArticleService.class);
    Article a1 = new Article();
    a1.setId(1L);
    a1.setArticleKey("a1");
    a1.setAnnotation("Article annotation. Read other article first: ${article['a2']}");
    a1.setContent("Article text. See: ${article['a2']}. And now - here is the screenshot: ${img['screenshot1.jpg']}");
    when(articleService.findArticleByKeyAndLocale(eq("a1"), any(Locale.class))).thenReturn(a1);
    Attachment att1 = new Attachment();
    att1.setId(1L);
    att1.setArticleId(1L);
    att1.setName("screenshot1.jpg");
    when(articleService.findArticleAttachments(1L)).thenReturn(new Attachment[] { att1 });
    Article a2 = new Article();
    a2.setId(1L);
    a2.setArticleKey("a2");
    a2.setTitle("Title of article 2");
    a2.setAnnotation("Article annotation. Read other article first: ${article['a2']}");
    a2.setContent("Article text. See: ${article['a2']}. And now - here is the screenshot: ${img['screenshot1.jpg']}");
    when(articleService.findArticleByKeyAndLocale(eq("a2"), any(Locale.class))).thenReturn(a2);
    ArticleRendererImpl fixture = new ArticleRendererImpl();
    fixture.setArticleAbsoluteUrlBuilder(articleAbsoluteUrlBuilder);
    fixture.setArticleService(articleService);
    fixture.setStringTemplateCompiler(new StringTemplateCompilerlImpl());
    RenderedArticle result = fixture.renderArticle("a1", Locale.ENGLISH);
    assertNotNull(result);
    String a2href = "<a href=\"url-article:a2\" title=\"Title of article 2\">Title of article 2</a>";
    assertEquals("Article annotation. Read other article first: " + a2href, result.getAnnotation());
    assertEquals("Article text. See: " + a2href + ". And now - here is the screenshot: <img class=\"article-image\" src=\"url-att:screenshot1.jpg\" alt=\"screenshot1.jpg\" />", result.getContent());
}
Also used : Locale(java.util.Locale) ArticleService(org.summerb.microservices.articles.api.ArticleService) StringTemplateCompilerlImpl(org.summerb.utils.stringtemplate.impl.StringTemplateCompilerlImpl) ArticleAbsoluteUrlBuilder(org.summerb.microservices.articles.api.ArticleAbsoluteUrlBuilder) RenderedArticle(org.summerb.microservices.articles.api.dto.consuming.RenderedArticle) Article(org.summerb.microservices.articles.api.dto.Article) ArticleRendererImpl(org.summerb.microservices.articles.impl.ArticleRendererImpl) Attachment(org.summerb.microservices.articles.api.dto.Attachment) UrlBuilderTestImpl(integr.ru.skarpushin.services.articles.impl.UrlBuilderTestImpl) RenderedArticle(org.summerb.microservices.articles.api.dto.consuming.RenderedArticle) Test(org.junit.Test)

Example 5 with Article

use of org.summerb.microservices.articles.api.dto.Article in project summerb by skarpushin.

the class ArticleServiceImplTest method testCreateArticle_expectOk.

@Test
public void testCreateArticle_expectOk() throws Exception {
    Article a = buildTestDto();
    Article result = articleService.create(a);
    assertNotNull(result);
    Article b = articleService.findById(result.getId());
    assertNotNull(b);
    assertEquals(a.getArticleKey(), b.getArticleKey());
    assertEquals(a.getTitle(), b.getTitle());
    assertEquals(a.getAnnotation(), b.getAnnotation());
    assertEquals(a.getContent(), b.getContent());
    assertEquals(a.getArticleGroup(), b.getArticleGroup());
}
Also used : Article(org.summerb.microservices.articles.api.dto.Article) Test(org.junit.Test)

Aggregations

Article (org.summerb.microservices.articles.api.dto.Article)15 Locale (java.util.Locale)6 Test (org.junit.Test)5 RenderedArticle (org.summerb.microservices.articles.api.dto.consuming.RenderedArticle)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 Attachment (org.summerb.microservices.articles.api.dto.Attachment)3 EventBus (com.google.common.eventbus.EventBus)2 HashMap (java.util.HashMap)2 ArticleRenderer (org.summerb.microservices.articles.api.ArticleRenderer)2 UrlBuilderTestImpl (integr.ru.skarpushin.services.articles.impl.UrlBuilderTestImpl)1 LinkedList (java.util.LinkedList)1 FileItem (org.apache.commons.fileupload.FileItem)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 GenericEntityNotFoundException (org.summerb.approaches.jdbccrud.api.exceptions.GenericEntityNotFoundException)1 PageMessage (org.summerb.approaches.springmvc.model.PageMessage)1 FieldValidationException (org.summerb.approaches.validation.FieldValidationException)1 ArticleAbsoluteUrlBuilder (org.summerb.microservices.articles.api.ArticleAbsoluteUrlBuilder)1 ArticleService (org.summerb.microservices.articles.api.ArticleService)1 ArticleRendererImpl (org.summerb.microservices.articles.impl.ArticleRendererImpl)1 ArticleVm (org.summerb.microservices.articles.mvc.vm.ArticleVm)1