Commit f9ab031e authored by Liang Ding's avatar Liang Ding

🎨 Fix #12657

parent b247815b
<?xml version="1.0" encoding="UTF-8"?>
<!--
Description: Solo POM.
Version: 3.18.3.47, Feb 16, 2019
Version: 3.18.3.48, Feb 22, 2019
Author: <a href="http://88250.b3log.org">Liang Ding</a>
Author: <a href="http://www.annpeter.cn">Ann Peter</a>
Author: <a href="http://vanessa.b3log.org">Vanessa</a>
......@@ -74,7 +74,7 @@
</scm>
<properties>
<org.b3log.latke.version>2.4.42</org.b3log.latke.version>
<org.b3log.latke.version>2.4.43</org.b3log.latke.version>
<slf4j.version>1.7.5</slf4j.version>
<jsoup.version>1.9.1</jsoup.version>
......
......@@ -84,6 +84,8 @@ public final class SoloServletListener extends AbstractServletListener {
validateSkin();
final InitService initService = beanManager.getReference(InitService.class);
initService.initTables();
if (initService.isInited()) {
// Upgrade check https://github.com/b3log/solo/issues/12040
final UpgradeService upgradeService = beanManager.getReference(UpgradeService.class);
......
......@@ -28,9 +28,9 @@ import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.Role;
import org.b3log.latke.model.User;
import org.b3log.latke.plugin.PluginManager;
import org.b3log.latke.repository.Query;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.Transaction;
import org.b3log.latke.repository.jdbc.util.Connections;
import org.b3log.latke.repository.jdbc.util.JdbcRepositories;
import org.b3log.latke.repository.jdbc.util.JdbcRepositories.CreateTableResult;
import org.b3log.latke.service.LangPropsService;
......@@ -48,9 +48,6 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;
import java.util.List;
import java.util.Set;
......@@ -162,43 +159,40 @@ public class InitService {
return true;
}
try (final Connection connection = Connections.getConnection()) {
final PreparedStatement preparedStatement = connection.prepareStatement("SELECT COUNT(1) AS `c` FROM `" + userRepository.getName() + "`");
final ResultSet resultSet = preparedStatement.executeQuery();
resultSet.next();
final int c = resultSet.getInt("c");
inited = 0 < c;
try {
inited = !optionRepository.getList(new Query()).isEmpty();
if (!inited && !printedInitMsg) {
LOGGER.log(Level.WARN, "Solo has not been initialized, please open your browser and visit [" + Latkes.getServePath() + "] to init Solo");
printedInitMsg = true;
}
return inited;
} catch (final Exception e) {
if (!printedInitMsg) {
LOGGER.log(Level.WARN, "Solo has not been initialized, please open your browser and visit [" + Latkes.getServePath() + "] to init Solo");
}
printedInitMsg = true;
LOGGER.log(Level.ERROR, "Check init failed", e);
System.exit(-1);
return false;
}
}
/**
* Initializes Solo.
*
* @param requestJSONObject the specified request json object, for example,
* {
* "userName": "",
* "userEmail": "",
* "userAvatar": "", // optional
* "userB3Key": "", // optional
* "userGitHubId": "" // optional
* }
* @throws ServiceException service exception
* Initializes database tables.
*/
public void init(final JSONObject requestJSONObject) throws ServiceException {
if (isInited()) {
public void initTables() {
try {
final String tablePrefix = Latkes.getLocalProperty("jdbc.tablePrefix") + "_";
final boolean userTableExist = JdbcRepositories.existTable(tablePrefix + User.USER);
final boolean optionTableExist = JdbcRepositories.existTable(tablePrefix + Option.OPTION);
if (userTableExist && optionTableExist) {
return;
}
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Check tables failed", e);
LOGGER.log(Level.DEBUG, "Solo is running with database [{0}], creates all tables", Latkes.getRuntimeDatabase());
System.exit(-1);
}
LOGGER.info("It's your first time setup Solo, initialize tables in database [" + Latkes.getRuntimeDatabase() + "]");
if (Latkes.RuntimeDatabase.H2 == Latkes.getRuntimeDatabase()) {
String dataDir = Latkes.getLocalProperty("jdbc.URL");
......@@ -209,9 +203,28 @@ public class InitService {
final List<CreateTableResult> createTableResults = JdbcRepositories.initAllTables();
for (final CreateTableResult createTableResult : createTableResults) {
LOGGER.log(Level.DEBUG, "Create table result [tableName={0}, isSuccess={1}]",
LOGGER.log(Level.DEBUG, "Creates table result [tableName={0}, isSuccess={1}]",
createTableResult.getName(), createTableResult.isSuccess());
}
}
/**
* Initializes Solo.
*
* @param requestJSONObject the specified request json object, for example,
* {
* "userName": "",
* "userEmail": "",
* "userAvatar": "", // optional
* "userB3Key": "", // optional
* "userGitHubId": "" // optional
* }
* @throws ServiceException service exception
*/
public void init(final JSONObject requestJSONObject) throws ServiceException {
if (isInited()) {
return;
}
final Transaction transaction = userRepository.beginTransaction();
try {
......@@ -270,7 +283,7 @@ public class InitService {
final JSONObject comment = new JSONObject();
comment.put(Keys.OBJECT_ID, articleId);
comment.put(Comment.COMMENT_NAME, "Daniel");
comment.put(Comment.COMMENT_NAME, "88250");
comment.put(Comment.COMMENT_EMAIL, "d@b3log.org");
comment.put(Comment.COMMENT_URL, "https://hacpai.com/member/88250");
comment.put(Comment.COMMENT_CONTENT, langPropsService.get("helloWorld.comment.content"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment