use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class CommentMgmtService method removePageComment.
/**
* Removes a comment of a page with the specified comment id.
*
* @param commentId the given comment id
* @throws ServiceException service exception
*/
public void removePageComment(final String commentId) throws ServiceException {
final Transaction transaction = commentRepository.beginTransaction();
try {
final JSONObject comment = commentRepository.get(commentId);
final String pageId = comment.getString(Comment.COMMENT_ON_ID);
// Step 1: Remove comment
commentRepository.remove(commentId);
// Step 2: Update page comment count
decPageCommentCount(pageId);
// Step 3: Update blog statistic comment count
statisticMgmtService.decBlogCommentCount();
statisticMgmtService.decPublishedBlogCommentCount();
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Removes a comment of a page failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class CommentMgmtService method addPageComment.
/**
* Adds page comment with the specified request json object.
*
* @param requestJSONObject the specified request json object, for example, <pre>
* {
* "oId": "", // page id
* "commentName": "",
* "commentEmail": "",
* "commentURL": "", // optional
* "commentContent": "",
* "commentOriginalCommentId": "" // optional
* }
* </pre>
*
* @return add result, for example, <pre>
* {
* "oId": "", // generated comment id
* "commentDate": "", // format: yyyy-MM-dd HH:mm:ss
* "commentOriginalCommentName": "" // optional, corresponding to argument "commentOriginalCommentId"
* "commentThumbnailURL": "",
* "commentSharpURL": "",
* "commentContent": "", // processed XSS HTML
* "commentName": "", // processed XSS
* "commentURL": "", // optional
* "isReply": boolean,
* "page": {},
* "commentOriginalCommentId": "" // optional
* "commentable": boolean,
* "permalink": "" // page.pagePermalink
* }
* </pre>
*
* @throws ServiceException service exception
*/
public JSONObject addPageComment(final JSONObject requestJSONObject) throws ServiceException {
final JSONObject ret = new JSONObject();
ret.put(Common.IS_REPLY, false);
final Transaction transaction = commentRepository.beginTransaction();
try {
final String pageId = requestJSONObject.getString(Keys.OBJECT_ID);
final JSONObject page = pageRepository.get(pageId);
ret.put(Page.PAGE, page);
final String commentName = requestJSONObject.getString(Comment.COMMENT_NAME);
final String commentEmail = requestJSONObject.getString(Comment.COMMENT_EMAIL).trim().toLowerCase();
final String commentURL = requestJSONObject.optString(Comment.COMMENT_URL);
final String commentContent = requestJSONObject.getString(Comment.COMMENT_CONTENT);
final String originalCommentId = requestJSONObject.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID);
ret.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, originalCommentId);
// Step 1: Add comment
final JSONObject comment = new JSONObject();
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, "");
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, "");
JSONObject originalComment = null;
comment.put(Comment.COMMENT_NAME, commentName);
comment.put(Comment.COMMENT_EMAIL, commentEmail);
comment.put(Comment.COMMENT_URL, commentURL);
comment.put(Comment.COMMENT_CONTENT, commentContent);
final JSONObject preference = preferenceQueryService.getPreference();
final Date date = new Date();
comment.put(Comment.COMMENT_DATE, date);
ret.put(Comment.COMMENT_DATE, DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
ret.put("commentDate2", date);
ret.put(Common.COMMENTABLE, preference.getBoolean(Option.ID_C_COMMENTABLE) && page.getBoolean(Page.PAGE_COMMENTABLE));
ret.put(Common.PERMALINK, page.getString(Page.PAGE_PERMALINK));
if (!Strings.isEmptyOrNull(originalCommentId)) {
originalComment = commentRepository.get(originalCommentId);
if (null != originalComment) {
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, originalCommentId);
final String originalCommentName = originalComment.getString(Comment.COMMENT_NAME);
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, originalCommentName);
ret.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, originalCommentName);
ret.put(Common.IS_REPLY, true);
} else {
LOGGER.log(Level.WARN, "Not found orginal comment[id={0}] of reply[name={1}, content={2}]", originalCommentId, commentName, commentContent);
}
}
setCommentThumbnailURL(comment);
ret.put(Comment.COMMENT_THUMBNAIL_URL, comment.getString(Comment.COMMENT_THUMBNAIL_URL));
// Sets comment on page....
comment.put(Comment.COMMENT_ON_ID, pageId);
comment.put(Comment.COMMENT_ON_TYPE, Page.PAGE);
final String commentId = Ids.genTimeMillisId();
ret.put(Keys.OBJECT_ID, commentId);
// Save comment sharp URL
final String commentSharpURL = Comments.getCommentSharpURLForPage(page, commentId);
ret.put(Comment.COMMENT_NAME, commentName);
ret.put(Comment.COMMENT_CONTENT, commentContent);
ret.put(Comment.COMMENT_URL, commentURL);
ret.put(Comment.COMMENT_SHARP_URL, commentSharpURL);
comment.put(Comment.COMMENT_SHARP_URL, commentSharpURL);
comment.put(Keys.OBJECT_ID, commentId);
commentRepository.add(comment);
// Step 2: Update page comment count
incPageCommentCount(pageId);
// Step 3: Update blog statistic comment count
statisticMgmtService.incBlogCommentCount();
statisticMgmtService.incPublishedBlogCommentCount();
// Step 4: Send an email to admin
try {
sendNotificationMail(page, comment, originalComment, preference);
} catch (final Exception e) {
LOGGER.log(Level.WARN, "Send mail failed", e);
}
// Step 5: Fire add comment event
final JSONObject eventData = new JSONObject();
eventData.put(Comment.COMMENT, comment);
eventData.put(Page.PAGE, page);
eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.ADD_COMMENT_TO_PAGE, eventData));
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(e);
}
return ret;
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class ArticleRepositoryImplTestCase method getMostCommentArticles.
/**
* Get Most Comment Articles.
*
* @throws Exception exception
*/
@Test(dependsOnMethods = { "add", "previousAndNext" })
public void getMostCommentArticles() throws Exception {
final ArticleRepository articleRepository = getArticleRepository();
final JSONObject article = new JSONObject();
article.put(Article.ARTICLE_TITLE, "article title3");
article.put(Article.ARTICLE_ABSTRACT, "article abstract");
article.put(Article.ARTICLE_TAGS_REF, "tag1, tag2");
article.put(Article.ARTICLE_AUTHOR_EMAIL, "test@gmail.com");
article.put(Article.ARTICLE_COMMENT_COUNT, 2);
article.put(Article.ARTICLE_VIEW_COUNT, 2);
article.put(Article.ARTICLE_CONTENT, "article content");
article.put(Article.ARTICLE_PERMALINK, "article permalink3");
article.put(Article.ARTICLE_HAD_BEEN_PUBLISHED, true);
article.put(Article.ARTICLE_IS_PUBLISHED, true);
article.put(Article.ARTICLE_PUT_TOP, false);
article.put(Article.ARTICLE_CREATE_DATE, new Date());
article.put(Article.ARTICLE_UPDATE_DATE, new Date());
article.put(Article.ARTICLE_RANDOM_DOUBLE, Math.random());
article.put(Article.ARTICLE_SIGN_ID, "1");
article.put(Article.ARTICLE_COMMENTABLE, true);
article.put(Article.ARTICLE_VIEW_PWD, "");
article.put(Article.ARTICLE_EDITOR_TYPE, "");
final Transaction transaction = articleRepository.beginTransaction();
articleRepository.add(article);
transaction.commit();
List<JSONObject> mostCommentArticles = articleRepository.getMostCommentArticles(2);
Assert.assertNotNull(mostCommentArticles);
Assert.assertEquals(mostCommentArticles.size(), 2);
Assert.assertEquals(mostCommentArticles.get(0).getInt(Article.ARTICLE_COMMENT_COUNT), 2);
Assert.assertEquals(mostCommentArticles.get(1).getInt(Article.ARTICLE_COMMENT_COUNT), 1);
mostCommentArticles = articleRepository.getMostCommentArticles(1);
Assert.assertNotNull(mostCommentArticles);
Assert.assertEquals(mostCommentArticles.size(), 1);
Assert.assertEquals(mostCommentArticles.get(0).getInt(Article.ARTICLE_COMMENT_COUNT), 2);
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class ArticleRepositoryImplTestCase method previousAndNext.
/**
* Get by permalink.
*
* @throws Exception exception
*/
@Test(dependsOnMethods = { "add" })
public void previousAndNext() throws Exception {
final ArticleRepository articleRepository = getArticleRepository();
final JSONObject article = new JSONObject();
article.put(Article.ARTICLE_TITLE, "article title2");
article.put(Article.ARTICLE_ABSTRACT, "article abstract");
article.put(Article.ARTICLE_TAGS_REF, "tag1, tag2");
article.put(Article.ARTICLE_AUTHOR_EMAIL, "test@gmail.com");
article.put(Article.ARTICLE_COMMENT_COUNT, 1);
article.put(Article.ARTICLE_VIEW_COUNT, 1);
article.put(Article.ARTICLE_CONTENT, "article content");
article.put(Article.ARTICLE_PERMALINK, "article permalink2");
article.put(Article.ARTICLE_HAD_BEEN_PUBLISHED, true);
article.put(Article.ARTICLE_IS_PUBLISHED, true);
article.put(Article.ARTICLE_PUT_TOP, false);
article.put(Article.ARTICLE_CREATE_DATE, new Date());
article.put(Article.ARTICLE_UPDATE_DATE, new Date());
article.put(Article.ARTICLE_RANDOM_DOUBLE, Math.random());
article.put(Article.ARTICLE_SIGN_ID, "1");
article.put(Article.ARTICLE_COMMENTABLE, true);
article.put(Article.ARTICLE_VIEW_PWD, "");
article.put(Article.ARTICLE_EDITOR_TYPE, "");
final Transaction transaction = articleRepository.beginTransaction();
articleRepository.add(article);
transaction.commit();
Assert.assertEquals(articleRepository.count(), 2);
JSONObject previousArticle = articleRepository.getPreviousArticle(article.getString(Keys.OBJECT_ID));
Assert.assertNotNull(previousArticle);
Assert.assertEquals(previousArticle.getString(Article.ARTICLE_TITLE), "article title1");
Assert.assertEquals(previousArticle.getString(Article.ARTICLE_PERMALINK), "article permalink1");
Assert.assertNull(previousArticle.opt(Keys.OBJECT_ID));
previousArticle = articleRepository.getByPermalink(previousArticle.getString(Article.ARTICLE_PERMALINK));
final JSONObject nextArticle = articleRepository.getNextArticle(previousArticle.getString(Keys.OBJECT_ID));
Assert.assertNotNull(previousArticle);
Assert.assertEquals(nextArticle.getString(Article.ARTICLE_TITLE), "article title2");
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class ArticleRepositoryImplTestCase method add.
/**
* Adds successfully.
*
* @throws Exception exception
*/
@Test
public void add() throws Exception {
final ArticleRepository articleRepository = getArticleRepository();
final JSONObject article = new JSONObject();
article.put(Article.ARTICLE_TITLE, "article title1");
article.put(Article.ARTICLE_ABSTRACT, "article abstract");
article.put(Article.ARTICLE_TAGS_REF, "tag1, tag2");
article.put(Article.ARTICLE_AUTHOR_EMAIL, "test@gmail.com");
article.put(Article.ARTICLE_COMMENT_COUNT, 0);
article.put(Article.ARTICLE_VIEW_COUNT, 0);
article.put(Article.ARTICLE_CONTENT, "article content");
article.put(Article.ARTICLE_PERMALINK, "article permalink1");
article.put(Article.ARTICLE_HAD_BEEN_PUBLISHED, true);
article.put(Article.ARTICLE_IS_PUBLISHED, true);
article.put(Article.ARTICLE_PUT_TOP, false);
article.put(Article.ARTICLE_CREATE_DATE, new Date());
article.put(Article.ARTICLE_UPDATE_DATE, new Date());
article.put(Article.ARTICLE_RANDOM_DOUBLE, Math.random());
article.put(Article.ARTICLE_SIGN_ID, "1");
article.put(Article.ARTICLE_COMMENTABLE, true);
article.put(Article.ARTICLE_VIEW_PWD, "");
article.put(Article.ARTICLE_EDITOR_TYPE, "");
final Transaction transaction = articleRepository.beginTransaction();
articleRepository.add(article);
transaction.commit();
final JSONArray results = articleRepository.getByAuthorEmail("test@gmail.com", 1, Integer.MAX_VALUE).getJSONArray(Keys.RESULTS);
Assert.assertEquals(results.length(), 1);
}
Aggregations