Search in sources :

Example 21 with AbstractFreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.

the class AdminProcessor method addArticle.

/**
 * Adds an article.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/add-article", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void addArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String userName = request.getParameter(User.USER_NAME);
    final JSONObject author = userQueryService.getUserByName(userName);
    if (null == author) {
        final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
        context.setRenderer(renderer);
        renderer.setTemplateName("admin/error.ftl");
        final Map<String, Object> dataModel = renderer.getDataModel();
        dataModel.put(Keys.MSG, langPropsService.get("notFoundUserLabel"));
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        return;
    }
    final String timeStr = request.getParameter(Common.TIME);
    final String articleTitle = request.getParameter(Article.ARTICLE_TITLE);
    final String articleTags = request.getParameter(Article.ARTICLE_TAGS);
    final String articleContent = request.getParameter(Article.ARTICLE_CONTENT);
    String rewardContent = request.getParameter(Article.ARTICLE_REWARD_CONTENT);
    final String rewardPoint = request.getParameter(Article.ARTICLE_REWARD_POINT);
    long time = System.currentTimeMillis();
    try {
        final Date date = DateUtils.parseDate(timeStr, new String[] { "yyyy-MM-dd'T'HH:mm" });
        time = date.getTime();
        final int random = RandomUtils.nextInt(9999);
        time += random;
    } catch (final ParseException e) {
        LOGGER.log(Level.ERROR, "Parse time failed, using current time instead");
    }
    final JSONObject article = new JSONObject();
    article.put(Article.ARTICLE_TITLE, articleTitle);
    article.put(Article.ARTICLE_TAGS, articleTags);
    article.put(Article.ARTICLE_CONTENT, articleContent);
    article.put(Article.ARTICLE_REWARD_CONTENT, rewardContent);
    article.put(Article.ARTICLE_REWARD_POINT, Integer.valueOf(rewardPoint));
    article.put(User.USER_NAME, userName);
    article.put(Common.TIME, time);
    try {
        articleMgmtService.addArticleByAdmin(article);
    } catch (final ServiceException e) {
        final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
        context.setRenderer(renderer);
        renderer.setTemplateName("admin/error.ftl");
        final Map<String, Object> dataModel = renderer.getDataModel();
        dataModel.put(Keys.MSG, e.getMessage());
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        return;
    }
    response.sendRedirect(Latkes.getServePath() + "/admin/articles");
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONObject(org.json.JSONObject) ParseException(java.text.ParseException) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 22 with AbstractFreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.

the class AdminProcessor method addDomain.

/**
 * Adds a domain.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/add-domain", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void addDomain(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String domainTitle = request.getParameter(Domain.DOMAIN_TITLE);
    if (StringUtils.isBlank(domainTitle)) {
        final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
        context.setRenderer(renderer);
        renderer.setTemplateName("admin/error.ftl");
        final Map<String, Object> dataModel = renderer.getDataModel();
        dataModel.put(Keys.MSG, langPropsService.get("invalidDomainTitleLabel"));
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        return;
    }
    if (null != domainQueryService.getByTitle(domainTitle)) {
        final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
        context.setRenderer(renderer);
        renderer.setTemplateName("admin/error.ftl");
        final Map<String, Object> dataModel = renderer.getDataModel();
        dataModel.put(Keys.MSG, langPropsService.get("duplicatedDomainLabel"));
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        return;
    }
    String domainId;
    try {
        final JSONObject domain = new JSONObject();
        domain.put(Domain.DOMAIN_TITLE, domainTitle);
        domain.put(Domain.DOMAIN_URI, domainTitle);
        domain.put(Domain.DOMAIN_DESCRIPTION, domainTitle);
        domain.put(Domain.DOMAIN_STATUS, Domain.DOMAIN_STATUS_C_VALID);
        domainId = domainMgmtService.addDomain(domain);
    } catch (final ServiceException e) {
        final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
        context.setRenderer(renderer);
        renderer.setTemplateName("admin/error.ftl");
        final Map<String, Object> dataModel = renderer.getDataModel();
        dataModel.put(Keys.MSG, e.getMessage());
        dataModelService.fillHeaderAndFooter(request, response, dataModel);
        return;
    }
    response.sendRedirect(Latkes.getServePath() + "/admin/domain/" + domainId);
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 23 with AbstractFreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.

the class AdminProcessor method showAddArticle.

/**
 * Shows add article.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/add-article", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showAddArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("admin/add-article.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    dataModelService.fillHeaderAndFooter(request, response, dataModel);
}
Also used : JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 24 with AbstractFreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.

the class AdminProcessor method updateReservedWord.

/**
 * Updates a reserved word.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @param id       the specified reserved wordid
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/reserved-word/{id}", method = HTTPRequestMethod.POST)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void updateReservedWord(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String id) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("admin/reserved-word.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    final JSONObject word = optionQueryService.getOption(id);
    dataModel.put(Common.WORD, word);
    final Enumeration<String> parameterNames = request.getParameterNames();
    while (parameterNames.hasMoreElements()) {
        final String name = parameterNames.nextElement();
        final String value = request.getParameter(name);
        word.put(name, value);
    }
    optionMgmtService.updateOption(id, word);
    dataModelService.fillHeaderAndFooter(request, response, dataModel);
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 25 with AbstractFreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer in project symphony by b3log.

the class AdminProcessor method showTag.

/**
 * Shows a tag.
 *
 * @param context  the specified context
 * @param request  the specified request
 * @param response the specified response
 * @param tagId    the specified tag id
 * @throws Exception exception
 */
@RequestProcessing(value = "/admin/tag/{tagId}", method = HTTPRequestMethod.GET)
@Before(adviceClass = { StopwatchStartAdvice.class, PermissionCheck.class })
@After(adviceClass = { PermissionGrant.class, StopwatchEndAdvice.class })
public void showTag(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String tagId) throws Exception {
    final AbstractFreeMarkerRenderer renderer = new SkinRenderer(request);
    context.setRenderer(renderer);
    renderer.setTemplateName("admin/tag.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    final JSONObject tag = tagQueryService.getTag(tagId);
    dataModel.put(Tag.TAG, tag);
    dataModelService.fillHeaderAndFooter(request, response, dataModel);
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) Before(org.b3log.latke.servlet.annotation.Before) After(org.b3log.latke.servlet.annotation.After) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)120 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)119 JSONObject (org.json.JSONObject)117 After (org.b3log.latke.servlet.annotation.After)105 Before (org.b3log.latke.servlet.annotation.Before)105 ServiceException (org.b3log.latke.service.ServiceException)22 IOException (java.io.IOException)15 Date (java.util.Date)12 ArrayList (java.util.ArrayList)8 JSONArray (org.json.JSONArray)8 Auth (com.qiniu.util.Auth)7 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)7 ConsoleRenderer (org.b3log.solo.processor.renderer.ConsoleRenderer)7 ParseException (java.text.ParseException)6 List (java.util.List)6 EventException (org.b3log.latke.event.EventException)3 JSONException (org.json.JSONException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Locale (java.util.Locale)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2