Search in sources :

Example 6 with Blog

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

the class BlogFilterAbstract method getMapFilterByLabelAndCategory.

// 通过类别和标签筛选出指定博主的符合条件的博文id及对应的类别id数组
// 类别和标签没有限制时返回指定博主的所有博文id集
private Map<Integer, int[]> getMapFilterByLabelAndCategory(int bloggerId, int[] categoryIds, int[] labelIds, BlogStatusEnum status) {
    // 查询博主的所有标签和类别
    List<Blog> blogs = blogDao.listAllCategoryAndLabel(bloggerId, status.getCode());
    // 筛选符合条件的博文的id
    // 方便后面复用categories的id数组
    Map<Integer, int[]> map = new HashMap<>();
    String ch = dbProperties.getStringFiledSplitCharacterForNumber();
    if (categoryIds == null && labelIds == null) {
        // 两者都没限定
        for (Blog blog : blogs) {
            int[] categoriesIds = StringUtils.intStringDistinctToArray(blog.getCategoryIds(), ch);
            map.put(blog.getId(), categoriesIds);
        }
    } else if (categoryIds != null && labelIds != null) {
        // 两者都限定
        for (Blog blog : blogs) {
            int accordCount = 0;
            int[] categoriesIds = StringUtils.intStringDistinctToArray(blog.getCategoryIds(), ch);
            int[] labels = StringUtils.intStringDistinctToArray(blog.getLabelIds(), ch);
            if (categoriesIds == null || labels == null)
                continue;
            for (int categoryId : categoriesIds) {
                if (CollectionUtils.intArrayContain(categoryIds, categoryId)) {
                    accordCount++;
                    break;
                }
            }
            for (int labelId : labels) {
                if (CollectionUtils.intArrayContain(labelIds, labelId)) {
                    accordCount++;
                    break;
                }
            }
            if (accordCount == 2) {
                map.put(blog.getId(), categoriesIds);
            }
        }
    } else if (categoryIds != null) {
        // 只限定了categoryIds
        for (Blog blog : blogs) {
            int[] categoriesIds = StringUtils.intStringDistinctToArray(blog.getCategoryIds(), ch);
            // 博文没有分类,直接检查下一篇博文
            if (categoriesIds == null)
                continue;
            for (int categoryId : categoriesIds) {
                if (CollectionUtils.intArrayContain(categoryIds, categoryId)) {
                    map.put(blog.getId(), categoriesIds);
                    break;
                }
            }
        }
    } else {
        // 只限定了label
        for (Blog blog : blogs) {
            int[] categoriesIds = StringUtils.intStringDistinctToArray(blog.getCategoryIds(), ch);
            int[] labels = StringUtils.intStringDistinctToArray(blog.getLabelIds(), ch);
            // 博文没有标签,直接检查下一篇
            if (labels == null)
                continue;
            for (int labelId : labels) {
                if (CollectionUtils.intArrayContain(labelIds, labelId)) {
                    map.put(blog.getId(), categoriesIds);
                    break;
                }
            }
        }
    }
    return map;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Blog(com.duan.blogos.entity.blog.Blog)

Example 7 with Blog

use of com.duan.blogos.entity.blog.Blog 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 8 with Blog

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

the class BlogFilterAbstract method sortAndConstructResult.

// 对筛选出的博文进行排序并重组结果集
private T sortAndConstructResult(List<Blog> blogs, BlogSortRule sortRule, Map<Integer, int[]> map) {
    // 用于排序
    List<BlogStatistics> temp = new ArrayList<>();
    // 方便排序后的重组
    Map<Integer, Blog> blogHashMap = new HashMap<>();
    // 从 html 中获得图片
    Map<Integer, String> blogImgs = new HashMap<>();
    for (Blog blog : blogs) {
        int blogId = blog.getId();
        BlogStatistics statistics = statisticsDao.getStatistics(blogId);
        temp.add(statistics);
        blogHashMap.put(blogId, blog);
        String content = blog.getContent();
        Pattern pattern = Pattern.compile("<img src=\"(.*)\" .*>");
        Matcher matcher = pattern.matcher(content);
        if (matcher.find())
            blogImgs.put(blogId, matcher.group(1));
    }
    BlogListItemComparatorFactory factory = new BlogListItemComparatorFactory();
    temp.sort(factory.get(sortRule.getRule(), sortRule.getOrder()));
    return constructResult(blogHashMap, temp, map, blogImgs);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) BlogStatistics(com.duan.blogos.entity.blog.BlogStatistics) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BlogListItemComparatorFactory(com.duan.blogos.manager.comparator.BlogListItemComparatorFactory) Blog(com.duan.blogos.entity.blog.Blog)

Example 9 with Blog

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

the class EditBlogPageController method mainPage.

@RequestMapping
public ModelAndView mainPage(HttpServletRequest request, @RequestParam(value = "bid", required = false) Integer bloggerId, @RequestParam(value = "blogId", required = false) Integer blogId) {
    ModelAndView mv = new ModelAndView();
    if (bloggerId == null || !bloggerValidateManager.checkBloggerSignIn(request, bloggerId)) {
        mv.setViewName("/error/error");
        mv.addObject("errorMsg", "请先登录");
    } else {
        if (blogId != null) {
            ResultBean<Blog> blog = blogService.getBlog(bloggerId, blogId);
            Blog data = blog.getData();
            mv.addObject("blogId", blogId);
            mv.addObject("categoryId", data.getCategoryIds());
            mv.addObject("labelIds", data.getLabelIds());
            mv.addObject("blogTitle", data.getTitle());
            mv.addObject("blogSummary", data.getSummary());
            if (data.getState().equals(BlogStatusEnum.PRIVATE.getCode())) {
                mv.addObject("blogIsPrivate", true);
            }
            mv.addObject("blogContentMd", StringUtils.stringToUnicode(data.getContentMd()));
        }
        ResultBean<BloggerStatisticsDTO> loginBgStat = statisticsService.getBloggerStatistics(bloggerId);
        mv.addObject("loginBgStat", loginBgStat.getData());
        mv.setViewName("/blogger/edit_blog");
    }
    return mv;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) BloggerStatisticsDTO(com.duan.blogos.dto.blogger.BloggerStatisticsDTO) Blog(com.duan.blogos.entity.blog.Blog) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with Blog

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

the class BlogDaoTest method update.

@Test
public void update() {
    Blog blog = new Blog();
    blog.setId(1);
    blog.setSummary("相关搜索");
}
Also used : Blog(com.duan.blogos.entity.blog.Blog) Test(org.junit.Test) BaseTest(com.duan.blogos.BaseTest)

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