Search in sources :

Example 6 with Article

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

the class ArticleServiceImplTest method testCreateArticleContent_testDataTruncationError.

@Test
public void testCreateArticleContent_testDataTruncationError() throws Exception {
    Article a = buildTestDto();
    a.setArticleKey(generateLongString(Article.FN_KEY_SIZE, "Z"));
    a.setLang("ZZ");
    a.setTitle(generateLongString(Article.FN_TITLE_SIZE, "Ф"));
    a.setAnnotation(generateLongString(Article.FN_ANNOTATION_SIZE, "Ф"));
    a.setContent(generateLongString(Article.FN_ANNOTATION_SIZE, "Ф"));
    a.setArticleGroup(generateLongString(Article.FN_GROUP_SIZE, "Z"));
    articleService.create(a);
    // Update data with wrong length text
    try {
        a = buildTestDto();
        a.setArticleKey(generateLongString(Article.FN_KEY_SIZE + 1, "ф"));
        a.setLang("ZZZ");
        a.setTitle(generateLongString(Article.FN_TITLE_SIZE + 1, "Ф"));
        a.setAnnotation(generateLongString(Article.FN_ANNOTATION_SIZE + 1, "ф"));
        a.setContent(generateLongString(Article.FN_ANNOTATION_SIZE + 1, "ф"));
        a.setArticleGroup(generateLongString(Article.FN_GROUP_SIZE + 1, "Z"));
        articleService.create(a);
        fail("FVE expected");
    } catch (FieldValidationException fve) {
        assertEquals(5, fve.getErrors().size());
        assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_KEY));
        assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_LANG));
        assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_TITLE));
        assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_ANNOTATION));
        assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_GROUP));
    }
}
Also used : FieldValidationException(org.summerb.approaches.validation.FieldValidationException) Article(org.summerb.microservices.articles.api.dto.Article) Test(org.junit.Test)

Example 7 with Article

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

the class AttachmentServiceImplTest method createTestArticle.

private long createTestArticle() throws FieldValidationException, NotAuthorizedException {
    Article dto = new Article();
    dto.setArticleKey("key");
    dto.setLang("en");
    dto.setTitle("tttl");
    dto.setContent("cccc");
    Article result = articleService.create(dto);
    return result.getId();
}
Also used : Article(org.summerb.microservices.articles.api.dto.Article)

Example 8 with Article

use of org.summerb.microservices.articles.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.microservices.emailsender.impl.EmailMessageBuilderImpl) LocaleAwareEmailMessageBuilderImpl(org.summerb.microservices.emailsender.impl.LocaleAwareEmailMessageBuilderImpl) Article(org.summerb.microservices.articles.api.dto.Article) StringTemplateStaticImpl(org.summerb.utils.stringtemplate.impl.StringTemplateStaticImpl) LocaleAwareEmailMessageBuilderImpl(org.summerb.microservices.emailsender.impl.LocaleAwareEmailMessageBuilderImpl)

Example 9 with Article

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

the class ArticlesAuthoringController method updateArticle.

@RequestMapping(method = RequestMethod.POST, value = "/{id}")
@ResponseBody
public Map<String, ? extends Object> updateArticle(@RequestBody Article dto, @PathVariable("id") long id, HttpServletResponse response) {
    try {
        Article currentVersion = articleService.findById(id);
        currentVersion.setTitle(dto.getTitle());
        currentVersion.setAnnotation(dto.getAnnotation());
        currentVersion.setContent(dto.getContent());
        Article newVersion = articleService.update(currentVersion);
        // ret
        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put(ATTR_ARTICLE, newVersion);
        return ret;
    } catch (Throwable t) {
        log.error("Failed to update article content", t);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        String msg = exceptionTranslator.buildUserMessage(t, CurrentRequestUtils.getLocale());
        return Collections.singletonMap(ATTR_ERROR, msg);
    }
}
Also used : HashMap(java.util.HashMap) Article(org.summerb.microservices.articles.api.dto.Article) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 10 with Article

use of org.summerb.microservices.articles.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());
        articleService.addArticleAttachment(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));
        // TODO: Navigate to article authoring instead!
        return viewNameArticleAuthoring;
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) PageMessage(org.summerb.approaches.springmvc.model.PageMessage) Article(org.summerb.microservices.articles.api.dto.Article) Attachment(org.summerb.microservices.articles.api.dto.Attachment) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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