Search in sources :

Example 1 with TextHTMLRenderer

use of org.b3log.latke.servlet.renderer.TextHTMLRenderer in project solo by b3log.

the class ArticleProcessor method getArticleContent.

/**
     * Gets article content with the specified context.
     *
     * @param context the specified context
     * @param request the specified request
     */
@RequestProcessing(value = "/get-article-content", method = HTTPRequestMethod.GET)
public void getArticleContent(final HTTPRequestContext context, final HttpServletRequest request) {
    final String articleId = request.getParameter("id");
    if (Strings.isEmptyOrNull(articleId)) {
        return;
    }
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    String content;
    try {
        content = articleQueryService.getArticleContent(request, articleId);
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, "Can not get article content", e);
        return;
    }
    if (null == content) {
        return;
    }
    renderer.setContent(content);
}
Also used : ServiceException(org.b3log.latke.service.ServiceException) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 2 with TextHTMLRenderer

use of org.b3log.latke.servlet.renderer.TextHTMLRenderer in project solo by b3log.

the class RepairProcessor method removeUnusedArticleProperties.

/**
     * Removes unused properties of each article.
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/fix/normalization/articles/properties", method = HTTPRequestMethod.POST)
public void removeUnusedArticleProperties(final HTTPRequestContext context) {
    LOGGER.log(Level.INFO, "Processes remove unused article properties");
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    Transaction transaction = null;
    try {
        final JSONArray articles = articleRepository.get(new Query()).getJSONArray(Keys.RESULTS);
        if (articles.length() <= 0) {
            renderer.setContent("No unused article properties");
            return;
        }
        transaction = articleRepository.beginTransaction();
        final Set<String> keyNames = Repositories.getKeyNames(Article.ARTICLE);
        for (int i = 0; i < articles.length(); i++) {
            final JSONObject article = articles.getJSONObject(i);
            final JSONArray names = article.names();
            final Set<String> nameSet = CollectionUtils.<String>jsonArrayToSet(names);
            if (nameSet.removeAll(keyNames)) {
                for (final String unusedName : nameSet) {
                    article.remove(unusedName);
                }
                articleRepository.update(article.getString(Keys.OBJECT_ID), article);
                LOGGER.log(Level.INFO, "Found an article[id={0}] exists unused properties[{1}]", new Object[] { article.getString(Keys.OBJECT_ID), nameSet });
            }
        }
        transaction.commit();
    } catch (final Exception e) {
        if (null != transaction && transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        renderer.setContent("Removes unused article properties failed, error msg[" + e.getMessage() + "]");
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 3 with TextHTMLRenderer

use of org.b3log.latke.servlet.renderer.TextHTMLRenderer in project solo by b3log.

the class RepairProcessor method removeAllDataPOST.

/**
     * Removes all data.
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/rm-all-data.do", method = HTTPRequestMethod.POST)
public void removeAllDataPOST(final HTTPRequestContext context) {
    LOGGER.info("Removing all data....");
    boolean succeed = false;
    try {
        remove(beanManager.getReference(ArchiveDateArticleRepositoryImpl.class));
        remove(beanManager.getReference(ArchiveDateRepositoryImpl.class));
        remove(beanManager.getReference(ArticleRepositoryImpl.class));
        remove(beanManager.getReference(CommentRepositoryImpl.class));
        remove(beanManager.getReference(LinkRepositoryImpl.class));
        remove(beanManager.getReference(OptionRepositoryImpl.class));
        remove(beanManager.getReference(PageRepositoryImpl.class));
        remove(beanManager.getReference(PluginRepositoryImpl.class));
        remove(beanManager.getReference(StatisticRepositoryImpl.class));
        remove(beanManager.getReference(TagArticleRepositoryImpl.class));
        remove(beanManager.getReference(TagRepositoryImpl.class));
        remove(beanManager.getReference(UserRepositoryImpl.class));
        succeed = true;
    } catch (final Exception e) {
        LOGGER.log(Level.WARN, "Removed partial data only", e);
    }
    final StringBuilder htmlBuilder = new StringBuilder();
    htmlBuilder.append("<html><head><title>Result</title></head><body>");
    try {
        final TextHTMLRenderer renderer = new TextHTMLRenderer();
        context.setRenderer(renderer);
        if (succeed) {
            htmlBuilder.append("Removed all data!");
        } else {
            htmlBuilder.append("Refresh this page and run this remover again.");
        }
        htmlBuilder.append("</body></html>");
        renderer.setContent(htmlBuilder.toString());
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        try {
            context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
    }
    LOGGER.info("Removed all data....");
}
Also used : TagRepositoryImpl(org.b3log.solo.repository.impl.TagRepositoryImpl) PageRepositoryImpl(org.b3log.solo.repository.impl.PageRepositoryImpl) UserRepositoryImpl(org.b3log.solo.repository.impl.UserRepositoryImpl) IOException(java.io.IOException) ArchiveDateArticleRepositoryImpl(org.b3log.solo.repository.impl.ArchiveDateArticleRepositoryImpl) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) PluginRepositoryImpl(org.b3log.solo.repository.impl.PluginRepositoryImpl) CommentRepositoryImpl(org.b3log.solo.repository.impl.CommentRepositoryImpl) TagArticleRepositoryImpl(org.b3log.solo.repository.impl.TagArticleRepositoryImpl) ArchiveDateRepositoryImpl(org.b3log.solo.repository.impl.ArchiveDateRepositoryImpl) OptionRepositoryImpl(org.b3log.solo.repository.impl.OptionRepositoryImpl) ArticleRepositoryImpl(org.b3log.solo.repository.impl.ArticleRepositoryImpl) TagArticleRepositoryImpl(org.b3log.solo.repository.impl.TagArticleRepositoryImpl) ArchiveDateArticleRepositoryImpl(org.b3log.solo.repository.impl.ArchiveDateArticleRepositoryImpl) StatisticRepositoryImpl(org.b3log.solo.repository.impl.StatisticRepositoryImpl) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) LinkRepositoryImpl(org.b3log.solo.repository.impl.LinkRepositoryImpl) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 4 with TextHTMLRenderer

use of org.b3log.latke.servlet.renderer.TextHTMLRenderer in project solo by b3log.

the class RepairProcessor method repairTagArticleCounter.

/**
     * Repairs tag article counter.
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/fix/tag-article-counter-repair.do", method = HTTPRequestMethod.GET)
@Transactional
public void repairTagArticleCounter(final HTTPRequestContext context) {
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    try {
        final JSONObject result = tagRepository.get(new Query());
        final JSONArray tagArray = result.getJSONArray(Keys.RESULTS);
        final List<JSONObject> tags = CollectionUtils.jsonArrayToList(tagArray);
        for (final JSONObject tag : tags) {
            final String tagId = tag.getString(Keys.OBJECT_ID);
            final JSONObject tagArticleResult = tagArticleRepository.getByTagId(tagId, 1, Integer.MAX_VALUE);
            final JSONArray tagArticles = tagArticleResult.getJSONArray(Keys.RESULTS);
            final int tagRefCnt = tagArticles.length();
            int publishedTagRefCnt = 0;
            for (int i = 0; i < tagRefCnt; i++) {
                final JSONObject tagArticle = tagArticles.getJSONObject(i);
                final String articleId = tagArticle.getString(Article.ARTICLE + "_" + Keys.OBJECT_ID);
                final JSONObject article = articleRepository.get(articleId);
                if (null == article) {
                    tagArticleRepository.remove(tagArticle.optString(Keys.OBJECT_ID));
                    continue;
                }
                if (article.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
                    publishedTagRefCnt++;
                }
            }
            tag.put(Tag.TAG_REFERENCE_COUNT, tagRefCnt);
            tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedTagRefCnt);
            tagRepository.update(tagId, tag);
            LOGGER.log(Level.INFO, "Repaired tag[title={0}, refCnt={1}, publishedTagRefCnt={2}]", new Object[] { tag.getString(Tag.TAG_TITLE), tagRefCnt, publishedTagRefCnt });
        }
        renderer.setContent("Repair sucessfully!");
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        renderer.setContent("Repairs failed, error msg[" + e.getMessage() + "]");
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing) Transactional(org.b3log.latke.repository.annotation.Transactional)

Example 5 with TextHTMLRenderer

use of org.b3log.latke.servlet.renderer.TextHTMLRenderer in project solo by b3log.

the class RepairProcessor method restoreSigns.

/**
     * Restores the signs of preference to default.
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/fix/restore-signs.do", method = HTTPRequestMethod.GET)
public void restoreSigns(final HTTPRequestContext context) {
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    try {
        final JSONObject preference = preferenceQueryService.getPreference();
        final String originalSigns = preference.getString(Option.ID_C_SIGNS);
        preference.put(Option.ID_C_SIGNS, Option.DefaultPreference.DEFAULT_SIGNS);
        preferenceMgmtService.updatePreference(preference);
        // Sends the sample signs to developer
        final Message msg = new MailService.Message();
        msg.setFrom(preference.getString(Option.ID_C_ADMIN_EMAIL));
        msg.addRecipient("DL88250@gmail.com");
        msg.setSubject("Restore signs");
        msg.setHtmlBody(originalSigns + "<p>Admin email: " + preference.getString(Option.ID_C_ADMIN_EMAIL) + "</p>");
        MAIL_SVC.send(msg);
        renderer.setContent("Restores signs succeeded.");
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        renderer.setContent("Restores signs failed, error msg[" + e.getMessage() + "]");
    }
}
Also used : JSONObject(org.json.JSONObject) Message(org.b3log.latke.mail.MailService.Message) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)7 TextHTMLRenderer (org.b3log.latke.servlet.renderer.TextHTMLRenderer)7 IOException (java.io.IOException)6 ExecutionException (java.util.concurrent.ExecutionException)6 JSONObject (org.json.JSONObject)4 JSONArray (org.json.JSONArray)2 Message (org.b3log.latke.mail.MailService.Message)1 Transactional (org.b3log.latke.repository.annotation.Transactional)1 ServiceException (org.b3log.latke.service.ServiceException)1 ArchiveDateArticleRepositoryImpl (org.b3log.solo.repository.impl.ArchiveDateArticleRepositoryImpl)1 ArchiveDateRepositoryImpl (org.b3log.solo.repository.impl.ArchiveDateRepositoryImpl)1 ArticleRepositoryImpl (org.b3log.solo.repository.impl.ArticleRepositoryImpl)1 CommentRepositoryImpl (org.b3log.solo.repository.impl.CommentRepositoryImpl)1 LinkRepositoryImpl (org.b3log.solo.repository.impl.LinkRepositoryImpl)1 OptionRepositoryImpl (org.b3log.solo.repository.impl.OptionRepositoryImpl)1 PageRepositoryImpl (org.b3log.solo.repository.impl.PageRepositoryImpl)1 PluginRepositoryImpl (org.b3log.solo.repository.impl.PluginRepositoryImpl)1 StatisticRepositoryImpl (org.b3log.solo.repository.impl.StatisticRepositoryImpl)1 TagArticleRepositoryImpl (org.b3log.solo.repository.impl.TagArticleRepositoryImpl)1 TagRepositoryImpl (org.b3log.solo.repository.impl.TagRepositoryImpl)1