Commit 15880f86 authored by Liang Ding's avatar Liang Ding

#12633

parent 04047692
...@@ -389,7 +389,6 @@ public final class SoloServletListener extends AbstractServletListener { ...@@ -389,7 +389,6 @@ public final class SoloServletListener extends AbstractServletListener {
final RepairConsole repairConsole = beanManager.getReference(RepairConsole.class); final RepairConsole repairConsole = beanManager.getReference(RepairConsole.class);
DispatcherServlet.get("/fix/restore-signs", repairConsole::restoreSigns); DispatcherServlet.get("/fix/restore-signs", repairConsole::restoreSigns);
DispatcherServlet.get("/fix/tag-article-counter-repair", repairConsole::repairTagArticleCounter);
final TagConsole tagConsole = beanManager.getReference(TagConsole.class); final TagConsole tagConsole = beanManager.getReference(TagConsole.class);
DispatcherServlet.get("/console/tags", tagConsole::getTags); DispatcherServlet.get("/console/tags", tagConsole::getTags);
......
...@@ -17,21 +17,16 @@ ...@@ -17,21 +17,16 @@
*/ */
package org.b3log.solo.processor.console; package org.b3log.solo.processor.console;
import org.b3log.latke.Keys;
import org.b3log.latke.ioc.Inject; import org.b3log.latke.ioc.Inject;
import org.b3log.latke.logging.Level; import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger; import org.b3log.latke.logging.Logger;
import org.b3log.latke.repository.Query;
import org.b3log.latke.repository.Transaction;
import org.b3log.latke.servlet.RequestContext; import org.b3log.latke.servlet.RequestContext;
import org.b3log.latke.servlet.annotation.Before; import org.b3log.latke.servlet.annotation.Before;
import org.b3log.latke.servlet.annotation.RequestProcessor; import org.b3log.latke.servlet.annotation.RequestProcessor;
import org.b3log.latke.servlet.renderer.TextHtmlRenderer; import org.b3log.latke.servlet.renderer.TextHtmlRenderer;
import org.b3log.solo.mail.MailService; import org.b3log.solo.mail.MailService;
import org.b3log.solo.mail.MailServiceFactory; import org.b3log.solo.mail.MailServiceFactory;
import org.b3log.solo.model.Article;
import org.b3log.solo.model.Option; import org.b3log.solo.model.Option;
import org.b3log.solo.model.Tag;
import org.b3log.solo.repository.ArticleRepository; import org.b3log.solo.repository.ArticleRepository;
import org.b3log.solo.repository.TagArticleRepository; import org.b3log.solo.repository.TagArticleRepository;
import org.b3log.solo.repository.TagRepository; import org.b3log.solo.repository.TagRepository;
...@@ -39,16 +34,13 @@ import org.b3log.solo.service.PreferenceMgmtService; ...@@ -39,16 +34,13 @@ import org.b3log.solo.service.PreferenceMgmtService;
import org.b3log.solo.service.PreferenceQueryService; import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.service.StatisticMgmtService; import org.b3log.solo.service.StatisticMgmtService;
import org.b3log.solo.service.StatisticQueryService; import org.b3log.solo.service.StatisticQueryService;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.List;
/** /**
* Provides patches on some special issues. * Provides patches on some special issues.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.2.0.19, Dec 11, 2018 * @version 1.2.0.20, Jan 28, 2019
* @since 0.3.1 * @since 0.3.1
*/ */
@RequestProcessor @RequestProcessor
...@@ -128,55 +120,4 @@ public class RepairConsole { ...@@ -128,55 +120,4 @@ public class RepairConsole {
renderer.setContent("Restores signs failed, error msg [" + e.getMessage() + "]"); renderer.setContent("Restores signs failed, error msg [" + e.getMessage() + "]");
} }
} }
/**
* Repairs tag article counter.
*
* @param context the specified context
*/
public void repairTagArticleCounter(final RequestContext context) {
final TextHtmlRenderer renderer = new TextHtmlRenderer();
context.setRenderer(renderer);
final Transaction transaction = tagRepository.beginTransaction();
try {
final List<JSONObject> tags = tagRepository.getList(new Query());
for (final JSONObject tag : tags) {
final String tagId = tag.getString(Keys.OBJECT_ID);
final JSONObject tagArticleResult = tagArticleRepository.getByTagId(tagId, 1, Integer.MAX_VALUE);
final JSONArray tagArticles = tagArticleResult.getJSONArray(Keys.RESULTS);
final int tagRefCnt = tagArticles.length();
int publishedTagRefCnt = 0;
for (int i = 0; i < tagRefCnt; i++) {
final JSONObject tagArticle = tagArticles.getJSONObject(i);
final String articleId = tagArticle.getString(Article.ARTICLE + "_" + Keys.OBJECT_ID);
final JSONObject article = articleRepository.get(articleId);
if (null == article) {
tagArticleRepository.remove(tagArticle.optString(Keys.OBJECT_ID));
continue;
}
if (article.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
publishedTagRefCnt++;
}
}
tag.put(Tag.TAG_REFERENCE_COUNT, tagRefCnt);
tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedTagRefCnt);
tagRepository.update(tagId, tag);
LOGGER.log(Level.INFO, "Repaired tag[title={0}, refCnt={1}, publishedTagRefCnt={2}]",
tag.getString(Tag.TAG_TITLE), tagRefCnt, publishedTagRefCnt);
}
transaction.commit();
renderer.setContent("Repair successfully!");
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
renderer.setContent("Repairs failed, error msg [" + e.getMessage() + "]");
}
}
} }
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
package org.b3log.solo.repository; package org.b3log.solo.repository;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
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.solo.model.Article; import org.b3log.solo.model.Article;
...@@ -36,6 +38,11 @@ import java.util.List; ...@@ -36,6 +38,11 @@ import java.util.List;
@Repository @Repository
public class TagArticleRepository extends AbstractRepository { public class TagArticleRepository extends AbstractRepository {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(TagArticleRepository.class);
/** /**
* Public constructor. * Public constructor.
*/ */
...@@ -43,6 +50,23 @@ public class TagArticleRepository extends AbstractRepository { ...@@ -43,6 +50,23 @@ public class TagArticleRepository extends AbstractRepository {
super(Tag.TAG + "_" + Article.ARTICLE); super(Tag.TAG + "_" + Article.ARTICLE);
} }
/**
* Gets article count of a tag specified by the given tag id.
*
* @param tagId the given tag id
* @return article count, returns {@code -1} if occurred an exception
*/
public int getArticleCount(final String tagId) {
final Query query = new Query().setFilter(new PropertyFilter(Tag.TAG + "_" + Keys.OBJECT_ID, FilterOperator.EQUAL, tagId));
try {
return (int) count(query);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets tag [" + tagId + "]'s article count failed", e);
return -1;
}
}
/** /**
* Gets tag-article relations by the specified article id. * Gets tag-article relations by the specified article id.
* *
......
...@@ -25,16 +25,14 @@ import org.b3log.solo.model.Tag; ...@@ -25,16 +25,14 @@ import org.b3log.solo.model.Tag;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.text.Collator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
/** /**
* Tag repository. * Tag repository.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.3, Jan 15, 2019 * @version 1.0.0.4, Jan 28, 2019
* @since 0.3.1 * @since 0.3.1
*/ */
@Repository @Repository
...@@ -102,11 +100,23 @@ public class TagRepository extends AbstractRepository { ...@@ -102,11 +100,23 @@ public class TagRepository extends AbstractRepository {
* @throws RepositoryException repository exception * @throws RepositoryException repository exception
*/ */
public List<JSONObject> getMostUsedTags(final int num) throws RepositoryException { public List<JSONObject> getMostUsedTags(final int num) throws RepositoryException {
final Query query = new Query().addSort(Tag.TAG_PUBLISHED_REFERENCE_COUNT, SortDirection.DESCENDING). final List<JSONObject> records = select("SELECT\n" +
setPage(1, num).setPageCount(1); "\t`tag_oId`,\n" +
final List<JSONObject> tagJoList = getList(query); "\tcount(*) AS cnt\n" +
Collections.sort(tagJoList, (o1, o2) -> Collator.getInstance(java.util.Locale.CHINA).compare(o1.optString(Tag.TAG_TITLE), o2.optString(Tag.TAG_TITLE))); "FROM\n" +
getName() + "\t`tag_article`\n" +
"GROUP BY\n" +
"\ttag_oId\n" +
"ORDER BY\n" +
"\tcnt DESC\n" +
"LIMIT ?", num);
final List<JSONObject> ret = new ArrayList<>();
for (final JSONObject record : records) {
final String tagId = record.optString(Tag.TAG + "_" + Keys.OBJECT_ID);
final JSONObject tag = get(tagId);
ret.add(tag);
}
return tagJoList; return ret;
} }
} }
...@@ -497,7 +497,6 @@ public class ArticleMgmtService { ...@@ -497,7 +497,6 @@ public class ArticleMgmtService {
final Transaction transaction = articleRepository.beginTransaction(); final Transaction transaction = articleRepository.beginTransaction();
try { try {
decTagRefCount(articleId);
unArchiveDate(articleId); unArchiveDate(articleId);
removeTagArticleRelations(articleId); removeTagArticleRelations(articleId);
...@@ -587,43 +586,6 @@ public class ArticleMgmtService { ...@@ -587,43 +586,6 @@ public class ArticleMgmtService {
} }
} }
/**
* Decrements reference count of every tag of an article specified by the
* given article id.
*
* @param articleId the given article id
* @throws ServiceException service exception
*/
private void decTagRefCount(final String articleId) throws ServiceException {
try {
final List<JSONObject> tags = tagRepository.getByArticleId(articleId);
final JSONObject article = articleRepository.get(articleId);
for (final JSONObject tag : tags) {
final String tagId = tag.getString(Keys.OBJECT_ID);
final int refCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT);
tag.put(Tag.TAG_REFERENCE_COUNT, refCnt - 1);
final int publishedRefCnt = tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
if (article.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt - 1);
} else {
tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt);
}
tagRepository.update(tagId, tag);
LOGGER.log(Level.TRACE, "Deced tag[title={0}, refCnt={1}, publishedRefCnt={2}] of article[id={3}]",
tag.getString(Tag.TAG_TITLE), tag.getInt(Tag.TAG_REFERENCE_COUNT), tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT),
articleId);
}
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Decs tag references count of article[id" + articleId + "] failed", e);
throw new ServiceException(e);
}
LOGGER.log(Level.DEBUG, "Deced all tag reference count of article[id={0}]", articleId);
}
/** /**
* Un-archive an article specified by the given specified article id. * Un-archive an article specified by the given specified article id.
* *
...@@ -728,48 +690,12 @@ public class ArticleMgmtService { ...@@ -728,48 +690,12 @@ public class ArticleMgmtService {
} }
} }
LOGGER.log(Level.DEBUG, "Tags unchanged[{0}]", tagsUnchanged); LOGGER.log(Level.DEBUG, "Tags unchanged [{0}]", tagsUnchanged);
for (final JSONObject tagUnchanged : tagsUnchanged) {
final String tagId = tagUnchanged.optString(Keys.OBJECT_ID);
if (null == tagId) {
continue; // Unchanged tag always exist id
}
final int publishedRefCnt = tagUnchanged.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
if (oldArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
if (!newArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
tagUnchanged.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt - 1);
tagRepository.update(tagId, tagUnchanged);
}
} else {
if (newArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
tagUnchanged.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt + 1);
tagRepository.update(tagId, tagUnchanged);
}
}
}
for (final JSONObject tagDropped : tagsDropped) {
final String tagId = tagDropped.getString(Keys.OBJECT_ID);
final int refCnt = tagDropped.getInt(Tag.TAG_REFERENCE_COUNT);
tagDropped.put(Tag.TAG_REFERENCE_COUNT, refCnt - 1);
final int publishedRefCnt = tagDropped.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
if (oldArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
tagDropped.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt - 1);
}
tagRepository.update(tagId, tagDropped);
}
final String[] tagIdsDropped = new String[tagsDropped.size()]; final String[] tagIdsDropped = new String[tagsDropped.size()];
for (int i = 0; i < tagIdsDropped.length; i++) { for (int i = 0; i < tagIdsDropped.length; i++) {
final JSONObject tag = tagsDropped.get(i); final JSONObject tag = tagsDropped.get(i);
final String id = tag.getString(Keys.OBJECT_ID); final String id = tag.getString(Keys.OBJECT_ID);
tagIdsDropped[i] = id; tagIdsDropped[i] = id;
} }
...@@ -779,7 +705,6 @@ public class ArticleMgmtService { ...@@ -779,7 +705,6 @@ public class ArticleMgmtService {
for (int i = 0; i < tagStrings.length; i++) { for (int i = 0; i < tagStrings.length; i++) {
final JSONObject tag = tagsNeedToAdd.get(i); final JSONObject tag = tagsNeedToAdd.get(i);
final String tagTitle = tag.getString(Tag.TAG_TITLE); final String tagTitle = tag.getString(Tag.TAG_TITLE);
tagStrings[i] = tagTitle; tagStrings[i] = tagTitle;
} }
final JSONArray tags = tag(tagStrings, newArticle); final JSONArray tags = tag(tagStrings, newArticle);
...@@ -859,13 +784,6 @@ public class ArticleMgmtService { ...@@ -859,13 +784,6 @@ public class ArticleMgmtService {
tagTitle, article.optString(Article.ARTICLE_TITLE)); tagTitle, article.optString(Article.ARTICLE_TITLE));
tag = new JSONObject(); tag = new JSONObject();
tag.put(Tag.TAG_TITLE, tagTitle); tag.put(Tag.TAG_TITLE, tagTitle);
tag.put(Tag.TAG_REFERENCE_COUNT, 1);
if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) { // Publish article directly
tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, 1);
} else { // Save as draft
tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, 0);
}
tagId = tagRepository.add(tag); tagId = tagRepository.add(tag);
tag.put(Keys.OBJECT_ID, tagId); tag.put(Keys.OBJECT_ID, tagId);
} else { } else {
...@@ -873,18 +791,8 @@ public class ArticleMgmtService { ...@@ -873,18 +791,8 @@ public class ArticleMgmtService {
LOGGER.log(Level.TRACE, "Found a existing tag[title={0}, id={1}] in article[title={2}]", LOGGER.log(Level.TRACE, "Found a existing tag[title={0}, id={1}] in article[title={2}]",
tag.optString(Tag.TAG_TITLE), tag.optString(Keys.OBJECT_ID), article.optString(Article.ARTICLE_TITLE)); tag.optString(Tag.TAG_TITLE), tag.optString(Keys.OBJECT_ID), article.optString(Article.ARTICLE_TITLE));
final JSONObject tagTmp = new JSONObject(); final JSONObject tagTmp = new JSONObject();
tagTmp.put(Keys.OBJECT_ID, tagId); tagTmp.put(Keys.OBJECT_ID, tagId);
tagTmp.put(Tag.TAG_TITLE, tagTitle); tagTmp.put(Tag.TAG_TITLE, tagTitle);
final int refCnt = tag.optInt(Tag.TAG_REFERENCE_COUNT);
final int publishedRefCnt = tag.optInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
tagTmp.put(Tag.TAG_REFERENCE_COUNT, refCnt + 1);
if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
tagTmp.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt + 1);
} else {
tagTmp.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt);
}
tagRepository.update(tagId, tagTmp); tagRepository.update(tagId, tagTmp);
} }
......
...@@ -272,7 +272,6 @@ public class DataModelService { ...@@ -272,7 +272,6 @@ public class DataModelService {
Stopwatchs.start("Fill Tags"); Stopwatchs.start("Fill Tags");
try { try {
final List<JSONObject> tags = tagQueryService.getTags(); final List<JSONObject> tags = tagQueryService.getTags();
Collections.sort(tags, Comparator.comparingInt(t -> -t.optInt(Tag.TAG_REFERENCE_COUNT)));
dataModel.put(Tag.TAGS, tags); dataModel.put(Tag.TAGS, tags);
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, "Fills tags failed", e); LOGGER.log(Level.ERROR, "Fills tags failed", e);
......
...@@ -21,14 +21,12 @@ import org.b3log.latke.Keys; ...@@ -21,14 +21,12 @@ import org.b3log.latke.Keys;
import org.b3log.latke.ioc.Inject; import org.b3log.latke.ioc.Inject;
import org.b3log.latke.logging.Level; import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger; import org.b3log.latke.logging.Logger;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.Transaction; import org.b3log.latke.repository.Transaction;
import org.b3log.latke.service.ServiceException; import org.b3log.latke.service.ServiceException;
import org.b3log.latke.service.annotation.Service; import org.b3log.latke.service.annotation.Service;
import org.b3log.solo.model.Tag;
import org.b3log.solo.repository.CategoryTagRepository; import org.b3log.solo.repository.CategoryTagRepository;
import org.b3log.solo.repository.TagArticleRepository;
import org.b3log.solo.repository.TagRepository; import org.b3log.solo.repository.TagRepository;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.List; import java.util.List;
...@@ -37,7 +35,7 @@ import java.util.List; ...@@ -37,7 +35,7 @@ import java.util.List;
* Tag management service. * Tag management service.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.2, Mar 31, 2017 * @version 1.0.0.3, Jan 28, 2019
* @since 0.4.0 * @since 0.4.0
*/ */
@Service @Service
...@@ -67,27 +65,10 @@ public class TagMgmtService { ...@@ -67,27 +65,10 @@ public class TagMgmtService {
private CategoryTagRepository categoryTagRepository; private CategoryTagRepository categoryTagRepository;
/** /**
* Decrements reference count of every tag of an published article specified * Tag-Article repository.
* by the given article id.
*
* @param articleId the given article id
* @throws JSONException json exception
* @throws RepositoryException repository exception
*/ */
public void decTagPublishedRefCount(final String articleId) throws JSONException, RepositoryException { @Inject
final List<JSONObject> tags = tagRepository.getByArticleId(articleId); private TagArticleRepository tagArticleRepository;
for (final JSONObject tag : tags) {
final String tagId = tag.getString(Keys.OBJECT_ID);
final int refCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT);
tag.put(Tag.TAG_REFERENCE_COUNT, refCnt);
final int publishedRefCnt = tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt - 1);
tagRepository.update(tagId, tag);
}
}
/** /**
* Removes all unused tags. * Removes all unused tags.
...@@ -99,14 +80,11 @@ public class TagMgmtService { ...@@ -99,14 +80,11 @@ public class TagMgmtService {
try { try {
final List<JSONObject> tags = tagQueryService.getTags(); final List<JSONObject> tags = tagQueryService.getTags();
for (int i = 0; i < tags.size(); i++) { for (int i = 0; i < tags.size(); i++) {
final JSONObject tag = tags.get(i); final JSONObject tag = tags.get(i);
final int tagRefCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT); final String tagId = tag.optString(Keys.OBJECT_ID);
final int articleCount = tagArticleRepository.getArticleCount(tagId);
if (0 == tagRefCnt) { if (1 > articleCount) {
final String tagId = tag.getString(Keys.OBJECT_ID);
categoryTagRepository.removeByTagId(tagId); categoryTagRepository.removeByTagId(tagId);
tagRepository.remove(tagId); tagRepository.remove(tagId);
} }
......
...@@ -22,22 +22,20 @@ import org.b3log.latke.logging.Level; ...@@ -22,22 +22,20 @@ import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger; import org.b3log.latke.logging.Logger;
import org.b3log.latke.repository.Query; import org.b3log.latke.repository.Query;
import org.b3log.latke.repository.RepositoryException; import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.SortDirection;
import org.b3log.latke.service.ServiceException; import org.b3log.latke.service.ServiceException;
import org.b3log.latke.service.annotation.Service; import org.b3log.latke.service.annotation.Service;
import org.b3log.solo.model.Tag; import org.b3log.solo.model.Tag;
import org.b3log.solo.repository.TagArticleRepository;
import org.b3log.solo.repository.TagRepository; import org.b3log.solo.repository.TagRepository;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
* Tag query service. * Tag query service.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.4, Aug 27, 2018 * @version 1.1.0.5, Jan 28, 2019
* @since 0.4.0 * @since 0.4.0
*/ */
@Service @Service
...@@ -54,6 +52,22 @@ public class TagQueryService { ...@@ -54,6 +52,22 @@ public class TagQueryService {
@Inject @Inject
private TagRepository tagRepository; private TagRepository tagRepository;
/**
* Tag-Article repository.
*/
@Inject
private TagArticleRepository tagArticleRepository;
/**
* Gets article count of a tag specified by the given tag id.
*
* @param tagId the given tag id
* @return article count, returns {@code -1} if occurred an exception
*/
public int getArticleCount(final String tagId) {
return tagArticleRepository.getArticleCount(tagId);
}
/** /**
* Gets a tag by the specified tag title. * Gets a tag by the specified tag title.
* *
...@@ -127,56 +141,6 @@ public class TagQueryService { ...@@ -127,56 +141,6 @@ public class TagQueryService {
} }
} }
/**
* Gets top (reference count descending) 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> getTopTags(final int fetchSize) throws ServiceException {
try {
final Query query = new Query().setPageCount(1).setPageSize(fetchSize).
addSort(Tag.TAG_PUBLISHED_REFERENCE_COUNT, SortDirection.DESCENDING);
return tagRepository.getList(query);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Gets top tags failed", e);
throw new ServiceException(e);
}
}
/**
* 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);
return tagRepository.getList(query);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Gets bottom tags failed", e);
throw new ServiceException(e);
}
}
/** /**
* Sets the tag repository with the specified tag repository. * Sets the tag repository with the specified tag repository.
* *
......
...@@ -31,7 +31,7 @@ import java.util.List; ...@@ -31,7 +31,7 @@ import java.util.List;
* {@link ArticleQueryService} test case. * {@link ArticleQueryService} test case.
* *
* @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, Oct 15, 2018 * @version 1.1.0.2, Jan 28, 2019
*/ */
@Test(suiteName = "service") @Test(suiteName = "service")
public class ArticleQueryServiceTestCase extends AbstractTestCase { public class ArticleQueryServiceTestCase extends AbstractTestCase {
...@@ -152,8 +152,9 @@ public class ArticleQueryServiceTestCase extends AbstractTestCase { ...@@ -152,8 +152,9 @@ public class ArticleQueryServiceTestCase extends AbstractTestCase {
final String tagId = tag.getString(Keys.OBJECT_ID); final String tagId = tag.getString(Keys.OBJECT_ID);
final ArticleQueryService articleQueryService = getArticleQueryService(); final ArticleQueryService articleQueryService = getArticleQueryService();
final List<JSONObject> articles = articleQueryService.getArticlesByTag(tagId, 1, Integer.MAX_VALUE); final JSONObject articlesResult = articleQueryService.getArticlesByTag(tagId, 1, Integer.MAX_VALUE);
Assert.assertNotNull(articles); Assert.assertNotNull(articlesResult);
final List<JSONObject> articles = (List<JSONObject>) articlesResult.opt(Keys.RESULTS);
Assert.assertEquals(articles.size(), 1); Assert.assertEquals(articles.size(), 1);
} }
......
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