Search in sources :

Example 1 with BlogStatusEnum

use of com.duan.blogos.enums.BlogStatusEnum 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 2 with BlogStatusEnum

use of com.duan.blogos.enums.BlogStatusEnum in project BlogSystem by DuanJiaNing.

the class BloggerBlogController method list.

/**
 * 检索博文
 */
@RequestMapping(method = RequestMethod.GET)
public ResultBean<List<BlogListItemDTO>> list(HttpServletRequest request, @PathVariable Integer bloggerId, @RequestParam(value = "cids", required = false) String categoryIds, @RequestParam(value = "lids", required = false) String labelIds, @RequestParam(value = "kword", required = false) String keyWord, @RequestParam(value = "offset", required = false) Integer offset, @RequestParam(value = "rows", required = false) Integer rows, @RequestParam(value = "sort", required = false) String sort, @RequestParam(value = "order", required = false) String order, @RequestParam(value = "status", required = false) Integer status) {
    handleBloggerSignInCheck(request, bloggerId);
    // 检查排序规则
    String sor = sort == null ? Rule.VIEW_COUNT.name() : sort.toUpperCase();
    String ord = order == null ? Order.DESC.name() : order.toUpperCase();
    handleSortRuleCheck(request, sor, ord);
    String sp = websiteProperties.getUrlConditionSplitCharacter();
    int[] cids = StringUtils.intStringDistinctToArray(categoryIds, sp);
    int[] lids = StringUtils.intStringDistinctToArray(labelIds, sp);
    // 检查博文类别和标签
    handleCategoryAndLabelCheck(request, bloggerId, cids, lids);
    BlogStatusEnum stat = null;
    if (status != null)
        stat = BlogStatusEnum.valueOf(status);
    // status传参错误
    if (stat == null)
        stat = BlogStatusEnum.PUBLIC;
    // 执行数据查询
    BlogSortRule rule = new BlogSortRule(Rule.valueOf(sor), Order.valueOf(ord));
    int os = offset == null || offset < 0 ? 0 : offset;
    int rs = rows == null || rows < 0 ? bloggerProperties.getRequestBlogListCount() : rows;
    ResultBean<List<BlogListItemDTO>> listResultBean = bloggerBlogService.listFilterAll(cids, lids, keyWord, bloggerId, os, rs, rule, stat);
    if (listResultBean == null)
        handlerEmptyResult(request);
    return listResultBean;
}
Also used : BlogStatusEnum(com.duan.blogos.enums.BlogStatusEnum) List(java.util.List) BlogSortRule(com.duan.blogos.common.BlogSortRule)

Example 3 with BlogStatusEnum

use of com.duan.blogos.enums.BlogStatusEnum in project BlogSystem by DuanJiaNing.

the class BlogValidateManager method isBlogStatusAllow.

/**
 * 检查目标博文状态是否允许,一般用户只允许在“公开”,“私有”,“回收站”之间切换。
 *
 * @param status 状态值
 * @return 允许返回true
 */
public boolean isBlogStatusAllow(int status) {
    List<BlogStatusEnum> list = Arrays.asList(BlogStatusEnum.PUBLIC, BlogStatusEnum.PRIVATE, BlogStatusEnum.DELETED);
    int contain = 0;
    for (BlogStatusEnum s : list) {
        if (s.getCode() == status)
            contain++;
    }
    return contain > 0;
}
Also used : BlogStatusEnum(com.duan.blogos.enums.BlogStatusEnum)

Example 4 with BlogStatusEnum

use of com.duan.blogos.enums.BlogStatusEnum in project BlogSystem by DuanJiaNing.

the class BloggerBlogController method update.

/**
 * 更新博文
 */
@RequestMapping(value = "/{blogId}", method = RequestMethod.PUT)
public ResultBean update(HttpServletRequest request, @PathVariable Integer bloggerId, @PathVariable Integer blogId, @RequestParam(value = "title", required = false) String newTitle, @RequestParam(value = "content", required = false) String newContent, @RequestParam(value = "contentMd", required = false) String newContentMd, @RequestParam(value = "summary", required = false) String newSummary, @RequestParam(value = "cids", required = false) String newCategoryIds, @RequestParam(value = "lids", required = false) String newLabelIds, @RequestParam(value = "kword", required = false) String newKeyWord, @RequestParam(value = "status", required = false) Integer newStatus) {
    // 所有参数都为null,则不更新。
    if (Stream.of(newTitle, newContent, newSummary, newCategoryIds, newLabelIds, newKeyWord, newStatus).filter(Objects::nonNull).count() <= 0)
        throw exceptionManager.getParameterIllegalException(new RequestContext(request));
    // 检查修改到的博文状态是否允许
    if (newStatus != null && !blogValidateManager.isBlogStatusAllow(newStatus))
        throw exceptionManager.getParameterIllegalException(new RequestContext(request));
    handleBloggerSignInCheck(request, bloggerId);
    handleBlogExistAndCreatorCheck(request, bloggerId, blogId);
    // 将 Unicode 解码
    newContent = StringUtils.unicodeToString(newContent);
    newContentMd = StringUtils.unicodeToString(newContentMd);
    handleBlogContentCheck(request, newTitle, newContent, newContentMd, newSummary, newKeyWord);
    String sp = websiteProperties.getUrlConditionSplitCharacter();
    int[] cids = newCategoryIds == null ? null : StringUtils.intStringDistinctToArray(newCategoryIds, sp);
    int[] lids = newLabelIds == null ? null : StringUtils.intStringDistinctToArray(newLabelIds, sp);
    // 检查博文类别和标签
    handleCategoryAndLabelCheck(request, bloggerId, cids, lids);
    String[] kw = newKeyWord == null ? null : StringUtils.stringArrayToArray(newKeyWord, sp);
    BlogStatusEnum stat = newStatus == null ? null : BlogStatusEnum.valueOf(newStatus);
    // 执行更新
    if (!bloggerBlogService.updateBlog(bloggerId, blogId, cids, lids, stat, newTitle, newContent, newContentMd, newSummary, kw))
        handlerOperateFail(request);
    return new ResultBean<>("");
}
Also used : BlogStatusEnum(com.duan.blogos.enums.BlogStatusEnum) Objects(java.util.Objects) RequestContext(org.springframework.web.servlet.support.RequestContext) ResultBean(com.duan.blogos.restful.ResultBean)

Aggregations

BlogStatusEnum (com.duan.blogos.enums.BlogStatusEnum)4 ResultBean (com.duan.blogos.restful.ResultBean)2 BlogSortRule (com.duan.blogos.common.BlogSortRule)1 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.blogger.BlogListItemDTO)1 Blog (com.duan.blogos.entity.blog.Blog)1 BlogCategory (com.duan.blogos.entity.blog.BlogCategory)1 BlogStatistics (com.duan.blogos.entity.blog.BlogStatistics)1 BloggerPictureCategoryEnum (com.duan.blogos.enums.BloggerPictureCategoryEnum)1 LuceneException (com.duan.blogos.exception.internal.LuceneException)1 SQLException (com.duan.blogos.exception.internal.SQLException)1 DataFillingManager (com.duan.blogos.manager.DataFillingManager)1 ImageManager (com.duan.blogos.manager.ImageManager)1 WebsiteProperties (com.duan.blogos.manager.properties.WebsiteProperties)1 BlogFilterAbstract (com.duan.blogos.service.BlogFilterAbstract)1 BloggerBlogService (com.duan.blogos.service.blogger.BloggerBlogService)1 CollectionUtils (com.duan.blogos.util.CollectionUtils)1 StringUtils (com.duan.blogos.util.StringUtils)1