use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class ArticleMgmtService method topArticle.
/**
* Puts an article specified by the given article id to top or cancel top.
*
* @param articleId the given article id
* @param top the specified flag, {@code true} to top, {@code false} to
* cancel top
* @throws ServiceException service exception
*/
public void topArticle(final String articleId, final boolean top) throws ServiceException {
final Transaction transaction = articleRepository.beginTransaction();
try {
final JSONObject topArticle = articleRepository.get(articleId);
topArticle.put(ARTICLE_PUT_TOP, top);
articleRepository.update(articleId, topArticle);
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Can't put the article[oId{0}] to top", articleId);
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class ArticleMgmtService method addArticle.
/**
* Adds an article from the specified request json object.
*
* @param requestJSONObject the specified request json object, for example,
* <pre>
* {
* "article": {
* "articleAuthorEmail": "",
* "articleTitle": "",
* "articleAbstract": "",
* "articleContent": "",
* "articleTags": "tag1,tag2,tag3",
* "articleIsPublished": boolean,
* "articlePermalink": "", // optional
* "postToCommunity": boolean, // optional, default is true
* "articleSignId": "" // optional, default is "0",
* "articleCommentable": boolean,
* "articleViewPwd": "",
* "articleEditorType": "", // optional, preference specified if not exists this key
* "oId": "" // optional, generate it if not exists this key
* }
* }
* </pre>
* @return generated article id
* @throws ServiceException service exception
*/
public String addArticle(final JSONObject requestJSONObject) throws ServiceException {
// TODO: add article args check
final Transaction transaction = articleRepository.beginTransaction();
try {
final JSONObject article = requestJSONObject.getJSONObject(Article.ARTICLE);
final String ret = addArticleInternal(article);
transaction.commit();
return ret;
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(e.getMessage());
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class ArticleMgmtService method removeArticle.
/**
* Removes the article specified by the given id.
*
* @param articleId the given id
* @throws ServiceException service exception
*/
public void removeArticle(final String articleId) throws ServiceException {
LOGGER.log(Level.DEBUG, "Removing an article[id={0}]", articleId);
final Transaction transaction = articleRepository.beginTransaction();
try {
decTagRefCount(articleId);
unArchiveDate(articleId);
removeTagArticleRelations(articleId);
removeArticleComments(articleId);
final JSONObject article = articleRepository.get(articleId);
articleRepository.remove(articleId);
statisticMgmtService.decBlogArticleCount();
if (article.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
statisticMgmtService.decPublishedBlogArticleCount();
}
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);
author.put(UserExt.USER_ARTICLE_COUNT, author.optInt(UserExt.USER_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, "Removes an article[id=" + articleId + "] failed", e);
throw new ServiceException(e);
}
LOGGER.log(Level.DEBUG, "Removed an article[id={0}]", articleId);
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class ArticleMgmtService method incViewCount.
/**
* Increments the view count of the article specified by the given article id.
*
* @param articleId the given article id
* @throws ServiceException service exception
*/
public void incViewCount(final String articleId) throws ServiceException {
JSONObject article;
try {
article = articleRepository.get(articleId);
if (null == article) {
return;
}
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Gets article [id=" + articleId + "] failed", e);
return;
}
final Transaction transaction = articleRepository.beginTransaction();
try {
article.put(Article.ARTICLE_VIEW_COUNT, article.getInt(Article.ARTICLE_VIEW_COUNT) + 1);
articleRepository.update(articleId, article);
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.WARN, "Updates article view count failed");
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class MetaWeblogAPI method addArticle.
/**
* Adds the specified article.
*
* @param article the specified article
* @throws Exception exception
*/
private void addArticle(final JSONObject article) throws Exception {
final Transaction transaction = articleRepository.beginTransaction();
try {
articleMgmtService.addArticleInternal(article);
transaction.commit();
} catch (final ServiceException e) {
if (transaction.isActive()) {
transaction.rollback();
}
throw e;
}
}
Aggregations