aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-06-18 14:46:57 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-06-18 14:46:57 +1200
commit04d300ca65f448301047c6f97bd88da13026b2bc (patch)
treeedea73a692170e127bf603bfeec74b8798b0ba1c
parent16e6c030243d5a3bd77df4faddba91bdfea0bfad (diff)
downloadtraccar-server-04d300ca65f448301047c6f97bd88da13026b2bc.tar.gz
traccar-server-04d300ca65f448301047c6f97bd88da13026b2bc.tar.bz2
traccar-server-04d300ca65f448301047c6f97bd88da13026b2bc.zip
Refactor database init process
-rw-r--r--debug.xml3
-rw-r--r--src/org/traccar/Context.java2
-rw-r--r--src/org/traccar/database/DataManager.java79
-rw-r--r--src/org/traccar/model/User.java5
4 files changed, 42 insertions, 47 deletions
diff --git a/debug.xml b/debug.xml
index 698f7bdc9..a2650ccd0 100644
--- a/debug.xml
+++ b/debug.xml
@@ -37,6 +37,7 @@
<entry key='database.user'>sa</entry>
<entry key='database.password'></entry>
<entry key='database.mock'>true</entry>
+ <entry key='database.checkTable'>traccar</entry>
<entry key='database.createSchema'>
CREATE TABLE user (
@@ -113,7 +114,7 @@
longitude DOUBLE NOT NULL,
zoom INT NOT NULL);
- CREATE TABLE traccar1 (
+ CREATE TABLE traccar (
id INT PRIMARY KEY AUTO_INCREMENT);
</entry>
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java
index b3fe881e6..13a3b33c4 100644
--- a/src/org/traccar/Context.java
+++ b/src/org/traccar/Context.java
@@ -111,6 +111,8 @@ public class Context {
}
serverManager = new ServerManager();
+
+ dataManager.initDatabaseSchema();
serverManager.init();
}
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 2b5373197..317af8c31 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -112,10 +112,6 @@ public class DataManager {
}
dataSource = ds;
}
-
- if (Boolean.valueOf(properties.getProperty("web.new"))) {
- createDatabaseSchema();
- }
}
public Device getDeviceByUniqueId(String uniqueId) throws SQLException {
@@ -132,41 +128,43 @@ public class DataManager {
return devices.get(uniqueId);
}
- private void createDatabaseSchema() throws SQLException {
+ public void initDatabaseSchema() throws SQLException {
- Connection connection = dataSource.getConnection();
- ResultSet result = connection.getMetaData().getTables(
- connection.getCatalog(), null, null, null);
-
- boolean exist = false;
- while (result.next()) {
- if (result.getString("TABLE_NAME").equalsIgnoreCase("traccar1")) {
- exist = true;
- break;
+ if (Boolean.valueOf(properties.getProperty("web.new"))) {
+
+ Connection connection = dataSource.getConnection();
+ ResultSet result = connection.getMetaData().getTables(
+ connection.getCatalog(), null, null, null);
+
+ boolean exist = false;
+ String checkTable = properties.getProperty("database.checkTable");
+ while (result.next()) {
+ if (result.getString("TABLE_NAME").equalsIgnoreCase(checkTable)) {
+ exist = true;
+ break;
+ }
}
- }
- if (exist) {
- return;
- }
-
- QueryBuilder.create(dataSource, properties.getProperty("database.createSchema")).executeUpdate();
-
- User admin = new User();
- admin.setName("admin");
- admin.setEmail("admin");
- admin.setAdmin(true);
- admin.setPassword("admin");
- admin.setId(QueryBuilder.create(dataSource, properties.getProperty("database.insertUser"))
- .setObject(admin)
- .executeUpdate());
-
- Server server = new Server();
- server.setRegistration(true);
- QueryBuilder.create(dataSource, properties.getProperty("database.insertServer"))
- .setObject(server)
- .executeUpdate();
+ if (exist) {
+ return;
+ }
+
+ QueryBuilder.create(dataSource, properties.getProperty("database.createSchema")).executeUpdate();
+
+ User admin = new User();
+ admin.setName("admin");
+ admin.setEmail("admin");
+ admin.setAdmin(true);
+ admin.setPassword("admin");
+ addUser(admin);
- mockData(admin.getId());
+ Server server = new Server();
+ server.setRegistration(true);
+ QueryBuilder.create(dataSource, properties.getProperty("database.insertServer"))
+ .setObject(server)
+ .executeUpdate();
+
+ mockData(admin.getId());
+ }
}
private void mockData(long userId) {
@@ -177,10 +175,7 @@ public class DataManager {
device.setName("test1");
device.setUniqueId("123456789012345");
addDevice(device);
- QueryBuilder.create(dataSource, properties.getProperty("database.linkDevice"))
- .setLong("userId", userId)
- .setLong("deviceId", device.getId())
- .executeUpdate();
+ linkDevice(userId, device.getId());
Position position = new Position();
position.setDeviceId(device.getId());
@@ -200,9 +195,7 @@ public class DataManager {
position.setLongitude(174.7743053);
addPosition(position);
- QueryBuilder.create(dataSource, properties.getProperty("database.updateLatestPosition"))
- .setObject(position)
- .executeUpdate();
+ updateLatestPosition(position);
} catch (SQLException error) {
Log.warning(error);
diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java
index f7c55c0d6..a838ef018 100644
--- a/src/org/traccar/model/User.java
+++ b/src/org/traccar/model/User.java
@@ -41,9 +41,7 @@ public class User implements Factory {
private String hashedPassword;
@IgnoreOnSerialization
public String getHashedPassword() { return hashedPassword; }
- public void setHashedPassword(String hashedPassword) {
- this.hashedPassword = hashedPassword;
- }
+ public void setHashedPassword(String hashedPassword) { this.hashedPassword = hashedPassword; }
private String salt;
@IgnoreOnSerialization
@@ -87,4 +85,5 @@ public class User implements Factory {
this.hashedPassword = hashingResult.hash;
this.salt = hashingResult.salt;
}
+
}