Search in sources :

Example 11 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class CommentQueryService method getComments.

/**
     * Gets comments of an article or page specified by the on id.
     *
     * @param onId the specified on id
     * @return a list of comments, returns an empty list if not found
     * @throws ServiceException service exception
     */
public List<JSONObject> getComments(final String onId) throws ServiceException {
    try {
        final List<JSONObject> ret = new ArrayList<JSONObject>();
        final List<JSONObject> comments = commentRepository.getComments(onId, 1, Integer.MAX_VALUE);
        for (final JSONObject comment : comments) {
            comment.put(Comment.COMMENT_TIME, ((Date) comment.get(Comment.COMMENT_DATE)).getTime());
            // 1.9.0 向后兼容
            comment.put("commentDate2", comment.get(Comment.COMMENT_DATE));
            comment.put(Comment.COMMENT_NAME, comment.getString(Comment.COMMENT_NAME));
            String url = comment.getString(Comment.COMMENT_URL);
            if (StringUtils.contains(url, "<")) {
                // legacy issue https://github.com/b3log/solo/issues/12091
                url = "";
            }
            comment.put(Comment.COMMENT_URL, url);
            // Assumes this comment is not a reply
            comment.put(Common.IS_REPLY, false);
            final String email = comment.optString(Comment.COMMENT_EMAIL);
            comment.put(Comment.COMMENT_THUMBNAIL_URL, Thumbnails.getGravatarURL(email, "128"));
            if (!Strings.isEmptyOrNull(comment.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID))) {
                // This comment is a reply
                comment.put(Common.IS_REPLY, true);
            }
            String commentContent = comment.optString(Comment.COMMENT_CONTENT);
            commentContent = Emotions.convert(commentContent);
            commentContent = Markdowns.toHTML(commentContent);
            comment.put(Comment.COMMENT_CONTENT, commentContent);
            ret.add(comment);
        }
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets comments failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ArrayList(java.util.ArrayList) ServiceException(org.b3log.latke.service.ServiceException)

Example 12 with ServiceException

use of org.b3log.latke.service.ServiceException 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 13 with ServiceException

use of org.b3log.latke.service.ServiceException 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 14 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class LinkQueryService method getLinks.

/**
     * Gets links by the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,
     * <pre>
     * {
     *     "paginationCurrentPageNum": 1,
     *     "paginationPageSize": 20,
     *     "paginationWindowSize": 10
     * }, see {@link Pagination} for more details
     * </pre>
     * @return for example,
     * <pre>
     * {
     *     "pagination": {
     *         "paginationPageCount": 100,
     *         "paginationPageNums": [1, 2, 3, 4, 5]
     *     },
     *     "links": [{
     *         "oId": "",
     *         "linkTitle": "",
     *         "linkAddress": "",
     *         ""linkDescription": ""
     *      }, ....]
     * }
     * </pre>
     * @throws ServiceException service exception
     * @see Pagination
     */
public JSONObject getLinks(final JSONObject requestJSONObject) throws ServiceException {
    final JSONObject ret = new JSONObject();
    try {
        final int currentPageNum = requestJSONObject.getInt(Pagination.PAGINATION_CURRENT_PAGE_NUM);
        final int pageSize = requestJSONObject.getInt(Pagination.PAGINATION_PAGE_SIZE);
        final int windowSize = requestJSONObject.getInt(Pagination.PAGINATION_WINDOW_SIZE);
        final Query query = new Query().setCurrentPageNum(currentPageNum).setPageSize(pageSize).addSort(Link.LINK_ORDER, SortDirection.ASCENDING);
        final JSONObject result = linkRepository.get(query);
        final int pageCount = result.getJSONObject(Pagination.PAGINATION).getInt(Pagination.PAGINATION_PAGE_COUNT);
        final JSONObject pagination = new JSONObject();
        final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
        pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
        pagination.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);
        final JSONArray links = result.getJSONArray(Keys.RESULTS);
        ret.put(Pagination.PAGINATION, pagination);
        ret.put(Link.LINKS, links);
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets links failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException)

Example 15 with ServiceException

use of org.b3log.latke.service.ServiceException 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)

Aggregations

ServiceException (org.b3log.latke.service.ServiceException)102 JSONObject (org.json.JSONObject)91 JSONException (org.json.JSONException)45 RepositoryException (org.b3log.latke.repository.RepositoryException)34 Transaction (org.b3log.latke.repository.Transaction)31 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)23 JSONArray (org.json.JSONArray)17 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)16 EventException (org.b3log.latke.event.EventException)14 IOException (java.io.IOException)11 ParseException (java.text.ParseException)10 ArrayList (java.util.ArrayList)8 Query (org.b3log.latke.repository.Query)8 Date (java.util.Date)7 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)6 Template (freemarker.template.Template)3 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)3 ArchiveDate (org.b3log.solo.model.ArchiveDate)3 URL (java.net.URL)2 HashMap (java.util.HashMap)2