Search in sources :

Example 36 with Transaction

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

the class StatisticMgmtService method incBlogViewCount.

/**
     * Blog statistic view count +1.
     *
     * <p>
     * If it is a search engine bot made the specified request, will NOT increment blog statistic view count.
     * </p>
     *
     * <p>
     * There is a cron job (/console/stat/viewcnt) to flush the blog view count from cache to datastore.
     * </p>
     *
     * @param request the specified request
     * @param response the specified response
     * @throws ServiceException service exception
     * @see Requests#searchEngineBotRequest(javax.servlet.http.HttpServletRequest)
     */
public void incBlogViewCount(final HttpServletRequest request, final HttpServletResponse response) throws ServiceException {
    if (Requests.searchEngineBotRequest(request)) {
        return;
    }
    if (Requests.hasBeenServed(request, response)) {
        return;
    }
    final Transaction transaction = statisticRepository.beginTransaction();
    JSONObject statistic = null;
    try {
        statistic = statisticRepository.get(Statistic.STATISTIC);
        if (null == statistic) {
            return;
        }
        LOGGER.log(Level.TRACE, "Before inc blog view count[statistic={0}]", statistic);
        int blogViewCnt = statistic.optInt(Statistic.STATISTIC_BLOG_VIEW_COUNT);
        ++blogViewCnt;
        statistic.put(Statistic.STATISTIC_BLOG_VIEW_COUNT, blogViewCnt);
        statisticRepository.update(Statistic.STATISTIC, statistic);
        transaction.commit();
    } catch (final RepositoryException e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates blog view count failed", e);
        return;
    }
    LOGGER.log(Level.TRACE, "Inced blog view count[statistic={0}]", statistic);
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 37 with Transaction

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

the class TagMgmtService method removeUnusedTags.

/**
     * Removes all unused tags.
     *
     * @throws ServiceException if get tags failed, or remove failed
     */
public void removeUnusedTags() throws ServiceException {
    final Transaction transaction = tagRepository.beginTransaction();
    try {
        final List<JSONObject> tags = tagQueryService.getTags();
        for (int i = 0; i < tags.size(); i++) {
            final JSONObject tag = tags.get(i);
            final int tagRefCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT);
            if (0 == tagRefCnt) {
                final String tagId = tag.getString(Keys.OBJECT_ID);
                tagRepository.remove(tagId);
            }
        }
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Removes unused tags 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)

Example 38 with Transaction

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

the class UserMgmtService method removeUser.

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

Example 39 with Transaction

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

the class UserMgmtService method updateUser.

/**
     * Updates a user by the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,      <pre>
     * {
     *     "oId": "",
     *     "userName": "",
     *     "userEmail": "",
     *     "userPassword": "", // Unhashed
     *     "userRole": "", // optional
     *     "userURL": "", // optional
     * }
     * </pre>
     *
     * @throws ServiceException service exception
     */
public void updateUser(final JSONObject requestJSONObject) throws ServiceException {
    final Transaction transaction = userRepository.beginTransaction();
    try {
        final String oldUserId = requestJSONObject.optString(Keys.OBJECT_ID);
        final JSONObject oldUser = userRepository.get(oldUserId);
        if (null == oldUser) {
            throw new ServiceException(langPropsService.get("updateFailLabel"));
        }
        final String userNewEmail = requestJSONObject.optString(User.USER_EMAIL).toLowerCase().trim();
        // Check email is whether duplicated
        final JSONObject mayBeAnother = userRepository.getByEmail(userNewEmail);
        if (null != mayBeAnother && !mayBeAnother.optString(Keys.OBJECT_ID).equals(oldUserId)) {
            // Exists someone else has the save email as requested
            throw new ServiceException(langPropsService.get("duplicatedEmailLabel"));
        }
        // Update
        final String userName = requestJSONObject.optString(User.USER_NAME);
        final String userPassword = requestJSONObject.optString(User.USER_PASSWORD);
        oldUser.put(User.USER_EMAIL, userNewEmail);
        oldUser.put(User.USER_NAME, userName);
        final boolean mybeHashed = HASHED_PASSWORD_LENGTH == userPassword.length();
        final String newHashedPassword = MD5.hash(userPassword);
        final String oldHashedPassword = oldUser.optString(User.USER_PASSWORD);
        if (!"demo.b3log.org".equals(Latkes.getServerHost())) {
            // Skips the Solo Online Demo (http://demo.b3log.org)
            if (!mybeHashed || (!oldHashedPassword.equals(userPassword) && !oldHashedPassword.equals(newHashedPassword))) {
                oldUser.put(User.USER_PASSWORD, newHashedPassword);
            }
        }
        final String userRole = requestJSONObject.optString(User.USER_ROLE);
        if (!Strings.isEmptyOrNull(userRole)) {
            oldUser.put(User.USER_ROLE, userRole);
        }
        final String userURL = requestJSONObject.optString(User.USER_URL);
        if (!Strings.isEmptyOrNull(userURL)) {
            oldUser.put(User.USER_URL, userURL);
        }
        final String userAvatar = requestJSONObject.optString(UserExt.USER_AVATAR);
        if (!StringUtils.equals(userAvatar, oldUser.optString(UserExt.USER_AVATAR))) {
            oldUser.put(UserExt.USER_AVATAR, userAvatar);
        }
        userRepository.update(oldUserId, oldUser);
        transaction.commit();
    } catch (final RepositoryException e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates 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 40 with Transaction

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

the class UserMgmtService method changeRole.

/**
     * Swithches the user role between "defaultRole" and "visitorRole" by the specified user id.
     *
     * @param userId the specified user id
     * @throws ServiceException exception
     * @see User
     */
public void changeRole(final String userId) throws ServiceException {
    final Transaction transaction = userRepository.beginTransaction();
    try {
        final JSONObject oldUser = userRepository.get(userId);
        if (null == oldUser) {
            throw new ServiceException(langPropsService.get("updateFailLabel"));
        }
        final String role = oldUser.optString(User.USER_ROLE);
        if (Role.VISITOR_ROLE.equals(role)) {
            oldUser.put(User.USER_ROLE, Role.DEFAULT_ROLE);
        } else if (Role.DEFAULT_ROLE.equals(role)) {
            oldUser.put(User.USER_ROLE, Role.VISITOR_ROLE);
        }
        userRepository.update(userId, oldUser);
        transaction.commit();
    } catch (final RepositoryException e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates 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)

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