Search in sources :

Example 1 with SQLException

use of com.duan.blogos.exception.internal.SQLException 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 SQLException

use of com.duan.blogos.exception.internal.SQLException in project BlogSystem by DuanJiaNing.

the class BloggerBlogServiceImpl method insertBlog.

@Override
public int insertBlog(int bloggerId, int[] categories, int[] labels, BlogStatusEnum status, String title, String content, String contentMd, String summary, String[] keyWords) {
    // 1 插入数据到bolg表
    String ch = dbProperties.getStringFiledSplitCharacterForNumber();
    String chs = dbProperties.getStringFiledSplitCharacterForString();
    Blog blog = new Blog();
    blog.setBloggerId(bloggerId);
    blog.setCategoryIds(StringUtils.intArrayToString(categories, ch));
    blog.setLabelIds(StringUtils.intArrayToString(labels, ch));
    blog.setState(status.getCode());
    blog.setTitle(title);
    blog.setContent(content);
    blog.setContentMd(contentMd);
    blog.setSummary(summary);
    blog.setKeyWords(StringUtils.arrayToString(keyWords, chs));
    blog.setWordCount(content.length());
    int effect = blogDao.insert(blog);
    if (effect <= 0)
        return -1;
    int blogId = blog.getId();
    // 2 插入数据到blog_statistics表(生成博文信息记录)
    BlogStatistics statistics = new BlogStatistics();
    statistics.setBlogId(blogId);
    effect = statisticsDao.insert(statistics);
    if (effect <= 0)
        throw new SQLException();
    // 3 解析本地图片引用并使自增
    int[] imids = parseContentForImageIds(content, bloggerId);
    // UPDATE: 2018/1/19 更新 自增并没有实际作用
    if (!CollectionUtils.isEmpty(imids)) {
        // 修改图片可见性,引用次数
        Arrays.stream(imids).forEach(id -> imageManager.imageInsertHandle(bloggerId, id));
    }
    // 4 lucene创建索引
    try {
        luceneIndexManager.add(blog);
    } catch (IOException e) {
        e.printStackTrace();
        throw new LuceneException(e);
    }
    return blogId;
}
Also used : SQLException(com.duan.blogos.exception.internal.SQLException) BlogStatistics(com.duan.blogos.entity.blog.BlogStatistics) IOException(java.io.IOException) Blog(com.duan.blogos.entity.blog.Blog) LuceneException(com.duan.blogos.exception.internal.LuceneException)

Example 3 with SQLException

use of com.duan.blogos.exception.internal.SQLException in project BlogSystem by DuanJiaNing.

the class BloggerCategoryServiceImpl method deleteCategoryAndMoveBlogsTo.

@Override
public boolean deleteCategoryAndMoveBlogsTo(int bloggerId, int categoryId, int newCategoryId) {
    BlogCategory category = categoryDao.getCategory(bloggerId, categoryId);
    if (category == null)
        return false;
    // 图片引用次数--
    Integer iconId;
    if ((iconId = category.getIconId()) != null && pictureDao.getUseCount(iconId) > 0) {
        pictureDao.updateUseCountMinus(iconId);
    }
    // 删除数据库类别记录
    int effectDelete = categoryDao.delete(categoryId);
    if (effectDelete <= 0)
        throw new SQLException();
    // 修改博文类别
    List<Blog> blogs = blogDao.listAllCategoryByBloggerId(bloggerId);
    String sp = dbProperties.getStringFiledSplitCharacterForNumber();
    // 移除类别即可
    if (newCategoryId <= 0) {
        blogs.forEach(blog -> {
            int[] cids = StringUtils.intStringDistinctToArray(blog.getCategoryIds(), sp);
            if (CollectionUtils.intArrayContain(cids, categoryId)) {
                int[] ar = ArrayUtils.removeFromArray(cids, categoryId);
                blog.setCategoryIds(StringUtils.intArrayToString(ar, sp));
                int effectUpdate = blogDao.update(blog);
                if (effectUpdate <= 0)
                    throw new SQLException();
            }
        });
    } else {
        // 替换类别
        blogs.forEach(blog -> {
            int[] cids = StringUtils.intStringDistinctToArray(blog.getCategoryIds(), sp);
            if (CollectionUtils.intArrayContain(cids, categoryId)) {
                ArrayUtils.replace(cids, categoryId, newCategoryId);
                blog.setCategoryIds(StringUtils.intArrayToString(cids, sp));
                int effectUpdate = blogDao.update(blog);
                if (effectUpdate <= 0)
                    throw new SQLException();
            }
        });
    }
    return true;
}
Also used : BlogCategory(com.duan.blogos.entity.blog.BlogCategory) SQLException(com.duan.blogos.exception.internal.SQLException) Blog(com.duan.blogos.entity.blog.Blog)

Example 4 with SQLException

use of com.duan.blogos.exception.internal.SQLException in project BlogSystem by DuanJiaNing.

the class BloggerLabelServiceImpl method deleteLabel.

@Override
public boolean deleteLabel(int bloggerId, int labelId) {
    // 检查标签存在及标签创建者是否为当前博主
    BlogLabel label = labelDao.getLabel(labelId);
    if (label == null || label.getBloggerId() != bloggerId)
        return false;
    // 删除数据库记录
    int effect = labelDao.delete(labelId);
    if (effect <= 0)
        return false;
    // 将所有拥有该标签的博文修改(j将标签移除)
    List<Blog> blogs = blogDao.listAllLabelByBloggerId(bloggerId);
    String ch = dbProperties.getStringFiledSplitCharacterForNumber();
    for (Blog blog : blogs) {
        int[] lids = StringUtils.intStringDistinctToArray(blog.getLabelIds(), ch);
        if (CollectionUtils.isEmpty(lids))
            continue;
        if (CollectionUtils.intArrayContain(lids, labelId)) {
            int[] ids = ArrayUtils.removeFromArray(lids, labelId);
            blog.setLabelIds(StringUtils.intArrayToString(ids, ch));
            if (blogDao.update(blog) <= 0)
                throw new SQLException();
        }
    }
    return true;
}
Also used : BlogLabel(com.duan.blogos.entity.blog.BlogLabel) SQLException(com.duan.blogos.exception.internal.SQLException) Blog(com.duan.blogos.entity.blog.Blog)

Aggregations

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