use of org.summerb.minicms.api.dto.Attachment 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;
}
}
use of org.summerb.minicms.api.dto.Attachment 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;
}
Aggregations