*
* @param context the specified context
*/
@RequestProcessing(value = "/fix/tag-article-counter-repair.do", method = HTTPRequestMethod.GET)
public void repairTagArticleCounter(final HTTPRequestContext context) {
final TextHTMLRenderer renderer = new TextHTMLRenderer();
context.setRenderer(renderer);
final Transaction transaction = tagRepository.beginTransaction();
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);
final boolean isPublished = article.getBoolean(Article.ARTICLE_IS_PUBLISHED);
if (isPublished) {
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});
}
transaction.commit();
renderer.setContent("Repair sucessfully!");
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.SEVERE, e.getMessage(), e);
renderer.setContent("Repairs failed, error msg[" + e.getMessage() + "]");
}
}