Search in sources :

Example 46 with Transaction

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);
    }
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) RepositoryException(org.b3log.latke.repository.RepositoryException) IOException(java.io.IOException)

Example 47 with Transaction

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;
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) ServiceException(org.b3log.latke.service.ServiceException) Date(java.util.Date) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) RepositoryException(org.b3log.latke.repository.RepositoryException) IOException(java.io.IOException)

Example 48 with Transaction

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);
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) ArticleRepository(org.b3log.solo.repository.ArticleRepository) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 49 with Transaction

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");
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) ArticleRepository(org.b3log.solo.repository.ArticleRepository) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 50 with Transaction

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);
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) JSONArray(org.json.JSONArray) ArticleRepository(org.b3log.solo.repository.ArticleRepository) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

Transaction (org.b3log.latke.repository.Transaction)57 JSONObject (org.json.JSONObject)49 ServiceException (org.b3log.latke.service.ServiceException)33 RepositoryException (org.b3log.latke.repository.RepositoryException)23 JSONException (org.json.JSONException)21 Test (org.testng.annotations.Test)16 Date (java.util.Date)9 ParseException (java.text.ParseException)8 EventException (org.b3log.latke.event.EventException)8 IOException (java.io.IOException)6 ArticleRepository (org.b3log.solo.repository.ArticleRepository)4 JSONArray (org.json.JSONArray)4 AbstractPlugin (org.b3log.latke.plugin.AbstractPlugin)3 PageRepository (org.b3log.solo.repository.PageRepository)3 URL (java.net.URL)2 Query (org.b3log.latke.repository.Query)2 OptionRepository (org.b3log.solo.repository.OptionRepository)2 TagArticleRepository (org.b3log.solo.repository.TagArticleRepository)2 TagRepository (org.b3log.solo.repository.TagRepository)2 MalformedURLException (java.net.MalformedURLException)1