Search in sources :

Example 16 with Transaction

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

the class UserMgmtService method addUser.

/**
     * Adds a user with the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,      <pre>
     * {
     *     "userName": "",
     *     "userEmail": "",
     *     "userPassword": "", // Unhashed
     *     "userURL": "", // optional, uses 'servePath' instead if not specified
     *     "userRole": "", // optional, uses {@value Role#DEFAULT_ROLE} instead if not specified
     *     "userAvatar": "" // optional, users generated gravatar url instead if not specified
     * }
     * </pre>,see {@link User} for more details
     *
     * @return generated user id
     * @throws ServiceException service exception
     */
public String addUser(final JSONObject requestJSONObject) throws ServiceException {
    final Transaction transaction = userRepository.beginTransaction();
    try {
        final JSONObject user = new JSONObject();
        final String userEmail = requestJSONObject.optString(User.USER_EMAIL).trim().toLowerCase();
        final JSONObject duplicatedUser = userRepository.getByEmail(userEmail);
        if (null != duplicatedUser) {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            throw new ServiceException(langPropsService.get("duplicatedEmailLabel"));
        }
        user.put(User.USER_EMAIL, userEmail);
        final String userName = requestJSONObject.optString(User.USER_NAME);
        user.put(User.USER_NAME, userName);
        final String userPassword = requestJSONObject.optString(User.USER_PASSWORD);
        user.put(User.USER_PASSWORD, MD5.hash(userPassword));
        String userURL = requestJSONObject.optString(User.USER_URL);
        if (Strings.isEmptyOrNull(userURL)) {
            userURL = Latkes.getServePath();
        }
        if (!Strings.isURL(userURL)) {
            throw new ServiceException(langPropsService.get("urlInvalidLabel"));
        }
        user.put(User.USER_URL, userURL);
        final String roleName = requestJSONObject.optString(User.USER_ROLE, Role.DEFAULT_ROLE);
        user.put(User.USER_ROLE, roleName);
        user.put(UserExt.USER_ARTICLE_COUNT, 0);
        user.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, 0);
        String userAvatar = requestJSONObject.optString(UserExt.USER_AVATAR);
        if (Strings.isEmptyOrNull(userAvatar)) {
            userAvatar = Thumbnails.getGravatarURL(userEmail, "128");
        }
        user.put(UserExt.USER_AVATAR, userAvatar);
        userRepository.add(user);
        transaction.commit();
        return user.optString(Keys.OBJECT_ID);
    } catch (final RepositoryException e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Adds a user failed", e);
        throw new ServiceException(e);
    }
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 17 with Transaction

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

the class LinkMgmtService method removeLink.

/**
     * Removes a link specified by the given link id.
     *
     * @param linkId the given link id
     * @throws ServiceException service exception
     */
public void removeLink(final String linkId) throws ServiceException {
    final Transaction transaction = linkRepository.beginTransaction();
    try {
        linkRepository.remove(linkId);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Removes a link[id=" + linkId + "] failed", e);
        throw new ServiceException(e);
    }
}
Also used : Transaction(org.b3log.latke.repository.Transaction) ServiceException(org.b3log.latke.service.ServiceException) ServiceException(org.b3log.latke.service.ServiceException)

Example 18 with Transaction

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

the class LinkMgmtService method changeOrder.

/**
     * Changes the order of a link specified by the given link id with the 
     * specified direction.
     *
     * @param linkId the given link id
     * @param direction the specified direction, "up"/"down"
     * @throws ServiceException service exception
     */
public void changeOrder(final String linkId, final String direction) throws ServiceException {
    final Transaction transaction = linkRepository.beginTransaction();
    try {
        final JSONObject srcLink = linkRepository.get(linkId);
        final int srcLinkOrder = srcLink.getInt(Link.LINK_ORDER);
        JSONObject targetLink = null;
        if ("up".equals(direction)) {
            targetLink = linkRepository.getUpper(linkId);
        } else {
            // Down
            targetLink = linkRepository.getUnder(linkId);
        }
        if (null == targetLink) {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            LOGGER.log(Level.WARN, "Cant not find the target link of source link[order={0}]", srcLinkOrder);
            return;
        }
        // Swaps
        srcLink.put(Link.LINK_ORDER, targetLink.getInt(Link.LINK_ORDER));
        targetLink.put(Link.LINK_ORDER, srcLinkOrder);
        linkRepository.update(srcLink.getString(Keys.OBJECT_ID), srcLink);
        linkRepository.update(targetLink.getString(Keys.OBJECT_ID), targetLink);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Changes link's order 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)

Example 19 with Transaction

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

the class OptionMgmtService method addOrUpdateOption.

/**
     * Adds or updates the specified option.
     * 
     * @param option the specified option
     * @return option id
     * @throws ServiceException 
     */
public String addOrUpdateOption(final JSONObject option) throws ServiceException {
    final Transaction transaction = optionRepository.beginTransaction();
    try {
        String id = option.optString(Keys.OBJECT_ID);
        if (Strings.isEmptyOrNull(id)) {
            id = optionRepository.add(option);
        } else {
            final JSONObject old = optionRepository.get(id);
            if (null == old) {
                // The id is specified by caller
                id = optionRepository.add(option);
            } else {
                old.put(Option.OPTION_CATEGORY, option.optString(Option.OPTION_CATEGORY));
                old.put(Option.OPTION_VALUE, option.optString(Option.OPTION_VALUE));
                optionRepository.update(id, old);
            }
        }
        transaction.commit();
        return id;
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        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)

Example 20 with Transaction

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

the class CommentMgmtService method removeArticleComment.

/**
     * Removes a comment of an article with the specified comment id.
     *
     * @param commentId the given comment id
     * @throws ServiceException service exception
     */
public void removeArticleComment(final String commentId) throws ServiceException {
    final Transaction transaction = commentRepository.beginTransaction();
    try {
        final JSONObject comment = commentRepository.get(commentId);
        final String articleId = comment.getString(Comment.COMMENT_ON_ID);
        // Step 1: Remove comment
        commentRepository.remove(commentId);
        // Step 2: Update article comment count
        decArticleCommentCount(articleId);
        // 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 an article 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)

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