Search in sources :

Example 1 with PostCategory

use of run.halo.app.model.entity.PostCategory 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));
    }
}
Also used : SheetMeta(run.halo.app.model.entity.SheetMeta) HashMap(java.util.HashMap) JournalComment(run.halo.app.model.entity.JournalComment) Attachment(run.halo.app.model.entity.Attachment) Journal(run.halo.app.model.entity.Journal) Photo(run.halo.app.model.entity.Photo) ContentPatchLog(run.halo.app.model.entity.ContentPatchLog) PostComment(run.halo.app.model.entity.PostComment) PostMeta(run.halo.app.model.entity.PostMeta) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ContentPatchLog(run.halo.app.model.entity.ContentPatchLog) Log(run.halo.app.model.entity.Log) Content(run.halo.app.model.entity.Content) ThemeUpdatedEvent(run.halo.app.event.theme.ThemeUpdatedEvent) Option(run.halo.app.model.entity.Option) CommentBlackList(run.halo.app.model.entity.CommentBlackList) Link(run.halo.app.model.entity.Link) SheetComment(run.halo.app.model.entity.SheetComment) Category(run.halo.app.model.entity.Category) PostCategory(run.halo.app.model.entity.PostCategory) User(run.halo.app.model.entity.User) OptionUpdatedEvent(run.halo.app.event.options.OptionUpdatedEvent) PostCategory(run.halo.app.model.entity.PostCategory) ThemeSetting(run.halo.app.model.entity.ThemeSetting) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Menu(run.halo.app.model.entity.Menu) PostTag(run.halo.app.model.entity.PostTag) Post(run.halo.app.model.entity.Post) BadRequestException(run.halo.app.exception.BadRequestException) PostTag(run.halo.app.model.entity.PostTag) Tag(run.halo.app.model.entity.Tag) Sheet(run.halo.app.model.entity.Sheet)

Example 2 with PostCategory

use of run.halo.app.model.entity.PostCategory 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);
}
Also used : PostCategory(run.halo.app.model.entity.PostCategory) Category(run.halo.app.model.entity.Category) PostMeta(run.halo.app.model.entity.PostMeta) PostCategory(run.halo.app.model.entity.PostCategory) Content(run.halo.app.model.entity.Content) PatchedContent(run.halo.app.model.entity.Content.PatchedContent) PostUpdatedEvent(run.halo.app.event.post.PostUpdatedEvent) PostTag(run.halo.app.model.entity.PostTag) Tag(run.halo.app.model.entity.Tag) PostTag(run.halo.app.model.entity.PostTag)

Example 3 with PostCategory

use of run.halo.app.model.entity.PostCategory in project halo by ruibaby.

the class PostServiceImpl method removeById.

@Override
public Post removeById(Integer postId) {
    Assert.notNull(postId, "Post id must not be null");
    log.debug("Removing post: [{}]", postId);
    // Remove post tags
    List<PostTag> postTags = postTagService.removeByPostId(postId);
    log.debug("Removed post tags: [{}]", postTags);
    // Remove post categories
    List<PostCategory> postCategories = postCategoryService.removeByPostId(postId);
    log.debug("Removed post categories: [{}]", postCategories);
    // Remove metas
    List<PostMeta> metas = postMetaService.removeByPostId(postId);
    log.debug("Removed post metas: [{}]", metas);
    // Remove post comments
    List<PostComment> postComments = postCommentService.removeByPostId(postId);
    log.debug("Removed post comments: [{}]", postComments);
    // Remove post content
    Content postContent = postContentService.removeById(postId);
    log.debug("Removed post content: [{}]", postContent);
    Post deletedPost = super.removeById(postId);
    // Log it
    eventPublisher.publishEvent(new LogEvent(this, postId.toString(), LogType.POST_DELETED, deletedPost.getTitle()));
    return deletedPost;
}
Also used : PostMeta(run.halo.app.model.entity.PostMeta) PostCategory(run.halo.app.model.entity.PostCategory) LogEvent(run.halo.app.event.logger.LogEvent) Content(run.halo.app.model.entity.Content) PatchedContent(run.halo.app.model.entity.Content.PatchedContent) Post(run.halo.app.model.entity.Post) PostComment(run.halo.app.model.entity.PostComment) PostTag(run.halo.app.model.entity.PostTag)

Example 4 with PostCategory

use of run.halo.app.model.entity.PostCategory in project halo by ruibaby.

the class PostCategoryServiceImpl method mergeOrCreateByIfAbsent.

@Override
public List<PostCategory> mergeOrCreateByIfAbsent(Integer postId, Set<Integer> categoryIds) {
    Assert.notNull(postId, "Post id must not be null");
    if (CollectionUtils.isEmpty(categoryIds)) {
        return Collections.emptyList();
    }
    // Build post categories
    List<PostCategory> postCategoriesStaging = categoryIds.stream().map(categoryId -> {
        PostCategory postCategory = new PostCategory();
        postCategory.setPostId(postId);
        postCategory.setCategoryId(categoryId);
        return postCategory;
    }).collect(Collectors.toList());
    List<PostCategory> postCategoriesToCreate = new LinkedList<>();
    List<PostCategory> postCategoriesToRemove = new LinkedList<>();
    // Find all exist post categories
    List<PostCategory> postCategories = postCategoryRepository.findAllByPostId(postId);
    postCategories.forEach(postCategory -> {
        if (!postCategoriesStaging.contains(postCategory)) {
            postCategoriesToRemove.add(postCategory);
        }
    });
    postCategoriesStaging.forEach(postCategoryStaging -> {
        if (!postCategories.contains(postCategoryStaging)) {
            postCategoriesToCreate.add(postCategoryStaging);
        }
    });
    // Remove post categories
    removeAll(postCategoriesToRemove);
    // Remove all post categories need to remove
    postCategories.removeAll(postCategoriesToRemove);
    // Add all created post categories
    postCategories.addAll(createInBatch(postCategoriesToCreate));
    // Create them
    return postCategories;
}
Also used : PostRepository(run.halo.app.repository.PostRepository) HaloUtils(run.halo.app.utils.HaloUtils) Autowired(org.springframework.beans.factory.annotation.Autowired) NotFoundException(run.halo.app.exception.NotFoundException) HashMap(java.util.HashMap) PostCategoryService(run.halo.app.service.PostCategoryService) Service(org.springframework.stereotype.Service) Map(java.util.Map) ServiceUtils(run.halo.app.utils.ServiceUtils) CategoryWithPostCountDTO(run.halo.app.model.dto.CategoryWithPostCountDTO) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) PostCategoryRepository(run.halo.app.repository.PostCategoryRepository) LinkedList(java.util.LinkedList) PostCategory(run.halo.app.model.entity.PostCategory) LinkedHashSet(java.util.LinkedHashSet) Post(run.halo.app.model.entity.Post) PostStatus(run.halo.app.model.enums.PostStatus) CategoryService(run.halo.app.service.CategoryService) Collection(java.util.Collection) Set(java.util.Set) CategoryVO(run.halo.app.model.vo.CategoryVO) Page(org.springframework.data.domain.Page) AbstractCrudService(run.halo.app.service.base.AbstractCrudService) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Consumer(java.util.function.Consumer) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) NonNull(org.springframework.lang.NonNull) Lazy(org.springframework.context.annotation.Lazy) Queue(java.util.Queue) Category(run.halo.app.model.entity.Category) OptionService(run.halo.app.service.OptionService) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) BeanUtils(org.springframework.beans.BeanUtils) Assert(org.springframework.util.Assert) PostCategory(run.halo.app.model.entity.PostCategory) LinkedList(java.util.LinkedList)

Example 5 with PostCategory

use of run.halo.app.model.entity.PostCategory in project halo by ruibaby.

the class PostCategoryServiceImpl method populatePostIds.

private void populatePostIds(List<CategoryVO> categoryTree) {
    Assert.notNull(categoryTree, "The categoryTree must not be null.");
    Map<Integer, Set<Integer>> categoryPostIdsMap = postCategoryRepository.findAll().stream().collect(Collectors.groupingBy(PostCategory::getCategoryId, Collectors.mapping(PostCategory::getPostId, Collectors.toSet())));
    walkCategoryTree(categoryTree, category -> {
        // Set post count
        Set<Integer> postIds = categoryPostIdsMap.getOrDefault(category.getId(), new LinkedHashSet<>());
        category.setPostIds(postIds);
    });
    CategoryVO categoryTreeRootNode = new CategoryVO();
    categoryTreeRootNode.setChildren(categoryTree);
    mergePostIdsFromBottomToTop(categoryTreeRootNode);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) PostCategory(run.halo.app.model.entity.PostCategory) CategoryVO(run.halo.app.model.vo.CategoryVO)

Aggregations

PostCategory (run.halo.app.model.entity.PostCategory)17 Post (run.halo.app.model.entity.Post)13 Category (run.halo.app.model.entity.Category)10 PostMeta (run.halo.app.model.entity.PostMeta)8 PostTag (run.halo.app.model.entity.PostTag)8 HashMap (java.util.HashMap)6 PostComment (run.halo.app.model.entity.PostComment)6 CategoryVO (run.halo.app.model.vo.CategoryVO)6 Collection (java.util.Collection)5 Collections (java.util.Collections)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 Map (java.util.Map)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 Content (run.halo.app.model.entity.Content)5 Tag (run.halo.app.model.entity.Tag)5 LinkedHashSet (java.util.LinkedHashSet)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Lazy (org.springframework.context.annotation.Lazy)4