/** * 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 int tagRefCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT); if (0 == tagRefCnt) { final String tagId = tag.getString(Keys.OBJECT_ID); tagRepository.remove(tagId); } } transaction.commit(); } catch (final Exception e) { if (transaction.isActive()) { transaction.rollback(); } LOGGER.log(Level.SEVERE, "Removes unused tags failed", e); throw new ServiceException(e); } }
/** * Tag management service. * * @author <a href="mailto:[email protected]">Liang Ding</a> * @version 1.0.0.1, Oct 26, 2011 * @since 0.4.0 */ public final class TagMgmtService { /** Logger. */ private static final Logger LOGGER = Logger.getLogger(TagMgmtService.class.getName()); /** Tag query service. */ private TagQueryService tagQueryService = TagQueryService.getInstance(); /** Tag repository. */ private TagRepository tagRepository = TagRepositoryImpl.getInstance(); /** * 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 int tagRefCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT); if (0 == tagRefCnt) { final String tagId = tag.getString(Keys.OBJECT_ID); tagRepository.remove(tagId); } } transaction.commit(); } catch (final Exception e) { if (transaction.isActive()) { transaction.rollback(); } LOGGER.log(Level.SEVERE, "Removes unused tags failed", e); throw new ServiceException(e); } } /** * Gets the {@link TagMgmtService} singleton. * * @return the singleton */ public static TagMgmtService getInstance() { return SingletonHolder.SINGLETON; } /** Private constructor. */ private TagMgmtService() {} /** * Singleton holder. * * @author <a href="mailto:[email protected]">Liang Ding</a> * @version 1.0.0.0, Oct 24, 2011 */ private static final class SingletonHolder { /** Singleton. */ private static final TagMgmtService SINGLETON = new TagMgmtService(); /** Private default constructor. */ private SingletonHolder() {} } }