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;
}
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;
}
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);
}
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;
}
Aggregations