Search in sources :

Example 11 with Article

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

the class ArticleRendererCachingImplTest method testRenderArticle_expectDependentWillBeEvictedAsWell.

@Test
public void testRenderArticle_expectDependentWillBeEvictedAsWell() 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 ra1v2 = new RenderedArticle();
    ra1v2.setId(1L);
    ra1v2.setLang("ru");
    ra1v2.setTitle("t1v2");
    ra1v2.setArticleKey("a1");
    ra1v2.setContent("c");
    ra1v2.setArticleReferences(Arrays.asList(2L, 3L));
    when(renderer.renderArticle("a1", locale)).thenReturn(ra1v2);
    Article a3 = new Article();
    a3.setArticleKey("a3");
    a3.setLang("ru");
    fixture.onArticleChanged(EntityChangedEvent.updated(a3));
    ra1v2 = fixture.renderArticle("a1", locale);
    assertEquals("t1v2", ra1v2.getTitle());
}
Also used : Locale(java.util.Locale) ArticleRenderer(org.summerb.minicms.api.ArticleRenderer) Article(org.summerb.minicms.api.dto.Article) RenderedArticle(org.summerb.minicms.api.dto.consuming.RenderedArticle) EventBus(com.google.common.eventbus.EventBus) RenderedArticle(org.summerb.minicms.api.dto.consuming.RenderedArticle) Test(org.junit.Test)

Example 12 with Article

use of org.summerb.minicms.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 : Article(org.summerb.minicms.api.dto.Article) RenderedArticle(org.summerb.minicms.api.dto.consuming.RenderedArticle) GenericEntityNotFoundException(org.summerb.easycrud.api.exceptions.GenericEntityNotFoundException)

Example 13 with Article

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

the class ArticlesAuthoringController method createAttachment.

// NOTE: File upload example:
// http://www.ioncannon.net/programming/975/spring-3-file-upload-example/
@RequestMapping(method = RequestMethod.POST, value = "/{articleId}/addAttachment")
public String createAttachment(@ModelAttribute(ATTR_ARTICLE_ATTACHMENT) ArticleAttachmentVm articleAttachmentVm, Model model, @PathVariable("articleId") long articleId) throws Exception {
    try {
        Preconditions.checkArgument(articleAttachmentVm.getFile() != null && articleAttachmentVm.getFile().getFileItem() != null, "File required");
        Article article = articleService.findById(articleId);
        Preconditions.checkArgument(article != null, "Article not found");
        Attachment att = articleAttachmentVm.getAttachment();
        att.setArticleId(articleId);
        FileItem fileItem = articleAttachmentVm.getFile().getFileItem();
        att.setSize(fileItem.getSize());
        String fileName = fileItem.getName();
        att.setName(fileName);
        att.setContents(articleAttachmentVm.getFile().getInputStream());
        attachmentService.create(att);
        return Views.redirect(String.format("article-authoring/%s", article.getArticleKey()));
    } catch (Throwable t) {
        log.error("Failed to create attachment", t);
        String msg = exceptionTranslator.buildUserMessage(t, CurrentRequestUtils.getLocale());
        addPageMessage(model.asMap(), new PageMessage(msg, MessageSeverity.Danger));
        // TBD: Navigate to article authoring instead!
        return viewNameArticleAuthoring;
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) PageMessage(org.summerb.webappboilerplate.model.PageMessage) Article(org.summerb.minicms.api.dto.Article) Attachment(org.summerb.minicms.api.dto.Attachment) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with Article

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

the class ArticlesAuthoringController method getArticle.

@RequestMapping(method = RequestMethod.GET, value = "/{articleKey}")
public String getArticle(Model model, @PathVariable("articleKey") String articleKey, Locale locale) throws NotAuthorizedException {
    model.addAttribute(ATTR_ARTICLE_KEY, articleKey);
    Map<Locale, Article> options = articleService.findArticleLocalizations(articleKey);
    if (options.size() == 0) {
        throw new RuntimeException("Article not found: " + articleKey);
    }
    List<ArticleVm> contents = new LinkedList<ArticleVm>();
    for (Entry<Locale, Article> entry : options.entrySet()) {
        ArticleVm contentPm = new ArticleVm();
        contentPm.setDto(entry.getValue());
        contentPm.setAttachments(new ListPm<Attachment>(Arrays.asList(attachmentService.findArticleAttachments(entry.getValue().getId()))));
        contents.add(contentPm);
    }
    ArticlesVm articlesVm = new ArticlesVm(contents);
    model.addAttribute(ATTR_ARTICLES, articlesVm.getMap());
    model.addAttribute(ATTR_ARTICLE, contents.get(0));
    return viewNameArticleAuthoring;
}
Also used : Locale(java.util.Locale) ArticlesVm(org.summerb.webappboilerplate.articles.vm.ArticlesVm) ArticleVm(org.summerb.webappboilerplate.articles.vm.ArticleVm) Article(org.summerb.minicms.api.dto.Article) Attachment(org.summerb.minicms.api.dto.Attachment) LinkedList(java.util.LinkedList) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with Article

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

the class SecurityMailsMessageBuilderFactoryImpl method createEmailMessageBuilderFromArticle.

@Override
public EmailMessageBuilder createEmailMessageBuilderFromArticle(String articleKey, User sender) {
    try {
        Map<Locale, Article> options = articleService.findArticleLocalizations(articleKey);
        if (options == null || options.size() == 0) {
            log.error("Emails will not be sent! Article not found: " + articleKey);
        }
        LocaleAwareEmailMessageBuilderImpl ret = new LocaleAwareEmailMessageBuilderImpl();
        for (Entry<Locale, Article> entry : options.entrySet()) {
            EmailMessageBuilderImpl builder = new EmailMessageBuilderImpl();
            builder.setLocale(entry.getKey());
            builder.setFromNameTemplate(new StringTemplateStaticImpl(sender.getDisplayName()));
            builder.setToNameTemplate(stringTemplateCompiler.compile("${to.displayName}"));
            builder.setSubjectTemplate(new StringTemplateStaticImpl(entry.getValue().getTitle()));
            builder.setBodyTemplate(stringTemplateCompiler.compile(entry.getValue().getContent()));
            ret.getLocaleSpecificBuilders().add(builder);
        }
        return ret;
    } catch (Throwable t) {
        throw new RuntimeException("Failed to create email message builder using article id: " + articleKey, t);
    }
}
Also used : Locale(java.util.Locale) EmailMessageBuilderImpl(org.summerb.email.impl.EmailMessageBuilderImpl) LocaleAwareEmailMessageBuilderImpl(org.summerb.email.impl.LocaleAwareEmailMessageBuilderImpl) Article(org.summerb.minicms.api.dto.Article) StringTemplateStaticImpl(org.summerb.stringtemplate.impl.StringTemplateStaticImpl) LocaleAwareEmailMessageBuilderImpl(org.summerb.email.impl.LocaleAwareEmailMessageBuilderImpl)

Aggregations

Article (org.summerb.minicms.api.dto.Article)15 Locale (java.util.Locale)6 Test (org.junit.Test)5 RenderedArticle (org.summerb.minicms.api.dto.consuming.RenderedArticle)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 Attachment (org.summerb.minicms.api.dto.Attachment)3 EventBus (com.google.common.eventbus.EventBus)2 HashMap (java.util.HashMap)2 ArticleRenderer (org.summerb.minicms.api.ArticleRenderer)2 UrlBuilderTestImpl (integr.org.summerb.minicms.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.easycrud.api.exceptions.GenericEntityNotFoundException)1 EmailMessageBuilderImpl (org.summerb.email.impl.EmailMessageBuilderImpl)1 LocaleAwareEmailMessageBuilderImpl (org.summerb.email.impl.LocaleAwareEmailMessageBuilderImpl)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