use of run.halo.app.model.entity.Category in project halo by ruibaby.
the class BackupServiceImpl method importData.
@Override
public void importData(MultipartFile file) throws IOException {
String jsonContent = FileUtils.readString(file.getInputStream());
ObjectMapper mapper = JsonUtils.createDefaultJsonMapper();
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<>() {
};
HashMap<String, Object> data = mapper.readValue(jsonContent, typeRef);
String version = (String) Objects.requireNonNullElse(data.get("version"), "");
if (!VersionUtil.hasSameMajorAndMinorVersion(HaloConst.HALO_VERSION, version)) {
throw new BadRequestException("导入数据的主次版本号与当前系统版本号不匹配,不支持导入!");
}
List<Attachment> attachments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("attachments")), Attachment[].class));
attachmentService.createInBatch(attachments);
List<Category> categories = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("categories")), Category[].class));
categoryService.createInBatch(categories);
List<Tag> tags = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("tags")), Tag[].class));
tagService.createInBatch(tags);
List<CommentBlackList> commentBlackList = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("comment_black_list")), CommentBlackList[].class));
commentBlackListService.createInBatch(commentBlackList);
List<Journal> journals = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("journals")), Journal[].class));
journalService.createInBatch(journals);
List<JournalComment> journalComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("journal_comments")), JournalComment[].class));
journalCommentService.createInBatch(journalComments);
List<Link> links = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("links")), Link[].class));
linkService.createInBatch(links);
List<Log> logs = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("logs")), Log[].class));
logService.createInBatch(logs);
List<Menu> menus = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("menus")), Menu[].class));
menuService.createInBatch(menus);
List<Option> options = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("options")), Option[].class));
optionService.createInBatch(options);
eventPublisher.publishEvent(new OptionUpdatedEvent(this));
List<Photo> photos = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("photos")), Photo[].class));
photoService.createInBatch(photos);
List<Post> posts = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("posts")), Post[].class));
postService.createInBatch(posts);
List<Content> contents = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("contents")), Content[].class));
contentService.createInBatch(contents);
List<ContentPatchLog> contentPatchLogs = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("content_patch_logs")), ContentPatchLog[].class));
contentPatchLogService.createInBatch(contentPatchLogs);
List<PostCategory> postCategories = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_categories")), PostCategory[].class));
postCategoryService.createInBatch(postCategories);
List<PostComment> postComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_comments")), PostComment[].class));
postCommentService.createInBatch(postComments);
List<PostMeta> postMetas = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_metas")), PostMeta[].class));
postMetaService.createInBatch(postMetas);
List<PostTag> postTags = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("post_tags")), PostTag[].class));
postTagService.createInBatch(postTags);
List<Sheet> sheets = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheets")), Sheet[].class));
sheetService.createInBatch(sheets);
List<SheetComment> sheetComments = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheet_comments")), SheetComment[].class));
sheetCommentService.createInBatch(sheetComments);
List<SheetMeta> sheetMetas = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("sheet_metas")), SheetMeta[].class));
sheetMetaService.createInBatch(sheetMetas);
List<ThemeSetting> themeSettings = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("theme_settings")), ThemeSetting[].class));
themeSettingService.createInBatch(themeSettings);
eventPublisher.publishEvent(new ThemeUpdatedEvent(this));
List<User> users = Arrays.asList(mapper.readValue(mapper.writeValueAsString(data.get("user")), User[].class));
if (users.size() > 0) {
userService.create(users.get(0));
}
}
use of run.halo.app.model.entity.Category in project halo by ruibaby.
the class PostAssembler method convertTo.
/**
* Converts to post detail vo.
*
* @param post post must not be null
* @param tags tags
* @param categories categories
* @param postMetaList postMetaList
* @return post detail vo
*/
@NonNull
public PostDetailVO convertTo(@NonNull Post post, @Nullable List<Tag> tags, @Nullable List<Category> categories, List<PostMeta> postMetaList) {
Assert.notNull(post, "Post must not be null");
// Convert to base detail vo
PostDetailVO postDetailVO = new PostDetailVO().convertFrom(post);
generateAndSetSummaryIfAbsent(post, postDetailVO);
// Extract ids
Set<Integer> tagIds = ServiceUtils.fetchProperty(tags, Tag::getId);
Set<Integer> categoryIds = ServiceUtils.fetchProperty(categories, Category::getId);
Set<Long> metaIds = ServiceUtils.fetchProperty(postMetaList, PostMeta::getId);
// Get post tag ids
postDetailVO.setTagIds(tagIds);
postDetailVO.setTags(tagService.convertTo(tags));
// Get post category ids
postDetailVO.setCategoryIds(categoryIds);
postDetailVO.setCategories(categoryService.convertTo(categories));
// Get post meta ids
postDetailVO.setMetaIds(metaIds);
postDetailVO.setMetas(postMetaService.convertTo(postMetaList));
postDetailVO.setCommentCount(postCommentService.countByStatusAndPostId(CommentStatus.PUBLISHED, post.getId()));
postDetailVO.setFullPath(buildFullPath(post));
PatchedContent postContent = post.getContent();
postDetailVO.setContent(postContent.getContent());
postDetailVO.setOriginalContent(postContent.getOriginalContent());
// Post currently drafting in process
Boolean inProgress = contentService.draftingInProgress(post.getId());
postDetailVO.setInProgress(inProgress);
return postDetailVO;
}
use of run.halo.app.model.entity.Category in project halo by ruibaby.
the class PostServiceImpl method createOrUpdate.
private PostDetailVO createOrUpdate(@NonNull Post post, Set<Integer> tagIds, Set<Integer> categoryIds, Set<PostMeta> metas) {
Assert.notNull(post, "Post param must not be null");
// if password is not empty
if (post.getStatus() != PostStatus.DRAFT && (StringUtils.isNotEmpty(post.getPassword()))) {
post.setStatus(PostStatus.INTIMATE);
}
post = super.createOrUpdateBy(post);
postTagService.removeByPostId(post.getId());
postCategoryService.removeByPostId(post.getId());
// List all tags
List<Tag> tags = tagService.listAllByIds(tagIds);
// List all categories
List<Category> categories = categoryService.listAllByIds(categoryIds);
// Create post tags
List<PostTag> postTags = postTagService.mergeOrCreateByIfAbsent(post.getId(), ServiceUtils.fetchProperty(tags, Tag::getId));
log.debug("Created post tags: [{}]", postTags);
// Create post categories
List<PostCategory> postCategories = postCategoryService.mergeOrCreateByIfAbsent(post.getId(), ServiceUtils.fetchProperty(categories, Category::getId));
log.debug("Created post categories: [{}]", postCategories);
// Create post meta data
List<PostMeta> postMetaList = postMetaService.createOrUpdateByPostId(post.getId(), metas);
log.debug("Created post metas: [{}]", postMetaList);
// Publish post updated event.
applicationContext.publishEvent(new PostUpdatedEvent(this, post));
// get draft content by head patch log id
Content postContent = postContentService.getById(post.getId());
post.setContent(postContentPatchLogService.getPatchedContentById(postContent.getHeadPatchLogId()));
// Convert to post detail vo
return postAssembler.convertTo(post, tags, categories, postMetaList);
}
use of run.halo.app.model.entity.Category in project halo by ruibaby.
the class PostServiceImpl method importMarkdown.
@Override
public PostDetailVO importMarkdown(String markdown, String filename) {
Assert.notNull(markdown, "Markdown document must not be null");
// Gets frontMatter
Map<String, List<String>> frontMatter = MarkdownUtils.getFrontMatter(markdown);
// remove frontMatter
markdown = MarkdownUtils.removeFrontMatter(markdown);
PostParam post = new PostParam();
post.setStatus(null);
List<String> elementValue;
Set<Integer> tagIds = new HashSet<>();
Set<Integer> categoryIds = new HashSet<>();
if (frontMatter.size() > 0) {
for (String key : frontMatter.keySet()) {
elementValue = frontMatter.get(key);
for (String ele : elementValue) {
ele = HaloUtils.strip(ele, "[", "]");
ele = StringUtils.strip(ele, "\"");
ele = StringUtils.strip(ele, "\'");
if ("".equals(ele)) {
continue;
}
switch(key) {
case "title":
post.setTitle(ele);
break;
case "date":
post.setCreateTime(DateUtils.parseDate(ele));
break;
case "permalink":
post.setSlug(ele);
break;
case "thumbnail":
post.setThumbnail(ele);
break;
case "status":
post.setStatus(PostStatus.valueOf(ele));
break;
case "comments":
post.setDisallowComment(Boolean.parseBoolean(ele));
break;
case "tags":
Tag tag;
for (String tagName : ele.split(",")) {
tagName = tagName.trim();
tagName = StringUtils.strip(tagName, "\"");
tagName = StringUtils.strip(tagName, "\'");
tag = tagService.getByName(tagName);
String slug = SlugUtils.slug(tagName);
if (null == tag) {
tag = tagService.getBySlug(slug);
}
if (null == tag) {
tag = new Tag();
tag.setName(tagName);
tag.setSlug(slug);
tag = tagService.create(tag);
}
tagIds.add(tag.getId());
}
break;
case "categories":
Integer lastCategoryId = null;
for (String categoryName : ele.split(",")) {
categoryName = categoryName.trim();
categoryName = StringUtils.strip(categoryName, "\"");
categoryName = StringUtils.strip(categoryName, "\'");
Category category = categoryService.getByName(categoryName);
if (null == category) {
category = new Category();
category.setName(categoryName);
category.setSlug(SlugUtils.slug(categoryName));
category.setDescription(categoryName);
if (lastCategoryId != null) {
category.setParentId(lastCategoryId);
}
category = categoryService.create(category);
}
lastCategoryId = category.getId();
categoryIds.add(lastCategoryId);
}
break;
default:
break;
}
}
}
}
if (null == post.getStatus()) {
post.setStatus(PostStatus.PUBLISHED);
}
if (StringUtils.isEmpty(post.getTitle())) {
post.setTitle(filename);
}
if (StringUtils.isEmpty(post.getSlug())) {
post.setSlug(SlugUtils.slug(post.getTitle()));
}
post.setOriginalContent(markdown);
return createBy(post.convertTo(), tagIds, categoryIds, false);
}
use of run.halo.app.model.entity.Category in project halo by ruibaby.
the class PostServiceImpl method convertToPostMarkdownVo.
private PostMarkdownVO convertToPostMarkdownVo(Post post) {
PostMarkdownVO postMarkdownVO = new PostMarkdownVO();
StringBuilder frontMatter = new StringBuilder("---\n");
frontMatter.append("title: ").append(post.getTitle()).append("\n");
frontMatter.append("date: ").append(post.getCreateTime()).append("\n");
frontMatter.append("updated: ").append(post.getUpdateTime()).append("\n");
// set fullPath
frontMatter.append("url: ").append(postAssembler.buildFullPath(post)).append("\n");
// set category
List<Category> categories = postCategoryService.listCategoriesBy(post.getId());
StringBuilder categoryContent = new StringBuilder();
for (int i = 0; i < categories.size(); i++) {
Category category = categories.get(i);
String categoryName = category.getName();
if (i == 0) {
categoryContent.append(categoryName);
} else {
categoryContent.append(" | ").append(categoryName);
}
}
frontMatter.append("categories: ").append(categoryContent.toString()).append("\n");
// set tags
List<Tag> tags = postTagService.listTagsBy(post.getId());
StringBuilder tagContent = new StringBuilder();
for (int i = 0; i < tags.size(); i++) {
Tag tag = tags.get(i);
String tagName = tag.getName();
if (i == 0) {
tagContent.append(tagName);
} else {
tagContent.append(" | ").append(tagName);
}
}
frontMatter.append("tags: ").append(tagContent).append("\n");
frontMatter.append("---\n");
postMarkdownVO.setFrontMatter(frontMatter.toString());
PatchedContent postContent = post.getContent();
postMarkdownVO.setOriginalContent(postContent.getOriginalContent());
postMarkdownVO.setTitle(post.getTitle());
postMarkdownVO.setSlug(post.getSlug());
return postMarkdownVO;
}
Aggregations