/*
* Copyright (c) 2009, 2010, 2011, 2012, B3log Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.b3log.solo.service;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.Keys;
import org.b3log.latke.repository.Transaction;
import org.b3log.latke.service.ServiceException;
import org.b3log.solo.model.Tag;
import org.b3log.solo.repository.TagRepository;
import org.b3log.solo.repository.impl.TagRepositoryImpl;
import org.json.JSONObject;
/**
* Tag management service.
*
* @author <a href="mailto:DL88250@gmail.com">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:DL88250@gmail.com">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() {
}
}
}