diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-01 12:22:18 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-01 12:22:18 +1200 |
commit | b2ccb23a541d5d927696385bdf96beb7f99348b2 (patch) | |
tree | 97025eaf8edd05281c78ddeb353f3c230f89afd4 /src/org/traccar/database | |
parent | 1d0e1d374bae176b829848b394155a917870e5a7 (diff) | |
download | traccar-server-b2ccb23a541d5d927696385bdf96beb7f99348b2.tar.gz traccar-server-b2ccb23a541d5d927696385bdf96beb7f99348b2.tar.bz2 traccar-server-b2ccb23a541d5d927696385bdf96beb7f99348b2.zip |
Manage server settings record
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r-- | src/org/traccar/database/DataManager.java | 41 | ||||
-rw-r--r-- | src/org/traccar/database/QueryBuilder.java | 9 |
2 files changed, 41 insertions, 9 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index c32f0b017..2b7d15b3d 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -26,9 +26,15 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.text.ParseException; -import java.util.*; +import java.util.Calendar; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Properties; import javax.sql.DataSource; - import org.traccar.Context; import org.traccar.helper.DriverDelegate; import org.traccar.helper.Log; @@ -36,6 +42,7 @@ import org.traccar.http.JsonConverter; import org.traccar.model.Device; import org.traccar.model.Permission; import org.traccar.model.Position; +import org.traccar.model.Server; import org.traccar.model.User; public class DataManager { @@ -307,6 +314,14 @@ public class DataManager { admin.setPassword("admin"); admin.setAdmin(true); addUser(admin); + + Server server = new Server(); + server.setRegistration(true); + QueryBuilder.create(dataSource, + "INSERT INTO server (registration, latitude, longitude, zoom)" + + "VALUES (:registration, :latitude, :longitude, :zoom);") + .setObject(server) + .executeUpdate(); mockData(admin.getId()); } @@ -348,17 +363,12 @@ public class DataManager { } public User login(String email, String password) throws SQLException { - Collection<User> result = QueryBuilder.create(dataSource, + return QueryBuilder.create(dataSource, "SELECT * FROM user WHERE email = :email AND " + "password = CAST(HASH('SHA256', STRINGTOUTF8(:password), 1000) AS VARCHAR);") .setString("email", email) .setString("password", password) - .executeQuery(new User()); - if (!result.isEmpty()) { - return result.iterator().next(); - } else { - return null; - } + .executeQuerySingle(new User()); } public void addUser(User user) throws SQLException { @@ -420,5 +430,18 @@ public class DataManager { .setDate("to", to) .executeQuery(new Position()); } + + public Server getServer() throws SQLException { + return QueryBuilder.create(dataSource, + "SELECT * FROM server;") + .executeQuerySingle(new Server()); + } + + public void updateServer(Server server) throws SQLException { + QueryBuilder.create(dataSource, + "UPDATE server SET registration = :registration WHERE id = :id;") + .setObject(server) + .executeUpdate(); + } } diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java index a35b07fee..386ed86d7 100644 --- a/src/org/traccar/database/QueryBuilder.java +++ b/src/org/traccar/database/QueryBuilder.java @@ -201,6 +201,15 @@ public class QueryBuilder { public void process(T object, ResultSet resultSet) throws SQLException; } + public <T extends Factory> T executeQuerySingle(T prototype) throws SQLException { + Collection<T> result = executeQuery(prototype); + if (!result.isEmpty()) { + return result.iterator().next(); + } else { + return null; + } + } + public <T extends Factory> Collection<T> executeQuery(T prototype) throws SQLException { List<T> result = new LinkedList<T>(); |