use of org.b3log.solo.model.ArchiveDate in project solo by b3log.
the class ArticleMgmtService method addArticleInternal.
/**
* Adds the specified article for internal invocation purposes.
*
* @param article the specified article
* @return generated article id
* @throws ServiceException service exception
*/
public String addArticleInternal(final JSONObject article) throws ServiceException {
String ret = article.optString(Keys.OBJECT_ID);
if (Strings.isEmptyOrNull(ret)) {
ret = Ids.genTimeMillisId();
article.put(Keys.OBJECT_ID, ret);
}
try {
// Step 1: Add tags
String tagsString = article.optString(Article.ARTICLE_TAGS_REF);
tagsString = tagsString.replaceAll(",", ",").replaceAll("、", ",");
article.put(Article.ARTICLE_TAGS_REF, tagsString);
final String[] tagTitles = tagsString.split(",");
final JSONArray tags = tag(tagTitles, article);
// Step 2; Set comment/view count to 0
article.put(Article.ARTICLE_COMMENT_COUNT, 0);
article.put(Article.ARTICLE_VIEW_COUNT, 0);
// Step 3: Set create/updat date
final JSONObject preference = preferenceQueryService.getPreference();
final Date date = new Date();
if (!article.has(Article.ARTICLE_CREATE_DATE)) {
article.put(Article.ARTICLE_CREATE_DATE, date);
}
article.put(Article.ARTICLE_UPDATE_DATE, article.opt(Article.ARTICLE_CREATE_DATE));
// Step 4: Set put top to false
article.put(Article.ARTICLE_PUT_TOP, false);
// Step 5: Add tag-article relations
addTagArticleRelation(tags, article);
// Step 6: Inc blog article count statictis
statisticMgmtService.incBlogArticleCount();
if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
statisticMgmtService.incPublishedBlogArticleCount();
}
// Step 7: Add archive date-article relations
archiveDate(article);
// Step 8: Set permalink
final String permalink = getPermalinkForAddArticle(article);
article.put(Article.ARTICLE_PERMALINK, permalink);
// Step 9: Add article sign id
final String signId = article.optString(Article.ARTICLE_SIGN_ID, "1");
article.put(Article.ARTICLE_SIGN_ID, signId);
// Step 10: Set had been published status
article.put(Article.ARTICLE_HAD_BEEN_PUBLISHED, false);
if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
// Publish it directly
article.put(Article.ARTICLE_HAD_BEEN_PUBLISHED, true);
}
// Step 11: Set random double
article.put(Article.ARTICLE_RANDOM_DOUBLE, Math.random());
// Step 12: Set post to community
final boolean postToCommunity = article.optBoolean(Common.POST_TO_COMMUNITY, true);
// Do not persist this property
article.remove(Common.POST_TO_COMMUNITY);
// Setp 13: Update user article statistic
final JSONObject author = userRepository.getByEmail(article.optString(Article.ARTICLE_AUTHOR_EMAIL));
final int userArticleCnt = author.optInt(UserExt.USER_ARTICLE_COUNT);
author.put(UserExt.USER_ARTICLE_COUNT, userArticleCnt + 1);
if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
author.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, author.optInt(UserExt.USER_PUBLISHED_ARTICLE_COUNT) + 1);
}
userRepository.update(author.optString(Keys.OBJECT_ID), author);
// Step 14: Set editor type
if (!article.has(Article.ARTICLE_EDITOR_TYPE)) {
article.put(Article.ARTICLE_EDITOR_TYPE, preference.optString(Option.ID_C_EDITOR_TYPE));
}
// Step 15: Add article
articleRepository.add(article);
// Restores the property
article.put(Common.POST_TO_COMMUNITY, postToCommunity);
if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
// Fire add article event
final JSONObject eventData = new JSONObject();
eventData.put(Article.ARTICLE, article);
eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.ADD_ARTICLE, eventData));
}
article.remove(Common.POST_TO_COMMUNITY);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Adds an article failed", e);
throw new ServiceException(e);
} catch (final EventException e) {
LOGGER.log(Level.WARN, "Adds an article event process failed", e);
}
return ret;
}
use of org.b3log.solo.model.ArchiveDate in project solo by b3log.
the class ArticleMgmtService method archiveDate.
/**
* Archive the create date with the specified article.
*
* @param article the specified article, for example,
* <pre>
* {
* ....,
* "oId": "",
* "articleCreateDate": java.util.Date,
* ....
* }
* </pre>
* @throws RepositoryException repository exception
*/
private void archiveDate(final JSONObject article) throws RepositoryException {
final Date createDate = (Date) article.opt(Article.ARTICLE_CREATE_DATE);
final String createDateString = DateFormatUtils.format(createDate, "yyyy/MM");
JSONObject archiveDate = archiveDateRepository.getByArchiveDate(createDateString);
if (null == archiveDate) {
archiveDate = new JSONObject();
try {
archiveDate.put(ArchiveDate.ARCHIVE_TIME, DateUtils.parseDate(createDateString, new String[] { "yyyy/MM" }).getTime());
archiveDate.put(ArchiveDate.ARCHIVE_DATE_ARTICLE_COUNT, 0);
archiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, 0);
archiveDateRepository.add(archiveDate);
} catch (final ParseException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
throw new RepositoryException(e);
}
}
final JSONObject newArchiveDate = new JSONObject(archiveDate, CollectionUtils.jsonArrayToArray(archiveDate.names(), String[].class));
newArchiveDate.put(ArchiveDate.ARCHIVE_DATE_ARTICLE_COUNT, archiveDate.optInt(ArchiveDate.ARCHIVE_DATE_ARTICLE_COUNT) + 1);
if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
newArchiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, archiveDate.optInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT) + 1);
}
archiveDateRepository.update(archiveDate.optString(Keys.OBJECT_ID), newArchiveDate);
final JSONObject archiveDateArticleRelation = new JSONObject();
archiveDateArticleRelation.put(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID, archiveDate.optString(Keys.OBJECT_ID));
archiveDateArticleRelation.put(Article.ARTICLE + "_" + Keys.OBJECT_ID, article.optString(Keys.OBJECT_ID));
archiveDateArticleRepository.add(archiveDateArticleRelation);
}
Aggregations