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;
}
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);
}
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);
}
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;
}
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("相关搜索");
}
Aggregations