From f1466358d853516cd69a6effc678798f2b7ec20b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 2 Jun 2015 18:49:33 +1200 Subject: Re-factor old database methods --- src/org/traccar/TrackerEventHandler.java | 22 +++-- src/org/traccar/database/DataManager.java | 145 ++++++++---------------------- 2 files changed, 46 insertions(+), 121 deletions(-) (limited to 'src') 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 positions = (List) 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 devices = new HashMap(); + 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 deviceResultSetProcessor = new NamedParameterStatement.ResultSetProcessor() { - @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 getDevices() throws SQLException { - if (queryGetDevices != null) { - return queryGetDevices.prepare().executeQuery(deviceResultSetProcessor); - } else { - return new LinkedList(); - } - } - - /** - * Devices cache - */ - private Map 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(); - 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 generatedKeysResultSetProcessor = new NamedParameterStatement.ResultSetProcessor() { - @Override - public Long processNextRow(ResultSet rs) throws SQLException { - return rs.getLong(1); - } - }; - - public synchronized Long addPosition(Position position) throws SQLException { - if (queryAddPosition != null) { - List 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 getAllDevices() throws SQLException { + return QueryBuilder.create(dataSource, properties.getProperty("database.selectDeviceAll")) + .executeQuery(new Device()); + } + public Collection 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, -- cgit v1.2.3