Search in sources :

Example 16 with RepositoryException

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

the class PreferenceQueryService method getPreference.

/**
     * Gets the user preference.
     *
     * @return user preference, returns {@code null} if not found
     * @throws ServiceException if repository exception
     */
public JSONObject getPreference() throws ServiceException {
    try {
        final JSONObject checkInit = optionRepository.get(Option.ID_C_ADMIN_EMAIL);
        if (null == checkInit) {
            return null;
        }
        final Query query = new Query();
        query.setFilter(new PropertyFilter(Option.OPTION_CATEGORY, FilterOperator.EQUAL, Option.CATEGORY_C_PREFERENCE));
        final JSONArray opts = optionRepository.get(query).optJSONArray(Keys.RESULTS);
        final JSONObject ret = new JSONObject();
        for (int i = 0; i < opts.length(); i++) {
            final JSONObject opt = opts.optJSONObject(i);
            ret.put(opt.optString(Keys.OBJECT_ID), opt.opt(Option.OPTION_VALUE));
        }
        return ret;
    } catch (final RepositoryException e) {
        return null;
    }
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 17 with RepositoryException

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

Example 18 with RepositoryException

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

the class StatisticMgmtService method incBlogArticleCount.

/**
     * Blog statistic article count +1.
     *
     * @throws RepositoryException repository exception
     */
public void incBlogArticleCount() throws RepositoryException {
    final JSONObject statistic = statisticRepository.get(Statistic.STATISTIC);
    if (null == statistic) {
        throw new RepositoryException("Not found statistic");
    }
    statistic.put(Statistic.STATISTIC_BLOG_ARTICLE_COUNT, statistic.optInt(Statistic.STATISTIC_BLOG_ARTICLE_COUNT) + 1);
    statisticRepository.update(Statistic.STATISTIC, statistic);
}
Also used : JSONObject(org.json.JSONObject) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 19 with RepositoryException

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

the class StatisticMgmtService method setBlogCommentCount.

/**
     * Sets blog comment count with the specified count.
     *
     * @param count the specified count
     * @throws JSONException json exception
     * @throws RepositoryException repository exception
     */
public void setBlogCommentCount(final int count) throws JSONException, RepositoryException {
    final JSONObject statistic = statisticRepository.get(Statistic.STATISTIC);
    if (null == statistic) {
        throw new RepositoryException("Not found statistic");
    }
    statistic.put(Statistic.STATISTIC_BLOG_COMMENT_COUNT, count);
    statisticRepository.update(Statistic.STATISTIC, statistic);
}
Also used : JSONObject(org.json.JSONObject) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 20 with RepositoryException

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

the class StatisticMgmtService method incPublishedBlogArticleCount.

/**
     * Blog statistic published article count +1.
     *
     * @throws RepositoryException repository exception
     */
public void incPublishedBlogArticleCount() throws RepositoryException {
    final JSONObject statistic = statisticRepository.get(Statistic.STATISTIC);
    if (null == statistic) {
        throw new RepositoryException("Not found statistic");
    }
    statistic.put(Statistic.STATISTIC_PUBLISHED_ARTICLE_COUNT, statistic.optInt(Statistic.STATISTIC_PUBLISHED_ARTICLE_COUNT) + 1);
    statisticRepository.update(Statistic.STATISTIC, statistic);
}
Also used : JSONObject(org.json.JSONObject) RepositoryException(org.b3log.latke.repository.RepositoryException)

Aggregations

RepositoryException (org.b3log.latke.repository.RepositoryException)35 JSONObject (org.json.JSONObject)32 ServiceException (org.b3log.latke.service.ServiceException)14 JSONArray (org.json.JSONArray)10 Query (org.b3log.latke.repository.Query)8 Transaction (org.b3log.latke.repository.Transaction)8 Date (java.util.Date)5 ParseException (java.text.ParseException)4 PropertyFilter (org.b3log.latke.repository.PropertyFilter)4 JSONException (org.json.JSONException)4 EventException (org.b3log.latke.event.EventException)2 ArchiveDate (org.b3log.solo.model.ArchiveDate)2 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 LatkeBeanManager (org.b3log.latke.ioc.LatkeBeanManager)1 GeneralUser (org.b3log.latke.user.GeneralUser)1 ArticleRepository (org.b3log.solo.repository.ArticleRepository)1 PageRepository (org.b3log.solo.repository.PageRepository)1