Skip to content

Commit

Permalink
Fix b3log#168
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Mar 18, 2016
1 parent 04be36b commit 4c9b41f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* Validates for article adding locally.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.2.3.6, Mar 8, 2016
* @version 1.2.3.7, Mar 18, 2016
* @since 0.2.0
*/
@Named
Expand Down Expand Up @@ -106,10 +106,12 @@ public static void validateArticleFields(final HttpServletRequest request,
final LangPropsService langPropsService = beanManager.getReference(LangPropsServiceImpl.class);
final TagQueryService tagQueryService = beanManager.getReference(TagQueryService.class);

final String articleTitle = requestJSONObject.optString(Article.ARTICLE_TITLE);
String articleTitle = requestJSONObject.optString(Article.ARTICLE_TITLE);
articleTitle = StringUtils.trim(articleTitle);
if (Strings.isEmptyOrNull(articleTitle) || articleTitle.length() > MAX_ARTICLE_TITLE_LENGTH) {
throw new RequestProcessAdviceException(new JSONObject().put(Keys.MSG, langPropsService.get("articleTitleErrorLabel")));
}
requestJSONObject.put(Article.ARTICLE_TITLE, articleTitle);

final int articleType = requestJSONObject.optInt(Article.ARTICLE_TYPE);
if (Article.isInvalidArticleType(articleType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@
*/
package org.b3log.symphony.repository;

import org.b3log.latke.Keys;
import org.b3log.latke.repository.AbstractRepository;
import org.b3log.latke.repository.FilterOperator;
import org.b3log.latke.repository.PropertyFilter;
import org.b3log.latke.repository.Query;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.annotation.Repository;
import org.b3log.symphony.model.Article;
import org.json.JSONArray;
import org.json.JSONObject;

/**
* Article repository.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Sep 28, 2012
* @version 1.1.0.0, Mar 18, 2016
* @since 0.2.0
*/
@Repository
Expand All @@ -35,4 +42,25 @@ public class ArticleRepository extends AbstractRepository {
public ArticleRepository() {
super(Article.ARTICLE);
}

/**
* Gets an article by the specified article title.
*
* @param articleTitle the specified article title
* @return an article, {@code null} if not found
* @throws RepositoryException repository exception
*/
public JSONObject getByTitle(final String articleTitle) throws RepositoryException {
final Query query = new Query().setFilter(new PropertyFilter(Article.ARTICLE_TITLE,
FilterOperator.EQUAL, articleTitle)).setPageCount(1);

final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS);

if (0 == array.length()) {
return null;
}

return array.optJSONObject(0);
}
}
31 changes: 29 additions & 2 deletions src/main/java/org/b3log/symphony/service/ArticleMgmtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ public synchronized String addArticle(final JSONObject requestJSONObject) throws
throw new ServiceException(langPropsService.get("invalidRewardPointLabel"));
}

String articleTitle = requestJSONObject.optString(Article.ARTICLE_TITLE);

try {
// check if admin allow to add article
final JSONObject option = optionRepository.get(Option.ID_C_MISC_ALLOW_ADD_ARTICLE);
Expand Down Expand Up @@ -264,6 +266,18 @@ public synchronized String addArticle(final JSONObject requestJSONObject) throws
}
}

final JSONObject maybeExist = articleRepository.getByTitle(articleTitle);
if (null != maybeExist) {
final String existArticleAuthorId = maybeExist.optString(Article.ARTICLE_AUTHOR_ID);
final JSONObject existArticleAuthor = userRepository.get(existArticleAuthorId);
final String userName = existArticleAuthor.optString(User.USER_NAME);
String msg = langPropsService.get("duplicatedArticleTitleLabel");
msg = msg.replace("{user}", "<a target='_blank' href='/member/" + userName + "'>" + userName + "</a>");
msg = msg.replace("{article}", "<a target='_blank' href='/article/" + maybeExist.optString(Keys.OBJECT_ID)
+ "'>" + articleTitle + "</a>");

throw new ServiceException(msg);
}
} catch (final RepositoryException e) {
throw new ServiceException(e);
}
Expand All @@ -278,7 +292,6 @@ public synchronized String addArticle(final JSONObject requestJSONObject) throws
final String clientArticleId = requestJSONObject.optString(Article.ARTICLE_CLIENT_ARTICLE_ID, ret);
final boolean isBroadcast = requestJSONObject.optBoolean(Article.ARTICLE_T_IS_BROADCAST);

String articleTitle = requestJSONObject.optString(Article.ARTICLE_TITLE);
articleTitle = Emotions.toAliases(articleTitle);
article.put(Article.ARTICLE_TITLE, articleTitle);

Expand Down Expand Up @@ -449,13 +462,28 @@ public synchronized String addArticle(final JSONObject requestJSONObject) throws
* @throws ServiceException service exception
*/
public synchronized void updateArticle(final JSONObject requestJSONObject) throws ServiceException {
String articleTitle = requestJSONObject.optString(Article.ARTICLE_TITLE);

try {
// check if admin allow to add article
final JSONObject option = optionRepository.get(Option.ID_C_MISC_ALLOW_ADD_ARTICLE);

if (!"0".equals(option.optString(Option.OPTION_VALUE))) {
throw new ServiceException(langPropsService.get("notAllowAddArticleLabel"));
}

final JSONObject maybeExist = articleRepository.getByTitle(articleTitle);
if (null != maybeExist) {
final String existArticleAuthorId = maybeExist.optString(Article.ARTICLE_AUTHOR_ID);
final JSONObject existArticleAuthor = userRepository.get(existArticleAuthorId);
final String userName = existArticleAuthor.optString(User.USER_NAME);
String msg = langPropsService.get("duplicatedArticleTitleLabel");
msg = msg.replace("{user}", "<a target='_blank' href='/member/" + userName + "'>" + userName + "</a>");
msg = msg.replace("{article}", "<a target='_blank' href='/article/" + maybeExist.optString(Keys.OBJECT_ID)
+ "'>" + articleTitle + "</a>");

throw new ServiceException(msg);
}
} catch (final RepositoryException e) {
throw new ServiceException(e);
}
Expand All @@ -473,7 +501,6 @@ public synchronized void updateArticle(final JSONObject requestJSONObject) throw

final boolean fromClient = requestJSONObject.has(Article.ARTICLE_CLIENT_ARTICLE_ID);

String articleTitle = requestJSONObject.optString(Article.ARTICLE_TITLE);
articleTitle = Emotions.toAliases(articleTitle);
oldArticle.put(Article.ARTICLE_TITLE, articleTitle);

Expand Down
7 changes: 4 additions & 3 deletions src/main/resources/lang_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

#
# Description: Symphony default language configurations(zh_CN).
# Version: 3.42.9.19, Mar 17, 2016
# Version: 3.43.9.19, Mar 18, 2016
# Author: Liang Ding
# Author: Liyuan Li
#

duplicatedArticleTitleLabel={user} \u66fe\u7ecf\u53d1\u5e03\u8fc7\u76f8\u540c\u6807\u9898\u7684\u6587\u7ae0\uff0c\u5148\u53bb\u770b\u770b\u5427\uff1a{article}
hotLabel=\u70ed\u95e8
navigationLabel=\u5bfc\u822a
sortLabel=\u6392\u5e8f
Expand Down Expand Up @@ -201,8 +202,8 @@ uncollectLabel=\u53d6\u6d88\u6536\u85cf
collectLabel=\u6536\u85cf
tagSeparatorTipLabel=\u975e\u5fc5\u586b\uff0c\u8bf7\u4f7f\u7528\u9017\u53f7\u5206\u9694\u6bcf\u4e2a\u6807\u7b7e
advancedUpdateLabel=\u9ad8\u7ea7\u66f4\u65b0
notAllowAddCommentLabel=\u7cfb\u7edf\u6682\u4e0d\u5141\u8bb8\u6dfb\u52a0\u8bc4\u8bba
notAllowAddArticleLabel=\u7cfb\u7edf\u6682\u4e0d\u5141\u8bb8\u6dfb\u52a0\u6587\u7ae0
notAllowAddCommentLabel=\u7cfb\u7edf\u6682\u4e0d\u5141\u8bb8\u53d1\u5e03\u8bc4\u8bba
notAllowAddArticleLabel=\u7cfb\u7edf\u6682\u4e0d\u5141\u8bb8\u53d1\u5e03\u6587\u7ae0
updateCaseOnlyLabel=\uff08\u53ea\u5141\u8bb8\u4fee\u6539\u5927\u5c0f\u5199\uff09
tagStatusLabel=\u6807\u7b7e\u72b6\u6001
iconPathLabel=\u56fe\u6807\u8def\u5f84
Expand Down

0 comments on commit 4c9b41f

Please sign in to comment.