Commit e5ec2661 authored by Liang Ding's avatar Liang Ding

🚧 #12256 填充侧边栏分类模型

parent 183ba177
...@@ -20,7 +20,7 @@ package org.b3log.solo.model; ...@@ -20,7 +20,7 @@ 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.4.5.1, Nov 1, 2015 * @version 1.5.5.1, Apr 8, 2017
* @since 0.3.1 * @since 0.3.1
*/ */
public final class Common { public final class Common {
...@@ -35,6 +35,11 @@ public final class Common { ...@@ -35,6 +35,11 @@ public final class Common {
*/ */
public static final String MOST_USED_TAGS = "mostUsedTags"; public static final String MOST_USED_TAGS = "mostUsedTags";
/**
* Most used categories.
*/
public static final String MOST_USED_CATEGORIES = "mostUsedCategories";
/** /**
* Most comment count articles. * Most comment count articles.
*/ */
......
...@@ -16,15 +16,6 @@ ...@@ -16,15 +16,6 @@
package org.b3log.solo.processor.util; package org.b3log.solo.processor.util;
import freemarker.template.Template; import freemarker.template.Template;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.time.DateFormatUtils; import org.apache.commons.lang.time.DateFormatUtils;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
import org.b3log.latke.Latkes; import org.b3log.latke.Latkes;
...@@ -47,30 +38,26 @@ import org.b3log.latke.util.*; ...@@ -47,30 +38,26 @@ import org.b3log.latke.util.*;
import org.b3log.latke.util.freemarker.Templates; import org.b3log.latke.util.freemarker.Templates;
import org.b3log.solo.SoloServletListener; import org.b3log.solo.SoloServletListener;
import org.b3log.solo.model.*; import org.b3log.solo.model.*;
import static org.b3log.solo.model.Article.ARTICLE_CONTENT; import org.b3log.solo.repository.*;
import org.b3log.solo.repository.ArchiveDateRepository; import org.b3log.solo.service.*;
import org.b3log.solo.repository.ArticleRepository;
import org.b3log.solo.repository.CommentRepository;
import org.b3log.solo.repository.LinkRepository;
import org.b3log.solo.repository.PageRepository;
import org.b3log.solo.repository.TagRepository;
import org.b3log.solo.repository.UserRepository;
import org.b3log.solo.service.ArticleQueryService;
import org.b3log.solo.service.OptionQueryService;
import org.b3log.solo.service.StatisticQueryService;
import org.b3log.solo.service.TagQueryService;
import org.b3log.solo.service.UserQueryService;
import org.b3log.solo.util.Thumbnails; import org.b3log.solo.util.Thumbnails;
import org.b3log.solo.util.comparator.Comparators; import org.b3log.solo.util.comparator.Comparators;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import static org.b3log.solo.model.Article.ARTICLE_CONTENT;
/** /**
* Filler utilities. * Filler utilities.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.5.11.13, Feb 26, 2017 * @version 1.6.11.13, Apr 8, 2017
* @since 0.3.1 * @since 0.3.1
*/ */
@Service @Service
...@@ -80,101 +67,89 @@ public class Filler { ...@@ -80,101 +67,89 @@ public class Filler {
* Logger. * Logger.
*/ */
private static final Logger LOGGER = Logger.getLogger(Filler.class.getName()); private static final Logger LOGGER = Logger.getLogger(Filler.class.getName());
/**
* {@code true} for published.
*/
private static final boolean PUBLISHED = true;
/**
* User service.
*/
private static UserService userService = UserServiceFactory.getUserService();
/** /**
* Topbar utilities. * Topbar utilities.
*/ */
@Inject @Inject
private TopBars topBars; private TopBars topBars;
/** /**
* Article repository. * Article repository.
*/ */
@Inject @Inject
private ArticleRepository articleRepository; private ArticleRepository articleRepository;
/** /**
* Comment repository. * Comment repository.
*/ */
@Inject @Inject
private CommentRepository commentRepository; private CommentRepository commentRepository;
/** /**
* Archive date repository. * Archive date repository.
*/ */
@Inject @Inject
private ArchiveDateRepository archiveDateRepository; private ArchiveDateRepository archiveDateRepository;
/**
* Category repository.
*/
@Inject
private CategoryRepository categoryRepository;
/** /**
* Tag repository. * Tag repository.
*/ */
@Inject @Inject
private TagRepository tagRepository; private TagRepository tagRepository;
/** /**
* Link repository. * Link repository.
*/ */
@Inject @Inject
private LinkRepository linkRepository; private LinkRepository linkRepository;
/** /**
* Page repository. * Page repository.
*/ */
@Inject @Inject
private PageRepository pageRepository; private PageRepository pageRepository;
/** /**
* Statistic query service. * Statistic query service.
*/ */
@Inject @Inject
private StatisticQueryService statisticQueryService; private StatisticQueryService statisticQueryService;
/** /**
* User repository. * User repository.
*/ */
@Inject @Inject
private UserRepository userRepository; private UserRepository userRepository;
/** /**
* Option query service.. * Option query service..
*/ */
@Inject @Inject
private OptionQueryService optionQueryService; private OptionQueryService optionQueryService;
/** /**
* Article query service. * Article query service.
*/ */
@Inject @Inject
private ArticleQueryService articleQueryService; private ArticleQueryService articleQueryService;
/**
* {@code true} for published.
*/
private static final boolean PUBLISHED = true;
/** /**
* Tag query service. * Tag query service.
*/ */
@Inject @Inject
private TagQueryService tagQueryService; private TagQueryService tagQueryService;
/** /**
* User query service. * User query service.
*/ */
@Inject @Inject
private UserQueryService userQueryService; private UserQueryService userQueryService;
/** /**
* Fill tag article.. * Fill tag article..
*/ */
@Inject @Inject
private FillTagArticles fillTagArticles; private FillTagArticles fillTagArticles;
/**
* User service.
*/
private static UserService userService = UserServiceFactory.getUserService();
/** /**
* Event manager. * Event manager.
*/ */
...@@ -331,6 +306,31 @@ public class Filler { ...@@ -331,6 +306,31 @@ public class Filler {
Stopwatchs.end(); Stopwatchs.end();
} }
/**
* Fills most used categories.
*
* @param dataModel data model
* @param preference the specified preference
* @throws ServiceException service exception
*/
public void fillMostUsedCategories(final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException {
Stopwatchs.start("Fill Most Used Categories");
try {
LOGGER.debug("Filling most used categories....");
final int mostUsedCategoryDisplayCnt = Integer.MAX_VALUE; // XXX: preference instead
final List<JSONObject> categories = categoryRepository.getMostUsedCategories(mostUsedCategoryDisplayCnt);
dataModel.put(Common.MOST_USED_CATEGORIES, categories);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Fills most used categories failed", e);
throw new ServiceException(e);
} finally {
Stopwatchs.end();
}
}
/** /**
* Fills most used tags. * Fills most used tags.
* *
...@@ -755,6 +755,10 @@ public class Filler { ...@@ -755,6 +755,10 @@ public class Filler {
fillRecentComments(dataModel, preference); fillRecentComments(dataModel, preference);
} }
if (Templates.hasExpression(template, "<#list mostUsedCategories as tag>")) {
fillMostUsedCategories(dataModel, preference);
}
if (Templates.hasExpression(template, "<#list mostUsedTags as tag>")) { if (Templates.hasExpression(template, "<#list mostUsedTags as tag>")) {
fillMostUsedTags(dataModel, preference); fillMostUsedTags(dataModel, preference);
} }
...@@ -806,6 +810,10 @@ public class Filler { ...@@ -806,6 +810,10 @@ public class Filler {
fillRecentComments(dataModel, preference); fillRecentComments(dataModel, preference);
} }
if (Templates.hasExpression(template, "<#list mostUsedCategories as tag>")) {
fillMostUsedCategories(dataModel, preference);
}
if (Templates.hasExpression(template, "<#list mostUsedTags as tag>")) { if (Templates.hasExpression(template, "<#list mostUsedTags as tag>")) {
fillMostUsedTags(dataModel, preference); fillMostUsedTags(dataModel, preference);
} }
...@@ -890,7 +898,7 @@ public class Filler { ...@@ -890,7 +898,7 @@ public class Filler {
/** /**
* Sets some extra properties into the specified article with the specified author and preference, performs content * Sets some extra properties into the specified article with the specified author and preference, performs content
* and abstract editor processing. * and abstract editor processing.
* * <p>
* <p> * <p>
* Article ext properties: * Article ext properties:
* <pre> * <pre>
...@@ -953,7 +961,7 @@ public class Filler { ...@@ -953,7 +961,7 @@ public class Filler {
/** /**
* Sets some extra properties into the specified article with the specified preference, performs content and * Sets some extra properties into the specified article with the specified preference, performs content and
* abstract editor processing. * abstract editor processing.
* * <p>
* <p> * <p>
* Article ext properties: * Article ext properties:
* <pre> * <pre>
...@@ -1014,11 +1022,11 @@ public class Filler { ...@@ -1014,11 +1022,11 @@ public class Filler {
/** /**
* Sets some extra properties into the specified article with the specified author and preference. * Sets some extra properties into the specified article with the specified author and preference.
* * <p>
* <p> * <p>
* The batch version of method {@linkplain #setArticleExProperties(HttpServletRequest, JSONObject, JSONObject, JSONObject)}. * The batch version of method {@linkplain #setArticleExProperties(HttpServletRequest, JSONObject, JSONObject, JSONObject)}.
* </p> * </p>
* * <p>
* <p> * <p>
* Article ext properties: * Article ext properties:
* <pre> * <pre>
...@@ -1047,11 +1055,11 @@ public class Filler { ...@@ -1047,11 +1055,11 @@ public class Filler {
/** /**
* Sets some extra properties into the specified article with the specified preference. * Sets some extra properties into the specified article with the specified preference.
* * <p>
* <p> * <p>
* The batch version of method {@linkplain #setArticleExProperties(HttpServletRequest, JSONObject, JSONObject)}. * The batch version of method {@linkplain #setArticleExProperties(HttpServletRequest, JSONObject, JSONObject)}.
* </p> * </p>
* * <p>
* <p> * <p>
* Article ext properties: * Article ext properties:
* <pre> * <pre>
...@@ -1079,7 +1087,7 @@ public class Filler { ...@@ -1079,7 +1087,7 @@ public class Filler {
/** /**
* Processes the abstract of the specified article with the specified preference. * Processes the abstract of the specified article with the specified preference.
* * <p>
* <p> * <p>
* <ul> * <ul>
* <li>If the abstract is {@code null}, sets it with ""</li> * <li>If the abstract is {@code null}, sets it with ""</li>
......
...@@ -19,11 +19,13 @@ import org.b3log.latke.repository.Repository; ...@@ -19,11 +19,13 @@ import org.b3log.latke.repository.Repository;
import org.b3log.latke.repository.RepositoryException; import org.b3log.latke.repository.RepositoryException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.List;
/** /**
* Category repository. * Category repository.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.1, Apr 1, 2017 * @version 1.2.0.1, Apr 8, 2017
* @since 2.0.0 * @since 2.0.0
*/ */
public interface CategoryRepository extends Repository { public interface CategoryRepository extends Repository {
...@@ -80,4 +82,13 @@ public interface CategoryRepository extends Repository { ...@@ -80,4 +82,13 @@ public interface CategoryRepository extends Repository {
* @throws RepositoryException repository exception * @throws RepositoryException repository exception
*/ */
JSONObject getByOrder(final int order) throws RepositoryException; JSONObject getByOrder(final int order) throws RepositoryException;
/**
* Gets most used categories (contains the most tags) with the specified number.
*
* @param num the specified number
* @return a list of most used categories, returns an empty list if not found
* @throws RepositoryException repository exception
*/
List<JSONObject> getMostUsedCategories(final int num) throws RepositoryException;
} }
...@@ -18,16 +18,23 @@ package org.b3log.solo.repository.impl; ...@@ -18,16 +18,23 @@ package org.b3log.solo.repository.impl;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
import org.b3log.latke.repository.*; import org.b3log.latke.repository.*;
import org.b3log.latke.repository.annotation.Repository; import org.b3log.latke.repository.annotation.Repository;
import org.b3log.latke.util.CollectionUtils;
import org.b3log.solo.model.Category; import org.b3log.solo.model.Category;
import org.b3log.solo.model.Tag;
import org.b3log.solo.repository.CategoryRepository; import org.b3log.solo.repository.CategoryRepository;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/** /**
* Category repository. * Category repository.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.0, Apr 1, 2017 * @version 1.1.0.1, Apr 8, 2017
* @since 2.0.0 * @since 2.0.0
*/ */
@Repository @Repository
...@@ -104,6 +111,20 @@ public class CategoryRepositoryImpl extends AbstractRepository implements Catego ...@@ -104,6 +111,20 @@ public class CategoryRepositoryImpl extends AbstractRepository implements Catego
return array.optJSONObject(0); return array.optJSONObject(0);
} }
@Override
public List<JSONObject> getMostUsedCategories(final int num) throws RepositoryException {
final Query query = new Query().addSort(Category.CATEGORY_TAG_CNT, SortDirection.DESCENDING).
setCurrentPageNum(1).setPageSize(num).setPageCount(1);
final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS);
final List<JSONObject> ret = CollectionUtils.jsonArrayToList(array);
sortJSONCategoryList(ret);
return ret;
}
@Override @Override
public JSONObject getUpper(final String id) throws RepositoryException { public JSONObject getUpper(final String id) throws RepositoryException {
final JSONObject category = get(id); final JSONObject category = get(id);
...@@ -153,4 +174,14 @@ public class CategoryRepositoryImpl extends AbstractRepository implements Catego ...@@ -153,4 +174,14 @@ public class CategoryRepositoryImpl extends AbstractRepository implements Catego
return array.optJSONObject(0); return array.optJSONObject(0);
} }
private void sortJSONCategoryList(final List<JSONObject> tagJoList) {
Collections.sort(tagJoList, new Comparator<JSONObject>() {
@Override
public int compare(final JSONObject o1, final JSONObject o2) {
return Collator.getInstance(java.util.Locale.CHINA).
compare(o1.optString(Tag.TAG_TITLE), o2.optString(Tag.TAG_TITLE));
}
});
}
} }
...@@ -142,9 +142,8 @@ public class ArticleQueryService { ...@@ -142,9 +142,8 @@ public class ArticleQueryService {
pagination.put(Pagination.PAGINATION_PAGE_NUMS, (Object) Collections.emptyList()); pagination.put(Pagination.PAGINATION_PAGE_NUMS, (Object) Collections.emptyList());
try { try {
final JSONArray categoryTags = final JSONArray categoryTags = categoryTagRepository.getByCategoryId(
categoryTagRepository.getByCategoryId(categoryId, 1, Integer.MAX_VALUE) categoryId, 1, Integer.MAX_VALUE).optJSONArray(Keys.RESULTS);
.optJSONArray(Keys.RESULTS);
if (categoryTags.length() <= 0) { if (categoryTags.length() <= 0) {
return ret; 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