diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-02 18:49:33 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-06-02 18:49:33 +1200 |
commit | f1466358d853516cd69a6effc678798f2b7ec20b (patch) | |
tree | deae5f432bb570d24b4bfb39a70aa9b73bbde270 /src/org/traccar | |
parent | d6429d5f2b23db26b9f1fba2918a4c2cbc75eaf4 (diff) | |
download | traccar-server-f1466358d853516cd69a6effc678798f2b7ec20b.tar.gz traccar-server-f1466358d853516cd69a6effc678798f2b7ec20b.tar.bz2 traccar-server-f1466358d853516cd69a6effc678798f2b7ec20b.zip |
Re-factor old database methods
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/TrackerEventHandler.java | 22 | ||||
-rw-r--r-- | src/org/traccar/database/DataManager.java | 145 |
2 files changed, 46 insertions, 121 deletions
diff --git a/src/org/traccar/TrackerEventHandler.java b/src/org/traccar/TrackerEventHandler.java index 8d8a03e35..eee866a6d 100644 --- a/src/org/traccar/TrackerEventHandler.java +++ b/src/org/traccar/TrackerEventHandler.java @@ -16,19 +16,20 @@ package org.traccar; import java.util.List; -import org.jboss.netty.channel.*; +import org.jboss.netty.channel.ChannelHandler; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.ChannelStateEvent; +import org.jboss.netty.channel.ExceptionEvent; +import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler; import org.jboss.netty.handler.timeout.IdleStateEvent; -import org.traccar.database.DataCache; import org.traccar.helper.Log; -import org.traccar.database.DataManager; -import org.traccar.model.Event; import org.traccar.model.Position; @ChannelHandler.Sharable public class TrackerEventHandler extends IdleStateAwareChannelHandler { - private Long processSinglePosition(Position position) { + private void processSinglePosition(Position position) { if (position == null) { Log.info("processSinglePosition null message"); } else { @@ -40,14 +41,11 @@ public class TrackerEventHandler extends IdleStateAwareChannelHandler { Log.info(s.toString()); } - // Write position to database - Long id = null; try { - id = Context.getDataManager().addPosition(position); + Context.getDataManager().addPosition(position); } catch (Exception error) { Log.warning(error); } - return id; } @Override @@ -55,18 +53,18 @@ public class TrackerEventHandler extends IdleStateAwareChannelHandler { Long id = null; Position lastPostition = null; if (e.getMessage() instanceof Position) { - id = processSinglePosition((Position) e.getMessage()); + processSinglePosition((Position) e.getMessage()); lastPostition = (Position) e.getMessage(); } else if (e.getMessage() instanceof List) { List<Position> positions = (List<Position>) e.getMessage(); for (Position position : positions) { - id = processSinglePosition(position); + processSinglePosition(position); lastPostition = position; } } if (lastPostition != null) { try { - Context.getDataManager().updateLatestPosition(lastPostition, id); + Context.getDataManager().updateLatestPosition(lastPostition); Context.getDataCache().update(lastPostition); } catch (Exception error) { Log.warning(error); diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 2698dd378..a075b773f 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -24,14 +24,10 @@ import java.sql.Driver; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.text.ParseException; -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.naming.InitialContext; @@ -48,7 +44,18 @@ import org.traccar.model.User; public class DataManager { + private static final long DEFAULT_REFRESH_DELAY = 300; + + private final Properties properties; + + private DataSource dataSource; + + private final Map<String, Device> devices = new HashMap<String, Device>(); + private long devicesLastUpdate; + private long devicesRefreshDelay; + public DataManager(Properties properties) throws Exception { + this.properties = properties; if (properties != null) { initDatabase(properties); @@ -62,22 +69,12 @@ public class DataManager { } } - private DataSource dataSource; - public DataSource getDataSource() { return dataSource; } - private NamedParameterStatement queryGetDevices; - private NamedParameterStatement queryAddPosition; - private NamedParameterStatement queryUpdateLatestPosition; - - private boolean useNewDatabase; - private void initDatabase(Properties properties) throws Exception { - useNewDatabase = Boolean.valueOf(properties.getProperty("http.new")); - String jndiName = properties.getProperty("database.jndi"); if (jndiName != null) { @@ -112,115 +109,30 @@ public class DataManager { dataSource = ds; } - // Load statements from configuration - String query; - - query = properties.getProperty("database.selectDevice"); - if (query != null) { - queryGetDevices = new NamedParameterStatement(query, dataSource); - } - - query = properties.getProperty("database.insertPosition"); - if (query != null) { - queryAddPosition = new NamedParameterStatement(query, dataSource, Statement.RETURN_GENERATED_KEYS); - } - - query = properties.getProperty("database.updateLatestPosition"); - if (query != null) { - queryUpdateLatestPosition = new NamedParameterStatement(query, dataSource); - } - - if (useNewDatabase) { + if (Boolean.valueOf(properties.getProperty("http.new"))) { createDatabaseSchema(); } } - private final NamedParameterStatement.ResultSetProcessor<Device> deviceResultSetProcessor = new NamedParameterStatement.ResultSetProcessor<Device>() { - @Override - public Device processNextRow(ResultSet rs) throws SQLException { - Device device = new Device(); - device.setId(rs.getLong("id")); - device.setUniqueId(rs.getString("imei")); - return device; - } - }; - - public List<Device> getDevices() throws SQLException { - if (queryGetDevices != null) { - return queryGetDevices.prepare().executeQuery(deviceResultSetProcessor); - } else { - return new LinkedList<Device>(); - } - } - - /** - * Devices cache - */ - private Map<String, Device> devices; - private Calendar devicesLastUpdate; - private long devicesRefreshDelay; - private static final long DEFAULT_REFRESH_DELAY = 300; - public Device getDeviceByUniqueId(String uniqueId) throws SQLException { - if (devices == null || !devices.containsKey(uniqueId) || - (Calendar.getInstance().getTimeInMillis() - devicesLastUpdate.getTimeInMillis() > devicesRefreshDelay)) { + if ((new Date().getTime() - devicesLastUpdate > devicesRefreshDelay) || !devices.containsKey(uniqueId)) { - devices = new HashMap<String, Device>(); - for (Device device : getDevices()) { + devices.clear(); + for (Device device : getAllDevices()) { devices.put(device.getUniqueId(), device); } - devicesLastUpdate = Calendar.getInstance(); + devicesLastUpdate = new Date().getTime(); } return devices.get(uniqueId); } - private NamedParameterStatement.ResultSetProcessor<Long> generatedKeysResultSetProcessor = new NamedParameterStatement.ResultSetProcessor<Long>() { - @Override - public Long processNextRow(ResultSet rs) throws SQLException { - return rs.getLong(1); - } - }; - - public synchronized Long addPosition(Position position) throws SQLException { - if (queryAddPosition != null) { - List<Long> result = assignVariables(queryAddPosition.prepare(), position).executeUpdate(generatedKeysResultSetProcessor); - if (result != null && !result.isEmpty()) { - return result.iterator().next(); - } - } - return null; - } - - public void updateLatestPosition(Position position, Long positionId) throws SQLException { - if (queryUpdateLatestPosition != null) { - assignVariables(queryUpdateLatestPosition.prepare(), position).setLong("id", positionId).executeUpdate(); - } - } - - private NamedParameterStatement.Params assignVariables(NamedParameterStatement.Params params, Position position) throws SQLException { - - params.setString("protocol", position.getProtocol()); - params.setLong("deviceId", position.getDeviceId()); - params.setTimestamp("deviceTime", position.getDeviceTime()); - params.setTimestamp("fixTime", position.getFixTime()); - params.setBoolean("valid", position.getValid()); - params.setDouble("altitude", position.getAltitude()); - params.setDouble("latitude", position.getLatitude()); - params.setDouble("longitude", position.getLongitude()); - params.setDouble("speed", position.getSpeed()); - params.setDouble("course", position.getCourse()); - params.setString("address", position.getAddress()); - params.setString("other", position.getOther()); - - // temporary - params.setTimestamp("time", position.getFixTime()); - params.setLong("device_id", position.getDeviceId()); - params.setLong("power", null); - params.setString("extended_info", position.getOther()); - - return params; + // TODO: possibly remove this method + public void updateLatestPosition(Position position) throws SQLException { + QueryBuilder.create(dataSource, properties.getProperty("database.updateLatestPosition")) + .setObject(position) + .executeUpdate(); } private void createDatabaseSchema() throws SQLException { @@ -395,6 +307,11 @@ public class DataManager { .executeQuery(new Permission()); } + public Collection<Device> getAllDevices() throws SQLException { + return QueryBuilder.create(dataSource, properties.getProperty("database.selectDeviceAll")) + .executeQuery(new Device()); + } + public Collection<Device> getDevices(long userId) throws SQLException { return QueryBuilder.create(dataSource, "SELECT * FROM device WHERE id IN (" + @@ -440,6 +357,16 @@ public class DataManager { .setDate("to", to) .executeQuery(new Position()); } + + public void addPosition(Position position) throws SQLException { + position.setId(QueryBuilder.create(dataSource, properties.getProperty("database.insertPosition")) + .setObject(position) + .setDate("time", position.getFixTime()) // tmp + .setLong("device_id", position.getDeviceId()) // tmp + .setLong("power", 0) // tmp + .setString("extended_info", position.getOther()) // tmp + .executeUpdate()); + } public Server getServer() throws SQLException { return QueryBuilder.create(dataSource, |