aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database/DataManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/database/DataManager.java')
-rw-r--r--src/org/traccar/database/DataManager.java309
1 files changed, 11 insertions, 298 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 4bf4b1b9a..6c9cca86c 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -23,6 +23,7 @@ import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
+import java.util.Map;
import javax.naming.InitialContext;
import javax.sql.DataSource;
@@ -39,17 +40,12 @@ import org.traccar.Config;
import org.traccar.helper.Log;
import org.traccar.model.AttributeAlias;
import org.traccar.model.Device;
-import org.traccar.model.DevicePermission;
import org.traccar.model.Event;
-import org.traccar.model.Group;
-import org.traccar.model.GroupPermission;
import org.traccar.model.Identifiable;
-import org.traccar.model.Notification;
import org.traccar.model.Position;
import org.traccar.model.Server;
import org.traccar.model.Statistics;
import org.traccar.model.User;
-import org.traccar.model.UserPermission;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@@ -153,17 +149,6 @@ public class DataManager {
}
}
- public Collection<User> getUsers() throws SQLException {
- return QueryBuilder.create(dataSource, getQuery("database.selectUsersAll"))
- .executeQuery(User.class);
- }
-
- public void addUser(User user) throws SQLException {
- user.setId(QueryBuilder.create(dataSource, getQuery("database.insertUser"), true)
- .setObject(user)
- .executeUpdate());
- }
-
public void updateUser(User user) throws SQLException {
QueryBuilder.create(dataSource, getQuery("database.updateUser"))
.setObject(user)
@@ -175,102 +160,12 @@ public class DataManager {
}
}
- public void removeUser(long userId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.deleteUser"))
- .setLong("id", userId)
- .executeUpdate();
- }
-
- 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 {
- return QueryBuilder.create(dataSource, getQuery("database.selectDevicesAll"))
- .executeQuery(Device.class);
- }
-
- public void addDevice(Device device) throws SQLException {
- device.setId(QueryBuilder.create(dataSource, getQuery("database.insertDevice"), true)
- .setObject(device)
- .executeUpdate());
- }
-
- public void updateDevice(Device device) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.updateDevice"))
- .setObject(device)
- .executeUpdate();
- }
-
public void updateDeviceStatus(Device device) throws SQLException {
QueryBuilder.create(dataSource, getQuery("database.updateDeviceStatus"))
.setObject(device)
.executeUpdate();
}
- public void removeDevice(long deviceId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.deleteDevice"))
- .setLong("id", deviceId)
- .executeUpdate();
- }
-
- public void linkDevice(long userId, long deviceId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkDevice"))
- .setLong("userId", userId)
- .setLong("deviceId", deviceId)
- .executeUpdate();
- }
-
- public void unlinkDevice(long userId, long deviceId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkDevice"))
- .setLong("userId", userId)
- .setLong("deviceId", deviceId)
- .executeUpdate();
- }
-
- public Collection<Group> getAllGroups() throws SQLException {
- return QueryBuilder.create(dataSource, getQuery("database.selectGroupsAll"))
- .executeQuery(Group.class);
- }
-
- 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)
@@ -323,24 +218,12 @@ public class DataManager {
.executeQuerySingle(Server.class);
}
- public void updateServer(Server server) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.updateServer"))
- .setObject(server)
- .executeUpdate();
- }
-
public Event getEvent(long eventId) throws SQLException {
return QueryBuilder.create(dataSource, getQuery("database.selectEvent"))
.setLong("id", eventId)
.executeQuerySingle(Event.class);
}
- public void addEvent(Event event) throws SQLException {
- event.setId(QueryBuilder.create(dataSource, getQuery("database.insertEvent"), true)
- .setObject(event)
- .executeUpdate());
- }
-
public Collection<Event> getEvents(long deviceId, Date from, Date to) throws SQLException {
return QueryBuilder.create(dataSource, getQuery("database.selectEvents"))
.setLong("deviceId", deviceId)
@@ -349,71 +232,6 @@ public class DataManager {
.executeQuery(Event.class);
}
- public void linkGeofence(long userId, long geofenceId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkGeofence"))
- .setLong("userId", userId)
- .setLong("geofenceId", geofenceId)
- .executeUpdate();
- }
-
- public void unlinkGeofence(long userId, long geofenceId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkGeofence"))
- .setLong("userId", userId)
- .setLong("geofenceId", geofenceId)
- .executeUpdate();
- }
-
- public void linkGroupGeofence(long groupId, long geofenceId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkGroupGeofence"))
- .setLong("groupId", groupId)
- .setLong("geofenceId", geofenceId)
- .executeUpdate();
- }
-
- public void unlinkGroupGeofence(long groupId, long geofenceId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkGroupGeofence"))
- .setLong("groupId", groupId)
- .setLong("geofenceId", geofenceId)
- .executeUpdate();
- }
-
- public void linkDeviceGeofence(long deviceId, long geofenceId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkDeviceGeofence"))
- .setLong("deviceId", deviceId)
- .setLong("geofenceId", geofenceId)
- .executeUpdate();
- }
-
- public void unlinkDeviceGeofence(long deviceId, long geofenceId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkDeviceGeofence"))
- .setLong("deviceId", deviceId)
- .setLong("geofenceId", geofenceId)
- .executeUpdate();
- }
-
- public Collection<Notification> getNotifications() throws SQLException {
- return QueryBuilder.create(dataSource, getQuery("database.selectNotifications"))
- .executeQuery(Notification.class);
- }
-
- public void addNotification(Notification notification) throws SQLException {
- notification.setId(QueryBuilder.create(dataSource, getQuery("database.insertNotification"), true)
- .setObject(notification)
- .executeUpdate());
- }
-
- public void updateNotification(Notification notification) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.updateNotification"))
- .setObject(notification)
- .executeUpdate();
- }
-
- public void removeNotification(Notification notification) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.deleteNotification"))
- .setLong("id", notification.getId())
- .executeUpdate();
- }
-
public Collection<AttributeAlias> getAttributeAliases() throws SQLException {
return QueryBuilder.create(dataSource, getQuery("database.selectAttributeAliases"))
.executeQuery(AttributeAlias.class);
@@ -450,121 +268,16 @@ public class DataManager {
.executeUpdate());
}
- public void linkCalendar(long userId, long calendarId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkCalendar"))
- .setLong("userId", userId)
- .setLong("calendarId", calendarId)
- .executeUpdate();
- }
-
- public void unlinkCalendar(long userId, long calendarId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkCalendar"))
- .setLong("userId", userId)
- .setLong("calendarId", calendarId)
- .executeUpdate();
- }
-
- public Collection<UserPermission> getUserPermissions() throws SQLException {
- return QueryBuilder.create(dataSource, getQuery("database.selectUserPermissions"))
- .executeQuery(UserPermission.class);
- }
-
- public void linkUser(long userId, long managedUserId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkUser"))
- .setLong("userId", userId)
- .setLong("managedUserId", managedUserId)
- .executeUpdate();
- }
-
- public void unlinkUser(long userId, long managedUserId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkUser"))
- .setLong("userId", userId)
- .setLong("managedUserId", managedUserId)
- .executeUpdate();
- }
-
- public void linkAttribute(long userId, long attributeId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkAttribute"))
- .setLong("userId", userId)
- .setLong("attributeId", attributeId)
- .executeUpdate();
- }
-
- public void unlinkAttribute(long userId, long attributeId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkAttribute"))
- .setLong("userId", userId)
- .setLong("attributeId", attributeId)
- .executeUpdate();
- }
-
- public void linkGroupAttribute(long groupId, long attributeId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkGroupAttribute"))
- .setLong("groupId", groupId)
- .setLong("attributeId", attributeId)
- .executeUpdate();
- }
-
- public void unlinkGroupAttribute(long groupId, long attributeId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkGroupAttribute"))
- .setLong("groupId", groupId)
- .setLong("attributeId", attributeId)
- .executeUpdate();
- }
-
- public void linkDeviceAttribute(long deviceId, long attributeId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkDeviceAttribute"))
- .setLong("deviceId", deviceId)
- .setLong("attributeId", attributeId)
- .executeUpdate();
- }
-
- public void unlinkDeviceAttribute(long deviceId, long attributeId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkDeviceAttribute"))
- .setLong("deviceId", deviceId)
- .setLong("attributeId", attributeId)
- .executeUpdate();
- }
-
- public void linkDriver(long userId, long driverId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkDriver"))
- .setLong("userId", userId)
- .setLong("driverId", driverId)
- .executeUpdate();
- }
-
- public void unlinkDriver(long userId, long driverId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkDriver"))
- .setLong("userId", userId)
- .setLong("driverId", driverId)
- .executeUpdate();
- }
-
- public void linkGroupDriver(long groupId, long driverId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkGroupDriver"))
- .setLong("groupId", groupId)
- .setLong("driverId", driverId)
- .executeUpdate();
- }
-
- public void unlinkGroupDriver(long groupId, long driverId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkGroupDriver"))
- .setLong("groupId", groupId)
- .setLong("driverId", driverId)
- .executeUpdate();
- }
-
- public void linkDeviceDriver(long deviceId, long driverId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.linkDeviceDriver"))
- .setLong("deviceId", deviceId)
- .setLong("driverId", driverId)
- .executeUpdate();
- }
-
- public void unlinkDeviceDriver(long deviceId, long driverId) throws SQLException {
- QueryBuilder.create(dataSource, getQuery("database.unlinkDeviceDriver"))
- .setLong("deviceId", deviceId)
- .setLong("driverId", driverId)
- .executeUpdate();
+ public void linkObject(Map<String, Long> permission, boolean link) throws SQLException {
+ String query = "database." + (!link ? "un" : "") + "link";
+ for (String key : permission.keySet()) {
+ query += key.substring(0, 1).toUpperCase() + key.replace("Id", "").substring(1);
+ }
+ QueryBuilder queryBuilder = QueryBuilder.create(dataSource, getQuery(query));
+ for (String key : permission.keySet()) {
+ queryBuilder.setLong(key, permission.get(key));
+ }
+ queryBuilder.executeUpdate();
}
public <T> Collection<T> getObjects(Class<T> clazz) throws SQLException {