Commit f81ea108 authored by Liang Ding's avatar Liang Ding

#12261 接口实现

parent 8d92c639
...@@ -20,11 +20,16 @@ package org.b3log.solo.model; ...@@ -20,11 +20,16 @@ package org.b3log.solo.model;
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="mailto:dongxu.wang@acm.org">Dongxu Wang</a> * @author <a href="mailto:dongxu.wang@acm.org">Dongxu Wang</a>
* @version 1.5.5.2, Jul 16, 2017 * @version 1.6.0.0, Sep 12, 2017
* @since 0.3.1 * @since 0.3.1
*/ */
public final class Common { public final class Common {
/**
* Key of keyword.
*/
public static final String KEYWORD = "keyword";
/** /**
* Key of data. * Key of data.
*/ */
......
/*
* Copyright (c) 2010-2017, b3log.org & hacpai.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.b3log.solo.processor;
import org.b3log.latke.Latkes;
import org.b3log.latke.ioc.inject.Inject;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.Pagination;
import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.servlet.HTTPRequestContext;
import org.b3log.latke.servlet.HTTPRequestMethod;
import org.b3log.latke.servlet.annotation.RequestProcessing;
import org.b3log.latke.servlet.annotation.RequestProcessor;
import org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer;
import org.b3log.latke.util.Strings;
import org.b3log.solo.model.Article;
import org.b3log.solo.model.Common;
import org.b3log.solo.processor.renderer.ConsoleRenderer;
import org.b3log.solo.processor.util.Filler;
import org.b3log.solo.service.ArticleQueryService;
import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.service.UserQueryService;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Search processor.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Sep 12, 2017
* @since 2.4.0
*/
@RequestProcessor
public class SearchProcessor {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(SearchProcessor.class);
/**
* Article query service.
*/
@Inject
private ArticleQueryService articleQueryService;
/**
* User query service.
*/
@Inject
private UserQueryService userQueryService;
/**
* Preference query service.
*/
@Inject
private PreferenceQueryService preferenceQueryService;
/**
* Language service.
*/
@Inject
private LangPropsService langPropsService;
/**
* Filler.
*/
@Inject
private Filler filler;
/**
* Searches articles.
*
* @param context the specified context
*/
@RequestProcessing(value = "/search", method = HTTPRequestMethod.GET)
public void search(final HTTPRequestContext context) {
final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
context.setRenderer(renderer);
renderer.setTemplateName("search.ftl");
final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.putAll(langs);
final HttpServletRequest request = context.getRequest();
String page = request.getParameter("p");
if (!Strings.isNumeric(page)) {
page = "1";
}
String keyword = request.getParameter(Common.KEYWORD);
keyword = Jsoup.clean(keyword, Whitelist.none());
dataModel.put(Common.KEYWORD, keyword);
final JSONObject result = articleQueryService.searchKeyword(keyword, Integer.valueOf(page), 15);
final List<JSONObject> articles = (List<JSONObject>) result.opt(Article.ARTICLES);
try {
final JSONObject preference = preferenceQueryService.getPreference();
final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
if (hasMultipleUsers) {
filler.setArticlesExProperties(request, articles, preference);
} else if (!articles.isEmpty()) {
final JSONObject author = articleQueryService.getAuthor(articles.get(0));
filler.setArticlesExProperties(request, articles, author, preference);
}
dataModel.put(Article.ARTICLES, articles);
dataModel.put(Pagination.PAGINATION, result.opt(Pagination.PAGINATION));
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Search articles failed");
dataModel.put(Article.ARTICLES, Collections.emptyList());
}
}
}
...@@ -122,7 +122,7 @@ public class ArticleQueryService { ...@@ -122,7 +122,7 @@ public class ArticleQueryService {
*/ */
@Inject @Inject
private LangPropsService langPropsService; private LangPropsService langPropsService;
/** /**
* Searches articles with the specified keyword. * Searches articles with the specified keyword.
* *
...@@ -130,10 +130,8 @@ public class ArticleQueryService { ...@@ -130,10 +130,8 @@ public class ArticleQueryService {
* @param currentPageNum the specified current page number * @param currentPageNum the specified current page number
* @param pageSize the specified page size * @param pageSize the specified page size
* @return result * @return result
* @throws ServiceException service exception
*/ */
public JSONObject searchKeyword(final String keyword, final int currentPageNum, final int pageSize) public JSONObject searchKeyword(final String keyword, final int currentPageNum, final int pageSize) {
throws ServiceException {
final JSONObject ret = new JSONObject(); final JSONObject ret = new JSONObject();
ret.put(Article.ARTICLES, (Object) Collections.emptyList()); ret.put(Article.ARTICLES, (Object) Collections.emptyList());
...@@ -159,13 +157,11 @@ public class ArticleQueryService { ...@@ -159,13 +157,11 @@ public class ArticleQueryService {
final List<JSONObject> articles = CollectionUtils.jsonArrayToList(result.optJSONArray(Keys.RESULTS)); final List<JSONObject> articles = CollectionUtils.jsonArrayToList(result.optJSONArray(Keys.RESULTS));
ret.put(Article.ARTICLES, (Object) articles); ret.put(Article.ARTICLES, (Object) articles);
return ret;
} catch (final RepositoryException | ServiceException e) { } catch (final RepositoryException | ServiceException e) {
LOGGER.log(Level.ERROR, "Searches articles error", e); LOGGER.log(Level.ERROR, "Searches articles error", e);
throw new ServiceException(e);
} }
return ret;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment