aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/model/DatabaseDataManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/model/DatabaseDataManager.java')
-rw-r--r--src/org/traccar/model/DatabaseDataManager.java130
1 files changed, 26 insertions, 104 deletions
diff --git a/src/org/traccar/model/DatabaseDataManager.java b/src/org/traccar/model/DatabaseDataManager.java
index 073d6ef71..ee86e6370 100644
--- a/src/org/traccar/model/DatabaseDataManager.java
+++ b/src/org/traccar/model/DatabaseDataManager.java
@@ -18,7 +18,6 @@ package org.traccar.model;
import java.sql.*;
import java.util.*;
import org.traccar.helper.AdvancedConnection;
-import org.traccar.helper.Log;
import org.traccar.helper.NamedParameterStatement;
/**
@@ -35,11 +34,8 @@ public class DatabaseDataManager implements DataManager {
* Database statements
*/
private NamedParameterStatement queryGetDevices;
- private NamedParameterStatement queryAddDevice;
- private NamedParameterStatement queryUpdateDevice;
- private NamedParameterStatement queryRemoveDevice;
- private NamedParameterStatement queryGetPositions;
private NamedParameterStatement queryAddPosition;
+ private NamedParameterStatement queryUpdateLatestPosition;
/**
* Initialize database
@@ -75,42 +71,18 @@ public class DatabaseDataManager implements DataManager {
queryGetDevices = new NamedParameterStatement(connection, query);
}
- query = properties.getProperty("database.insertDevice");
- if (query != null) {
- queryAddDevice = new NamedParameterStatement(connection, query);
- }
-
- query = properties.getProperty("database.updateDevice");
- if (query != null) {
- queryUpdateDevice = new NamedParameterStatement(connection, query);
- }
-
- query = properties.getProperty("database.deleteDevice");
- if (query != null) {
- queryRemoveDevice = new NamedParameterStatement(connection, query);
- }
-
- query = properties.getProperty("database.selectPosition");
- if (query != null) {
- queryGetPositions = new NamedParameterStatement(connection, query);
- }
-
query = properties.getProperty("database.insertPosition");
if (query != null) {
queryAddPosition = new NamedParameterStatement(connection, query);
}
- // Create database schema
- query = properties.getProperty("database.initialize");
- if (query != null) try {
- NamedParameterStatement initializeQuery = new NamedParameterStatement(connection, query);
- initializeQuery.prepare();
- initializeQuery.executeUpdate();
- } catch (Exception error) {
- Log.warning(error.getMessage());
+ query = properties.getProperty("database.updateLatestPosition");
+ if (query != null) {
+ queryUpdateLatestPosition = new NamedParameterStatement(connection, query);
}
}
+ @Override
public synchronized List<Device> getDevices() throws SQLException {
List<Device> deviceList = new LinkedList<Device>();
@@ -127,40 +99,6 @@ public class DatabaseDataManager implements DataManager {
return deviceList;
}
- public synchronized void addDevice(Device device) throws SQLException {
-
- queryAddDevice.prepare(Statement.RETURN_GENERATED_KEYS);
- queryAddDevice.setString("imei", device.getImei());
- queryAddDevice.executeUpdate();
-
- // Find generated id
- ResultSet result = queryAddDevice.getGeneratedKeys();
- if (result.next()) {
- device.setId(result.getLong(1));
- }
-
- devices = null;
- }
-
- public synchronized void updateDevice(Device device) throws SQLException {
-
- queryUpdateDevice.prepare();
- queryUpdateDevice.setLong("id", device.getId());
- queryUpdateDevice.setString("imei", device.getImei());
- queryUpdateDevice.executeUpdate();
-
- devices = null;
- }
-
- public synchronized void removeDevice(Device device) throws SQLException {
-
- queryRemoveDevice.prepare();
- queryRemoveDevice.setLong("id", device.getId());
- queryRemoveDevice.executeUpdate();
-
- devices = null;
- }
-
/**
* Devices cache
*/
@@ -168,6 +106,7 @@ public class DatabaseDataManager implements DataManager {
private Calendar devicesLastUpdate;
private Long devicesRefreshDelay;
+ @Override
public Device getDeviceByImei(String imei) throws SQLException {
if ((devices == null) || (Calendar.getInstance().getTimeInMillis() - devicesLastUpdate.getTimeInMillis() > devicesRefreshDelay)) {
@@ -182,43 +121,10 @@ public class DatabaseDataManager implements DataManager {
return devices.get(imei);
}
- public Device getDeviceByPhoneNumber(String phoneNumber) {
- // TODO: implement getDeviceByPhoneNumber
- return null;
- }
-
- public Device getDeviceByUniqueId(String uniqueId) {
- // TODO: implement getDeviceByUniqueId
- return null;
- }
-
- public synchronized List<Position> getPositions(Long deviceId) throws SQLException {
-
- List<Position> positionList = new LinkedList<Position>();
-
- queryGetPositions.prepare();
- queryGetPositions.setLong("device_id", deviceId);
- ResultSet result = queryGetPositions.executeQuery();
- while (result.next()) {
- // TODO: include other parameters
- Position position = new Position();
- position.setDeviceId(result.getLong("device_id"));
- position.setTime(result.getTimestamp("time"));
- position.setValid(result.getBoolean("valid"));
- position.setLatitude(result.getDouble("latitude"));
- position.setLongitude(result.getDouble("longitude"));
- position.setSpeed(result.getDouble("speed"));
- position.setCourse(result.getDouble("course"));
- position.setPower(result.getDouble("power"));
- positionList.add(position);
- }
-
- return positionList;
- }
-
- public synchronized void addPosition(Position position) throws SQLException {
+ @Override
+ public synchronized Long addPosition(Position position) throws SQLException {
- queryAddPosition.prepare();
+ queryAddPosition.prepare(Statement.RETURN_GENERATED_KEYS);
queryAddPosition.setLong("id", position.getId());
queryAddPosition.setLong("device_id", position.getDeviceId());
@@ -235,7 +141,23 @@ public class DatabaseDataManager implements DataManager {
queryAddPosition.executeUpdate();
- // TODO: probably return row id
+ ResultSet result = queryAddPosition.getGeneratedKeys();
+ if (result != null && result.next()) {
+ return result.getLong(1);
+ }
+
+ return null;
+ }
+
+ @Override
+ public void updateLatestPosition(Long deviceId, Long positionId) throws SQLException {
+
+ queryUpdateLatestPosition.prepare();
+
+ queryUpdateLatestPosition.setLong("device_id", deviceId);
+ queryUpdateLatestPosition.setLong("id", positionId);
+
+ queryUpdateLatestPosition.executeUpdate();
}
}