Search in sources :

Example 26 with Transactional

use of org.b3log.latke.repository.annotation.Transactional in project symphony by b3log.

the class InvitecodeMgmtService method expireInvitecodes.

/**
 * Expires invitecodes.
 */
@Transactional
public void expireInvitecodes() {
    final long now = System.currentTimeMillis();
    final long expired = now - Symphonys.getLong("invitecode.expired");
    final Query query = new Query().setCurrentPageNum(1).setPageSize(Integer.MAX_VALUE).setFilter(CompositeFilterOperator.and(new PropertyFilter(Invitecode.STATUS, FilterOperator.EQUAL, Invitecode.STATUS_C_UNUSED), new PropertyFilter(Invitecode.GENERATOR_ID, FilterOperator.NOT_EQUAL, Pointtransfer.ID_C_SYS), new PropertyFilter(Keys.OBJECT_ID, FilterOperator.LESS_THAN_OR_EQUAL, expired)));
    JSONObject result;
    try {
        result = invitecodeRepository.get(query);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Gets invitecodes failed", e);
        return;
    }
    final JSONArray data = result.optJSONArray(Keys.RESULTS);
    try {
        for (int i = 0; i < data.length(); i++) {
            final JSONObject invitecode = data.optJSONObject(i);
            final String invitecodeId = invitecode.optString(Keys.OBJECT_ID);
            invitecodeRepository.remove(invitecodeId);
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Expires invitecodes failed", e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException) Transactional(org.b3log.latke.repository.annotation.Transactional)

Example 27 with Transactional

use of org.b3log.latke.repository.annotation.Transactional in project symphony by b3log.

the class VoteMgmtService method voteCancel.

/**
 * Cancels the vote.
 *
 * @param userId   the specified user id
 * @param dataId   the specified data id
 * @param dataType the specified data type
 */
@Transactional
public void voteCancel(final String userId, final String dataId, final int dataType) {
    try {
        final int oldType = voteRepository.removeIfExists(userId, dataId, dataType);
        if (Vote.DATA_TYPE_C_ARTICLE == dataType) {
            final JSONObject article = articleRepository.get(dataId);
            if (null == article) {
                LOGGER.log(Level.ERROR, "Not found article [id={0}] to vote cancel", dataId);
                return;
            }
            if (Vote.TYPE_C_UP == oldType) {
                article.put(Article.ARTICLE_GOOD_CNT, article.optInt(Article.ARTICLE_GOOD_CNT) - 1);
            } else if (Vote.TYPE_C_DOWN == oldType) {
                article.put(Article.ARTICLE_BAD_CNT, article.optInt(Article.ARTICLE_BAD_CNT) - 1);
            }
            final int ups = article.optInt(Article.ARTICLE_GOOD_CNT);
            final int downs = article.optInt(Article.ARTICLE_BAD_CNT);
            final long t = article.optLong(Keys.OBJECT_ID) / 1000;
            final double redditScore = redditArticleScore(ups, downs, t);
            article.put(Article.REDDIT_SCORE, redditScore);
            updateTagArticleScore(article);
            articleRepository.update(dataId, article);
        } else if (Vote.DATA_TYPE_C_COMMENT == dataType) {
            final JSONObject comment = commentRepository.get(dataId);
            if (null == comment) {
                LOGGER.log(Level.ERROR, "Not found comment [id={0}] to vote cancel", dataId);
                return;
            }
            if (Vote.TYPE_C_UP == oldType) {
                comment.put(Comment.COMMENT_GOOD_CNT, comment.optInt(Comment.COMMENT_GOOD_CNT) - 1);
            } else if (Vote.TYPE_C_DOWN == oldType) {
                comment.put(Comment.COMMENT_BAD_CNT, comment.optInt(Comment.COMMENT_BAD_CNT) - 1);
            }
            final int ups = comment.optInt(Comment.COMMENT_GOOD_CNT);
            final int downs = comment.optInt(Comment.COMMENT_BAD_CNT);
            final double redditScore = redditCommentScore(ups, downs);
            comment.put(Comment.COMMENT_SCORE, redditScore);
            commentRepository.update(dataId, comment);
        } else {
            LOGGER.warn("Wrong data type [" + dataType + "]");
        }
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) RepositoryException(org.b3log.latke.repository.RepositoryException) Transactional(org.b3log.latke.repository.annotation.Transactional)

Example 28 with Transactional

use of org.b3log.latke.repository.annotation.Transactional in project symphony by b3log.

the class TagMgmtService method removeUnusedTags.

/**
 * Removes unused tags.
 */
@Transactional
public synchronized void removeUnusedTags() {
    LOGGER.info("Starting remove unused tags....");
    int removedCnt = 0;
    try {
        final JSONArray tags = tagRepository.get(new Query()).optJSONArray(Keys.RESULTS);
        for (int i = 0; i < tags.length(); i++) {
            final JSONObject tag = tags.optJSONObject(i);
            final String tagId = tag.optString(Keys.OBJECT_ID);
            if (// article ref cnt
            0 == tag.optInt(Tag.TAG_REFERENCE_CNT) && 0 == domainTagRepository.getByTagId(tagId, 1, Integer.MAX_VALUE).optJSONArray(Keys.RESULTS).length() && // tagUserLinkRefCnt
            0 == tagUserLinkRepository.countTagLink(tagId)) {
                final JSONArray userTagRels = userTagRepository.getByTagId(tagId, 1, Integer.MAX_VALUE).optJSONArray(Keys.RESULTS);
                if (1 == userTagRels.length() && Tag.TAG_TYPE_C_CREATOR == userTagRels.optJSONObject(0).optInt(Common.TYPE)) {
                    // Just the tag's creator but not use it now
                    tagRepository.remove(tagId);
                    removedCnt++;
                    LOGGER.info("Removed a unused tag [title=" + tag.optString(Tag.TAG_TITLE) + "]");
                }
            }
        }
        final JSONObject tagCntOption = optionRepository.get(Option.ID_C_STATISTIC_TAG_COUNT);
        final int tagCnt = tagCntOption.optInt(Option.OPTION_VALUE);
        tagCntOption.put(Option.OPTION_VALUE, tagCnt - removedCnt);
        optionRepository.update(Option.ID_C_STATISTIC_TAG_COUNT, tagCntOption);
        LOGGER.info("Removed [" + removedCnt + "] unused tags");
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Removes unused tags failed", e);
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Transactional(org.b3log.latke.repository.annotation.Transactional)

Example 29 with Transactional

use of org.b3log.latke.repository.annotation.Transactional in project symphony by b3log.

the class VerifycodeMgmtService method removeByCode.

/**
 * Removes a verifycode with the specified code.
 *
 * @param code the specified code
 */
@Transactional
public void removeByCode(final String code) {
    final Query query = new Query().setFilter(new PropertyFilter(Verifycode.CODE, FilterOperator.EQUAL, code));
    try {
        final JSONArray results = verifycodeRepository.get(query).optJSONArray(Keys.RESULTS);
        if (1 > results.length()) {
            return;
        }
        verifycodeRepository.remove(results.optJSONObject(0).optString(Keys.OBJECT_ID));
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Removes by code [" + code + "] failed", e);
    }
}
Also used : JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException) Transactional(org.b3log.latke.repository.annotation.Transactional)

Example 30 with Transactional

use of org.b3log.latke.repository.annotation.Transactional in project symphony by b3log.

the class NotificationMgmtService method addArticleNewWatcherNotification.

/**
 * Adds a 'article new watcher' type notification with the specified request json object.
 *
 * @param requestJSONObject the specified request json object, for example,
 *                          "userId": "",
 *                          "dataId": "" // article id-follower user id
 * @throws ServiceException service exception
 */
@Transactional
public void addArticleNewWatcherNotification(final JSONObject requestJSONObject) throws ServiceException {
    try {
        requestJSONObject.put(Notification.NOTIFICATION_DATA_TYPE, Notification.DATA_TYPE_C_ARTICLE_NEW_WATCHER);
        addNotification(requestJSONObject);
    } catch (final RepositoryException e) {
        final String msg = "Adds notification [type=article_new_watcher] failed";
        LOGGER.log(Level.ERROR, msg, e);
        throw new ServiceException(msg);
    }
}
Also used : ServiceException(org.b3log.latke.service.ServiceException) Transactional(org.b3log.latke.repository.annotation.Transactional)

Aggregations

Transactional (org.b3log.latke.repository.annotation.Transactional)52 ServiceException (org.b3log.latke.service.ServiceException)42 JSONObject (org.json.JSONObject)20 JSONArray (org.json.JSONArray)11 RepositoryException (org.b3log.latke.repository.RepositoryException)6 Query (org.b3log.latke.repository.Query)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URL (java.net.URL)1 ExecutionException (java.util.concurrent.ExecutionException)1 PropertyFilter (org.b3log.latke.repository.PropertyFilter)1 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)1 TextHTMLRenderer (org.b3log.latke.servlet.renderer.TextHTMLRenderer)1