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));
}
}
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();
}
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);
}
}
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);
}
}
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;
}
}
Aggregations