content: /*
   * Solo - A small and beautiful blogging system written in Java.
   * Copyright (c) 2010-present, b3log.org
   *
   * Solo is licensed under Mulan PSL v2.
   * You can use this software according to the terms and conditions of the Mulan PSL v2.
   * You may obtain a copy of Mulan PSL v2 at:
   *         http://license.coscl.org.cn/MulanPSL2
   * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
   * See the Mulan PSL v2 for more details.
   */
  package org.b3log.solo.service;
  
  import org.apache.logging.log4j.Level;
  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
  import org.b3log.latke.Keys;
  import org.b3log.latke.ioc.Inject;
  import org.b3log.latke.repository.Transaction;
  import org.b3log.latke.service.ServiceException;
  import org.b3log.latke.service.annotation.Service;
  import org.b3log.solo.repository.CategoryTagRepository;
  import org.b3log.solo.repository.TagArticleRepository;
  import org.b3log.solo.repository.TagRepository;
  import org.json.JSONObject;
  
  import java.util.List;
  
  /**
   * Tag management service.
   *
   * @author <a href="http://88250.b3log.org">Liang Ding</a>
   * @version 1.0.0.3, Jan 28, 2019
   * @since 0.4.0
   */
  @Service
  public class TagMgmtService {
  
      /**
       * Logger.
       */
      private static final Logger LOGGER = LogManager.getLogger(TagMgmtService.class);
  
      /**
       * Tag query service.
       */
      @Inject
      private TagQueryService tagQueryService;
  
      /**
       * Tag repository.
       */
      @Inject
      private TagRepository tagRepository;
  
      /**
       * Category-tag repository.
       */
      @Inject
      private CategoryTagRepository categoryTagRepository;
  
      /**
       * Tag-Article repository.
       */
      @Inject
      private TagArticleRepository tagArticleRepository;
  
      /**
       * Removes all unused tags.
       *
       * @throws ServiceException if get tags failed, or remove failed
       */
      public void removeUnusedTags() throws ServiceException {
          final Transaction transaction = tagRepository.beginTransaction();
  
          try {
              final List<JSONObject> tags = tagQueryService.getTags();
              for (int i = 0; i < tags.size(); i++) {
                  final JSONObject tag = tags.get(i);
                  final String tagId = tag.optString(Keys.OBJECT_ID);
                  final int articleCount = tagArticleRepository.getArticleCount(tagId);
                  if (1 > articleCount) {
                      categoryTagRepository.removeByTagId(tagId);
                      tagRepository.remove(tagId);
                  }
              }
  
              transaction.commit();
          } catch (final Exception e) {
              if (transaction.isActive()) {
                  transaction.rollback();
              }
  
              LOGGER.log(Level.ERROR, "Removes unused tags failed", e);
  
              throw new ServiceException(e);
          }
      }
  }
  