From 04d300ca65f448301047c6f97bd88da13026b2bc Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 18 Jun 2015 14:46:57 +1200 Subject: Refactor database init process --- src/org/traccar/Context.java | 2 + src/org/traccar/database/DataManager.java | 79 ++++++++++++++----------------- src/org/traccar/model/User.java | 5 +- 3 files changed, 40 insertions(+), 46 deletions(-) (limited to 'src/org') 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; } + } -- cgit v1.2.3