diff options
Diffstat (limited to 'src/org/traccar/model/DatabaseDataManager.java')
-rw-r--r-- | src/org/traccar/model/DatabaseDataManager.java | 87 |
1 files changed, 47 insertions, 40 deletions
diff --git a/src/org/traccar/model/DatabaseDataManager.java b/src/org/traccar/model/DatabaseDataManager.java index 3799c5461..f8d8806a7 100644 --- a/src/org/traccar/model/DatabaseDataManager.java +++ b/src/org/traccar/model/DatabaseDataManager.java @@ -58,7 +58,7 @@ public class DatabaseDataManager implements DataManager { if (driverFile != null) { URL url = new URL("jar:file:" + new File(driverFile).getAbsolutePath() + "!/"); - URLClassLoader cl = new URLClassLoader(new URL[] { url }); + URLClassLoader cl = new URLClassLoader(new URL[]{url}); Driver d = (Driver) Class.forName(driver, true, cl).newInstance(); DriverManager.registerDriver(new DriverDelegate(d)); } else { @@ -120,7 +120,7 @@ public class DatabaseDataManager implements DataManager { if (devices == null || !devices.containsKey(imei)) { devices = new HashMap<String, Device>(); - for (Device device: getDevices()) { + for (Device device : getDevices()) { devices.put(device.getImei(), device); } } @@ -134,39 +134,7 @@ public class DatabaseDataManager implements DataManager { if (queryAddPosition != null) { queryAddPosition.prepare(Statement.RETURN_GENERATED_KEYS); - queryAddPosition.setLong("device_id", position.getDeviceId()); - queryAddPosition.setTimestamp("time", position.getTime()); - queryAddPosition.setBoolean("valid", position.getValid()); - queryAddPosition.setDouble("altitude", position.getAltitude()); - queryAddPosition.setDouble("latitude", position.getLatitude()); - queryAddPosition.setDouble("longitude", position.getLongitude()); - queryAddPosition.setDouble("speed", position.getSpeed()); - queryAddPosition.setDouble("course", position.getCourse()); - queryAddPosition.setString("address", position.getAddress()); - queryAddPosition.setString("extended_info", position.getExtendedInfo()); - - // DELME: Temporary compatibility support - XPath xpath = XPathFactory.newInstance().newXPath(); - try { - InputSource source = new InputSource(new StringReader(position.getExtendedInfo())); - String index = xpath.evaluate("/info/index", source); - if (!index.isEmpty()) { - queryAddPosition.setLong("id", Long.valueOf(index)); - } else { - queryAddPosition.setLong("id", null); - } - source = new InputSource(new StringReader(position.getExtendedInfo())); - String power = xpath.evaluate("/info/power", source); - if (!power.isEmpty()) { - queryAddPosition.setDouble("power", Double.valueOf(power)); - } else { - queryAddPosition.setLong("power", null); - } - } catch (XPathExpressionException e) { - Log.warning("Error in XML: " + position.getExtendedInfo(), e); - queryAddPosition.setLong("id", null); - queryAddPosition.setLong("power", null); - } + queryAddPosition = assignVariables(queryAddPosition, position); queryAddPosition.executeUpdate(); @@ -180,16 +148,55 @@ public class DatabaseDataManager implements DataManager { } @Override - public void updateLatestPosition(Long deviceId, Long positionId) throws SQLException { - + public void updateLatestPosition(Position position, Long positionId) throws SQLException { if (queryUpdateLatestPosition != null) { queryUpdateLatestPosition.prepare(); - - queryUpdateLatestPosition.setLong("device_id", deviceId); + + queryUpdateLatestPosition = assignVariables(queryUpdateLatestPosition, position); queryUpdateLatestPosition.setLong("id", positionId); - + queryUpdateLatestPosition.executeUpdate(); } } + private NamedParameterStatement assignVariables(NamedParameterStatement preparedStatement, Position position) throws SQLException { + + preparedStatement.setLong("device_id", position.getDeviceId()); + preparedStatement.setTimestamp("time", position.getTime()); + preparedStatement.setBoolean("valid", position.getValid()); + preparedStatement.setDouble("altitude", position.getAltitude()); + preparedStatement.setDouble("latitude", position.getLatitude()); + preparedStatement.setDouble("longitude", position.getLongitude()); + preparedStatement.setDouble("speed", position.getSpeed()); + preparedStatement.setDouble("course", position.getCourse()); + preparedStatement.setString("address", position.getAddress()); + preparedStatement.setString("extended_info", position.getExtendedInfo()); + + // DELME: Temporary compatibility support + XPath xpath = XPathFactory.newInstance().newXPath(); + try { + InputSource source = new InputSource(new StringReader(position.getExtendedInfo())); + String index = xpath.evaluate("/info/index", source); + if (!index.isEmpty()) { + preparedStatement.setLong("id", Long.valueOf(index)); + } else { + preparedStatement.setLong("id", null); + } + source = new InputSource(new StringReader(position.getExtendedInfo())); + String power = xpath.evaluate("/info/power", source); + if (!power.isEmpty()) { + preparedStatement.setDouble("power", Double.valueOf(power)); + } else { + preparedStatement.setLong("power", null); + } + } catch (XPathExpressionException e) { + Log.warning("Error in XML: " + position.getExtendedInfo(), e); + preparedStatement.setLong("id", null); + preparedStatement.setLong("power", null); + } + + return preparedStatement; + + } + } |