Commit c79fbf5d authored by Liang Ding's avatar Liang Ding

c

parent 47aaabf8
This source diff could not be displayed because it is too large. You can view the blob instead.
/*
* 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.processor;
import org.b3log.latke.Latkes;
import org.b3log.latke.annotation.RequestProcessing;
import org.b3log.latke.annotation.RequestProcessor;
import org.b3log.latke.servlet.HTTPRequestContext;
import org.b3log.latke.servlet.HTTPRequestMethod;
import org.b3log.latke.servlet.renderer.JSONRenderer;
import org.b3log.solo.SoloServletListener;
import org.b3log.solo.model.Statistic;
import org.b3log.solo.repository.StatisticRepository;
import org.b3log.solo.repository.impl.StatisticRepositoryImpl;
import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.service.TagQueryService;
import org.b3log.solo.util.Articles;
import org.json.JSONObject;
/**
* Blog processor.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Jun 28, 2012
* @since 0.4.6
*/
@RequestProcessor
public final class BlogProcessor {
/**
* Article utilities.
*/
private Articles articleUtils = Articles.getInstance();
/**
* Tag query service.
*/
private TagQueryService tagQueryService = TagQueryService.getInstance();
/**
* Preference query service.
*/
private PreferenceQueryService preferenceQueryService = PreferenceQueryService.getInstance();
/**
* Statistic repository.
*/
private StatisticRepository statisticRepository = StatisticRepositoryImpl.getInstance();
/**
* Gets blog information.
*
* <ul>
* <li>Time of the recent updated article</li>
* <li>Article count</li>
* <li>Comment count</li>
* <li>Tag count</li>
* <li>Serve path</li>
* <li>Static serve path</li>
* <li>Solo version</li>
* <li>Runtime environment (GAE/LOCAL)</li>
* <li>Locale</li>
* </ul>
*
* @param context the specified context
* @throws Exception exception
*/
@RequestProcessing(value = "/blog/info", method = HTTPRequestMethod.GET)
public void getRecentArticleTime(final HTTPRequestContext context) throws Exception {
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
final JSONObject jsonObject = new JSONObject();
renderer.setJSONObject(jsonObject);
jsonObject.put("recentArticleTime", articleUtils.getRecentArticleTime());
final JSONObject statistic = statisticRepository.get(Statistic.STATISTIC);
jsonObject.put("articleCount", statistic.getLong(Statistic.STATISTIC_PUBLISHED_ARTICLE_COUNT));
jsonObject.put("commentCount", statistic.getLong(Statistic.STATISTIC_PUBLISHED_BLOG_COMMENT_COUNT));
jsonObject.put("tagCount", tagQueryService.getTagCount());
jsonObject.put("servePath", Latkes.getServePath());
jsonObject.put("staticServePath", Latkes.getStaticServePath());
jsonObject.put("version", SoloServletListener.VERSION);
jsonObject.put("runtimeEnv", Latkes.getRuntimeEnv());
jsonObject.put("locale", Latkes.getLocale());
}
}
/*
* 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.processor;
import org.b3log.latke.Latkes;
import org.b3log.latke.annotation.RequestProcessing;
import org.b3log.latke.annotation.RequestProcessor;
import org.b3log.latke.servlet.HTTPRequestContext;
import org.b3log.latke.servlet.HTTPRequestMethod;
import org.b3log.latke.servlet.renderer.JSONRenderer;
import org.b3log.solo.SoloServletListener;
import org.b3log.solo.model.Statistic;
import org.b3log.solo.repository.StatisticRepository;
import org.b3log.solo.repository.impl.StatisticRepositoryImpl;
import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.service.TagQueryService;
import org.b3log.solo.util.Articles;
import org.json.JSONObject;
/**
* Blog processor.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Jun 28, 2012
* @since 0.4.6
*/
@RequestProcessor
public final class BlogProcessor {
/**
* Article utilities.
*/
private Articles articleUtils = Articles.getInstance();
/**
* Tag query service.
*/
private TagQueryService tagQueryService = TagQueryService.getInstance();
/**
* Preference query service.
*/
private PreferenceQueryService preferenceQueryService = PreferenceQueryService.getInstance();
/**
* Statistic repository.
*/
private StatisticRepository statisticRepository = StatisticRepositoryImpl.getInstance();
/**
* Gets blog information.
*
* <ul>
* <li>Time of the recent updated article</li>
* <li>Article count</li>
* <li>Comment count</li>
* <li>Tag count</li>
* <li>Serve path</li>
* <li>Static serve path</li>
* <li>Solo version</li>
* <li>Runtime environment (GAE/LOCAL)</li>
* <li>Locale</li>
* </ul>
*
* @param context the specified context
* @throws Exception exception
*/
@RequestProcessing(value = "/blog/info", method = HTTPRequestMethod.GET)
public void getRecentArticleTime(final HTTPRequestContext context) throws Exception {
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
final JSONObject jsonObject = new JSONObject();
renderer.setJSONObject(jsonObject);
jsonObject.put("recentArticleTime", articleUtils.getRecentArticleTime());
final JSONObject statistic = statisticRepository.get(Statistic.STATISTIC);
jsonObject.put("articleCount", statistic.getLong(Statistic.STATISTIC_PUBLISHED_ARTICLE_COUNT));
jsonObject.put("commentCount", statistic.getLong(Statistic.STATISTIC_PUBLISHED_BLOG_COMMENT_COUNT));
jsonObject.put("tagCount", tagQueryService.getTagCount());
jsonObject.put("servePath", Latkes.getServePath());
jsonObject.put("staticServePath", Latkes.getStaticServePath());
jsonObject.put("version", SoloServletListener.VERSION);
jsonObject.put("runtimeEnv", Latkes.getRuntimeEnv());
jsonObject.put("locale", Latkes.getLocale());
}
}
/*
* 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.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 java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.Keys;
import org.b3log.latke.model.Plugin;
import org.b3log.latke.plugin.AbstractPlugin;
import org.b3log.latke.plugin.PluginStatus;
import org.b3log.latke.repository.Query;
import org.b3log.latke.util.CollectionUtils;
import org.b3log.solo.repository.impl.PluginRepositoryImpl;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Plugin utilities.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.3, Sep 11, 2011
*/
public final class Plugins {
/**
* Logger.
*/
private static final Logger LOGGER =
Logger.getLogger(Plugins.class.getName());
/**
* Plugin repository.
*/
private static final PluginRepositoryImpl PLUGIN_REPOS =
PluginRepositoryImpl.getInstance();
/**
* Updates datastore plugin descriptions with the specified plugins.
*
* @param plugins the specified plugins
* @throws Exception exception
*/
public static void refresh(final List<AbstractPlugin> plugins)
throws Exception {
final JSONObject result = PLUGIN_REPOS.get(new Query());
final JSONArray pluginArray = result.getJSONArray(Keys.RESULTS);
final List<JSONObject> persistedPlugins =
CollectionUtils.jsonArrayToList(pluginArray);
// Disables plugin repository cache to avoid remove all cache
PLUGIN_REPOS.setCacheEnabled(false);
try {
// Reads plugin status from datastore and clear plugin datastore
for (final JSONObject oldPluginDesc : persistedPlugins) {
final String descId = oldPluginDesc.getString(Keys.OBJECT_ID);
final AbstractPlugin plugin = get(plugins, descId);
PLUGIN_REPOS.remove(descId);
if (null != plugin) {
final String status =
oldPluginDesc.getString(Plugin.PLUGIN_STATUS);
plugin.setStatus(PluginStatus.valueOf(status));
}
}
// Adds these plugins into datastore
for (final AbstractPlugin plugin : plugins) {
final JSONObject pluginDesc = plugin.toJSONObject();
PLUGIN_REPOS.add(pluginDesc);
LOGGER.log(Level.FINEST, "Refreshed plugin[{0}]", pluginDesc);
}
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, "Refresh plugins failed", e);
}
PLUGIN_REPOS.setCacheEnabled(true);
}
/**
* Gets a plugin in the specified plugins with the specified id.
*
* @param plugins the specified plugins
* @param id the specified id, must NOT be {@code null}
* @return a plugin, returns {@code null} if not found
*/
private static AbstractPlugin get(final List<AbstractPlugin> plugins,
final String id) {
if (null == id) {
throw new IllegalArgumentException("id must not be null");
}
for (final AbstractPlugin plugin : plugins) {
if (id.equals(plugin.getId())) {
return plugin;
}
}
return null;
}
/**
* Private default constructor.
*/
private Plugins() {
}
}
/*
* 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.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.Keys;
import org.b3log.latke.model.Plugin;
import org.b3log.latke.plugin.AbstractPlugin;
import org.b3log.latke.plugin.PluginStatus;
import org.b3log.latke.repository.Query;
import org.b3log.latke.util.CollectionUtils;
import org.b3log.solo.repository.impl.PluginRepositoryImpl;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Plugin utilities.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.3, Sep 11, 2011
*/
public final class Plugins {
/**
* Logger.
*/
private static final Logger LOGGER =
Logger.getLogger(Plugins.class.getName());
/**
* Plugin repository.
*/
private static final PluginRepositoryImpl PLUGIN_REPOS =
PluginRepositoryImpl.getInstance();
/**
* Updates datastore plugin descriptions with the specified plugins.
*
* @param plugins the specified plugins
* @throws Exception exception
*/
public static void refresh(final List<AbstractPlugin> plugins)
throws Exception {
final JSONObject result = PLUGIN_REPOS.get(new Query());
final JSONArray pluginArray = result.getJSONArray(Keys.RESULTS);
final List<JSONObject> persistedPlugins =
CollectionUtils.jsonArrayToList(pluginArray);
// Disables plugin repository cache to avoid remove all cache
PLUGIN_REPOS.setCacheEnabled(false);
try {
// Reads plugin status from datastore and clear plugin datastore
for (final JSONObject oldPluginDesc : persistedPlugins) {
final String descId = oldPluginDesc.getString(Keys.OBJECT_ID);
final AbstractPlugin plugin = get(plugins, descId);
PLUGIN_REPOS.remove(descId);
if (null != plugin) {
final String status =
oldPluginDesc.getString(Plugin.PLUGIN_STATUS);
plugin.setStatus(PluginStatus.valueOf(status));
}
}
// Adds these plugins into datastore
for (final AbstractPlugin plugin : plugins) {
final JSONObject pluginDesc = plugin.toJSONObject();
PLUGIN_REPOS.add(pluginDesc);
LOGGER.log(Level.FINEST, "Refreshed plugin[{0}]", pluginDesc);
}
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, "Refresh plugins failed", e);
}
PLUGIN_REPOS.setCacheEnabled(true);
}
/**
* Gets a plugin in the specified plugins with the specified id.
*
* @param plugins the specified plugins
* @param id the specified id, must NOT be {@code null}
* @return a plugin, returns {@code null} if not found
*/
private static AbstractPlugin get(final List<AbstractPlugin> plugins,
final String id) {
if (null == id) {
throw new IllegalArgumentException("id must not be null");
}
for (final AbstractPlugin plugin : plugins) {
if (id.equals(plugin.getId())) {
return plugin;
}
}
return null;
}
/**
* Private default constructor.
*/
private Plugins() {
}
}
/*
* 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() {
}
}
/*
* 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.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
import org.b3log.solo.model.Tag;
import org.b3log.solo.repository.TagRepository;
import org.b3log.latke.Keys;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.solo.repository.impl.TagRepositoryImpl;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Tag utilities.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.6, Mar 8, 2011
*/
public final class Tags {
/**
* Logger.
*/
private static final Logger LOGGER =
Logger.getLogger(Tags.class.getName());
/**
* Tag repository.
*/
private TagRepository tagRepository =
TagRepositoryImpl.getInstance();
/**
* Article utilities.
*/
private Articles articleUtils = Articles.getInstance();
/**
* Decrements reference count of every tag of an published article specified
* by the given article id.
*
* @param articleId the given article id
* @throws JSONException json exception
* @throws RepositoryException repository exception
*/
public void decTagPublishedRefCount(final String articleId)
throws JSONException, RepositoryException {
final List<JSONObject> tags = tagRepository.getByArticleId(articleId);
for (final JSONObject tag : tags) {
final String tagId = tag.getString(Keys.OBJECT_ID);
final int refCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT);
tag.put(Tag.TAG_REFERENCE_COUNT, refCnt);
final int publishedRefCnt =
tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt - 1);
tagRepository.update(tagId, tag);
}
}
/**
* Removes tags of unpublished articles from the specified tags.
*
* @param tags the specified tags
* @throws JSONException json exception
* @throws RepositoryException repository exception
*/
public void removeForUnpublishedArticles(
final List<JSONObject> tags) throws JSONException,
RepositoryException {
final Iterator<JSONObject> iterator = tags.iterator();
while (iterator.hasNext()) {
final JSONObject tag = iterator.next();
if (0 == tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT)) {
iterator.remove();
}
}
}
/**
* Gets the {@link Tags} singleton.
*
* @return the singleton
*/
public static Tags getInstance() {
return SingletonHolder.SINGLETON;
}
/**
* Private default constructor.
*/
private Tags() {
}
/**
* Singleton holder.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Jan 12, 2011
*/
private static final class SingletonHolder {
/**
* Singleton.
*/
private static final Tags SINGLETON = new Tags();
/**
* 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.util.Iterator;
import java.util.List;
import java.util.logging.Logger;
import org.b3log.solo.model.Tag;
import org.b3log.solo.repository.TagRepository;
import org.b3log.latke.Keys;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.solo.repository.impl.TagRepositoryImpl;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Tag utilities.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.6, Mar 8, 2011
*/
public final class Tags {
/**
* Logger.
*/
private static final Logger LOGGER =
Logger.getLogger(Tags.class.getName());
/**
* Tag repository.
*/
private TagRepository tagRepository =
TagRepositoryImpl.getInstance();
/**
* Article utilities.
*/
private Articles articleUtils = Articles.getInstance();
/**
* Decrements reference count of every tag of an published article specified
* by the given article id.
*
* @param articleId the given article id
* @throws JSONException json exception
* @throws RepositoryException repository exception
*/
public void decTagPublishedRefCount(final String articleId)
throws JSONException, RepositoryException {
final List<JSONObject> tags = tagRepository.getByArticleId(articleId);
for (final JSONObject tag : tags) {
final String tagId = tag.getString(Keys.OBJECT_ID);
final int refCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT);
tag.put(Tag.TAG_REFERENCE_COUNT, refCnt);
final int publishedRefCnt =
tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt - 1);
tagRepository.update(tagId, tag);
}
}
/**
* Removes tags of unpublished articles from the specified tags.
*
* @param tags the specified tags
* @throws JSONException json exception
* @throws RepositoryException repository exception
*/
public void removeForUnpublishedArticles(
final List<JSONObject> tags) throws JSONException,
RepositoryException {
final Iterator<JSONObject> iterator = tags.iterator();
while (iterator.hasNext()) {
final JSONObject tag = iterator.next();
if (0 == tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT)) {
iterator.remove();
}
}
}
/**
* Gets the {@link Tags} singleton.
*
* @return the singleton
*/
public static Tags getInstance() {
return SingletonHolder.SINGLETON;
}
/**
* Private default constructor.
*/
private Tags() {
}
/**
* Singleton holder.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Jan 12, 2011
*/
private static final class SingletonHolder {
/**
* Singleton.
*/
private static final Tags SINGLETON = new Tags();
/**
* 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.util.Date;
import java.util.TimeZone;
import org.b3log.latke.util.freemarker.Templates;
import org.b3log.solo.model.ArchiveDate;
import org.b3log.solo.model.Comment;
/**
* Time zone utilities.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.3, Dec 29, 2011
*/
public final class TimeZones {
/**
* Gets the current date with the specified time zone id.
*
* @param timeZoneId the specified time zone id
* @return date
*/
public static Date getTime(final String timeZoneId) {
final TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
final TimeZone defaultTimeZone = TimeZone.getDefault();
TimeZone.setDefault(timeZone);
final Date ret = new Date();
TimeZone.setDefault(defaultTimeZone);
return ret;
}
/**
* Sets time zone by the specified time zone id.
*
* <p>
* This method will call {@linkplain TimeZone#setDefault(java.util.TimeZone)},
* and set time zone for all date formats and template configuration.
* </p>
*
* @param timeZoneId the specified time zone id
*/
public static void setTimeZone(final String timeZoneId) {
final TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
TimeZone.setDefault(timeZone);
System.setProperty("user.timezone", timeZoneId);
ArchiveDate.DATE_FORMAT.setTimeZone(timeZone);
Comment.DATE_FORMAT.setTimeZone(timeZone);
Templates.MAIN_CFG.setTimeZone(timeZone);
Templates.MOBILE_CFG.setTimeZone(timeZone);
}
/**
* Private default constructor.
*/
private TimeZones() {
}
}
/*
* 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.util.Date;
import java.util.TimeZone;
import org.b3log.latke.util.freemarker.Templates;
import org.b3log.solo.model.ArchiveDate;
import org.b3log.solo.model.Comment;
/**
* Time zone utilities.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.3, Dec 29, 2011
*/
public final class TimeZones {
/**
* Gets the current date with the specified time zone id.
*
* @param timeZoneId the specified time zone id
* @return date
*/
public static Date getTime(final String timeZoneId) {
final TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
final TimeZone defaultTimeZone = TimeZone.getDefault();
TimeZone.setDefault(timeZone);
final Date ret = new Date();
TimeZone.setDefault(defaultTimeZone);
return ret;
}
/**
* Sets time zone by the specified time zone id.
*
* <p>
* This method will call {@linkplain TimeZone#setDefault(java.util.TimeZone)},
* and set time zone for all date formats and template configuration.
* </p>
*
* @param timeZoneId the specified time zone id
*/
public static void setTimeZone(final String timeZoneId) {
final TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
TimeZone.setDefault(timeZone);
System.setProperty("user.timezone", timeZoneId);
ArchiveDate.DATE_FORMAT.setTimeZone(timeZone);
Comment.DATE_FORMAT.setTimeZone(timeZone);
Templates.MAIN_CFG.setTimeZone(timeZone);
Templates.MOBILE_CFG.setTimeZone(timeZone);
}
/**
* Private default constructor.
*/
private TimeZones() {
}
}
/**
* Utilities.
*/
package org.b3log.solo.util;
/**
* Utilities.
*/
package org.b3log.solo.util;
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