Skip to content

Commit a210282

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/2.2.0-dev'
2 parents 786c176 + b73917d commit a210282

File tree

3 files changed

+71
-55
lines changed

3 files changed

+71
-55
lines changed

src/main/java/org/b3log/solo/event/rhythm/ArticleSender.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,21 @@
4343

4444
/**
4545
* This listener is responsible for sending article to B3log Rhythm.
46-
*
4746
* <p>
4847
* The B3log Rhythm article update interface: http://rhythm.b3log.org/article (POST).
4948
* </p>
5049
*
5150
* @author <a href="http://88250.b3log.org">Liang Ding</a>
5251
* @author ArmstrongCN
53-
* @version 1.0.2.8, Nov 20, 2015
52+
* @version 1.0.2.9, Jun 26, 2017
5453
* @since 0.3.1
5554
*/
5655
public final class ArticleSender extends AbstractEventListener<JSONObject> {
5756

5857
/**
5958
* Logger.
6059
*/
61-
private static final Logger LOGGER = Logger.getLogger(ArticleSender.class.getName());
62-
63-
/**
64-
* URL fetch service.
65-
*/
66-
private final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
60+
private static final Logger LOGGER = Logger.getLogger(ArticleSender.class);
6761

6862
/**
6963
* URL of adding article to Rhythm.
@@ -79,6 +73,11 @@ public final class ArticleSender extends AbstractEventListener<JSONObject> {
7973
}
8074
}
8175

76+
/**
77+
* URL fetch service.
78+
*/
79+
private final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
80+
8281
@Override
8382
public void action(final Event<JSONObject> event) throws EventException {
8483
final JSONObject data = event.getData();
@@ -87,7 +86,6 @@ public void action(final Event<JSONObject> event) throws EventException {
8786
event.getType(), data, ArticleSender.class.getName());
8887
try {
8988
final JSONObject originalArticle = data.getJSONObject(Article.ARTICLE);
90-
9189
if (!originalArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
9290
LOGGER.log(Level.DEBUG, "Ignores post article[title={0}] to Rhythm", originalArticle.getString(Article.ARTICLE_TITLE));
9391

@@ -98,7 +96,6 @@ public void action(final Event<JSONObject> event) throws EventException {
9896
final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class);
9997

10098
final JSONObject preference = preferenceQueryService.getPreference();
101-
10299
if (null == preference) {
103100
throw new EventException("Not found preference");
104101
}
@@ -108,7 +105,7 @@ public void action(final Event<JSONObject> event) throws EventException {
108105
}
109106

110107
if (Latkes.getServePath().contains("localhost")) {
111-
LOGGER.log(Level.INFO, "Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm",
108+
LOGGER.log(Level.TRACE, "Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm",
112109
originalArticle.getString(Keys.OBJECT_ID), originalArticle.getString(Article.ARTICLE_TITLE));
113110
return;
114111
}

src/main/java/org/b3log/solo/event/rhythm/ArticleUpdater.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,20 @@
4343

4444
/**
4545
* This listener is responsible for updating article to B3log Rhythm.
46-
*
4746
* <p>
4847
* The B3log Rhythm article update interface: http://rhythm.b3log.org/article (PUT).
4948
* </p>
5049
*
5150
* @author <a href="http://88250.b3log.org">Liang Ding</a>
52-
* @version 1.0.0.2, Nov 20, 2015
51+
* @version 1.0.0.3, Jun 26, 2017
5352
* @since 0.6.0
5453
*/
5554
public final class ArticleUpdater extends AbstractEventListener<JSONObject> {
5655

5756
/**
5857
* Logger.
5958
*/
60-
private static final Logger LOGGER = Logger.getLogger(ArticleUpdater.class.getName());
61-
62-
/**
63-
* URL fetch service.
64-
*/
65-
private final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
59+
private static final Logger LOGGER = Logger.getLogger(ArticleUpdater.class);
6660

6761
/**
6862
* URL of updating article to Rhythm.
@@ -78,6 +72,11 @@ public final class ArticleUpdater extends AbstractEventListener<JSONObject> {
7872
}
7973
}
8074

75+
/**
76+
* URL fetch service.
77+
*/
78+
private final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
79+
8180
@Override
8281
public void action(final Event<JSONObject> event) throws EventException {
8382
final JSONObject data = event.getData();
@@ -86,7 +85,6 @@ public void action(final Event<JSONObject> event) throws EventException {
8685
event.getType(), data, ArticleUpdater.class.getName());
8786
try {
8887
final JSONObject originalArticle = data.getJSONObject(Article.ARTICLE);
89-
9088
if (!originalArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
9189
LOGGER.log(Level.DEBUG, "Ignores post article[title={0}] to Rhythm", originalArticle.getString(Article.ARTICLE_TITLE));
9290

@@ -97,7 +95,6 @@ public void action(final Event<JSONObject> event) throws EventException {
9795
final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class);
9896

9997
final JSONObject preference = preferenceQueryService.getPreference();
100-
10198
if (null == preference) {
10299
throw new EventException("Not found preference");
103100
}
@@ -107,7 +104,7 @@ public void action(final Event<JSONObject> event) throws EventException {
107104
}
108105

109106
if (Latkes.getServePath().contains("localhost")) {
110-
LOGGER.log(Level.INFO, "Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm",
107+
LOGGER.log(Level.TRACE, "Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm",
111108
originalArticle.getString(Keys.OBJECT_ID), originalArticle.getString(Article.ARTICLE_TITLE));
112109
return;
113110
}

src/main/java/org/b3log/solo/service/ImportService.java

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.b3log.latke.logging.Logger;
99
import org.b3log.latke.model.User;
1010
import org.b3log.latke.service.annotation.Service;
11+
import org.b3log.latke.util.Strings;
1112
import org.b3log.solo.SoloServletListener;
1213
import org.b3log.solo.model.Article;
1314
import org.json.JSONObject;
@@ -30,7 +31,7 @@ public class ImportService {
3031
/**
3132
* Logger.
3233
*/
33-
private static final Logger LOGGER = Logger.getLogger(UpgradeService.class);
34+
private static final Logger LOGGER = Logger.getLogger(ImportService.class);
3435

3536
/**
3637
* Default tag.
@@ -50,45 +51,66 @@ public class ImportService {
5051
private UserQueryService userQueryService;
5152

5253
public void importMarkdowns() {
53-
final ServletContext servletContext = SoloServletListener.getServletContext();
54-
final String markdownsPath = servletContext.getRealPath("markdowns");
55-
LOGGER.debug("Import directory [" + markdownsPath + "]");
54+
new Thread(() -> {
55+
final ServletContext servletContext = SoloServletListener.getServletContext();
56+
final String markdownsPath = servletContext.getRealPath("markdowns");
57+
LOGGER.debug("Import directory [" + markdownsPath + "]");
5658

57-
JSONObject admin;
58-
try {
59-
admin = userQueryService.getAdmin();
60-
} catch (final Exception e) {
61-
return;
62-
}
63-
64-
if (null == admin) { // Not init yet
65-
return;
66-
}
67-
68-
final String adminEmail = admin.optString(User.USER_EMAIL);
59+
JSONObject admin;
60+
try {
61+
admin = userQueryService.getAdmin();
62+
} catch (final Exception e) {
63+
return;
64+
}
6965

70-
final Collection<File> mds = FileUtils.listFiles(new File(markdownsPath), new String[]{"md"}, true);
71-
for (final File md : mds) {
72-
final String fileName = md.getName();
73-
if (StringUtils.equalsIgnoreCase(fileName, "README.md")) {
74-
continue;
66+
if (null == admin) { // Not init yet
67+
return;
7568
}
7669

77-
try {
78-
final String fileContent = FileUtils.readFileToString(md, "UTF-8");
79-
final JSONObject article = parseArticle(fileName, fileContent);
80-
article.put(Article.ARTICLE_AUTHOR_EMAIL, adminEmail);
70+
final String adminEmail = admin.optString(User.USER_EMAIL);
71+
72+
int succCnt = 0, failCnt = 0;
73+
final Set<String> failSet = new TreeSet<>();
74+
final Collection<File> mds = FileUtils.listFiles(new File(markdownsPath), new String[]{"md"}, true);
75+
for (final File md : mds) {
76+
final String fileName = md.getName();
77+
if (StringUtils.equalsIgnoreCase(fileName, "README.md")) {
78+
continue;
79+
}
80+
81+
try {
82+
final String fileContent = FileUtils.readFileToString(md, "UTF-8");
83+
final JSONObject article = parseArticle(fileName, fileContent);
84+
article.put(Article.ARTICLE_AUTHOR_EMAIL, adminEmail);
85+
86+
final JSONObject request = new JSONObject();
87+
request.put(Article.ARTICLE, article);
88+
89+
final String id = articleMgmtService.addArticle(request);
90+
FileUtils.moveFile(md, new File(md.getPath() + "." + id));
91+
LOGGER.info("Imported article [" + article.optString(Article.ARTICLE_TITLE) + "]");
92+
succCnt++;
93+
} catch (final Exception e) {
94+
LOGGER.log(Level.ERROR, "Import file [" + fileName + "] failed", e);
95+
96+
failCnt++;
97+
failSet.add(fileName);
98+
}
99+
}
81100

82-
final JSONObject request = new JSONObject();
83-
request.put(Article.ARTICLE, article);
101+
final StringBuilder logBuilder = new StringBuilder();
102+
logBuilder.append("[").append(succCnt).append("] imported, [").append(failCnt).append("] failed");
103+
if (failCnt > 0) {
104+
logBuilder.append(": ").append(Strings.LINE_SEPARATOR);
84105

85-
final String id = articleMgmtService.addArticle(request);
86-
FileUtils.moveFile(md, new File(md.getPath() + "." + id));
87-
LOGGER.info("Imported article [" + article.optString(Article.ARTICLE_TITLE) + "]");
88-
} catch (final Exception e) {
89-
LOGGER.log(Level.ERROR, "Import file [" + fileName + "] failed", e);
106+
for (final String fail : failSet) {
107+
logBuilder.append(" ").append(fail).append(Strings.LINE_SEPARATOR);
108+
}
109+
} else {
110+
logBuilder.append(" :p");
90111
}
91-
}
112+
LOGGER.info(logBuilder.toString());
113+
}).start();
92114
}
93115

94116
private JSONObject parseArticle(final String fileName, final String fileContent) {

0 commit comments

Comments
 (0)