Search in sources :

Example 56 with ServiceException

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

the class OptionMgmtService method removeOption.

/**
     * Removes the option specified by the given option id. 
     * 
     * @param optionId the given option id
     * @throws ServiceException service exception
     */
public void removeOption(final String optionId) throws ServiceException {
    final Transaction transaction = optionRepository.beginTransaction();
    try {
        optionRepository.remove(optionId);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        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 57 with ServiceException

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

the class OptionQueryService method getOptions.

/**
     * Gets options with the specified category.
     * 
     * <p>
     * All options with the specified category will be merged into one json object as the return value.
     * </p>
     * 
     * @param category the specified category
     * @return all options with the specified category, for example,
     * <pre>
     * {
     *     "${optionId}": "${optionValue}",
     *     ....
     * }
     * </pre>, returns {@code null} if not found
     * @throws ServiceException service exception
     */
public JSONObject getOptions(final String category) throws ServiceException {
    final Query query = new Query();
    query.setFilter(new PropertyFilter(Option.OPTION_CATEGORY, FilterOperator.EQUAL, category));
    try {
        final JSONObject result = optionRepository.get(query);
        final JSONArray options = result.getJSONArray(Keys.RESULTS);
        if (0 == options.length()) {
            return null;
        }
        final JSONObject ret = new JSONObject();
        for (int i = 0; i < options.length(); i++) {
            final JSONObject option = options.getJSONObject(i);
            ret.put(option.getString(Keys.OBJECT_ID), option.getString(Option.OPTION_VALUE));
        }
        return ret;
    } catch (final Exception e) {
        throw new ServiceException(e);
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 58 with ServiceException

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

the class PageMgmtService method changeOrder.

/**
     * Changes the order of a page specified by the given page id with the specified direction.
     *
     * @param pageId the given page id
     * @param direction the specified direction, "up"/"down"
     * @throws ServiceException service exception
     */
public void changeOrder(final String pageId, final String direction) throws ServiceException {
    final Transaction transaction = pageRepository.beginTransaction();
    try {
        final JSONObject srcPage = pageRepository.get(pageId);
        final int srcPageOrder = srcPage.getInt(Page.PAGE_ORDER);
        JSONObject targetPage;
        if ("up".equals(direction)) {
            targetPage = pageRepository.getUpper(pageId);
        } else {
            // Down
            targetPage = pageRepository.getUnder(pageId);
        }
        if (null == targetPage) {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            LOGGER.log(Level.WARN, "Cant not find the target page of source page[order={0}]", srcPageOrder);
            return;
        }
        // Swaps
        srcPage.put(Page.PAGE_ORDER, targetPage.getInt(Page.PAGE_ORDER));
        targetPage.put(Page.PAGE_ORDER, srcPageOrder);
        pageRepository.update(srcPage.getString(Keys.OBJECT_ID), srcPage);
        pageRepository.update(targetPage.getString(Keys.OBJECT_ID), targetPage);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Changes page'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) JSONException(org.json.JSONException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 59 with ServiceException

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

the class PageMgmtService method removePage.

/**
     * Removes a page specified by the given page id.
     *
     * @param pageId the given page id
     * @throws ServiceException service exception
     */
public void removePage(final String pageId) throws ServiceException {
    final Transaction transaction = pageRepository.beginTransaction();
    try {
        LOGGER.log(Level.DEBUG, "Removing a page[id={0}]", pageId);
        removePageComments(pageId);
        pageRepository.remove(pageId);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Removes a page[id=" + pageId + "] 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) JSONException(org.json.JSONException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 60 with ServiceException

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

the class PageMgmtService method updatePage.

/**
     * Updates a page by the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,      <pre>
     * {
     *     "page": {
     *         "oId": "",
     *         "pageTitle": "",
     *         "pageContent": "",
     *         "pageOrder": int,
     *         "pageCommentCount": int,
     *         "pagePermalink": "",
     *         "pageCommentable": boolean,
     *         "pageType": "",
     *         "pageOpenTarget": "",
     *         "pageEditorType": "" // optional, preference specified if not exists this key
     *     }
     * }, see {@link Page} for more details
     * </pre>
     *
     * @throws ServiceException service exception
     */
public void updatePage(final JSONObject requestJSONObject) throws ServiceException {
    final Transaction transaction = pageRepository.beginTransaction();
    try {
        final JSONObject page = requestJSONObject.getJSONObject(Page.PAGE);
        final String pageId = page.getString(Keys.OBJECT_ID);
        final JSONObject oldPage = pageRepository.get(pageId);
        final JSONObject newPage = new JSONObject(page, JSONObject.getNames(page));
        newPage.put(Page.PAGE_ORDER, oldPage.getInt(Page.PAGE_ORDER));
        newPage.put(Page.PAGE_COMMENT_COUNT, oldPage.getInt(Page.PAGE_COMMENT_COUNT));
        String permalink = page.optString(Page.PAGE_PERMALINK).trim();
        final String oldPermalink = oldPage.getString(Page.PAGE_PERMALINK);
        if (!oldPermalink.equals(permalink)) {
            if (Strings.isEmptyOrNull(permalink)) {
                permalink = "/pages/" + pageId + ".html";
            }
            if (Page.PAGE.equals(page.getString(Page.PAGE_TYPE))) {
                if (!permalink.startsWith("/")) {
                    permalink = "/" + permalink;
                }
                if (PermalinkQueryService.invalidPagePermalinkFormat(permalink)) {
                    if (transaction.isActive()) {
                        transaction.rollback();
                    }
                    throw new ServiceException(langPropsService.get("invalidPermalinkFormatLabel"));
                }
                if (!oldPermalink.equals(permalink) && permalinkQueryService.exist(permalink)) {
                    if (transaction.isActive()) {
                        transaction.rollback();
                    }
                    throw new ServiceException(langPropsService.get("duplicatedPermalinkLabel"));
                }
            }
        }
        newPage.put(Page.PAGE_PERMALINK, permalink.replaceAll(" ", "-"));
        if (!oldPage.getString(Page.PAGE_PERMALINK).equals(permalink)) {
            // The permalink has been updated
            // Updates related comments' links
            processCommentsForPageUpdate(newPage);
        }
        // Set editor type
        if (!newPage.has(Page.PAGE_EDITOR_TYPE)) {
            final JSONObject preference = preferenceQueryService.getPreference();
            newPage.put(Page.PAGE_EDITOR_TYPE, preference.optString(Option.ID_C_EDITOR_TYPE));
        }
        pageRepository.update(pageId, newPage);
        transaction.commit();
        LOGGER.log(Level.DEBUG, "Updated a page[id={0}]", pageId);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), 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) JSONException(org.json.JSONException) RepositoryException(org.b3log.latke.repository.RepositoryException)

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