use of org.b3log.latke.repository.Query in project rhythm by csfwff.
the class VisitMgmtService method add.
/**
* Adds the specified visit.
*
* @param visit the specified visit
* @return {@code true} if visited before, returns {@code false} otherwise
*/
@Transactional
public boolean add(final JSONObject visit) {
try {
final String url = visit.optString(Visit.VISIT_URL);
final String ip = visit.optString(Visit.VISIT_IP);
final Query query = new Query().setFilter(CompositeFilterOperator.and(new PropertyFilter(Visit.VISIT_URL, FilterOperator.EQUAL, url), new PropertyFilter(Visit.VISIT_IP, FilterOperator.EQUAL, ip))).setPageCount(1);
final long count = visitRepository.count(query);
if (0 < count) {
return true;
}
String ua = visit.optString(Visit.VISIT_UA);
if (StringUtils.length(ua) > Common.MAX_LENGTH_UA) {
ua = StringUtils.substring(ua, 0, Common.MAX_LENGTH_UA);
}
visit.put(Visit.VISIT_UA, ua);
String referer = visit.optString(Visit.VISIT_REFERER_URL);
if (StringUtils.length(referer) > Common.MAX_LENGTH_URL) {
referer = StringUtils.substring(referer, 0, Common.MAX_LENGTH_URL);
}
visit.put(Visit.VISIT_REFERER_URL, referer);
visitRepository.add(visit);
return false;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Adds a visit failed", e);
return true;
}
}
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);
}
}
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);
}
}
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;
}
}
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);
}
}
Aggregations