Search in sources :

Example 1 with LuceneException

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

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

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

the class BlogFilterAbstract method filterByLucene.

/**
 * 关键字不为null时需要通过lucene进行全文检索
 *
 * @param keyWord     关键字
 * @param categoryIds 类别id
 * @param labelIds    标签id
 * @param bloggerId   博主id
 * @param offset      结果偏移量
 * @param rows        结果行数
 * @param sortRule    排序规则
 * @param status      博文状态
 * @return 经过筛选、排序的结果集
 */
protected T filterByLucene(String keyWord, int[] categoryIds, int[] labelIds, int bloggerId, int offset, int rows, BlogSortRule sortRule, BlogStatusEnum status) {
    // ------------------------关键字筛选
    int[] ids;
    try {
        // 搜索结果无法使用类似于sql limit的方式分页,这里一次性将所有结果查询出,后续考虑使用缓存实现分页
        ids = luceneIndexManager.search(keyWord, 10000);
    } catch (IOException | ParseException e) {
        e.printStackTrace();
        throw new LuceneException(e);
    }
    // 关键字为首要条件
    if (CollectionUtils.isEmpty(ids))
        return null;
    // 关键字检索得到的博文集合
    List<Integer> filterByLuceneIds = new ArrayList<>();
    // UPDATE 取最前面的rows条结果
    int row = Math.min(rows, ids.length);
    for (int i = 0; i < row; i++) filterByLuceneIds.add(ids[i]);
    // ----------------------类别、标签筛选
    Map<Integer, int[]> map = getMapFilterByLabelAndCategory(bloggerId, categoryIds, labelIds, status);
    Integer[] mids = map.keySet().toArray(new Integer[map.size()]);
    // 类别、标签检索得到的博文集合
    List<Integer> filterByOtherIds = Arrays.asList(mids);
    // 求两者交集得到最终结果集
    List<Integer> resultIds = filterByLuceneIds.stream().filter(filterByOtherIds::contains).collect(Collectors.toList());
    if (CollectionUtils.isEmpty(resultIds))
        return null;
    // 构造结果,排序并重组
    count.set(resultIds.size());
    List<Blog> blogs = blogDao.listBlogByBlogIds(resultIds, status.getCode(), offset, rows);
    return sortAndConstructResult(blogs, sortRule, map);
}
Also used : IOException(java.io.IOException) LuceneException(com.duan.blogos.exception.internal.LuceneException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ParseException(org.apache.lucene.queryparser.classic.ParseException) Blog(com.duan.blogos.entity.blog.Blog)

Example 4 with LuceneException

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

Aggregations

Blog (com.duan.blogos.entity.blog.Blog)4 LuceneException (com.duan.blogos.exception.internal.LuceneException)4 IOException (java.io.IOException)4 BlogStatistics (com.duan.blogos.entity.blog.BlogStatistics)2 SQLException (com.duan.blogos.exception.internal.SQLException)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 BlogCategory (com.duan.blogos.entity.blog.BlogCategory)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 StringUtils (com.duan.blogos.util.StringUtils)1