Search in sources :

Example 26 with Query

use of org.b3log.latke.repository.Query in project solo by b3log.

the class PageQueryService method getPages.

/**
     * Gets pages 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]
     *     },
     *     "pages": [{
     *         "oId": "",
     *         "pageTitle": "",
     *         "pageCommentCount": int,
     *         "pageOrder": int,
     *         "pagePermalink": "",
     *         "pageCommentable": boolean,
     *         "pageType": "",
     *         "pageOpenTarget": ""
     *      }, ....]
     * }
     * </pre>
     * @throws ServiceException service exception
     * @see Pagination
     */
public JSONObject getPages(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(Page.PAGE_ORDER, SortDirection.ASCENDING).setPageCount(1);
        final JSONObject result = pageRepository.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 pages = result.getJSONArray(Keys.RESULTS);
        for (int i = 0; i < pages.length(); i++) {
            // remove unused properties
            final JSONObject page = pages.getJSONObject(i);
            page.remove(Page.PAGE_CONTENT);
        }
        ret.put(Pagination.PAGINATION, pagination);
        ret.put(Page.PAGES, pages);
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets pages 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 27 with Query

use of org.b3log.latke.repository.Query in project solo by b3log.

the class PluginMgmtService method refresh.

/**
     * Updates datastore plugin descriptions with the specified plugins.
     * 
     * @param plugins the specified plugins
     * @throws Exception exception 
     */
public void refresh(final List<AbstractPlugin> plugins) throws Exception {
    if (!initService.isInited()) {
        return;
    }
    final JSONObject result = pluginRepository.get(new Query());
    final JSONArray pluginArray = result.getJSONArray(Keys.RESULTS);
    final List<JSONObject> persistedPlugins = CollectionUtils.jsonArrayToList(pluginArray);
    try {
        // Reads plugin status from datastore and clear plugin datastore
        for (final JSONObject oldPluginDesc : persistedPlugins) {
            final String descId = oldPluginDesc.getString(Keys.OBJECT_ID);
            final AbstractPlugin plugin = get(plugins, descId);
            pluginRepository.remove(descId);
            if (null != plugin) {
                final String status = oldPluginDesc.getString(Plugin.PLUGIN_STATUS);
                final String setting = oldPluginDesc.optString(Plugin.PLUGIN_SETTING);
                plugin.setStatus(PluginStatus.valueOf(status));
                try {
                    if (StringUtils.isNotBlank(setting)) {
                        plugin.setSetting(new JSONObject(setting));
                    }
                } catch (final JSONException e) {
                    LOGGER.log(Level.WARN, "the formatter of the old config failed to convert to json", e);
                }
            }
        }
        // Adds these plugins into datastore
        for (final AbstractPlugin plugin : plugins) {
            final JSONObject pluginDesc = plugin.toJSONObject();
            pluginRepository.add(pluginDesc);
            LOGGER.log(Level.TRACE, "Refreshed plugin[{0}]", pluginDesc);
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Refresh plugins failed", e);
    }
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) AbstractPlugin(org.b3log.latke.plugin.AbstractPlugin) JSONException(org.json.JSONException) JSONException(org.json.JSONException)

Example 28 with Query

use of org.b3log.latke.repository.Query in project solo by b3log.

the class PreferenceQueryService method getPreference.

/**
     * Gets the user preference.
     *
     * @return user preference, returns {@code null} if not found
     * @throws ServiceException if repository exception
     */
public JSONObject getPreference() throws ServiceException {
    try {
        final JSONObject checkInit = optionRepository.get(Option.ID_C_ADMIN_EMAIL);
        if (null == checkInit) {
            return null;
        }
        final Query query = new Query();
        query.setFilter(new PropertyFilter(Option.OPTION_CATEGORY, FilterOperator.EQUAL, Option.CATEGORY_C_PREFERENCE));
        final JSONArray opts = optionRepository.get(query).optJSONArray(Keys.RESULTS);
        final JSONObject ret = new JSONObject();
        for (int i = 0; i < opts.length(); i++) {
            final JSONObject opt = opts.optJSONObject(i);
            ret.put(opt.optString(Keys.OBJECT_ID), opt.opt(Option.OPTION_VALUE));
        }
        return ret;
    } catch (final RepositoryException e) {
        return null;
    }
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 29 with Query

use of org.b3log.latke.repository.Query in project solo by b3log.

the class OptionQueryService method getOptions.

/**
     * Gets options with the specified category.
     * 
     * <p>
     * All options with the specified category will be merged into one json object as the return value.
     * </p>
     * 
     * @param category the specified category
     * @return all options with the specified category, for example,
     * <pre>
     * {
     *     "${optionId}": "${optionValue}",
     *     ....
     * }
     * </pre>, returns {@code null} if not found
     * @throws ServiceException service exception
     */
public JSONObject getOptions(final String category) throws ServiceException {
    final Query query = new Query();
    query.setFilter(new PropertyFilter(Option.OPTION_CATEGORY, FilterOperator.EQUAL, category));
    try {
        final JSONObject result = optionRepository.get(query);
        final JSONArray options = result.getJSONArray(Keys.RESULTS);
        if (0 == options.length()) {
            return null;
        }
        final JSONObject ret = new JSONObject();
        for (int i = 0; i < options.length(); i++) {
            final JSONObject option = options.getJSONObject(i);
            ret.put(option.getString(Keys.OBJECT_ID), option.getString(Option.OPTION_VALUE));
        }
        return ret;
    } catch (final Exception e) {
        throw new ServiceException(e);
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 30 with Query

use of org.b3log.latke.repository.Query in project solo by b3log.

the class TagQueryService method getBottomTags.

/**
     * Gets bottom (reference count ascending) tags.
     *
     * @param fetchSize the specified fetch size
     * @return for example,      <pre>
     * [
     *     {"tagTitle": "", "tagReferenceCount": int, ....},
     *     ....
     * ]
     * </pre>, returns an empty list if not found
     *
     * @throws ServiceException service exception
     */
public List<JSONObject> getBottomTags(final int fetchSize) throws ServiceException {
    try {
        final Query query = new Query().setPageCount(1).setPageSize(fetchSize).addSort(Tag.TAG_PUBLISHED_REFERENCE_COUNT, SortDirection.ASCENDING);
        final JSONObject result = tagRepository.get(query);
        final JSONArray tagArray = result.optJSONArray(Keys.RESULTS);
        return CollectionUtils.jsonArrayToList(tagArray);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Gets bottom tags failed", e);
        throw new ServiceException(e);
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) RepositoryException(org.b3log.latke.repository.RepositoryException)

Aggregations

Query (org.b3log.latke.repository.Query)54 JSONObject (org.json.JSONObject)54 JSONArray (org.json.JSONArray)47 PropertyFilter (org.b3log.latke.repository.PropertyFilter)28 RepositoryException (org.b3log.latke.repository.RepositoryException)9 ServiceException (org.b3log.latke.service.ServiceException)9 Test (org.testng.annotations.Test)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 PrintWriter (java.io.PrintWriter)6 StringWriter (java.io.StringWriter)6 ServletContext (javax.servlet.ServletContext)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 Date (java.util.Date)5 URL (org.b3log.solo.model.sitemap.URL)4 JSONException (org.json.JSONException)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 CompositeFilter (org.b3log.latke.repository.CompositeFilter)2 Filter (org.b3log.latke.repository.Filter)2 Transaction (org.b3log.latke.repository.Transaction)2