Search in sources :

Example 71 with ServiceException

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

the class UserConsole method removeUser.

/**
     * Removes a user by the specified request.
     *
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": ""
     * }
     * </pre>
     * </p>
     *
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/user/*", method = HTTPRequestMethod.DELETE)
public void removeUser(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    if (!userQueryService.isAdminLoggedIn(request)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();
    renderer.setJSONObject(jsonObject);
    try {
        final String userId = request.getRequestURI().substring((Latkes.getContextPath() + "/console/user/").length());
        userMgmtService.removeUser(userId);
        jsonObject.put(Keys.STATUS_CODE, true);
        jsonObject.put(Keys.MSG, langPropsService.get("removeSuccLabel"));
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        jsonObject.put(Keys.STATUS_CODE, false);
        jsonObject.put(Keys.MSG, langPropsService.get("removeFailLabel"));
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 72 with ServiceException

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

the class UserConsole method updateUser.

/**
     * Updates a user by the specified request.
     *
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": ""
     * }
     * </pre>
     * </p>
     *
     * @param request the specified http servlet request, for example,      <pre>
     * {
     *     "oId": "",
     *     "userName": "",
     *     "userEmail": "",
     *     "userPassword": "", // Unhashed
     *     "userRole": "", // optional
     *     "userURL": "", // optional
     *     "userAvatar": "" // optional
     * }
     * </pre>
     *
     * @param context the specified http request context
     * @param response the specified http servlet response
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/user/", method = HTTPRequestMethod.PUT)
public void updateUser(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    if (!userQueryService.isAdminLoggedIn(request)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject ret = new JSONObject();
    try {
        final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
        userMgmtService.updateUser(requestJSONObject);
        ret.put(Keys.STATUS_CODE, true);
        ret.put(Keys.MSG, langPropsService.get("updateSuccLabel"));
        renderer.setJSONObject(ret);
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, e.getMessage());
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 73 with ServiceException

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

the class FillTagArticles method exec.

@Override
public Object exec(final List arguments) throws TemplateModelException {
    if (arguments.size() != ARG_SIZE) {
        LOGGER.debug("FillTagArticles with wrong arguments!");
        throw new TemplateModelException("Wrong arguments!");
    }
    final String tagTitle = (String) arguments.get(0);
    final int currentPageNum = Integer.parseInt((String) arguments.get(1));
    final int pageSize = Integer.parseInt((String) arguments.get(2));
    try {
        final JSONObject result = tagQueryService.getTagByTitle(tagTitle);
        if (null == result) {
            return new ArrayList<JSONObject>();
        }
        final JSONObject tag = result.getJSONObject(Tag.TAG);
        final String tagId = tag.getString(Keys.OBJECT_ID);
        final List<JSONObject> list = articleQueryService.getArticlesByTag(tagId, currentPageNum, pageSize);
        return list;
    } catch (final ServiceException e) {
        e.printStackTrace();
    } catch (final JSONException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException)

Example 74 with ServiceException

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

the class Filler method fillPageNavigations.

/**
     * Fills page navigations.
     *
     * @param dataModel data model
     * @throws ServiceException service exception
     */
private void fillPageNavigations(final Map<String, Object> dataModel) throws ServiceException {
    Stopwatchs.start("Fill Navigations");
    try {
        LOGGER.debug("Filling page navigations....");
        final List<JSONObject> pages = pageRepository.getPages();
        for (final JSONObject page : pages) {
            if ("page".equals(page.optString(Page.PAGE_TYPE))) {
                final String permalink = page.optString(Page.PAGE_PERMALINK);
                page.put(Page.PAGE_PERMALINK, Latkes.getServePath() + permalink);
            }
        }
        dataModel.put(Common.PAGE_NAVIGATIONS, pages);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Fills page navigations failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException)

Example 75 with ServiceException

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

the class Filler method fillMostViewCountArticles.

/**
     * Fills most view count articles.
     *
     * @param dataModel data model
     * @param preference the specified preference
     * @throws ServiceException service exception
     */
public void fillMostViewCountArticles(final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException {
    Stopwatchs.start("Fill Most View Articles");
    try {
        LOGGER.debug("Filling the most view count articles....");
        final int mostCommentArticleDisplayCnt = preference.getInt(Option.ID_C_MOST_VIEW_ARTICLE_DISPLAY_CNT);
        final List<JSONObject> mostViewCountArticles = articleRepository.getMostViewCountArticles(mostCommentArticleDisplayCnt);
        dataModel.put(Common.MOST_VIEW_COUNT_ARTICLES, mostViewCountArticles);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Fills most view count articles failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) EventException(org.b3log.latke.event.EventException) 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