Search in sources :

Example 6 with Transaction

use of org.b3log.latke.repository.Transaction in project solo by b3log.

the class UserRepositoryImplTestCase method test.

/**
     * Tests.
     * 
     * @throws Exception exception
     */
@Test
public void test() throws Exception {
    final UserRepository userRepository = getUserRepository();
    final JSONObject another = new JSONObject();
    another.put(User.USER_NAME, "test1");
    another.put(User.USER_EMAIL, "test1@gmail.com");
    another.put(User.USER_PASSWORD, "pass1");
    another.put(User.USER_URL, "http://b3log.org");
    another.put(User.USER_ROLE, Role.DEFAULT_ROLE);
    another.put(UserExt.USER_ARTICLE_COUNT, 0);
    another.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, 0);
    another.put(UserExt.USER_AVATAR, "");
    Transaction transaction = userRepository.beginTransaction();
    userRepository.add(another);
    transaction.commit();
    Assert.assertNull(userRepository.getAdmin());
    JSONObject admin = new JSONObject();
    admin.put(User.USER_NAME, "test");
    admin.put(User.USER_EMAIL, "test@gmail.com");
    admin.put(User.USER_PASSWORD, "pass");
    admin.put(User.USER_URL, "http://b3log.org");
    admin.put(User.USER_ROLE, Role.ADMIN_ROLE);
    admin.put(UserExt.USER_ARTICLE_COUNT, 0);
    admin.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, 0);
    admin.put(UserExt.USER_AVATAR, "");
    transaction = userRepository.beginTransaction();
    userRepository.add(admin);
    transaction.commit();
    Assert.assertTrue(userRepository.isAdminEmail("test@gmail.com"));
    Assert.assertFalse(userRepository.isAdminEmail("notFound@gmail.com"));
    admin = userRepository.getAdmin();
    Assert.assertNotNull(admin);
    Assert.assertEquals("test", admin.optString(User.USER_NAME));
    final JSONObject result = userRepository.get(new Query().setFilter(new PropertyFilter(User.USER_NAME, FilterOperator.EQUAL, "test1")));
    final JSONArray users = result.getJSONArray(Keys.RESULTS);
    Assert.assertEquals(users.length(), 1);
    Assert.assertEquals(users.getJSONObject(0).getString(User.USER_EMAIL), "test1@gmail.com");
    final JSONObject notFound = userRepository.getByEmail("not.found@gmail.com");
    Assert.assertNull(notFound);
    final JSONObject found = userRepository.getByEmail("test1@gmail.com");
    Assert.assertNotNull(found);
    Assert.assertEquals(found.getString(User.USER_PASSWORD), "pass1");
}
Also used : UserRepository(org.b3log.solo.repository.UserRepository) JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter) Test(org.testng.annotations.Test)

Example 7 with Transaction

use of org.b3log.latke.repository.Transaction in project solo by b3log.

the class ArchiveDateArticleRepositoryImplTestCase method add.

/**
     * Adds successfully.
     *
     * @throws Exception exception
     */
@Test
public void add() throws Exception {
    final ArchiveDateArticleRepository archiveDateArticleRepository = getArchiveDateArticleRepository();
    final JSONObject archiveDateArticle = new JSONObject();
    archiveDateArticle.put(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID, "archiveDateId");
    archiveDateArticle.put(Article.ARTICLE + "_" + Keys.OBJECT_ID, "articleId");
    final Transaction transaction = archiveDateArticleRepository.beginTransaction();
    archiveDateArticleRepository.add(archiveDateArticle);
    transaction.commit();
    final JSONObject found = archiveDateArticleRepository.getByArticleId("articleId");
    Assert.assertNotNull(found);
    final JSONObject notFound = archiveDateArticleRepository.getByArticleId("not found");
    Assert.assertNull(notFound);
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) ArchiveDateArticleRepository(org.b3log.solo.repository.ArchiveDateArticleRepository) Test(org.testng.annotations.Test)

Example 8 with Transaction

use of org.b3log.latke.repository.Transaction in project solo by b3log.

the class ArchiveDateRepositoryImplTestCase method add.

/**
     * Adds successfully.
     *
     * @throws Exception exception
     */
@Test
public void add() throws Exception {
    final ArchiveDateRepository archiveDateRepository = getArchiveDateRepository();
    final JSONObject archiveDate = new JSONObject();
    archiveDate.put(ArchiveDate.ARCHIVE_TIME, DateUtils.parseDate("2011/12", new String[] { "yyyy/MM" }).getTime());
    archiveDate.put(ArchiveDate.ARCHIVE_DATE_ARTICLE_COUNT, 1);
    archiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, 1);
    final Transaction transaction = archiveDateRepository.beginTransaction();
    archiveDateRepository.add(archiveDate);
    transaction.commit();
    final List<JSONObject> archiveDates = archiveDateRepository.getArchiveDates();
    Assert.assertNotNull(archiveDates);
    Assert.assertEquals(1, archiveDates.size());
}
Also used : ArchiveDateRepository(org.b3log.solo.repository.ArchiveDateRepository) JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) Test(org.testng.annotations.Test)

Example 9 with Transaction

use of org.b3log.latke.repository.Transaction in project solo by b3log.

the class ArticleMgmtService method updateArticlesRandomValue.

/**
     * Updates the random values of articles fetched with the specified update
     * count.
     *
     * @param updateCnt the specified update count
     * @throws ServiceException service exception
     */
public void updateArticlesRandomValue(final int updateCnt) throws ServiceException {
    final Transaction transaction = articleRepository.beginTransaction();
    try {
        final List<JSONObject> randomArticles = articleRepository.getRandomly(updateCnt);
        for (final JSONObject article : randomArticles) {
            article.put(Article.ARTICLE_RANDOM_DOUBLE, Math.random());
            articleRepository.update(article.getString(Keys.OBJECT_ID), article);
        }
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.WARN, "Updates article random value failed");
        throw new ServiceException(e);
    }
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) EventException(org.b3log.latke.event.EventException) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) ParseException(java.text.ParseException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 10 with Transaction

use of org.b3log.latke.repository.Transaction in project solo by b3log.

the class ArticleMgmtService method updateArticle.

/**
     * Updates an article by the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,
     * <pre>
     * {
     *     "article": {
     *         "oId": "",
     *         "articleTitle": "",
     *         "articleAbstract": "",
     *         "articleContent": "",
     *         "articleTags": "tag1,tag2,tag3",
     *         "articlePermalink": "", // optional
     *         "articleIsPublished": boolean,
     *         "articleSignId": "", // optional
     *         "articleCommentable": boolean,
     *         "articleViewPwd": "",
     *         "articleEditorType": "" // optional, preference specified if not exists this key
     *     }
     * }
     * </pre>
     * @throws ServiceException service exception
     */
public void updateArticle(final JSONObject requestJSONObject) throws ServiceException {
    final JSONObject ret = new JSONObject();
    final Transaction transaction = articleRepository.beginTransaction();
    try {
        final JSONObject article = requestJSONObject.getJSONObject(ARTICLE);
        final String tagsString = article.optString(Article.ARTICLE_TAGS_REF);
        article.put(Article.ARTICLE_TAGS_REF, tagsString.replaceAll(",", ",").replaceAll("、", ","));
        final String articleId = article.getString(Keys.OBJECT_ID);
        // Set permalink
        final JSONObject oldArticle = articleRepository.get(articleId);
        final String permalink = getPermalinkForUpdateArticle(oldArticle, article, (Date) oldArticle.get(ARTICLE_CREATE_DATE));
        article.put(ARTICLE_PERMALINK, permalink);
        processTagsForArticleUpdate(oldArticle, article);
        if (!oldArticle.getString(Article.ARTICLE_PERMALINK).equals(permalink)) {
            // The permalink has been updated
            // Updates related comments' links
            processCommentsForArticleUpdate(article);
        }
        // Fill auto properties
        fillAutoProperties(oldArticle, article);
        // Set date
        article.put(ARTICLE_UPDATE_DATE, oldArticle.get(ARTICLE_UPDATE_DATE));
        final JSONObject preference = preferenceQueryService.getPreference();
        final Date date = new Date();
        // The article to update has no sign
        if (!article.has(Article.ARTICLE_SIGN_ID)) {
            article.put(Article.ARTICLE_SIGN_ID, "0");
        }
        if (article.getBoolean(ARTICLE_IS_PUBLISHED)) {
            // Publish it
            if (articleQueryService.hadBeenPublished(oldArticle)) {
                // Edit update date only for published article
                article.put(ARTICLE_UPDATE_DATE, date);
            } else {
                // This article is a draft and this is the first time to publish it
                article.put(ARTICLE_CREATE_DATE, date);
                article.put(ARTICLE_UPDATE_DATE, date);
                article.put(ARTICLE_HAD_BEEN_PUBLISHED, true);
            }
        } else {
            // Save as draft
            if (articleQueryService.hadBeenPublished(oldArticle)) {
                // Save update date only for published article
                article.put(ARTICLE_UPDATE_DATE, date);
            } else {
                // Reset create/update date to indicate this is an new draft
                article.put(ARTICLE_CREATE_DATE, date);
                article.put(ARTICLE_UPDATE_DATE, date);
            }
        }
        // Set editor type
        if (!article.has(Article.ARTICLE_EDITOR_TYPE)) {
            article.put(Article.ARTICLE_EDITOR_TYPE, preference.optString(Option.ID_C_EDITOR_TYPE));
        }
        final boolean publishNewArticle = !oldArticle.getBoolean(ARTICLE_IS_PUBLISHED) && article.getBoolean(ARTICLE_IS_PUBLISHED);
        // Set statistic
        if (publishNewArticle) {
            // This article is updated from unpublished to published
            statisticMgmtService.incPublishedBlogArticleCount();
            final int blogCmtCnt = statisticQueryService.getPublishedBlogCommentCount();
            final int articleCmtCnt = article.getInt(ARTICLE_COMMENT_COUNT);
            statisticMgmtService.setPublishedBlogCommentCount(blogCmtCnt + articleCmtCnt);
            final JSONObject author = userRepository.getByEmail(article.optString(Article.ARTICLE_AUTHOR_EMAIL));
            author.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, author.optInt(UserExt.USER_PUBLISHED_ARTICLE_COUNT) + 1);
            userRepository.update(author.optString(Keys.OBJECT_ID), author);
        }
        if (publishNewArticle) {
            incArchiveDatePublishedRefCount(articleId);
        }
        // Update
        final boolean postToCommunity = article.optBoolean(Common.POST_TO_COMMUNITY, true);
        // Do not persist this property
        article.remove(Common.POST_TO_COMMUNITY);
        articleRepository.update(articleId, article);
        // Restores the property
        article.put(Common.POST_TO_COMMUNITY, postToCommunity);
        if (publishNewArticle) {
            // Fire add article event
            final JSONObject eventData = new JSONObject();
            eventData.put(ARTICLE, article);
            eventData.put(Keys.RESULTS, ret);
            try {
                eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.ADD_ARTICLE, eventData));
            } catch (final EventException e) {
                LOGGER.log(Level.ERROR, e.getMessage(), e);
            }
        } else {
            // Fire update article event
            final JSONObject eventData = new JSONObject();
            eventData.put(ARTICLE, article);
            eventData.put(Keys.RESULTS, ret);
            try {
                eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.UPDATE_ARTICLE, eventData));
            } catch (final EventException e) {
                LOGGER.log(Level.ERROR, e.getMessage(), e);
            }
        }
        transaction.commit();
    } catch (final ServiceException e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates an article failed", e);
        throw e;
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates an article failed", e);
        throw new ServiceException(e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) ServiceException(org.b3log.latke.service.ServiceException) EventException(org.b3log.latke.event.EventException) Date(java.util.Date) ArchiveDate(org.b3log.solo.model.ArchiveDate) EventException(org.b3log.latke.event.EventException) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) ParseException(java.text.ParseException) RepositoryException(org.b3log.latke.repository.RepositoryException)

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