aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r--src/org/traccar/database/ConnectionManager.java17
-rw-r--r--src/org/traccar/database/DataManager.java8
-rw-r--r--src/org/traccar/database/QueryBuilder.java170
3 files changed, 106 insertions, 89 deletions
diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java
index e45c83651..450f2f61f 100644
--- a/src/org/traccar/database/ConnectionManager.java
+++ b/src/org/traccar/database/ConnectionManager.java
@@ -18,6 +18,7 @@ package org.traccar.database;
import java.net.SocketAddress;
import java.sql.SQLException;
import java.util.Collection;
+import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
@@ -27,6 +28,7 @@ import java.util.Set;
import org.jboss.netty.channel.Channel;
import org.traccar.Protocol;
import org.traccar.helper.Log;
+import org.traccar.model.Device;
import org.traccar.model.Position;
public class ConnectionManager {
@@ -47,13 +49,14 @@ public class ConnectionManager {
}
}
- public void setActiveDevice(long deviceId, Protocol protocol, Channel channel, SocketAddress remoteAddress) {
+ public void addActiveDevice(long deviceId, Protocol protocol, Channel channel, SocketAddress remoteAddress) {
activeDevices.put(deviceId, new ActiveDevice(deviceId, protocol, channel, remoteAddress));
}
public void removeActiveDevice(Channel channel) {
for (ActiveDevice activeDevice : activeDevices.values()) {
if (activeDevice.getChannel() == channel) {
+ updateDevice(activeDevice.getDeviceId(), Device.STATUS_OFFLINE, new Date());
activeDevices.remove(activeDevice.getDeviceId());
break;
}
@@ -64,12 +67,18 @@ public class ConnectionManager {
return activeDevices.get(deviceId);
}
- public synchronized void update(Position position) {
+ public synchronized void updateDevice(long deviceId, String status, Date time) {
+ // TODO update cache and call listener
+ /*Log.debug(deviceId + " " + status + " "
+ + new SimpleDateFormat(Log.DATE_FORMAT).format(time));*/
+ }
+
+ public synchronized void updatePosition(Position position) {
long deviceId = position.getDeviceId();
positions.put(deviceId, position);
if (listeners.containsKey(deviceId)) {
for (DataCacheListener listener : listeners.get(deviceId)) {
- listener.onUpdate(position);
+ listener.onUpdatePosition(position);
}
}
}
@@ -92,7 +101,7 @@ public class ConnectionManager {
}
public interface DataCacheListener {
- void onUpdate(Position position);
+ void onUpdatePosition(Position position);
}
public void addListener(Collection<Long> devices, DataCacheListener listener) {
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index af2dd559a..24a07a05c 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -172,7 +172,7 @@ public class DataManager implements IdentityManager {
version = schema.getVersion();
}
- if (version != 301) {
+ if (version != 302) {
Log.error("Wrong database schema version (" + version + ")");
throw new RuntimeException();
}
@@ -241,7 +241,11 @@ public class DataManager implements IdentityManager {
User user = QueryBuilder.create(dataSource, getQuery("database.loginUser"))
.setString("email", email)
.executeQuerySingle(new User());
- return user != null && user.isPasswordValid(password) ? user : null;
+ if (user != null && user.isPasswordValid(password)) {
+ return user;
+ } else {
+ return null;
+ }
}
public Collection<User> getUsers() throws SQLException {
diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java
index c3cde0723..ca6335556 100644
--- a/src/org/traccar/database/QueryBuilder.java
+++ b/src/org/traccar/database/QueryBuilder.java
@@ -281,6 +281,92 @@ public final class QueryBuilder {
}
}
+ private <T extends Factory> void addProcessors(
+ List<ResultSetProcessor<T>> processors, Class<?> parameterType, final Method method, final String name) {
+
+ if (parameterType.equals(boolean.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ method.invoke(object, resultSet.getBoolean(name));
+ } catch (IllegalAccessException | InvocationTargetException error) {
+ Log.warning(error);
+ }
+ }
+ });
+ } else if (parameterType.equals(int.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ method.invoke(object, resultSet.getInt(name));
+ } catch (IllegalAccessException | InvocationTargetException error) {
+ Log.warning(error);
+ }
+ }
+ });
+ } else if (parameterType.equals(long.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ method.invoke(object, resultSet.getLong(name));
+ } catch (IllegalAccessException | InvocationTargetException error) {
+ Log.warning(error);
+ }
+ }
+ });
+ } else if (parameterType.equals(double.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ method.invoke(object, resultSet.getDouble(name));
+ } catch (IllegalAccessException | InvocationTargetException error) {
+ Log.warning(error);
+ }
+ }
+ });
+ } else if (parameterType.equals(String.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ method.invoke(object, resultSet.getString(name));
+ } catch (IllegalAccessException | InvocationTargetException error) {
+ Log.warning(error);
+ }
+ }
+ });
+ } else if (parameterType.equals(Date.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try {
+ Timestamp timestamp = resultSet.getTimestamp(name);
+ if (timestamp != null) {
+ method.invoke(object, new Date(timestamp.getTime()));
+ }
+ } catch (IllegalAccessException | InvocationTargetException error) {
+ Log.warning(error);
+ }
+ }
+ });
+ } else if (parameterType.equals(Map.class)) {
+ processors.add(new ResultSetProcessor<T>() {
+ @Override
+ public void process(T object, ResultSet resultSet) throws SQLException {
+ try (JsonReader reader = Json.createReader(new StringReader(resultSet.getString(name)))) {
+ method.invoke(object, MiscFormatter.fromJson(reader.readObject()));
+ } catch (IllegalAccessException | InvocationTargetException | JsonParsingException error) {
+ Log.warning(error);
+ }
+ }
+ });
+ }
+ }
+
public <T extends Factory> Collection<T> executeQuery(T prototype) throws SQLException {
List<T> result = new LinkedList<>();
@@ -313,89 +399,7 @@ public final class QueryBuilder {
continue;
}
- Class<?> parameterType = method.getParameterTypes()[0];
-
- if (parameterType.equals(boolean.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- method.invoke(object, resultSet.getBoolean(name));
- } catch (IllegalAccessException | InvocationTargetException error) {
- Log.warning(error);
- }
- }
- });
- } else if (parameterType.equals(int.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- method.invoke(object, resultSet.getInt(name));
- } catch (IllegalAccessException | InvocationTargetException error) {
- Log.warning(error);
- }
- }
- });
- } else if (parameterType.equals(long.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- method.invoke(object, resultSet.getLong(name));
- } catch (IllegalAccessException | InvocationTargetException error) {
- Log.warning(error);
- }
- }
- });
- } else if (parameterType.equals(double.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- method.invoke(object, resultSet.getDouble(name));
- } catch (IllegalAccessException | InvocationTargetException error) {
- Log.warning(error);
- }
- }
- });
- } else if (parameterType.equals(String.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- method.invoke(object, resultSet.getString(name));
- } catch (IllegalAccessException | InvocationTargetException error) {
- Log.warning(error);
- }
- }
- });
- } else if (parameterType.equals(Date.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try {
- Timestamp timestamp = resultSet.getTimestamp(name);
- if (timestamp != null) {
- method.invoke(object, new Date(timestamp.getTime()));
- }
- } catch (IllegalAccessException | InvocationTargetException error) {
- Log.warning(error);
- }
- }
- });
- } else if (parameterType.equals(Map.class)) {
- processors.add(new ResultSetProcessor<T>() {
- @Override
- public void process(T object, ResultSet resultSet) throws SQLException {
- try (JsonReader reader = Json.createReader(new StringReader(resultSet.getString(name)))) {
- method.invoke(object, MiscFormatter.fromJson(reader.readObject()));
- } catch (IllegalAccessException | InvocationTargetException | JsonParsingException error) {
- Log.warning(error);
- }
- }
- });
- }
+ addProcessors(processors, method.getParameterTypes()[0], method, name);
}
}