* @throws ServiceException service exception
*/
public void updateArticle(final JSONObject requestJSONObject) throws ServiceException {
final JSONObject ret = new JSONObject();
final Transaction transaction = articleRepository.beginTransaction();
try {
final JSONObject article = requestJSONObject.getJSONObject(ARTICLE);
final String articleId = article.getString(Keys.OBJECT_ID);
// Set permalink
final JSONObject oldArticle = articleRepository.get(articleId);
final String permalink = getPermalinkForUpdateArticle(oldArticle, article, (Date) oldArticle.get(ARTICLE_CREATE_DATE));
article.put(ARTICLE_PERMALINK, permalink);
processTagsForArticleUpdate(oldArticle, article);
if (!oldArticle.getString(Article.ARTICLE_PERMALINK).equals(permalink)) { // The permalink has been updated
// Updates related comments' links
processCommentsForArticleUpdate(article);
}
// Fill auto properties
fillAutoProperties(oldArticle, article);
// Set date
article.put(ARTICLE_UPDATE_DATE, oldArticle.get(ARTICLE_UPDATE_DATE));
final JSONObject preference = preferenceQueryService.getPreference();
final Date date = new Date();
// The article to update has no sign
if (!article.has(Article.ARTICLE_SIGN_ID)) {
article.put(Article.ARTICLE_SIGN_ID, "0");
}
if (article.getBoolean(ARTICLE_IS_PUBLISHED)) { // Publish it
if (articleQueryService.hadBeenPublished(oldArticle)) {
// Edit update date only for published article
article.put(ARTICLE_UPDATE_DATE, date);
} else { // This article is a draft and this is the first time to publish it
article.put(ARTICLE_CREATE_DATE, date);
article.put(ARTICLE_UPDATE_DATE, date);
article.put(ARTICLE_HAD_BEEN_PUBLISHED, true);
}
} else { // Save as draft
if (articleQueryService.hadBeenPublished(oldArticle)) {
// Save update date only for published article
article.put(ARTICLE_UPDATE_DATE, date);
} else {
// Reset create/update date to indicate this is an new draft
article.put(ARTICLE_CREATE_DATE, date);
article.put(ARTICLE_UPDATE_DATE, date);
}
}
// Set editor type
article.put(Article.ARTICLE_EDITOR_TYPE, preference.optString(Preference.EDITOR_TYPE));
final boolean publishNewArticle = !oldArticle.getBoolean(ARTICLE_IS_PUBLISHED) && article.getBoolean(ARTICLE_IS_PUBLISHED);
// Set statistic
if (publishNewArticle) {
// This article is updated from unpublished to published
statisticMgmtService.incPublishedBlogArticleCount();
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);
}
if (publishNewArticle) {
incArchiveDatePublishedRefCount(articleId);
}
// Update
final boolean postToCommunity = article.optBoolean(Common.POST_TO_COMMUNITY, true);
article.remove(Common.POST_TO_COMMUNITY); // Do not persist this property
articleRepository.update(articleId, article);
article.put(Common.POST_TO_COMMUNITY, postToCommunity); // Restores the property
if (publishNewArticle) {
// Fire add article event
final JSONObject eventData = new JSONObject();
eventData.put(ARTICLE, article);
eventData.put(Keys.RESULTS, ret);
try {
eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.ADD_ARTICLE, eventData));
} catch (final EventException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
}
} else {
// Fire update article event
final JSONObject eventData = new JSONObject();
eventData.put(ARTICLE, article);
eventData.put(Keys.RESULTS, ret);
try {
eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.UPDATE_ARTICLE, eventData));
} catch (final EventException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
}
}
transaction.commit();
} catch (final ServiceException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates an article failed", e);
throw e;
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates an article failed", e);
throw new ServiceException(e.getMessage());