Search in sources :

Example 31 with Transaction

use of org.b3log.latke.repository.Transaction 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 32 with Transaction

use of org.b3log.latke.repository.Transaction 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 33 with Transaction

use of org.b3log.latke.repository.Transaction 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 34 with Transaction

use of org.b3log.latke.repository.Transaction 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)

Example 35 with Transaction

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

the class PageMgmtService method addPage.

/**
     * Adds a page with the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,      <pre>
     * {
     *     "page": {
     *         "pageTitle": "",
     *         "pageContent": "",
     *         "pageOpenTarget": "",
     *         "pageCommentable": boolean,
     *         "pageType": "",
     *         "pagePermalink": "", // optional
     *         "pageEditorType": "" // optional, preference specified if not exists this key
     *     }
     * }, see {@link Page} for more details
     * </pre>
     *
     * @return generated page id
     * @throws ServiceException if permalink format checks failed or persists failed
     */
public String addPage(final JSONObject requestJSONObject) throws ServiceException {
    final Transaction transaction = pageRepository.beginTransaction();
    try {
        final JSONObject page = requestJSONObject.getJSONObject(Page.PAGE);
        page.put(Page.PAGE_COMMENT_COUNT, 0);
        final int maxOrder = pageRepository.getMaxOrder();
        page.put(Page.PAGE_ORDER, maxOrder + 1);
        String permalink = page.optString(Page.PAGE_PERMALINK);
        if (Strings.isEmptyOrNull(permalink)) {
            permalink = "/pages/" + Ids.genTimeMillisId() + ".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 (permalinkQueryService.exist(permalink)) {
                if (transaction.isActive()) {
                    transaction.rollback();
                }
                throw new ServiceException(langPropsService.get("duplicatedPermalinkLabel"));
            }
        }
        page.put(Page.PAGE_PERMALINK, permalink.replaceAll(" ", "-"));
        // Set editor type
        if (!page.has(Page.PAGE_EDITOR_TYPE)) {
            final JSONObject preference = preferenceQueryService.getPreference();
            page.put(Page.PAGE_EDITOR_TYPE, preference.optString(Option.ID_C_EDITOR_TYPE));
        }
        final String ret = pageRepository.add(page);
        transaction.commit();
        return ret;
    } catch (final JSONException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        if (transaction.isActive()) {
            transaction.rollback();
        }
        throw new ServiceException(e);
    } catch (final RepositoryException 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) JSONException(org.json.JSONException) 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