Search in sources :

Example 1 with Blog

use of com.duan.blogos.entity.blog.Blog in project BlogSystem by DuanJiaNing.

the class BlogBrowseServiceImpl method getBlogMainContent.

@Override
public ResultBean<BlogMainContentDTO> getBlogMainContent(int blogId) {
    // 查询数据
    Blog blog = blogDao.getBlogById(blogId);
    if (blog == null)
        return null;
    String ch = dbProperties.getStringFiledSplitCharacterForNumber();
    int[] cids = StringUtils.intStringDistinctToArray(blog.getCategoryIds(), ch);
    int[] lids = StringUtils.intStringDistinctToArray(blog.getLabelIds(), ch);
    List<BlogCategory> categories = cids == null ? null : categoryDao.listCategoryById(cids);
    List<BlogLabel> labels = lids == null ? null : labelDao.listLabelById(lids);
    // 填充数据
    String sc = dbProperties.getStringFiledSplitCharacterForString();
    BlogMainContentDTO dto = dataFillingManager.blogMainContentToDTO(blog, categories, labels, sc);
    return new ResultBean<>(dto);
}
Also used : BlogLabel(com.duan.blogos.entity.blog.BlogLabel) BlogCategory(com.duan.blogos.entity.blog.BlogCategory) Blog(com.duan.blogos.entity.blog.Blog) BlogMainContentDTO(com.duan.blogos.dto.blog.BlogMainContentDTO) ResultBean(com.duan.blogos.restful.ResultBean)

Example 2 with Blog

use of com.duan.blogos.entity.blog.Blog in project BlogSystem by DuanJiaNing.

the class BloggerBlogServiceImpl method updateBlog.

@Override
public boolean updateBlog(int bloggerId, int blogId, int[] newCategories, int[] newLabels, BlogStatusEnum newStatus, String newTitle, String newContent, String newContentMd, String newSummary, String[] newKeyWords) {
    // 1 更新博文中引用的本地图片(取消引用的useCount--,新增的useCount++)
    Blog oldBlog = blogDao.getBlogById(blogId);
    if (newContent != null) {
        if (!oldBlog.getContent().equals(newContent)) {
            // 1 2 3 4
            final int[] oldIids = parseContentForImageIds(oldBlog.getContent(), bloggerId);
            // 1 3 4 6
            final int[] newIids = parseContentForImageIds(newContent, bloggerId);
            // 求交集 1 3 4
            int[] array = IntStream.of(oldIids).filter(value -> {
                for (int id : newIids) if (id == value)
                    return true;
                return false;
            }).toArray();
            // -- 2
            int[] allM = new int[oldIids.length + array.length];
            System.arraycopy(oldIids, 0, allM, 0, oldIids.length);
            System.arraycopy(array, 0, allM, oldIids.length, array.length);
            IntStream.of(allM).distinct().forEach(pictureDao::updateUseCountMinus);
            // ++ 6
            int[] allP = new int[newIids.length + array.length];
            System.arraycopy(newIids, 0, allP, 0, newIids.length);
            System.arraycopy(array, 0, allP, newIids.length, array.length);
            IntStream.of(allP).distinct().forEach(id -> {
                pictureDao.updateUseCountPlus(id);
                // 将用到的图片修改为public(有必要的话)
                try {
                    imageManager.moveImageAndUpdateDbIfNecessary(bloggerId, id, BloggerPictureCategoryEnum.PUBLIC);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }
    }
    // 2 更新博文
    String ch = dbProperties.getStringFiledSplitCharacterForNumber();
    String chs = dbProperties.getStringFiledSplitCharacterForString();
    Blog blog = new Blog();
    blog.setId(blogId);
    if (newCategories != null)
        blog.setCategoryIds(StringUtils.intArrayToString(newCategories, ch));
    if (newLabels != null)
        blog.setLabelIds(StringUtils.intArrayToString(newLabels, ch));
    // 博文未通过审核时不能修改状态
    if (newStatus != null && !oldBlog.getState().equals(BlogStatusEnum.VERIFY.getCode()))
        blog.setState(newStatus.getCode());
    if (newTitle != null)
        blog.setTitle(newTitle);
    if (newContent != null)
        blog.setContent(newContent);
    if (newSummary != null)
        blog.setSummary(newSummary);
    if (newContentMd != null)
        blog.setContentMd(newContentMd);
    if (newKeyWords != null)
        blog.setKeyWords(StringUtils.arrayToString(newKeyWords, chs));
    int effect = blogDao.update(blog);
    if (effect <= 0)
        throw new SQLException();
    // 3 更新lucene
    try {
        luceneIndexManager.update(blog);
    } catch (IOException e) {
        e.printStackTrace();
        throw new LuceneException(e);
    }
    return true;
}
Also used : IntStream(java.util.stream.IntStream) BlogCategoryDao(com.duan.blogos.dao.blog.BlogCategoryDao) java.util(java.util) BlogStatisticsDao(com.duan.blogos.dao.blog.BlogStatisticsDao) Autowired(org.springframework.beans.factory.annotation.Autowired) BloggerPictureDao(com.duan.blogos.dao.blogger.BloggerPictureDao) Matcher(java.util.regex.Matcher) Blog(com.duan.blogos.entity.blog.Blog) Service(org.springframework.stereotype.Service) CollectionUtils(com.duan.blogos.util.CollectionUtils) BloggerBlogService(com.duan.blogos.service.blogger.BloggerBlogService) DataFillingManager(com.duan.blogos.manager.DataFillingManager) BlogCategory(com.duan.blogos.entity.blog.BlogCategory) BlogListItemDTO(com.duan.blogos.dto.blogger.BlogListItemDTO) BlogStatusEnum(com.duan.blogos.enums.BlogStatusEnum) IOException(java.io.IOException) StringUtils(com.duan.blogos.util.StringUtils) LuceneException(com.duan.blogos.exception.internal.LuceneException) WebsiteProperties(com.duan.blogos.manager.properties.WebsiteProperties) BlogFilterAbstract(com.duan.blogos.service.BlogFilterAbstract) ResultBean(com.duan.blogos.restful.ResultBean) SQLException(com.duan.blogos.exception.internal.SQLException) BloggerPictureCategoryEnum(com.duan.blogos.enums.BloggerPictureCategoryEnum) ImageManager(com.duan.blogos.manager.ImageManager) Pattern(java.util.regex.Pattern) BlogStatistics(com.duan.blogos.entity.blog.BlogStatistics) SQLException(com.duan.blogos.exception.internal.SQLException) IOException(java.io.IOException) Blog(com.duan.blogos.entity.blog.Blog) LuceneException(com.duan.blogos.exception.internal.LuceneException)

Example 3 with Blog

use of com.duan.blogos.entity.blog.Blog in project BlogSystem by DuanJiaNing.

the class BloggerBlogServiceImpl method deleteBlog.

@Override
public boolean deleteBlog(int bloggerId, int blogId) {
    Blog blog = blogDao.getBlogById(blogId);
    if (blog == null)
        return false;
    // 1 删除博文记录
    int effect = blogDao.delete(blogId);
    if (effect <= 0)
        return false;
    // 2 删除统计信息
    int effectS = statisticsDao.deleteByUnique(blogId);
    // MAYBUG 断点调试时effectS始终为0,但最终事务提交时记录却会正确删除,??? 因而注释下面的判断
    // if (effectS <= 0) throw new UnknownException(blog");
    // 3 图片引用useCount--
    int[] ids = parseContentForImageIds(blog.getContent(), bloggerId);
    if (!CollectionUtils.isEmpty(ids))
        Arrays.stream(ids).forEach(pictureDao::updateUseCountMinus);
    // 4 删除lucene索引
    try {
        luceneIndexManager.delete(blogId);
    } catch (IOException e) {
        e.printStackTrace();
        throw new LuceneException(e);
    }
    return true;
}
Also used : IOException(java.io.IOException) Blog(com.duan.blogos.entity.blog.Blog) LuceneException(com.duan.blogos.exception.internal.LuceneException)

Example 4 with Blog

use of com.duan.blogos.entity.blog.Blog in project BlogSystem by DuanJiaNing.

the class BloggerBlogServiceImpl method constructResult.

@Override
protected ResultBean<List<BlogListItemDTO>> constructResult(Map<Integer, Blog> blogHashMap, List<BlogStatistics> statistics, Map<Integer, int[]> blogIdMapCategoryIds, Map<Integer, String> blogImgs) {
    // 重组结果
    List<BlogListItemDTO> result = new ArrayList<>();
    for (BlogStatistics s : statistics) {
        Integer blogId = s.getBlogId();
        int[] ids = blogIdMapCategoryIds.get(blogId);
        List<BlogCategory> categories = CollectionUtils.isEmpty(ids) ? null : categoryDao.listCategoryById(ids);
        Blog blog = blogHashMap.get(blogId);
        BlogListItemDTO dto = dataFillingManager.bloggerBlogListItemToDTO(blog, s, categories);
        result.add(dto);
    }
    return new ResultBean<>(result);
}
Also used : BlogCategory(com.duan.blogos.entity.blog.BlogCategory) BlogStatistics(com.duan.blogos.entity.blog.BlogStatistics) BlogListItemDTO(com.duan.blogos.dto.blogger.BlogListItemDTO) Blog(com.duan.blogos.entity.blog.Blog) ResultBean(com.duan.blogos.restful.ResultBean)

Example 5 with Blog

use of com.duan.blogos.entity.blog.Blog in project BlogSystem by DuanJiaNing.

the class BloggerBlogController method get.

/**
 * 获取指定博文
 */
@RequestMapping(value = "/{blogId}", method = RequestMethod.GET)
public ResultBean<Blog> get(HttpServletRequest request, @PathVariable Integer bloggerId, @PathVariable Integer blogId) {
    handleBloggerSignInCheck(request, bloggerId);
    ResultBean<Blog> blog = bloggerBlogService.getBlog(bloggerId, blogId);
    if (blog == null)
        handlerEmptyResult(request);
    // 编码为 Unicode
    Blog bg = blog.getData();
    bg.setContent(StringUtils.stringToUnicode(bg.getContent()));
    bg.setContentMd(StringUtils.stringToUnicode(bg.getContentMd()));
    return blog;
}
Also used : Blog(com.duan.blogos.entity.blog.Blog)

Aggregations

Blog (com.duan.blogos.entity.blog.Blog)17 ResultBean (com.duan.blogos.restful.ResultBean)6 BlogCategory (com.duan.blogos.entity.blog.BlogCategory)5 BlogStatistics (com.duan.blogos.entity.blog.BlogStatistics)5 LuceneException (com.duan.blogos.exception.internal.LuceneException)4 SQLException (com.duan.blogos.exception.internal.SQLException)4 IOException (java.io.IOException)4 BlogLabel (com.duan.blogos.entity.blog.BlogLabel)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 BaseTest (com.duan.blogos.BaseTest)2 BlogListItemDTO (com.duan.blogos.dto.blogger.BlogListItemDTO)2 BloggerStatisticsDTO (com.duan.blogos.dto.blogger.BloggerStatisticsDTO)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Test (org.junit.Test)2 BlogCategoryDao (com.duan.blogos.dao.blog.BlogCategoryDao)1 BlogStatisticsDao (com.duan.blogos.dao.blog.BlogStatisticsDao)1 BloggerPictureDao (com.duan.blogos.dao.blogger.BloggerPictureDao)1 BlogListItemDTO (com.duan.blogos.dto.blog.BlogListItemDTO)1 BlogMainContentDTO (com.duan.blogos.dto.blog.BlogMainContentDTO)1