* @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 String timeZoneId = preference.getString(Preference.TIME_ZONE_ID);
final Date date = TimeZones.getTime(timeZoneId);
if (article.getBoolean(ARTICLE_IS_PUBLISHED)) { // Publish it
if (articleUtils.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 (articleUtils.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
statistics.incPublishedBlogArticleCount();
final int blogCmtCnt = statistics.getPublishedBlogCommentCount();
final int articleCmtCnt = article.getInt(ARTICLE_COMMENT_COUNT);
statistics.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
articleRepository.update(articleId, article);
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.SEVERE, 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.SEVERE, e.getMessage(), e);
}
}
transaction.commit();
} catch (final ServiceException e) {
if (transaction.isActive()) {
transaction.rollback();
}
throw e;
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.SEVERE, "Updates an article failed", e);
throw new ServiceException(e.getMessage());