aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database/DataManager.java
diff options
context:
space:
mode:
authorHans van den Elsen <hans.elsen@esds.nl>2016-03-11 22:16:25 +0100
committerHans van den Elsen <hans.elsen@esds.nl>2016-03-11 22:16:25 +0100
commit4606737cc07b736f9c8f98ae680b928c94c082c8 (patch)
treebdd29b24a16403b1e8a3d7cd4325391063b34b8a /src/org/traccar/database/DataManager.java
parent3ced99384b1967f5cde9abdf30c1c0184a5ae3f6 (diff)
parentab5d21e16ac44957e04da0f06daf148af95e96be (diff)
downloadtrackermap-server-4606737cc07b736f9c8f98ae680b928c94c082c8.tar.gz
trackermap-server-4606737cc07b736f9c8f98ae680b928c94c082c8.tar.bz2
trackermap-server-4606737cc07b736f9c8f98ae680b928c94c082c8.zip
Merge remote-tracking branch 'refs/remotes/tananaev/master'
Diffstat (limited to 'src/org/traccar/database/DataManager.java')
-rw-r--r--src/org/traccar/database/DataManager.java101
1 files changed, 90 insertions, 11 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 73984952e..d2da4e701 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012 - 2015 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2012 - 2016 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
@@ -36,10 +37,13 @@ import liquibase.exception.LiquibaseException;
import liquibase.resource.FileSystemResourceAccessor;
import liquibase.resource.ResourceAccessor;
import org.traccar.Config;
+import org.traccar.Context;
import org.traccar.helper.Log;
import org.traccar.model.Device;
+import org.traccar.model.Group;
+import org.traccar.model.GroupPermission;
import org.traccar.model.MiscFormatter;
-import org.traccar.model.Permission;
+import org.traccar.model.DevicePermission;
import org.traccar.model.Position;
import org.traccar.model.Server;
import org.traccar.model.User;
@@ -53,10 +57,14 @@ public class DataManager implements IdentityManager {
private DataSource dataSource;
+ private final long dataRefreshDelay;
+
private final Map<Long, Device> devicesById = new HashMap<>();
private final Map<String, Device> devicesByUniqueId = new HashMap<>();
private long devicesLastUpdate;
- private final long devicesRefreshDelay;
+
+ private final Map<Long, Group> groupsById = new HashMap<>();
+ private long groupsLastUpdate;
public DataManager(Config config) throws Exception {
this.config = config;
@@ -64,7 +72,7 @@ public class DataManager implements IdentityManager {
initDatabase();
initDatabaseSchema();
- devicesRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000;
+ dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000;
}
public DataSource getDataSource() {
@@ -112,7 +120,7 @@ public class DataManager implements IdentityManager {
}
private void updateDeviceCache(boolean force) throws SQLException {
- if (System.currentTimeMillis() - devicesLastUpdate > devicesRefreshDelay || force) {
+ if (System.currentTimeMillis() - devicesLastUpdate > dataRefreshDelay || force) {
devicesById.clear();
devicesByUniqueId.clear();
for (Device device : getAllDevices()) {
@@ -140,6 +148,25 @@ public class DataManager implements IdentityManager {
return devicesByUniqueId.get(uniqueId);
}
+ private void updateGroupCache(boolean force) throws SQLException {
+ if (System.currentTimeMillis() - groupsLastUpdate > dataRefreshDelay || force) {
+ groupsById.clear();
+ for (Group group : getAllGroups()) {
+ groupsById.put(group.getId(), group);
+ }
+ groupsLastUpdate = System.currentTimeMillis();
+ }
+ }
+
+ public Group getGroupById(long id) {
+ try {
+ updateGroupCache(!groupsById.containsKey(id));
+ } catch (SQLException e) {
+ Log.warning(e);
+ }
+ return groupsById.get(id);
+ }
+
private String getQuery(String key) {
String query = config.getString(key);
if (query == null) {
@@ -221,9 +248,14 @@ public class DataManager implements IdentityManager {
.executeUpdate();
}
- public Collection<Permission> getPermissions() throws SQLException {
- return QueryBuilder.create(dataSource, getQuery("database.getPermissionsAll"))
- .executeQuery(Permission.class);
+ public Collection<DevicePermission> getDevicePermissions() throws SQLException {
+ return QueryBuilder.create(dataSource, getQuery("database.selectDevicePermissions"))
+ .executeQuery(DevicePermission.class);
+ }
+
+ public Collection<GroupPermission> getGroupPermissions() throws SQLException {
+ return QueryBuilder.create(dataSource, getQuery("database.selectGroupPermissions"))
+ .executeQuery(GroupPermission.class);
}
public Collection<Device> getAllDevices() throws SQLException {
@@ -232,9 +264,11 @@ public class DataManager implements IdentityManager {
}
public Collection<Device> getDevices(long userId) throws SQLException {
- return QueryBuilder.create(dataSource, getQuery("database.selectDevices"))
- .setLong("userId", userId)
- .executeQuery(Device.class);
+ Collection<Device> devices = new ArrayList<>();
+ for (long id : Context.getPermissionsManager().getDevicePermissions(userId)) {
+ devices.add(getDeviceById(id));
+ }
+ return devices;
}
public void addDevice(Device device) throws SQLException {
@@ -286,6 +320,51 @@ public class DataManager implements IdentityManager {
AsyncServlet.sessionRefreshUser(userId);
}
+ public Collection<Group> getAllGroups() throws SQLException {
+ return QueryBuilder.create(dataSource, getQuery("database.selectGroupsAll"))
+ .executeQuery(Group.class);
+ }
+
+ public Collection<Group> getGroups(long userId) throws SQLException {
+ Collection<Group> groups = new ArrayList<>();
+ for (long id : Context.getPermissionsManager().getGroupPermissions(userId)) {
+ groups.add(getGroupById(id));
+ }
+ return groups;
+ }
+
+ public void addGroup(Group group) throws SQLException {
+ group.setId(QueryBuilder.create(dataSource, getQuery("database.insertGroup"), true)
+ .setObject(group)
+ .executeUpdate());
+ }
+
+ public void updateGroup(Group group) throws SQLException {
+ QueryBuilder.create(dataSource, getQuery("database.updateGroup"))
+ .setObject(group)
+ .executeUpdate();
+ }
+
+ public void removeGroup(long groupId) throws SQLException {
+ QueryBuilder.create(dataSource, getQuery("database.deleteGroup"))
+ .setLong("id", groupId)
+ .executeUpdate();
+ }
+
+ public void linkGroup(long userId, long groupId) throws SQLException {
+ QueryBuilder.create(dataSource, getQuery("database.linkGroup"))
+ .setLong("userId", userId)
+ .setLong("groupId", groupId)
+ .executeUpdate();
+ }
+
+ public void unlinkGroup(long userId, long groupId) throws SQLException {
+ QueryBuilder.create(dataSource, getQuery("database.unlinkGroup"))
+ .setLong("userId", userId)
+ .setLong("groupId", groupId)
+ .executeUpdate();
+ }
+
public Collection<Position> getPositions(long deviceId, Date from, Date to) throws SQLException {
return QueryBuilder.create(dataSource, getQuery("database.selectPositions"))
.setLong("deviceId", deviceId)