* @param context the specified context
*/
@RequestProcessing(value = "/fix/tag-article-counter-repair.do", method = HTTPRequestMethod.GET)
@Transactional
public void repairTagArticleCounter(final HTTPRequestContext context) {
final TextHTMLRenderer renderer = new TextHTMLRenderer();
context.setRenderer(renderer);
try {
final JSONObject result = tagRepository.get(new Query());
final JSONArray tagArray = result.getJSONArray(Keys.RESULTS);
final List<JSONObject> tags = CollectionUtils.jsonArrayToList(tagArray);
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}]",
new Object[] {tag.getString(Tag.TAG_TITLE), tagRefCnt, publishedTagRefCnt});
}
renderer.setContent("Repair sucessfully!");
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
renderer.setContent("Repairs failed, error msg[" + e.getMessage() + "]");
}
}