Commit 58e80c3b authored by Liang Ding's avatar Liang Ding

#12633

parent eb69e76b
...@@ -40,10 +40,11 @@ public final class ArchiveDate { ...@@ -40,10 +40,11 @@ public final class ArchiveDate {
*/ */
public static final String ARCHIVE_TIME = "archiveTime"; public static final String ARCHIVE_TIME = "archiveTime";
//// Transient ////
/** /**
* Key of archive date article count. * Key of archive date article count.
*/ */
public static final String ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT = "archiveDatePublishedArticleCount"; public static final String ARCHIVE_DATE_T_PUBLISHED_ARTICLE_COUNT = "archiveDatePublishedArticleCount";
/** /**
* Archive date year. * Archive date year.
......
...@@ -455,7 +455,7 @@ public class ArticleProcessor { ...@@ -455,7 +455,7 @@ public class ArticleProcessor {
final JSONObject archiveDate = archiveQueryResult.getJSONObject(ArchiveDate.ARCHIVE_DATE); final JSONObject archiveDate = archiveQueryResult.getJSONObject(ArchiveDate.ARCHIVE_DATE);
final String archiveDateId = archiveDate.getString(Keys.OBJECT_ID); final String archiveDateId = archiveDate.getString(Keys.OBJECT_ID);
final int articleCount = archiveDate.getInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT); final int articleCount = archiveDateQueryService.getArchiveDateArticleCount(archiveDateId);
final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize); final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize);
final List<JSONObject> articles = articleQueryService.getArticlesByArchiveDate(archiveDateId, currentPageNum, pageSize); final List<JSONObject> articles = articleQueryService.getArticlesByArchiveDate(archiveDateId, currentPageNum, pageSize);
...@@ -616,7 +616,7 @@ public class ArticleProcessor { ...@@ -616,7 +616,7 @@ public class ArticleProcessor {
final JSONObject preference = preferenceQueryService.getPreference(); final JSONObject preference = preferenceQueryService.getPreference();
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 articleCount = archiveDate.getInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT); final int articleCount = archiveDateQueryService.getArchiveDateArticleCount(archiveDateId);
final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize); final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize);
final List<JSONObject> articles = articleQueryService.getArticlesByArchiveDate(archiveDateId, currentPageNum, pageSize); final List<JSONObject> articles = articleQueryService.getArticlesByArchiveDate(archiveDateId, currentPageNum, pageSize);
...@@ -810,7 +810,6 @@ public class ArticleProcessor { ...@@ -810,7 +810,6 @@ public class ArticleProcessor {
* @param archiveDateString the specified archive data string * @param archiveDateString the specified archive data string
* @param archiveDate the specified archive date * @param archiveDate the specified archive date
* @return page title for caching * @return page title for caching
* @throws Exception exception
*/ */
private String prepareShowArchiveArticles(final JSONObject preference, private String prepareShowArchiveArticles(final JSONObject preference,
final Map<String, Object> dataModel, final Map<String, Object> dataModel,
...@@ -818,7 +817,7 @@ public class ArticleProcessor { ...@@ -818,7 +817,7 @@ public class ArticleProcessor {
final int currentPageNum, final int currentPageNum,
final int pageCount, final int pageCount,
final String archiveDateString, final String archiveDateString,
final JSONObject archiveDate) throws Exception { final JSONObject archiveDate) {
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 int windowSize = preference.getInt(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE);
......
...@@ -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.ArchiveDate; import org.b3log.solo.model.ArchiveDate;
...@@ -35,6 +37,11 @@ import org.json.JSONObject; ...@@ -35,6 +37,11 @@ import org.json.JSONObject;
@Repository @Repository
public class ArchiveDateArticleRepository extends AbstractRepository { public class ArchiveDateArticleRepository extends AbstractRepository {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(ArchiveDateArticleRepository.class);
/** /**
* Public constructor. * Public constructor.
*/ */
...@@ -42,6 +49,24 @@ public class ArchiveDateArticleRepository extends AbstractRepository { ...@@ -42,6 +49,24 @@ public class ArchiveDateArticleRepository extends AbstractRepository {
super((ArchiveDate.ARCHIVE_DATE + "_" + Article.ARTICLE).toLowerCase()); super((ArchiveDate.ARCHIVE_DATE + "_" + Article.ARTICLE).toLowerCase());
} }
/**
* Gets 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
*/
public int getArticleCount(final String archiveDateId) {
final Query query = new Query().setFilter(new PropertyFilter(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID, FilterOperator.EQUAL, archiveDateId));
try {
return (int) count(query);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets archivedate [" + archiveDateId + "]'s article count failed", e);
return -1;
}
}
/** /**
* Gets archive date-article relations by the specified archive date id. * Gets archive date-article relations by the specified archive date id.
* *
......
...@@ -28,14 +28,13 @@ import org.json.JSONArray; ...@@ -28,14 +28,13 @@ import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.text.ParseException; import java.text.ParseException;
import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
* Archive date repository. * Archive date 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, Sep 30, 2018 * @version 1.0.0.4, Jan 28, 2019
* @since 0.3.1 * @since 0.3.1
*/ */
@Repository @Repository
...@@ -105,25 +104,7 @@ public class ArchiveDateRepository extends AbstractRepository { ...@@ -105,25 +104,7 @@ public class ArchiveDateRepository extends AbstractRepository {
*/ */
public List<JSONObject> getArchiveDates() throws RepositoryException { public List<JSONObject> getArchiveDates() throws RepositoryException {
final Query query = new Query().addSort(ArchiveDate.ARCHIVE_TIME, SortDirection.DESCENDING).setPageCount(1); final Query query = new Query().addSort(ArchiveDate.ARCHIVE_TIME, SortDirection.DESCENDING).setPageCount(1);
final List<JSONObject> ret = getList(query);
removeForUnpublishedArticles(ret);
return ret; return getList(query);
}
/**
* Removes archive dates of unpublished articles from the specified archive
* dates.
*
* @param archiveDates the specified archive dates
*/
private void removeForUnpublishedArticles(final List<JSONObject> archiveDates) {
final Iterator<JSONObject> iterator = archiveDates.iterator();
while (iterator.hasNext()) {
final JSONObject archiveDate = iterator.next();
if (0 == archiveDate.optInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT)) {
iterator.remove();
}
}
} }
} }
...@@ -24,6 +24,7 @@ import org.b3log.latke.repository.RepositoryException; ...@@ -24,6 +24,7 @@ import org.b3log.latke.repository.RepositoryException;
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.ArchiveDate; import org.b3log.solo.model.ArchiveDate;
import org.b3log.solo.repository.ArchiveDateArticleRepository;
import org.b3log.solo.repository.ArchiveDateRepository; import org.b3log.solo.repository.ArchiveDateRepository;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -33,7 +34,7 @@ import java.util.List; ...@@ -33,7 +34,7 @@ import java.util.List;
* Archive date query service. * Archive date query 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.0, Feb 7, 2012 * @version 1.1.0.0, Jan 28, 2019
* @since 0.4.0 * @since 0.4.0
*/ */
@Service @Service
...@@ -50,6 +51,22 @@ public class ArchiveDateQueryService { ...@@ -50,6 +51,22 @@ public class ArchiveDateQueryService {
@Inject @Inject
private ArchiveDateRepository archiveDateRepository; private ArchiveDateRepository archiveDateRepository;
/**
* Archive date-Article repository.
*/
@Inject
private ArchiveDateArticleRepository archiveDateArticleRepository;
/**
* Gets 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
*/
public int getArchiveDateArticleCount(final String archiveDateId) {
return archiveDateArticleRepository.getArticleCount(archiveDateId);
}
/** /**
* Gets all archive dates. * Gets all archive dates.
* *
......
...@@ -31,7 +31,6 @@ import org.b3log.latke.repository.Transaction; ...@@ -31,7 +31,6 @@ import org.b3log.latke.repository.Transaction;
import org.b3log.latke.service.LangPropsService; import org.b3log.latke.service.LangPropsService;
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.latke.util.CollectionUtils;
import org.b3log.latke.util.Ids; import org.b3log.latke.util.Ids;
import org.b3log.solo.event.EventTypes; import org.b3log.solo.event.EventTypes;
import org.b3log.solo.model.*; import org.b3log.solo.model.*;
...@@ -196,11 +195,8 @@ public class ArticleMgmtService { ...@@ -196,11 +195,8 @@ public class ArticleMgmtService {
try { try {
final JSONObject article = articleRepository.get(articleId); final JSONObject article = articleRepository.get(articleId);
article.put(ARTICLE_IS_PUBLISHED, false); article.put(ARTICLE_IS_PUBLISHED, false);
tagMgmtService.decTagPublishedRefCount(articleId); tagMgmtService.decTagPublishedRefCount(articleId);
decArchiveDatePublishedRefCount(articleId);
articleRepository.update(articleId, article); articleRepository.update(articleId, article);
transaction.commit(); transaction.commit();
...@@ -329,10 +325,6 @@ public class ArticleMgmtService { ...@@ -329,10 +325,6 @@ public class ArticleMgmtService {
final boolean publishNewArticle = !oldArticle.getBoolean(ARTICLE_IS_PUBLISHED) && article.getBoolean(ARTICLE_IS_PUBLISHED); final boolean publishNewArticle = !oldArticle.getBoolean(ARTICLE_IS_PUBLISHED) && article.getBoolean(ARTICLE_IS_PUBLISHED);
if (publishNewArticle) {
incArchiveDatePublishedRefCount(articleId);
}
// Update // Update
final boolean postToCommunity = article.optBoolean(Common.POST_TO_COMMUNITY, true); final boolean postToCommunity = article.optBoolean(Common.POST_TO_COMMUNITY, true);
article.remove(Common.POST_TO_COMMUNITY); // Do not persist this property article.remove(Common.POST_TO_COMMUNITY); // Do not persist this property
...@@ -642,21 +634,9 @@ public class ArticleMgmtService { ...@@ -642,21 +634,9 @@ public class ArticleMgmtService {
try { try {
final JSONObject archiveDateArticleRelation = archiveDateArticleRepository.getByArticleId(articleId); final JSONObject archiveDateArticleRelation = archiveDateArticleRepository.getByArticleId(articleId);
final String archiveDateId = archiveDateArticleRelation.getString(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID); final String archiveDateId = archiveDateArticleRelation.getString(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID);
final JSONObject archiveDate = archiveDateRepository.get(archiveDateId); final int articleCount = archiveDateArticleRepository.getArticleCount(archiveDateId);
if (1 > articleCount) {
int archiveDatePublishedArticleCnt = archiveDate.getInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT);
final JSONObject article = articleRepository.get(articleId);
if (article.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
--archiveDatePublishedArticleCnt;
}
if (0 == archiveDatePublishedArticleCnt) {
archiveDateRepository.remove(archiveDateId); archiveDateRepository.remove(archiveDateId);
} else {
final JSONObject newArchiveDate = new JSONObject(archiveDate, CollectionUtils.jsonArrayToArray(archiveDate.names(), String[].class));
newArchiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, archiveDatePublishedArticleCnt);
archiveDateRepository.update(archiveDateId, newArchiveDate);
} }
archiveDateArticleRepository.remove(archiveDateArticleRelation.getString(Keys.OBJECT_ID)); archiveDateArticleRepository.remove(archiveDateArticleRelation.getString(Keys.OBJECT_ID));
...@@ -934,7 +914,6 @@ public class ArticleMgmtService { ...@@ -934,7 +914,6 @@ public class ArticleMgmtService {
archiveDate = new JSONObject(); archiveDate = new JSONObject();
try { try {
archiveDate.put(ArchiveDate.ARCHIVE_TIME, DateUtils.parseDate(createDateString, new String[]{"yyyy/MM"}).getTime()); archiveDate.put(ArchiveDate.ARCHIVE_TIME, DateUtils.parseDate(createDateString, new String[]{"yyyy/MM"}).getTime());
archiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, 0);
archiveDateRepository.add(archiveDate); archiveDateRepository.add(archiveDate);
} catch (final ParseException e) { } catch (final ParseException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
...@@ -942,12 +921,6 @@ public class ArticleMgmtService { ...@@ -942,12 +921,6 @@ public class ArticleMgmtService {
} }
} }
final JSONObject newArchiveDate = new JSONObject(archiveDate, CollectionUtils.jsonArrayToArray(archiveDate.names(), String[].class));
if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
newArchiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, archiveDate.optInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT) + 1);
}
archiveDateRepository.update(archiveDate.optString(Keys.OBJECT_ID), newArchiveDate);
final JSONObject archiveDateArticleRelation = new JSONObject(); final JSONObject archiveDateArticleRelation = new JSONObject();
archiveDateArticleRelation.put(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID, archiveDate.optString(Keys.OBJECT_ID)); archiveDateArticleRelation.put(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID, archiveDate.optString(Keys.OBJECT_ID));
archiveDateArticleRelation.put(Article.ARTICLE + "_" + Keys.OBJECT_ID, article.optString(Keys.OBJECT_ID)); archiveDateArticleRelation.put(Article.ARTICLE + "_" + Keys.OBJECT_ID, article.optString(Keys.OBJECT_ID));
...@@ -1046,42 +1019,6 @@ public class ArticleMgmtService { ...@@ -1046,42 +1019,6 @@ public class ArticleMgmtService {
return ret.replaceAll(" ", "-"); return ret.replaceAll(" ", "-");
} }
/**
* Decrements reference count of archive date of an published article specified by the given article id.
*
* @param articleId the given article id
* @throws JSONException json exception
* @throws RepositoryException repository exception
*/
private void decArchiveDatePublishedRefCount(final String articleId)
throws JSONException, RepositoryException {
final JSONObject archiveDateArticleRelation = archiveDateArticleRepository.getByArticleId(articleId);
final String archiveDateId = archiveDateArticleRelation.getString(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID);
final JSONObject archiveDate = archiveDateRepository.get(archiveDateId);
archiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT,
archiveDate.getInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT) - 1);
archiveDateRepository.update(archiveDateId, archiveDate);
}
/**
* Increments reference count of archive date of an published article specified by the given article id.
*
* @param articleId the given article id
* @throws JSONException json exception
* @throws RepositoryException repository exception
*/
private void incArchiveDatePublishedRefCount(final String articleId)
throws JSONException, RepositoryException {
final JSONObject archiveDateArticleRelation = archiveDateArticleRepository.getByArticleId(articleId);
final String archiveDateId = archiveDateArticleRelation.getString(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID);
final JSONObject archiveDate = archiveDateRepository.get(archiveDateId);
archiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT,
archiveDate.getInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT) + 1);
archiveDateRepository.update(archiveDateId, archiveDate);
}
/** /**
* Sets archive date article repository with the specified archive date article repository. * Sets archive date article repository with the specified archive date article repository.
* *
......
...@@ -336,7 +336,6 @@ public class InitService { ...@@ -336,7 +336,6 @@ public class InitService {
try { try {
archiveDate.put(ArchiveDate.ARCHIVE_TIME, DateUtils.parseDate(createDateString, new String[]{"yyyy/MM"}).getTime()); archiveDate.put(ArchiveDate.ARCHIVE_TIME, DateUtils.parseDate(createDateString, new String[]{"yyyy/MM"}).getTime());
archiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, 1);
archiveDateRepository.add(archiveDate); archiveDateRepository.add(archiveDate);
} catch (final ParseException e) { } catch (final ParseException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
......
...@@ -47,7 +47,6 @@ public class ArchiveDateRepositoryImplTestCase extends AbstractTestCase { ...@@ -47,7 +47,6 @@ public class ArchiveDateRepositoryImplTestCase extends AbstractTestCase {
final JSONObject archiveDate = new JSONObject(); final JSONObject archiveDate = new JSONObject();
archiveDate.put(ArchiveDate.ARCHIVE_TIME, DateUtils.parseDate("2011/12", new String[]{"yyyy/MM"}).getTime()); archiveDate.put(ArchiveDate.ARCHIVE_TIME, DateUtils.parseDate("2011/12", new String[]{"yyyy/MM"}).getTime());
archiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, 1);
final Transaction transaction = archiveDateRepository.beginTransaction(); final Transaction transaction = archiveDateRepository.beginTransaction();
archiveDateRepository.add(archiveDate); archiveDateRepository.add(archiveDate);
...@@ -69,7 +68,5 @@ public class ArchiveDateRepositoryImplTestCase extends AbstractTestCase { ...@@ -69,7 +68,5 @@ public class ArchiveDateRepositoryImplTestCase extends AbstractTestCase {
final JSONObject archiveDate = archiveDateRepository.getByArchiveDate("2011/12"); final JSONObject archiveDate = archiveDateRepository.getByArchiveDate("2011/12");
Assert.assertNotNull(archiveDate); Assert.assertNotNull(archiveDate);
Assert.assertEquals(archiveDate.optInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT), 1);
//System.out.println(archiveDate.toString(SoloServletListener.JSON_PRINT_INDENT_FACTOR));
} }
} }
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