Search in sources :

Example 31 with JSONArray

use of org.json.JSONArray in project solo by b3log.

the class UpgradeService method upgradeArticles.

/**
     * Upgrades articles.
     *
     * @throws Exception exception
     */
private void upgradeArticles() throws Exception {
    LOGGER.log(Level.INFO, "Adds a property [articleEditorType] to each of articles");
    final JSONArray articles = articleRepository.get(new Query()).getJSONArray(Keys.RESULTS);
    if (articles.length() <= 0) {
        LOGGER.log(Level.TRACE, "No articles");
        return;
    }
    Transaction transaction = null;
    try {
        for (int i = 0; i < articles.length(); i++) {
            if (0 == i % STEP || !transaction.isActive()) {
                transaction = userRepository.beginTransaction();
            }
            final JSONObject article = articles.getJSONObject(i);
            final String articleId = article.optString(Keys.OBJECT_ID);
            LOGGER.log(Level.INFO, "Found an article[id={0}]", articleId);
            article.put(Article.ARTICLE_EDITOR_TYPE, "tinyMCE");
            articleRepository.update(article.getString(Keys.OBJECT_ID), article);
            if (0 == i % STEP) {
                transaction.commit();
                LOGGER.log(Level.TRACE, "Updated some articles");
            }
        }
        if (transaction.isActive()) {
            transaction.commit();
        }
        LOGGER.log(Level.TRACE, "Updated all articles");
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        throw e;
    }
}
Also used : Query(org.b3log.latke.repository.Query) Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 32 with JSONArray

use of org.json.JSONArray in project solo by b3log.

the class InitService method addHelloWorldArticle.

/**
     * Adds the specified "Hello World" article.
     *
     * @param article the specified "Hello World" article
     * @return generated article id
     * @throws RepositoryException repository exception
     */
private String addHelloWorldArticle(final JSONObject article) throws RepositoryException {
    final String ret = Ids.genTimeMillisId();
    try {
        article.put(Keys.OBJECT_ID, ret);
        // Step 1: Add tags
        final String tagsString = article.optString(Article.ARTICLE_TAGS_REF);
        final String[] tagTitles = tagsString.split(",");
        final JSONArray tags = tag(tagTitles, article);
        // Step 2: Add tag-article relations
        addTagArticleRelation(tags, article);
        // Step 3: Inc blog article and comment count statictis
        final JSONObject statistic = statisticRepository.get(Statistic.STATISTIC);
        statistic.put(Statistic.STATISTIC_BLOG_ARTICLE_COUNT, 1);
        statistic.put(Statistic.STATISTIC_PUBLISHED_ARTICLE_COUNT, 1);
        statistic.put(Statistic.STATISTIC_PUBLISHED_BLOG_COMMENT_COUNT, 1);
        statistic.put(Statistic.STATISTIC_BLOG_COMMENT_COUNT, 1);
        statisticRepository.update(Statistic.STATISTIC, statistic);
        // Step 4: Add archive date-article relations
        archiveDate(article);
        // Step 5: Add article
        articleRepository.add(article);
        // Step 6: Update admin user for article statistic
        final JSONObject admin = userRepository.getAdmin();
        admin.put(UserExt.USER_ARTICLE_COUNT, 1);
        admin.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, 1);
        userRepository.update(admin.optString(Keys.OBJECT_ID), admin);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Adds an article failed", e);
        throw new RepositoryException(e);
    }
    return ret;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 33 with JSONArray

use of org.json.JSONArray in project solo by b3log.

the class LinkQueryService method getLinks.

/**
     * Gets links by the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,
     * <pre>
     * {
     *     "paginationCurrentPageNum": 1,
     *     "paginationPageSize": 20,
     *     "paginationWindowSize": 10
     * }, see {@link Pagination} for more details
     * </pre>
     * @return for example,
     * <pre>
     * {
     *     "pagination": {
     *         "paginationPageCount": 100,
     *         "paginationPageNums": [1, 2, 3, 4, 5]
     *     },
     *     "links": [{
     *         "oId": "",
     *         "linkTitle": "",
     *         "linkAddress": "",
     *         ""linkDescription": ""
     *      }, ....]
     * }
     * </pre>
     * @throws ServiceException service exception
     * @see Pagination
     */
public JSONObject getLinks(final JSONObject requestJSONObject) throws ServiceException {
    final JSONObject ret = new JSONObject();
    try {
        final int currentPageNum = requestJSONObject.getInt(Pagination.PAGINATION_CURRENT_PAGE_NUM);
        final int pageSize = requestJSONObject.getInt(Pagination.PAGINATION_PAGE_SIZE);
        final int windowSize = requestJSONObject.getInt(Pagination.PAGINATION_WINDOW_SIZE);
        final Query query = new Query().setCurrentPageNum(currentPageNum).setPageSize(pageSize).addSort(Link.LINK_ORDER, SortDirection.ASCENDING);
        final JSONObject result = linkRepository.get(query);
        final int pageCount = result.getJSONObject(Pagination.PAGINATION).getInt(Pagination.PAGINATION_PAGE_COUNT);
        final JSONObject pagination = new JSONObject();
        final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
        pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
        pagination.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);
        final JSONArray links = result.getJSONArray(Keys.RESULTS);
        ret.put(Pagination.PAGINATION, pagination);
        ret.put(Link.LINKS, links);
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets links failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException)

Example 34 with JSONArray

use of org.json.JSONArray in project solo by b3log.

the class ArticleQueryService method getArticle.

/**
     * Gets an article by the specified article id.
     *
     * <p>
     * <b>Note</b>: The article content and abstract is raw (no editor type processing).
     * </p>
     *
     * @param articleId the specified article id
     * @return for example,      <pre>
     * {
     *     "article": {
     *         "oId": "",
     *         "articleTitle": "",
     *         "articleAbstract": "",
     *         "articleContent": "",
     *         "articlePermalink": "",
     *         "articleHadBeenPublished": boolean,
     *         "articleCreateDate": java.util.Date,
     *         "articleTags": [{
     *             "oId": "",
     *             "tagTitle": ""
     *         }, ....],
     *         "articleSignId": "",
     *         "articleViewPwd": "",
     *         "articleEditorType": "",
     *         "signs": [{
     *             "oId": "",
     *             "signHTML": ""
     *         }, ....]
     *     }
     * }
     * </pre>, returns {@code null} if not found
     *
     * @throws ServiceException service exception
     */
public JSONObject getArticle(final String articleId) throws ServiceException {
    try {
        final JSONObject ret = new JSONObject();
        final JSONObject article = articleRepository.get(articleId);
        if (null == article) {
            return null;
        }
        ret.put(ARTICLE, article);
        // Tags
        final JSONArray tags = new JSONArray();
        final List<JSONObject> tagArticleRelations = tagArticleRepository.getByArticleId(articleId);
        for (int i = 0; i < tagArticleRelations.size(); i++) {
            final JSONObject tagArticleRelation = tagArticleRelations.get(i);
            final String tagId = tagArticleRelation.getString(Tag.TAG + "_" + Keys.OBJECT_ID);
            final JSONObject tag = tagRepository.get(tagId);
            tags.put(tag);
        }
        article.put(ARTICLE_TAGS_REF, tags);
        // Signs
        final JSONObject preference = preferenceQueryService.getPreference();
        article.put(Sign.SIGNS, new JSONArray(preference.getString(Option.ID_C_SIGNS)));
        // Remove unused properties
        article.remove(ARTICLE_AUTHOR_EMAIL);
        article.remove(ARTICLE_COMMENT_COUNT);
        article.remove(ARTICLE_IS_PUBLISHED);
        article.remove(ARTICLE_PUT_TOP);
        article.remove(ARTICLE_UPDATE_DATE);
        article.remove(ARTICLE_VIEW_COUNT);
        article.remove(ARTICLE_RANDOM_DOUBLE);
        LOGGER.log(Level.DEBUG, "Got an article[id={0}]", articleId);
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets an article failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException)

Example 35 with JSONArray

use of org.json.JSONArray in project solo by b3log.

the class ArticleQueryService method getRelevantArticles.

/**
     * Gets the relevant published articles of the specified article.
     *
     * <p>
     * <b>Note</b>: The article content and abstract is raw (no editor type processing).
     * </p>
     *
     * @param article the specified article
     * @param preference the specified preference
     * @return a list of articles, returns an empty list if not found
     * @throws ServiceException service exception
     */
public List<JSONObject> getRelevantArticles(final JSONObject article, final JSONObject preference) throws ServiceException {
    try {
        final int displayCnt = preference.getInt(Option.ID_C_RELEVANT_ARTICLES_DISPLAY_CNT);
        final String[] tagTitles = article.getString(Article.ARTICLE_TAGS_REF).split(",");
        final int maxTagCnt = displayCnt > tagTitles.length ? tagTitles.length : displayCnt;
        final String articleId = article.getString(Keys.OBJECT_ID);
        final List<JSONObject> articles = new ArrayList<JSONObject>();
        for (int i = 0; i < maxTagCnt; i++) {
            // XXX: should average by tag?
            final String tagTitle = tagTitles[i];
            final JSONObject tag = tagRepository.getByTitle(tagTitle);
            final String tagId = tag.getString(Keys.OBJECT_ID);
            final JSONObject result = tagArticleRepository.getByTagId(tagId, 1, displayCnt);
            final JSONArray tagArticleRelations = result.getJSONArray(Keys.RESULTS);
            final int relationSize = displayCnt < tagArticleRelations.length() ? displayCnt : tagArticleRelations.length();
            for (int j = 0; j < relationSize; j++) {
                final JSONObject tagArticleRelation = tagArticleRelations.getJSONObject(j);
                final String relatedArticleId = tagArticleRelation.getString(Article.ARTICLE + "_" + Keys.OBJECT_ID);
                if (articleId.equals(relatedArticleId)) {
                    continue;
                }
                final JSONObject relevant = articleRepository.get(relatedArticleId);
                if (!relevant.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
                    continue;
                }
                boolean existed = false;
                for (final JSONObject relevantArticle : articles) {
                    if (relevantArticle.getString(Keys.OBJECT_ID).equals(relevant.getString(Keys.OBJECT_ID))) {
                        existed = true;
                    }
                }
                if (!existed) {
                    articles.add(relevant);
                }
            }
        }
        Collections.sort(articles, Comparators.ARTICLE_UPDATE_DATE_COMPARATOR);
        removeUnusedProperties(articles);
        if (displayCnt > articles.size()) {
            return articles;
        }
        final List<Integer> randomIntegers = CollectionUtils.getRandomIntegers(0, articles.size() - 1, displayCnt);
        final List<JSONObject> ret = new ArrayList<JSONObject>();
        for (final int index : randomIntegers) {
            ret.add(articles.get(index));
        }
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets relevant articles failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException)

Aggregations

JSONArray (org.json.JSONArray)1710 JSONObject (org.json.JSONObject)1191 JSONException (org.json.JSONException)738 ArrayList (java.util.ArrayList)323 IOException (java.io.IOException)243 Test (org.junit.Test)207 HashMap (java.util.HashMap)108 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)96 List (java.util.List)63 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)61 File (java.io.File)54 HashSet (java.util.HashSet)54 Date (java.util.Date)47 Query (org.b3log.latke.repository.Query)47 RandomString (edu.umass.cs.gnscommon.utils.RandomString)44 GraphObject (com.abewy.android.apps.klyph.core.graph.GraphObject)43 FileNotFoundException (java.io.FileNotFoundException)40 Map (java.util.Map)40 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)36 VolleyError (com.android.volley.VolleyError)35