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