Search in sources :

Example 6 with ServiceException

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

the class ArticleMgmtService method getPermalinkForAddArticle.

/**
     * Gets article permalink for adding article with the specified 
     * article.
     *
     * @param article the specified article
     * @return permalink
     * @throws ServiceException if invalid permalink occurs
     */
private String getPermalinkForAddArticle(final JSONObject article) throws ServiceException {
    final Date date = (Date) article.opt(Article.ARTICLE_CREATE_DATE);
    String ret = article.optString(Article.ARTICLE_PERMALINK);
    if (Strings.isEmptyOrNull(ret)) {
        ret = "/articles/" + DateFormatUtils.format(date, "yyyy/MM/dd") + "/" + article.optString(Keys.OBJECT_ID) + ".html";
    }
    if (!ret.startsWith("/")) {
        ret = "/" + ret;
    }
    if (PermalinkQueryService.invalidArticlePermalinkFormat(ret)) {
        throw new ServiceException(langPropsService.get("invalidPermalinkFormatLabel"));
    }
    if (permalinkQueryService.exist(ret)) {
        throw new ServiceException(langPropsService.get("duplicatedPermalinkLabel"));
    }
    return ret.replaceAll(" ", "-");
}
Also used : ServiceException(org.b3log.latke.service.ServiceException) Date(java.util.Date) ArchiveDate(org.b3log.solo.model.ArchiveDate)

Example 7 with ServiceException

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

the class PageQueryService method getPage.

/**
     * Gets a page by the specified page id.
     *
     * @param pageId the specified page id
     * @return for example,
     * <pre>
     * {
     *     "page": {
     *         "oId": "",
     *         "pageTitle": "",
     *         "pageContent": ""
     *         "pageOrder": int,
     *         "pagePermalink": "",
     *         "pageCommentCount": int,
     *         "pageCommentable": boolean,
     *         "pageType": "",
     *         "pageOpenTarget": ""
     *     }
     * }
     * </pre>, returns {@code null} if not found
     * @throws ServiceException service exception
     */
public JSONObject getPage(final String pageId) throws ServiceException {
    final JSONObject ret = new JSONObject();
    try {
        final JSONObject page = pageRepository.get(pageId);
        if (null == page) {
            return null;
        }
        ret.put(Page.PAGE, page);
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ServiceException(org.b3log.latke.service.ServiceException)

Example 8 with ServiceException

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

the class PreferenceMgmtService method updateReplyNotificationTemplate.

/**
     * Updates the reply notification template with the specified reply notification template.
     *
     * @param replyNotificationTemplate the specified reply notification template
     * @throws ServiceException service exception
     */
public void updateReplyNotificationTemplate(final JSONObject replyNotificationTemplate) throws ServiceException {
    final Transaction transaction = optionRepository.beginTransaction();
    try {
        final JSONObject bodyOpt = optionRepository.get(Option.ID_C_REPLY_NOTI_TPL_BODY);
        bodyOpt.put(Option.OPTION_VALUE, replyNotificationTemplate.optString("body"));
        optionRepository.update(Option.ID_C_REPLY_NOTI_TPL_BODY, bodyOpt);
        final JSONObject subjectOpt = optionRepository.get(Option.ID_C_REPLY_NOTI_TPL_SUBJECT);
        subjectOpt.put(Option.OPTION_VALUE, replyNotificationTemplate.optString("subject"));
        optionRepository.update(Option.ID_C_REPLY_NOTI_TPL_SUBJECT, subjectOpt);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates reply notification 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 9 with ServiceException

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

the class PreferenceQueryService method getReplyNotificationTemplate.

/**
     * Gets the reply notification template.
     *
     * @return reply notification template, returns {@code null} if not found
     * @throws ServiceException service exception
     */
public JSONObject getReplyNotificationTemplate() throws ServiceException {
    try {
        final JSONObject ret = new JSONObject();
        final JSONObject preference = getPreference();
        ret.put("subject", preference.optString(Option.ID_C_REPLY_NOTI_TPL_SUBJECT));
        ret.put("body", preference.optString(Option.ID_C_REPLY_NOTI_TPL_BODY));
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Updates reply notification template failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 10 with ServiceException

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

the class UserMgmtService method addUser.

/**
     * Adds a user with the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,      <pre>
     * {
     *     "userName": "",
     *     "userEmail": "",
     *     "userPassword": "", // Unhashed
     *     "userURL": "", // optional, uses 'servePath' instead if not specified
     *     "userRole": "", // optional, uses {@value Role#DEFAULT_ROLE} instead if not specified
     *     "userAvatar": "" // optional, users generated gravatar url instead if not specified
     * }
     * </pre>,see {@link User} for more details
     *
     * @return generated user id
     * @throws ServiceException service exception
     */
public String addUser(final JSONObject requestJSONObject) throws ServiceException {
    final Transaction transaction = userRepository.beginTransaction();
    try {
        final JSONObject user = new JSONObject();
        final String userEmail = requestJSONObject.optString(User.USER_EMAIL).trim().toLowerCase();
        final JSONObject duplicatedUser = userRepository.getByEmail(userEmail);
        if (null != duplicatedUser) {
            if (transaction.isActive()) {
                transaction.rollback();
            }
            throw new ServiceException(langPropsService.get("duplicatedEmailLabel"));
        }
        user.put(User.USER_EMAIL, userEmail);
        final String userName = requestJSONObject.optString(User.USER_NAME);
        user.put(User.USER_NAME, userName);
        final String userPassword = requestJSONObject.optString(User.USER_PASSWORD);
        user.put(User.USER_PASSWORD, MD5.hash(userPassword));
        String userURL = requestJSONObject.optString(User.USER_URL);
        if (Strings.isEmptyOrNull(userURL)) {
            userURL = Latkes.getServePath();
        }
        if (!Strings.isURL(userURL)) {
            throw new ServiceException(langPropsService.get("urlInvalidLabel"));
        }
        user.put(User.USER_URL, userURL);
        final String roleName = requestJSONObject.optString(User.USER_ROLE, Role.DEFAULT_ROLE);
        user.put(User.USER_ROLE, roleName);
        user.put(UserExt.USER_ARTICLE_COUNT, 0);
        user.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, 0);
        String userAvatar = requestJSONObject.optString(UserExt.USER_AVATAR);
        if (Strings.isEmptyOrNull(userAvatar)) {
            userAvatar = Thumbnails.getGravatarURL(userEmail, "128");
        }
        user.put(UserExt.USER_AVATAR, userAvatar);
        userRepository.add(user);
        transaction.commit();
        return user.optString(Keys.OBJECT_ID);
    } catch (final RepositoryException e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Adds a user failed", e);
        throw new ServiceException(e);
    }
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) 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