use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class ArticleMgmtService method cancelPublishArticle.
/**
* Cancels publish an article by the specified article id.
*
* @param articleId the specified article id
* @throws ServiceException service exception
*/
public void cancelPublishArticle(final String articleId) throws ServiceException {
final Transaction transaction = articleRepository.beginTransaction();
try {
final JSONObject article = articleRepository.get(articleId);
article.put(ARTICLE_IS_PUBLISHED, false);
tagMgmtService.decTagPublishedRefCount(articleId);
decArchiveDatePublishedRefCount(articleId);
articleRepository.update(articleId, article);
statisticMgmtService.decPublishedBlogArticleCount();
final int blogCmtCnt = statisticQueryService.getPublishedBlogCommentCount();
final int articleCmtCnt = article.getInt(ARTICLE_COMMENT_COUNT);
statisticMgmtService.setPublishedBlogCommentCount(blogCmtCnt - articleCmtCnt);
final JSONObject author = userRepository.getByEmail(article.optString(Article.ARTICLE_AUTHOR_EMAIL));
author.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, author.optInt(UserExt.USER_PUBLISHED_ARTICLE_COUNT) - 1);
userRepository.update(author.optString(Keys.OBJECT_ID), author);
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Cancels publish article failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class PluginMgmtService method setPluginStatus.
/**
* 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 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();
plugin.changeStatus();
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.ERROR, "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;
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class PreferenceMgmtService method updateReplyNotificationTemplate.
/**
* Updates the reply notification template with the specified reply notification template.
*
* @param replyNotificationTemplate the specified reply notification template
* @throws ServiceException service exception
*/
public void updateReplyNotificationTemplate(final JSONObject replyNotificationTemplate) throws ServiceException {
final Transaction transaction = optionRepository.beginTransaction();
try {
final JSONObject bodyOpt = optionRepository.get(Option.ID_C_REPLY_NOTI_TPL_BODY);
bodyOpt.put(Option.OPTION_VALUE, replyNotificationTemplate.optString("body"));
optionRepository.update(Option.ID_C_REPLY_NOTI_TPL_BODY, bodyOpt);
final JSONObject subjectOpt = optionRepository.get(Option.ID_C_REPLY_NOTI_TPL_SUBJECT);
subjectOpt.put(Option.OPTION_VALUE, replyNotificationTemplate.optString("subject"));
optionRepository.update(Option.ID_C_REPLY_NOTI_TPL_SUBJECT, subjectOpt);
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates reply notification failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class UpgradeService method perform.
/**
* Performs upgrade.
*
* @throws Exception upgrade fails
*/
private void perform() throws Exception {
LOGGER.log(Level.INFO, "Upgrading from version [{0}] to version [{1}]....", FROM_VER, TO_VER);
Transaction transaction = optionRepository.beginTransaction();
try {
final JSONObject versionOpt = optionRepository.get(Option.ID_C_VERSION);
versionOpt.put(Option.OPTION_VALUE, TO_VER);
optionRepository.update(Option.ID_C_VERSION, versionOpt);
transaction.commit();
LOGGER.log(Level.INFO, "Updated preference");
} catch (final Exception e) {
if (null != transaction && transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Upgrade failed!", e);
throw new Exception("Upgrade failed from version [" + FROM_VER + "] to version [" + TO_VER + ']');
}
LOGGER.log(Level.INFO, "Upgraded from version [{0}] to version [{1}] successfully :-)", FROM_VER, TO_VER);
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class UpgradeService method upgradeArticles.
/**
* Upgrades articles.
*
* @throws Exception exception
*/
private void upgradeArticles() throws Exception {
LOGGER.log(Level.INFO, "Adds a property [articleEditorType] to each of articles");
final JSONArray articles = articleRepository.get(new Query()).getJSONArray(Keys.RESULTS);
if (articles.length() <= 0) {
LOGGER.log(Level.TRACE, "No articles");
return;
}
Transaction transaction = null;
try {
for (int i = 0; i < articles.length(); i++) {
if (0 == i % STEP || !transaction.isActive()) {
transaction = userRepository.beginTransaction();
}
final JSONObject article = articles.getJSONObject(i);
final String articleId = article.optString(Keys.OBJECT_ID);
LOGGER.log(Level.INFO, "Found an article[id={0}]", articleId);
article.put(Article.ARTICLE_EDITOR_TYPE, "tinyMCE");
articleRepository.update(article.getString(Keys.OBJECT_ID), article);
if (0 == i % STEP) {
transaction.commit();
LOGGER.log(Level.TRACE, "Updated some articles");
}
}
if (transaction.isActive()) {
transaction.commit();
}
LOGGER.log(Level.TRACE, "Updated all articles");
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
throw e;
}
}
Aggregations