Commit eca2c958 authored by Vanessa's avatar Vanessa

c

parent f4d629b5
/*
* 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.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.Keys;
import org.b3log.latke.Latkes;
import org.b3log.latke.plugin.AbstractPlugin;
import org.b3log.latke.plugin.PluginManager;
import org.b3log.latke.plugin.PluginStatus;
import org.b3log.latke.repository.Transaction;
import org.b3log.latke.service.LangPropsService;
import org.b3log.solo.repository.PluginRepository;
import org.b3log.solo.repository.impl.PluginRepositoryImpl;
import org.json.JSONObject;
/**
* Plugin management service.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Oct 27, 2011
* @since 0.4.0
*/
public final class PluginMgmtService {
/**
* Logger.
*/
private static final Logger LOGGER =
Logger.getLogger(PluginMgmtService.class.getName());
/**
* Plugin repository.
*/
private PluginRepository pluginRepository =
PluginRepositoryImpl.getInstance();
/**
* Language service.
*/
private LangPropsService langPropsService = LangPropsService.getInstance();
/**
* Sets a plugin's status with the specified plugin id, status.
*
* @param pluginId the specified plugin id
* @param status the specified status, see {@link PluginStatus}
* @return for example,
* <pre>
* {
* "sc": boolean,
* "msg": ""
* }
* </pre>
*/
public JSONObject setPluginStatus(final String pluginId, final String status) {
final Map<String, String> langs =
langPropsService.getAll(Latkes.getLocale());
final PluginManager pluginManager = PluginManager.getInstance();
final List<AbstractPlugin> plugins = pluginManager.getPlugins();
final JSONObject ret = new JSONObject();
for (final AbstractPlugin plugin : plugins) {
if (plugin.getId().equals(pluginId)) {
final Transaction transaction =
pluginRepository.beginTransaction();
try {
plugin.setStatus(PluginStatus.valueOf(status));
pluginRepository.update(pluginId, plugin.toJSONObject());
transaction.commit();
pluginManager.update(plugin);
ret.put(Keys.STATUS_CODE, true);
ret.put(Keys.MSG, langs.get("setSuccLabel"));
return ret;
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.SEVERE, "Set plugin status error", e);
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langs.get("setFailLabel"));
return ret;
}
}
}
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langs.get("refreshAndRetryLabel"));
return ret;
}
/**
* Gets the {@link PluginMgmtService} singleton.
*
* @return the singleton
*/
public static PluginMgmtService getInstance() {
return SingletonHolder.SINGLETON;
}
/**
* Private constructor.
*/
private PluginMgmtService() {
}
/**
* Singleton holder.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Oct 27, 2011
*/
private static final class SingletonHolder {
/**
* Singleton.
*/
private static final PluginMgmtService SINGLETON =
new PluginMgmtService();
/**
* Private default constructor.
*/
private SingletonHolder() {
}
}
}
/*
* 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.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.Keys;
import org.b3log.latke.Latkes;
import org.b3log.latke.plugin.AbstractPlugin;
import org.b3log.latke.plugin.PluginManager;
import org.b3log.latke.plugin.PluginStatus;
import org.b3log.latke.repository.Transaction;
import org.b3log.latke.service.LangPropsService;
import org.b3log.solo.repository.PluginRepository;
import org.b3log.solo.repository.impl.PluginRepositoryImpl;
import org.json.JSONObject;
/**
* Plugin management service.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Oct 27, 2011
* @since 0.4.0
*/
public final class PluginMgmtService {
/**
* Logger.
*/
private static final Logger LOGGER =
Logger.getLogger(PluginMgmtService.class.getName());
/**
* Plugin repository.
*/
private PluginRepository pluginRepository =
PluginRepositoryImpl.getInstance();
/**
* Language service.
*/
private LangPropsService langPropsService = LangPropsService.getInstance();
/**
* Sets a plugin's status with the specified plugin id, status.
*
* @param pluginId the specified plugin id
* @param status the specified status, see {@link PluginStatus}
* @return for example,
* <pre>
* {
* "sc": boolean,
* "msg": ""
* }
* </pre>
*/
public JSONObject setPluginStatus(final String pluginId, final String status) {
final Map<String, String> langs =
langPropsService.getAll(Latkes.getLocale());
final PluginManager pluginManager = PluginManager.getInstance();
final List<AbstractPlugin> plugins = pluginManager.getPlugins();
final JSONObject ret = new JSONObject();
for (final AbstractPlugin plugin : plugins) {
if (plugin.getId().equals(pluginId)) {
final Transaction transaction =
pluginRepository.beginTransaction();
try {
plugin.setStatus(PluginStatus.valueOf(status));
pluginRepository.update(pluginId, plugin.toJSONObject());
transaction.commit();
pluginManager.update(plugin);
ret.put(Keys.STATUS_CODE, true);
ret.put(Keys.MSG, langs.get("setSuccLabel"));
return ret;
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.SEVERE, "Set plugin status error", e);
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langs.get("setFailLabel"));
return ret;
}
}
}
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langs.get("refreshAndRetryLabel"));
return ret;
}
/**
* Gets the {@link PluginMgmtService} singleton.
*
* @return the singleton
*/
public static PluginMgmtService getInstance() {
return SingletonHolder.SINGLETON;
}
/**
* Private constructor.
*/
private PluginMgmtService() {
}
/**
* Singleton holder.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Oct 27, 2011
*/
private static final class SingletonHolder {
/**
* Singleton.
*/
private static final PluginMgmtService SINGLETON =
new PluginMgmtService();
/**
* Private default constructor.
*/
private SingletonHolder() {
}
}
}
/*
* 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.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.service.ServiceException;
import org.b3log.solo.model.Preference;
import org.b3log.solo.repository.PreferenceRepository;
import org.b3log.solo.repository.impl.PreferenceRepositoryImpl;
import org.json.JSONObject;
/**
* Preference query service.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.1, Oct 31, 2011
* @since 0.4.0
*/
public final class PreferenceQueryService {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(PreferenceQueryService.class.getName());
/**
* Preference repository.
*/
private PreferenceRepository preferenceRepository = PreferenceRepositoryImpl.getInstance();
/**
* Gets the reply notification template.
*
* @return reply notification template, returns {@code null} if not found
* @throws ServiceException service exception
*/
public JSONObject getReplyNotificationTemplate() throws ServiceException {
try {
return preferenceRepository.get(Preference.REPLY_NOTIFICATION_TEMPLATE);
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, "Updates reply notification template failed", e);
throw new ServiceException(e);
}
}
/**
* Gets the user preference.
*
* <p>
* <b>Note</b>: Invoking the method will not load skin.
* </p>
*
* @return user preference, returns {@code null} if not found
* @throws ServiceException if repository exception
*/
public JSONObject getPreference() throws ServiceException {
try {
final JSONObject ret = preferenceRepository.get(Preference.PREFERENCE);
if (null == ret) {
LOGGER.log(Level.WARNING, "Can not load preference from datastore");
return null;
}
return ret;
} catch (final RepositoryException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new IllegalStateException(e);
}
}
/**
* Gets the {@link PreferenceQueryService} singleton.
*
* @return the singleton
*/
public static PreferenceQueryService getInstance() {
return SingletonHolder.SINGLETON;
}
/**
* Private constructor.
*/
private PreferenceQueryService() {
}
/**
* 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 PreferenceQueryService SINGLETON =
new PreferenceQueryService();
/**
* Private default constructor.
*/
private SingletonHolder() {
}
}
}
/*
* 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.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.service.ServiceException;
import org.b3log.solo.model.Preference;
import org.b3log.solo.repository.PreferenceRepository;
import org.b3log.solo.repository.impl.PreferenceRepositoryImpl;
import org.json.JSONObject;
/**
* Preference query service.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.1, Oct 31, 2011
* @since 0.4.0
*/
public final class PreferenceQueryService {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(PreferenceQueryService.class.getName());
/**
* Preference repository.
*/
private PreferenceRepository preferenceRepository = PreferenceRepositoryImpl.getInstance();
/**
* Gets the reply notification template.
*
* @return reply notification template, returns {@code null} if not found
* @throws ServiceException service exception
*/
public JSONObject getReplyNotificationTemplate() throws ServiceException {
try {
return preferenceRepository.get(Preference.REPLY_NOTIFICATION_TEMPLATE);
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, "Updates reply notification template failed", e);
throw new ServiceException(e);
}
}
/**
* Gets the user preference.
*
* <p>
* <b>Note</b>: Invoking the method will not load skin.
* </p>
*
* @return user preference, returns {@code null} if not found
* @throws ServiceException if repository exception
*/
public JSONObject getPreference() throws ServiceException {
try {
final JSONObject ret = preferenceRepository.get(Preference.PREFERENCE);
if (null == ret) {
LOGGER.log(Level.WARNING, "Can not load preference from datastore");
return null;
}
return ret;
} catch (final RepositoryException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new IllegalStateException(e);
}
}
/**
* Gets the {@link PreferenceQueryService} singleton.
*
* @return the singleton
*/
public static PreferenceQueryService getInstance() {
return SingletonHolder.SINGLETON;
}
/**
* Private constructor.
*/
private PreferenceQueryService() {
}
/**
* 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 PreferenceQueryService SINGLETON =
new PreferenceQueryService();
/**
* Private default constructor.
*/
private SingletonHolder() {
}
}
}
/*
* 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() {
}
}
}
/*
* 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() {
}
}
}
/*
* 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.Query;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.util.CollectionUtils;
import org.b3log.solo.model.Tag;
import org.b3log.solo.repository.TagRepository;
import org.b3log.solo.repository.impl.TagRepositoryImpl;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Tag query service.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.2, Nov 11, 2011
* @since 0.4.0
*/
public final class TagQueryService {
/**
* Logger.
*/
private static final Logger LOGGER =
Logger.getLogger(TagQueryService.class.getName());
/**
* Tag repository.
*/
private TagRepository tagRepository = TagRepositoryImpl.getInstance();
/**
* Gets a tag by the specified tag title.
*
* @param tagTitle the specified tag title
* @return for example,
* <pre>
* {
* "tag": {
* "oId": "",
* "tagTitle": "",
* "tagReferenceCount": int,
* "tagPublishedRefCount": int
* }
* }
* </pre>, returns {@code null} if not found
* @throws ServiceException service exception
*/
public JSONObject getTagByTitle(final String tagTitle)
throws ServiceException {
try {
final JSONObject ret = new JSONObject();
final JSONObject tag = tagRepository.getByTitle(tagTitle);
if (null == tag) {
return null;
}
ret.put(Tag.TAG, tag);
LOGGER.log(Level.FINER, "Got an tag[title={0}]", tagTitle);
return ret;
} catch (final RepositoryException e) {
LOGGER.log(Level.SEVERE, "Gets an article failed", e);
throw new ServiceException(e);
}
}
/**
* Gets all tags.
*
* @return for example,
* <pre>
* [
* {"tagTitle": "", "tagReferenceCount": int, ....},
* ....
* ]
* </pre>, returns an empty list if not found
* @throws ServiceException service exception
*/
public List<JSONObject> getTags() throws ServiceException {
try {
final Query query = new Query().setPageCount(1);
final JSONObject result = tagRepository.get(query);
final JSONArray tagArray = result.optJSONArray(Keys.RESULTS);
return CollectionUtils.jsonArrayToList(tagArray);
} catch (final RepositoryException e) {
LOGGER.log(Level.SEVERE, "Gets tags failed", e);
throw new ServiceException(e);
}
}
/**
* Gets the {@link TagQueryService} singleton.
*
* @return the singleton
*/
public static TagQueryService getInstance() {
return SingletonHolder.SINGLETON;
}
/**
* Private constructor.
*/
private TagQueryService() {
}
/**
* 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 TagQueryService SINGLETON =
new TagQueryService();
/**
* Private default constructor.
*/
private SingletonHolder() {
}
}
}
/*
* 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.Query;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.util.CollectionUtils;
import org.b3log.solo.model.Tag;
import org.b3log.solo.repository.TagRepository;
import org.b3log.solo.repository.impl.TagRepositoryImpl;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Tag query service.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.2, Nov 11, 2011
* @since 0.4.0
*/
public final class TagQueryService {
/**
* Logger.
*/
private static final Logger LOGGER =
Logger.getLogger(TagQueryService.class.getName());
/**
* Tag repository.
*/
private TagRepository tagRepository = TagRepositoryImpl.getInstance();
/**
* Gets a tag by the specified tag title.
*
* @param tagTitle the specified tag title
* @return for example,
* <pre>
* {
* "tag": {
* "oId": "",
* "tagTitle": "",
* "tagReferenceCount": int,
* "tagPublishedRefCount": int
* }
* }
* </pre>, returns {@code null} if not found
* @throws ServiceException service exception
*/
public JSONObject getTagByTitle(final String tagTitle)
throws ServiceException {
try {
final JSONObject ret = new JSONObject();
final JSONObject tag = tagRepository.getByTitle(tagTitle);
if (null == tag) {
return null;
}
ret.put(Tag.TAG, tag);
LOGGER.log(Level.FINER, "Got an tag[title={0}]", tagTitle);
return ret;
} catch (final RepositoryException e) {
LOGGER.log(Level.SEVERE, "Gets an article failed", e);
throw new ServiceException(e);
}
}
/**
* Gets all tags.
*
* @return for example,
* <pre>
* [
* {"tagTitle": "", "tagReferenceCount": int, ....},
* ....
* ]
* </pre>, returns an empty list if not found
* @throws ServiceException service exception
*/
public List<JSONObject> getTags() throws ServiceException {
try {
final Query query = new Query().setPageCount(1);
final JSONObject result = tagRepository.get(query);
final JSONArray tagArray = result.optJSONArray(Keys.RESULTS);
return CollectionUtils.jsonArrayToList(tagArray);
} catch (final RepositoryException e) {
LOGGER.log(Level.SEVERE, "Gets tags failed", e);
throw new ServiceException(e);
}
}
/**
* Gets the {@link TagQueryService} singleton.
*
* @return the singleton
*/
public static TagQueryService getInstance() {
return SingletonHolder.SINGLETON;
}
/**
* Private constructor.
*/
private TagQueryService() {
}
/**
* 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 TagQueryService SINGLETON =
new TagQueryService();
/**
* Private default constructor.
*/
private SingletonHolder() {
}
}
}
/*
* 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.util;
import java.io.StringReader;
import java.io.StringWriter;
import org.b3log.latke.util.Strings;
import org.tautua.markdownpapers.Markdown;
/**
* <a href="http://en.wikipedia.org/wiki/Markdown">Markdown</a> utilities.
*
* <p>Uses the <a href="http://markdown.tautua.org/">MarkdownPapers</a> as the converter.</p>
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Apr 28, 2012
* @since 0.4.5
*/
public final class Markdowns {
/**
* Converts the specified markdown text to HTML.
*
* @param markdownText the specified markdown text
* @return converted HTML, returns {@code null} if the specified markdown text is "" or {@code null}
* @throws Exception exception
*/
public static String toHTML(final String markdownText) throws Exception {
if (Strings.isEmptyOrNull(markdownText)) {
return null;
}
final StringWriter writer = new StringWriter();
final Markdown markdown = new Markdown();
markdown.transform(new StringReader(markdownText), writer);
return writer.toString();
}
/**
* Private constructor.
*/
private Markdowns() {
}
}
/*
* 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.util;
import java.io.StringReader;
import java.io.StringWriter;
import org.b3log.latke.util.Strings;
import org.tautua.markdownpapers.Markdown;
/**
* <a href="http://en.wikipedia.org/wiki/Markdown">Markdown</a> utilities.
*
* <p>Uses the <a href="http://markdown.tautua.org/">MarkdownPapers</a> as the converter.</p>
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Apr 28, 2012
* @since 0.4.5
*/
public final class Markdowns {
/**
* Converts the specified markdown text to HTML.
*
* @param markdownText the specified markdown text
* @return converted HTML, returns {@code null} if the specified markdown text is "" or {@code null}
* @throws Exception exception
*/
public static String toHTML(final String markdownText) throws Exception {
if (Strings.isEmptyOrNull(markdownText)) {
return null;
}
final StringWriter writer = new StringWriter();
final Markdown markdown = new Markdown();
markdown.transform(new StringReader(markdownText), writer);
return writer.toString();
}
/**
* Private constructor.
*/
private Markdowns() {
}
}
/*
* 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.util;
import org.b3log.latke.Keys;
import org.b3log.latke.model.Pagination;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Query result utilities.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.1, Oct 25, 2011
* @since 0.3.5
*/
public final class QueryResults {
/**
* Constructs a default query result.
*
* @return a default query result,
* <pre>
* {
* "sc": false
* }
* </pre>
*/
public static JSONObject defaultResult() {
return new JSONObject().put(Keys.STATUS_CODE, false);
}
/**
* Constructs a default query results.
*
* @return a default query results,
* <pre>
* {
* "pagination": {
* "paginationPageCount": 0
* },
* "rslts": []
* }
* </pre>
*/
public static JSONObject defaultResults() {
final JSONObject ret = new JSONObject();
final JSONObject pagination = new JSONObject();
ret.put(Pagination.PAGINATION, pagination);
pagination.put(Pagination.PAGINATION_PAGE_COUNT, 0);
final JSONArray results = new JSONArray();
ret.put(Keys.RESULTS, results);
return ret;
}
/**
* Private constructor.
*/
private QueryResults() {
}
}
/*
* 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.util;
import org.b3log.latke.Keys;
import org.b3log.latke.model.Pagination;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Query result utilities.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.1, Oct 25, 2011
* @since 0.3.5
*/
public final class QueryResults {
/**
* Constructs a default query result.
*
* @return a default query result,
* <pre>
* {
* "sc": false
* }
* </pre>
*/
public static JSONObject defaultResult() {
return new JSONObject().put(Keys.STATUS_CODE, false);
}
/**
* Constructs a default query results.
*
* @return a default query results,
* <pre>
* {
* "pagination": {
* "paginationPageCount": 0
* },
* "rslts": []
* }
* </pre>
*/
public static JSONObject defaultResults() {
final JSONObject ret = new JSONObject();
final JSONObject pagination = new JSONObject();
ret.put(Pagination.PAGINATION, pagination);
pagination.put(Pagination.PAGINATION_PAGE_COUNT, 0);
final JSONArray results = new JSONArray();
ret.put(Keys.RESULTS, results);
return ret;
}
/**
* Private constructor.
*/
private QueryResults() {
}
}
/**
* Utilities.
*/
package org.b3log.solo.util;
/**
* Utilities.
*/
package org.b3log.solo.util;
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment