Commit 36797681 authored by Liang Ding's avatar Liang Ding

🚧 #12256 分类文章查询接口

parent 827852e1
...@@ -28,17 +28,15 @@ import org.b3log.latke.servlet.annotation.RequestProcessing; ...@@ -28,17 +28,15 @@ import org.b3log.latke.servlet.annotation.RequestProcessing;
import org.b3log.latke.servlet.annotation.RequestProcessor; import org.b3log.latke.servlet.annotation.RequestProcessor;
import org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer; import org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer;
import org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer; import org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer;
import org.b3log.latke.util.Paginator;
import org.b3log.latke.util.Requests; import org.b3log.latke.util.Requests;
import org.b3log.latke.util.Strings; import org.b3log.latke.util.Strings;
import org.b3log.solo.model.Article; import org.b3log.solo.model.Article;
import org.b3log.solo.model.Category;
import org.b3log.solo.model.Common; import org.b3log.solo.model.Common;
import org.b3log.solo.model.Option; import org.b3log.solo.model.Option;
import org.b3log.solo.model.Tag;
import org.b3log.solo.processor.util.Filler; import org.b3log.solo.processor.util.Filler;
import org.b3log.solo.service.*; import org.b3log.solo.service.*;
import org.b3log.solo.util.Skins; import org.b3log.solo.util.Skins;
import org.b3log.solo.util.comparator.Comparators;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -48,7 +46,6 @@ import javax.servlet.http.HttpServletResponse; ...@@ -48,7 +46,6 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -56,7 +53,7 @@ import java.util.Map; ...@@ -56,7 +53,7 @@ import java.util.Map;
* Category processor. * Category processor.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Apr 7, 2017 * @version 1.0.0.1, Apr 8, 2017
* @since 2.0.0 * @since 2.0.0
*/ */
@RequestProcessor @RequestProcessor
...@@ -98,10 +95,10 @@ public class CategoryProcessor { ...@@ -98,10 +95,10 @@ public class CategoryProcessor {
private UserQueryService userQueryService; private UserQueryService userQueryService;
/** /**
* Tag query service. * Category query service.
*/ */
@Inject @Inject
private TagQueryService tagQueryService; private CategoryQueryService categoryQueryService;
/** /**
* Statistic management service. * Statistic management service.
...@@ -110,18 +107,18 @@ public class CategoryProcessor { ...@@ -110,18 +107,18 @@ public class CategoryProcessor {
private StatisticMgmtService statisticMgmtService; private StatisticMgmtService statisticMgmtService;
/** /**
* Gets the request page number from the specified request URI and tag title. * Gets the request page number from the specified request URI and category URI.
* *
* @param requestURI the specified request URI * @param requestURI the specified request URI
* @param tagTitle the specified tag title * @param categoryURI the specified category URI
* @return page number, returns {@code -1} if the specified request URI can not convert to an number * @return page number, returns {@code -1} if the specified request URI can not convert to an number
*/ */
private static int getCurrentPageNum(final String requestURI, final String tagTitle) { private static int getCurrentPageNum(final String requestURI, final String categoryURI) {
if (Strings.isEmptyOrNull(tagTitle)) { if (Strings.isEmptyOrNull(categoryURI)) {
return -1; return -1;
} }
final String pageNumString = requestURI.substring((Latkes.getContextPath() + "/tags/" + tagTitle + "/").length()); final String pageNumString = requestURI.substring((Latkes.getContextPath() + "/category/" + categoryURI + "/").length());
return Requests.getCurrentPageNum(pageNumString); return Requests.getCurrentPageNum(pageNumString);
} }
...@@ -171,42 +168,33 @@ public class CategoryProcessor { ...@@ -171,42 +168,33 @@ public class CategoryProcessor {
final int currentPageNum = getCurrentPageNum(requestURI, categoryURI); final int currentPageNum = getCurrentPageNum(requestURI, categoryURI);
if (-1 == currentPageNum) { if (-1 == currentPageNum) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); response.sendError(HttpServletResponse.SC_NOT_FOUND);
return; return;
} }
LOGGER.log(Level.DEBUG, "Category [URI={0}, currentPageNum={1}]", categoryURI, currentPageNum); LOGGER.log(Level.DEBUG, "Category [URI={0}, currentPageNum={1}]", categoryURI, currentPageNum);
categoryURI = URLDecoder.decode(categoryURI, "UTF-8"); categoryURI = URLDecoder.decode(categoryURI, "UTF-8");
final JSONObject result = tagQueryService.getTagByTitle(categoryURI); final JSONObject category = categoryQueryService.getByURI(categoryURI);
if (null == result) { if (null == category) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); response.sendError(HttpServletResponse.SC_NOT_FOUND);
return; return;
} }
final JSONObject tag = result.getJSONObject(Tag.TAG); dataModel.put(Category.CATEGORY, category);
final String tagId = tag.getString(Keys.OBJECT_ID);
final JSONObject preference = preferenceQueryService.getPreference(); final JSONObject preference = preferenceQueryService.getPreference();
Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT); final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
final int windowSize = preference.getInt(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE); final String categoryId = category.optString(Keys.OBJECT_ID);
final List<JSONObject> articles = articleQueryService.getArticlesByTag(tagId, currentPageNum, pageSize); final JSONObject result = articleQueryService.getCategoryArticles(categoryId, currentPageNum, pageSize);
final List<JSONObject> articles = (List<JSONObject>) result.opt(Article.ARTICLES);
if (articles.isEmpty()) { Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (final IOException ex) {
LOGGER.error(ex.getMessage());
}
}
final boolean hasMultipleUsers = userQueryService.hasMultipleUsers(); final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
if (hasMultipleUsers) { if (hasMultipleUsers) {
filler.setArticlesExProperties(request, articles, preference); filler.setArticlesExProperties(request, articles, preference);
} else { } else {
...@@ -216,36 +204,18 @@ public class CategoryProcessor { ...@@ -216,36 +204,18 @@ public class CategoryProcessor {
filler.setArticlesExProperties(request, articles, author, preference); filler.setArticlesExProperties(request, articles, author, preference);
} }
final int tagArticleCount = tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT); final int pageCount = result.optJSONObject(Pagination.PAGINATION).optInt(Pagination.PAGINATION_PAGE_COUNT);
final int pageCount = (int) Math.ceil((double) tagArticleCount / (double) pageSize); final List<Integer> pageNums = (List) result.optJSONObject(Pagination.PAGINATION).opt(Pagination.PAGINATION_PAGE_NUMS);
LOGGER.log(Level.TRACE, "Paginate tag-articles[currentPageNum={0}, pageSize={1}, pageCount={2}, windowSize={3}]",
new Object[]{currentPageNum, pageSize, pageCount, windowSize});
final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
LOGGER.log(Level.TRACE, "tag-articles[pageNums={0}]", pageNums);
Collections.sort(articles, Comparators.ARTICLE_CREATE_DATE_COMPARATOR);
fillPagination(dataModel, pageCount, currentPageNum, articles, pageNums); fillPagination(dataModel, pageCount, currentPageNum, articles, pageNums);
dataModel.put(Common.PATH, "/tags/" + URLEncoder.encode(categoryURI, "UTF-8")); dataModel.put(Common.PATH, "/category/" + URLEncoder.encode(categoryURI, "UTF-8"));
dataModel.put(Keys.OBJECT_ID, tagId);
dataModel.put(Tag.TAG, tag);
filler.fillSide(request, dataModel, preference); filler.fillSide(request, dataModel, preference);
filler.fillBlogHeader(request, response, dataModel, preference); filler.fillBlogHeader(request, response, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference); filler.fillBlogFooter(request, dataModel, preference);
statisticMgmtService.incBlogViewCount(request, response); statisticMgmtService.incBlogViewCount(request, response);
} catch (final ServiceException e) { } catch (final ServiceException | JSONException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} catch (final IOException ex) {
LOGGER.error(ex.getMessage());
}
} catch (final JSONException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
try { try {
......
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