Commit f8d79b02 authored by Liang Ding's avatar Liang Ding

🎨 Fix #12891 存档列表只考虑已发布的文章

parent 0fe7d297
......@@ -59,7 +59,7 @@ import java.util.*;
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="http://zephyr.b3log.org">Zephyr</a>
* @version 1.4.5.7, May 18, 2019
* @version 1.4.5.8, Sep 11, 2019
* @since 0.3.1
*/
@RequestProcessor
......@@ -500,7 +500,7 @@ public class ArticleProcessor {
final JSONObject archiveDate = archiveQueryResult.getJSONObject(ArchiveDate.ARCHIVE_DATE);
final String archiveDateId = archiveDate.getString(Keys.OBJECT_ID);
final int articleCount = archiveDateQueryService.getArchiveDateArticleCount(archiveDateId);
final int articleCount = archiveDateQueryService.getArchiveDatePublishedArticleCount(archiveDateId);
final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize);
final List<JSONObject> articles = articleQueryService.getArticlesByArchiveDate(archiveDateId, currentPageNum, pageSize);
......@@ -663,7 +663,7 @@ public class ArticleProcessor {
final JSONObject preference = optionQueryService.getPreference();
final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
final int articleCount = archiveDateQueryService.getArchiveDateArticleCount(archiveDateId);
final int articleCount = archiveDateQueryService.getArchiveDatePublishedArticleCount(archiveDateId);
final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize);
final List<JSONObject> articles = articleQueryService.getArticlesByArchiveDate(archiveDateId, currentPageNum, pageSize);
......
......@@ -18,6 +18,7 @@
package org.b3log.solo.repository;
import org.b3log.latke.Keys;
import org.b3log.latke.ioc.BeanManager;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.repository.*;
......@@ -27,11 +28,13 @@ import org.b3log.solo.model.Article;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.List;
/**
* Archive date-Article repository.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.4, Jan 15, 2019
* @version 1.1.0.0, Sep 11, 2019
* @since 0.3.1
*/
@Repository
......@@ -50,18 +53,27 @@ public class ArchiveDateArticleRepository extends AbstractRepository {
}
/**
* Gets article count of an archive date specified by the given archive date id.
* Gets published article count of an archive date specified by the given archive data id.
*
* @param archiveDateId the given archive date id
* @return article count, returns {@code -1} if occurred an exception
* @return published article count, returns {@code -1} if occurred an exception
*/
public int getArticleCount(final String archiveDateId) {
final Query query = new Query().setFilter(new PropertyFilter(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID, FilterOperator.EQUAL, archiveDateId));
public int getPublishedArticleCount(final String archiveDateId) {
try {
return (int) count(query);
final BeanManager beanManager = BeanManager.getInstance();
final ArticleRepository articleRepository = beanManager.getReference(ArticleRepository.class);
final ArchiveDateArticleRepository archiveDateArticleRepository = beanManager.getReference(ArchiveDateArticleRepository.class);
final StringBuilder queryCount = new StringBuilder("SELECT count(DISTINCT(article.oId)) as C FROM ");
final StringBuilder queryStr = new StringBuilder(articleRepository.getName() + " AS article,").
append(archiveDateArticleRepository.getName() + " AS archive_article").
append(" WHERE article.oId=archive_article.article_oId ").
append(" AND article.articleStatus=").append(Article.ARTICLE_STATUS_C_PUBLISHED).
append(" AND ").append("archive_article.archiveDate_oId=").append(archiveDateId);
final List<JSONObject> articlesCountResult = select(queryCount.append(queryStr.toString()).toString());
return articlesCountResult == null ? 0 : articlesCountResult.get(0).optInt("C");
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets archivedate [" + archiveDateId + "]'s article count failed", e);
LOGGER.log(Level.ERROR, "Gets archivedate [" + archiveDateId + "]'s published article count failed", e);
return -1;
}
......
......@@ -35,7 +35,7 @@ import java.util.List;
* Archive date repository.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.4, Jan 28, 2019
* @version 1.0.0.5, Sep 11, 2019
* @since 0.3.1
*/
@Repository
......@@ -102,10 +102,9 @@ public class ArchiveDateRepository extends AbstractRepository {
}
/**
* Gets archive dates.
* Get archive dates.
*
* @return a list of archive date, returns an empty list if
* not found
* @return a list of archive date, returns an empty list if not found
* @throws RepositoryException repository exception
*/
public List<JSONObject> getArchiveDates() throws RepositoryException {
......@@ -114,8 +113,8 @@ public class ArchiveDateRepository extends AbstractRepository {
final List<JSONObject> ret = getList(query);
for (final JSONObject archiveDate : ret) {
final String archiveDateId = archiveDate.optString(Keys.OBJECT_ID);
final int articleCount = archiveDateArticleRepository.getArticleCount(archiveDateId);
archiveDate.put(ArchiveDate.ARCHIVE_DATE_T_PUBLISHED_ARTICLE_COUNT, articleCount);
final int publishedArticleCount = archiveDateArticleRepository.getPublishedArticleCount(archiveDateId);
archiveDate.put(ArchiveDate.ARCHIVE_DATE_T_PUBLISHED_ARTICLE_COUNT, publishedArticleCount);
}
return ret;
......
......@@ -35,7 +35,7 @@ import java.util.List;
* Archive date query service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.0, Jan 28, 2019
* @version 1.1.0.1, Sep 11, 2019
* @since 0.4.0
*/
@Service
......@@ -59,13 +59,13 @@ public class ArchiveDateQueryService {
private ArchiveDateArticleRepository archiveDateArticleRepository;
/**
* Gets article count of an archive date specified by the given archive date id.
* Gets published article count of an archive date specified by the given archive date id.
*
* @param archiveDateId the given archive date id
* @return article count, returns {@code -1} if occurred an exception
* @return published article count, returns {@code -1} if occurred an exception
*/
public int getArchiveDateArticleCount(final String archiveDateId) {
return archiveDateArticleRepository.getArticleCount(archiveDateId);
public int getArchiveDatePublishedArticleCount(final String archiveDateId) {
return archiveDateArticleRepository.getPublishedArticleCount(archiveDateId);
}
/**
......@@ -108,7 +108,7 @@ public class ArchiveDateQueryService {
return null;
}
final int articleCount = archiveDateArticleRepository.getArticleCount(archiveDate.optString(Keys.OBJECT_ID));
final int articleCount = archiveDateArticleRepository.getPublishedArticleCount(archiveDate.optString(Keys.OBJECT_ID));
archiveDate.put(ArchiveDate.ARCHIVE_DATE_T_PUBLISHED_ARTICLE_COUNT, articleCount);
ret.put(ArchiveDate.ARCHIVE_DATE, archiveDate);
......
......@@ -52,7 +52,7 @@ import static org.b3log.solo.model.Article.*;
* Article management service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.3.2.0, Sep 1, 2019
* @version 1.3.2.1, Sep 11, 2019
* @since 0.3.5
*/
@Service
......@@ -435,6 +435,8 @@ public class ArticleMgmtService {
processTagsForArticleUpdate(oldArticle, article);
archiveDate(article);
if (!oldArticle.getString(Article.ARTICLE_PERMALINK).equals(permalink)) { // The permalink has been updated
// Updates related comments' links
processCommentsForArticleUpdate(article);
......@@ -689,8 +691,8 @@ public class ArticleMgmtService {
try {
final JSONObject archiveDateArticleRelation = archiveDateArticleRepository.getByArticleId(articleId);
final String archiveDateId = archiveDateArticleRelation.getString(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID);
final int articleCount = archiveDateArticleRepository.getArticleCount(archiveDateId);
if (1 > articleCount) {
final int publishedArticleCount = archiveDateArticleRepository.getPublishedArticleCount(archiveDateId);
if (1 > publishedArticleCount) {
archiveDateRepository.remove(archiveDateId);
}
......@@ -905,15 +907,17 @@ public class ArticleMgmtService {
*
* @param article the specified article, for example,
* {
* ....,
* "oId": "",
* "articleCreateDate": java.util.Date,
* ....
* }
* @throws RepositoryException repository exception
*/
private void archiveDate(final JSONObject article) throws RepositoryException {
final long created = article.optLong(Article.ARTICLE_CREATED);
if (Article.ARTICLE_STATUS_C_PUBLISHED != article.optInt(ARTICLE_STATUS)) {
return;
}
final long created = article.optLong(Keys.OBJECT_ID);
final String createDateString = DateFormatUtils.format(created, "yyyy/MM");
JSONObject archiveDate = archiveDateRepository.getByArchiveDate(createDateString);
if (null == archiveDate) {
......
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