Commit d1850024 authored by Liang Ding's avatar Liang Ding

#12633

parent c2dc15dd
......@@ -17,7 +17,9 @@
*/
package org.b3log.solo.repository;
import org.apache.commons.lang.StringUtils;
import org.b3log.latke.Keys;
import org.b3log.latke.Latkes;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.repository.*;
......@@ -26,6 +28,7 @@ import org.b3log.solo.model.Article;
import org.b3log.solo.model.Tag;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
......@@ -50,6 +53,37 @@ public class TagArticleRepository extends AbstractRepository {
super(Tag.TAG + "_" + Article.ARTICLE);
}
/**
* Gets most used tags with the specified number.
*
* @param num the specified number
* @return a list of most used tags, returns an empty list if not found
* @throws RepositoryException repository exception
*/
public List<JSONObject> getMostUsedTags(final int num) throws RepositoryException {
final String tableNamePrefix = StringUtils.isNotBlank(Latkes.getLocalProperty("jdbc.tablePrefix"))
? Latkes.getLocalProperty("jdbc.tablePrefix") + "_"
: "";
setDebug(true);
final List<JSONObject> records = select("SELECT\n" +
"\t`tag_oId`,\n" +
"\tcount(*) AS cnt\n" +
"FROM `" + tableNamePrefix + "tag_article`\n" +
"GROUP BY\n" +
"\t`tag_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 ret;
}
/**
* Gets article count of a tag specified by the given tag id.
*
......
......@@ -17,7 +17,9 @@
*/
package org.b3log.solo.repository;
import org.apache.commons.lang.StringUtils;
import org.b3log.latke.Keys;
import org.b3log.latke.Latkes;
import org.b3log.latke.ioc.Inject;
import org.b3log.latke.repository.*;
import org.b3log.latke.repository.annotation.Repository;
......@@ -91,31 +93,4 @@ public class TagRepository extends AbstractRepository {
return array.optJSONObject(0);
}
/**
* Gets most used tags with the specified number.
*
* @param num the specified number
* @return a list of most used tags, returns an empty list if not found
* @throws RepositoryException repository exception
*/
public List<JSONObject> getMostUsedTags(final int num) throws RepositoryException {
final List<JSONObject> records = select("SELECT\n" +
"\t`tag_oId`,\n" +
"\tcount(*) AS cnt\n" +
"FROM `" + getName() + "`\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 ret;
}
}
......@@ -100,10 +100,10 @@ public class DataModelService {
private CategoryRepository categoryRepository;
/**
* Tag repository.
* Tag-Article repository.
*/
@Inject
private TagRepository tagRepository;
private TagArticleRepository tagArticleRepository;
/**
* Link repository.
......@@ -343,7 +343,7 @@ public class DataModelService {
try {
LOGGER.debug("Filling most used tags....");
final int mostUsedTagDisplayCnt = preference.getInt(Option.ID_C_MOST_USED_TAG_DISPLAY_CNT);
final List<JSONObject> tags = tagRepository.getMostUsedTags(mostUsedTagDisplayCnt);
final List<JSONObject> tags = tagArticleRepository.getMostUsedTags(mostUsedTagDisplayCnt);
dataModel.put(Common.MOST_USED_TAGS, tags);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Fills most used tags failed", e);
......
......@@ -62,23 +62,4 @@ public class RepairConsoleTestCase extends AbstractTestCase {
final String content = response.body();
Assert.assertTrue(StringUtils.contains(content, "Restore signs succeeded."));
}
/**
* repairTagArticleCounter.
*
* @throws Exception exception
*/
@Test(dependsOnMethods = "init")
public void repairTagArticleCounter() throws Exception {
final MockHttpServletRequest request = mockRequest();
request.setRequestURI("/fix/tag-article-counter-repair");
mockAdminLogin(request);
final MockHttpServletResponse response = mockResponse();
mockDispatcherServletService(request, response);
final String content = response.body();
Assert.assertTrue(StringUtils.contains(content, "Repair successfully!"));
}
}
......@@ -33,7 +33,7 @@ import java.util.List;
* {@link TagArticleRepository} test case.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Dec 30, 2011
* @version 1.1.0.0, Jan 28, 2019
*/
@Test(suiteName = "repository")
public class TagArticleRepositoryImplTestCase extends AbstractTestCase {
......@@ -48,7 +48,6 @@ public class TagArticleRepositoryImplTestCase extends AbstractTestCase {
final TagArticleRepository tagArticleRepository = getTagArticleRepository();
final JSONObject tagArticle = new JSONObject();
tagArticle.put(Article.ARTICLE + "_" + Keys.OBJECT_ID, "article1 id");
tagArticle.put(Tag.TAG + "_" + Keys.OBJECT_ID, "tag1 id");
......@@ -64,11 +63,9 @@ public class TagArticleRepositoryImplTestCase extends AbstractTestCase {
*/
@Test(dependsOnMethods = "add")
public void getByArticleId() throws Exception {
final TagArticleRepository tagArticleRepository
= getTagArticleRepository();
final TagArticleRepository tagArticleRepository = getTagArticleRepository();
final List<JSONObject> tagArticle
= tagArticleRepository.getByArticleId("article1 id");
final List<JSONObject> tagArticle = tagArticleRepository.getByArticleId("article1 id");
Assert.assertNotNull(tagArticle);
Assert.assertEquals(0, tagArticleRepository.getByArticleId("").size());
......@@ -81,12 +78,23 @@ public class TagArticleRepositoryImplTestCase extends AbstractTestCase {
*/
@Test(dependsOnMethods = "add")
public void getByTagId() throws Exception {
final TagArticleRepository tagArticleRepository
= getTagArticleRepository();
final TagArticleRepository tagArticleRepository = getTagArticleRepository();
final JSONArray results
= tagArticleRepository.getByTagId("tag1 id", 1, Integer.MAX_VALUE).
getJSONArray(Keys.RESULTS);
final JSONArray results = tagArticleRepository.getByTagId("tag1 id", 1, Integer.MAX_VALUE).getJSONArray(Keys.RESULTS);
Assert.assertEquals(1, results.length());
}
/**
* Get Most Used Tags.
*
* @throws Exception exception
*/
@Test(dependsOnMethods = "add")
public void getMostUsedTags() throws Exception {
final TagArticleRepository tagArticleRepository = getTagArticleRepository();
final List<JSONObject> mostUsedTags = tagArticleRepository.getMostUsedTags(3);
Assert.assertNotNull(mostUsedTags);
Assert.assertEquals(1, mostUsedTags.size());
}
}
......@@ -71,31 +71,6 @@ public class TagRepositoryImplTestCase extends AbstractTestCase {
Assert.assertNull(notFound);
}
/**
* Get Most Used Tags.
*
* @throws Exception exception
*/
@Test(dependsOnMethods = "add")
public void getMostUsedTags() throws Exception {
final TagRepository tagRepository = getTagRepository();
final JSONObject tag = new JSONObject();
tag.put(Tag.TAG_TITLE, "tag title2");
final Transaction transaction = tagRepository.beginTransaction();
tagRepository.add(tag);
transaction.commit();
List<JSONObject> mostUsedTags = tagRepository.getMostUsedTags(3);
Assert.assertNotNull(mostUsedTags);
Assert.assertEquals(2, mostUsedTags.size());
mostUsedTags = tagRepository.getMostUsedTags(1);
Assert.assertNotNull(mostUsedTags);
Assert.assertEquals(1, mostUsedTags.size());
}
/**
* Get By ArticleId.
*
......
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