diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-12-07 10:09:08 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-12-07 10:09:08 +1300 |
commit | 5b57c0fdb81bae87ca487e1d0087f8927b3fd07f (patch) | |
tree | c3733bca54002ca694b6d7b83b147de87212c5eb /src/org/traccar/database | |
parent | 33b2a5c9ab70ffe7bbdd074ce430920d2a0d3941 (diff) | |
download | traccar-server-5b57c0fdb81bae87ca487e1d0087f8927b3fd07f.tar.gz traccar-server-5b57c0fdb81bae87ca487e1d0087f8927b3fd07f.tar.bz2 traccar-server-5b57c0fdb81bae87ca487e1d0087f8927b3fd07f.zip |
Replace factory method with reflection
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r-- | src/org/traccar/database/DataManager.java | 20 | ||||
-rw-r--r-- | src/org/traccar/database/QueryBuilder.java | 23 |
2 files changed, 23 insertions, 20 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 66d94ac91..7d8966d9a 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -171,7 +171,7 @@ public class DataManager implements IdentityManager { if (schemaVersionQuery != null) { Schema schema = QueryBuilder.create(dataSource, schemaVersionQuery) - .executeQuerySingle(new Schema()); + .executeQuerySingle(Schema.class); int version = 0; if (schema != null) { @@ -246,7 +246,7 @@ public class DataManager implements IdentityManager { public User login(String email, String password) throws SQLException { User user = QueryBuilder.create(dataSource, getQuery("database.loginUser")) .setString("email", email) - .executeQuerySingle(new User()); + .executeQuerySingle(User.class); if (user != null && user.isPasswordValid(password)) { return user; } else { @@ -256,13 +256,13 @@ public class DataManager implements IdentityManager { public Collection<User> getUsers() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectUsersAll")) - .executeQuery(new User()); + .executeQuery(User.class); } public User getUser(long userId) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectUser")) .setLong("id", userId) - .executeQuerySingle(new User()); + .executeQuerySingle(User.class); } public void addUser(User user) throws SQLException { @@ -297,18 +297,18 @@ public class DataManager implements IdentityManager { public Collection<Permission> getPermissions() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.getPermissionsAll")) - .executeQuery(new Permission()); + .executeQuery(Permission.class); } public Collection<Device> getAllDevices() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectDevicesAll")) - .executeQuery(new Device()); + .executeQuery(Device.class); } public Collection<Device> getDevices(long userId) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectDevices")) .setLong("userId", userId) - .executeQuery(new Device()); + .executeQuery(Device.class); } public void addDevice(Device device) throws SQLException { @@ -365,7 +365,7 @@ public class DataManager implements IdentityManager { .setLong("deviceId", deviceId) .setDate("from", from) .setDate("to", to) - .executeQuery(new Position()); + .executeQuery(Position.class); } public void addPosition(Position position) throws SQLException { @@ -392,12 +392,12 @@ public class DataManager implements IdentityManager { public Collection<Position> getLatestPositions() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectLatestPositions")) - .executeQuery(new Position()); + .executeQuery(Position.class); } public Server getServer() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectServers")) - .executeQuerySingle(new Server()); + .executeQuerySingle(Server.class); } public void updateServer(Server server) throws SQLException { diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java index ca6335556..d0c1c649f 100644 --- a/src/org/traccar/database/QueryBuilder.java +++ b/src/org/traccar/database/QueryBuilder.java @@ -38,7 +38,6 @@ import javax.json.stream.JsonParsingException; import javax.sql.DataSource; import org.traccar.Context; import org.traccar.helper.Log; -import org.traccar.model.Factory; import org.traccar.model.MiscFormatter; public final class QueryBuilder { @@ -272,8 +271,8 @@ public final class QueryBuilder { void process(T object, ResultSet resultSet) throws SQLException; } - public <T extends Factory> T executeQuerySingle(T prototype) throws SQLException { - Collection<T> result = executeQuery(prototype); + public <T> T executeQuerySingle(Class<T> clazz) throws SQLException { + Collection<T> result = executeQuery(clazz); if (!result.isEmpty()) { return result.iterator().next(); } else { @@ -281,7 +280,7 @@ public final class QueryBuilder { } } - private <T extends Factory> void addProcessors( + private <T> void addProcessors( List<ResultSetProcessor<T>> processors, Class<?> parameterType, final Method method, final String name) { if (parameterType.equals(boolean.class)) { @@ -367,7 +366,7 @@ public final class QueryBuilder { } } - public <T extends Factory> Collection<T> executeQuery(T prototype) throws SQLException { + public <T> Collection<T> executeQuery(Class<T> clazz) throws SQLException { List<T> result = new LinkedList<>(); if (query != null) { @@ -380,7 +379,7 @@ public final class QueryBuilder { List<ResultSetProcessor<T>> processors = new LinkedList<>(); - Method[] methods = prototype.getClass().getMethods(); + Method[] methods = clazz.getMethods(); for (final Method method : methods) { if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) { @@ -404,11 +403,15 @@ public final class QueryBuilder { } while (resultSet.next()) { - T object = (T) prototype.create(); - for (ResultSetProcessor<T> processor : processors) { - processor.process(object, resultSet); + try { + T object = clazz.newInstance(); + for (ResultSetProcessor<T> processor : processors) { + processor.process(object, resultSet); + } + result.add(object); + } catch (InstantiationException | IllegalAccessException e) { + throw new IllegalArgumentException(); } - result.add(object); } } |