diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-18 14:46:57 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-18 14:46:57 +1200 |
commit | 04d300ca65f448301047c6f97bd88da13026b2bc (patch) | |
tree | edea73a692170e127bf603bfeec74b8798b0ba1c /src | |
parent | 16e6c030243d5a3bd77df4faddba91bdfea0bfad (diff) | |
download | traccar-server-04d300ca65f448301047c6f97bd88da13026b2bc.tar.gz traccar-server-04d300ca65f448301047c6f97bd88da13026b2bc.tar.bz2 traccar-server-04d300ca65f448301047c6f97bd88da13026b2bc.zip |
Refactor database init process
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/Context.java | 2 | ||||
-rw-r--r-- | src/org/traccar/database/DataManager.java | 79 | ||||
-rw-r--r-- | src/org/traccar/model/User.java | 5 |
3 files changed, 40 insertions, 46 deletions
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; } + } |