diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2022-02-15 22:55:53 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2022-02-15 22:55:53 -0800 |
commit | dd8dbbf6fca2c61726431a8552640f2c1499b4a2 (patch) | |
tree | 838d5d85067824443d762171644c8d785e9ca635 /src/main/java/org/traccar/storage | |
parent | 17d8ffe18170c422ac2c6dccef8361e1ca684548 (diff) | |
download | trackermap-server-dd8dbbf6fca2c61726431a8552640f2c1499b4a2.tar.gz trackermap-server-dd8dbbf6fca2c61726431a8552640f2c1499b4a2.tar.bz2 trackermap-server-dd8dbbf6fca2c61726431a8552640f2c1499b4a2.zip |
Migrate permissions queries
Diffstat (limited to 'src/main/java/org/traccar/storage')
4 files changed, 75 insertions, 1 deletions
diff --git a/src/main/java/org/traccar/storage/DatabaseStorage.java b/src/main/java/org/traccar/storage/DatabaseStorage.java index 2c893ebbc..d73dc7b25 100644 --- a/src/main/java/org/traccar/storage/DatabaseStorage.java +++ b/src/main/java/org/traccar/storage/DatabaseStorage.java @@ -1,5 +1,6 @@ package org.traccar.storage; +import org.traccar.model.Permission; import org.traccar.storage.query.Columns; import org.traccar.storage.query.Condition; import org.traccar.storage.query.Limit; @@ -29,6 +30,7 @@ public class DatabaseStorage extends Storage { query.append(" FROM ").append(getTableName(clazz)); query.append(formatCondition(request.getCondition())); query.append(formatOrder(request.getOrder())); + query.append(formatLimit(request.getLimit())); try { QueryBuilder builder = QueryBuilder.create(dataSource, query.toString()); for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) { @@ -93,6 +95,54 @@ public class DatabaseStorage extends Storage { } } + @Override + public List<Permission> getPermissions(Class<?> ownerClass, Class<?> propertyClass) throws StorageException { + StringBuilder query = new StringBuilder("SELECT * FROM "); + query.append(Permission.getStorageName(ownerClass, propertyClass)); + try { + QueryBuilder builder = QueryBuilder.create(dataSource, query.toString()); + return builder.executePermissionsQuery(); + } catch (SQLException e) { + throw new StorageException(e); + } + } + + @Override + public void addPermission(Permission permission) throws StorageException { + StringBuilder query = new StringBuilder("INSERT INTO "); + query.append(permission.getStorageName()); + query.append(" VALUES ("); + query.append(permission.get().keySet().stream().map(key -> ':' + key).collect(Collectors.joining(", "))); + query.append(")"); + try { + QueryBuilder builder = QueryBuilder.create(dataSource, query.toString(), true); + for (var entry : permission.get().entrySet()) { + builder.setLong(entry.getKey(), entry.getValue()); + } + builder.executeUpdate(); + } catch (SQLException e) { + throw new StorageException(e); + } + } + + @Override + public void removePermission(Permission permission) throws StorageException { + StringBuilder query = new StringBuilder("DELETE FROM "); + query.append(permission.getStorageName()); + query.append(" WHERE "); + query.append(permission + .get().keySet().stream().map(key -> key + " = :" + key).collect(Collectors.joining(" AND "))); + try { + QueryBuilder builder = QueryBuilder.create(dataSource, query.toString(), true); + for (var entry : permission.get().entrySet()) { + builder.setLong(entry.getKey(), entry.getValue()); + } + builder.executeUpdate(); + } catch (SQLException e) { + throw new StorageException(e); + } + } + private String getTableName(Class<?> clazz) throws StorageException { StorageName storageName = clazz.getAnnotation(StorageName.class); if (storageName == null) { diff --git a/src/main/java/org/traccar/storage/MemoryStorage.java b/src/main/java/org/traccar/storage/MemoryStorage.java index 0ebd3b87e..0a2e5a81f 100644 --- a/src/main/java/org/traccar/storage/MemoryStorage.java +++ b/src/main/java/org/traccar/storage/MemoryStorage.java @@ -1,5 +1,6 @@ package org.traccar.storage; +import org.traccar.model.Permission; import org.traccar.storage.query.Request; import java.util.List; @@ -24,4 +25,19 @@ public class MemoryStorage extends Storage { public void removeObject(Class<?> clazz, Request request) throws StorageException { } + @Override + public List<Permission> getPermissions(Class<?> ownerClass, Class<?> propertyClass) throws StorageException { + return null; + } + + @Override + public void addPermission(Permission permission) throws StorageException { + + } + + @Override + public void removePermission(Permission permission) throws StorageException { + + } + } diff --git a/src/main/java/org/traccar/storage/QueryBuilder.java b/src/main/java/org/traccar/storage/QueryBuilder.java index edaacb74d..da8002f0b 100644 --- a/src/main/java/org/traccar/storage/QueryBuilder.java +++ b/src/main/java/org/traccar/storage/QueryBuilder.java @@ -472,7 +472,7 @@ public final class QueryBuilder { return 0; } - public List<Permission> executePermissionsQuery() throws SQLException, ClassNotFoundException { + public List<Permission> executePermissionsQuery() throws SQLException { List<Permission> result = new LinkedList<>(); if (query != null) { try { diff --git a/src/main/java/org/traccar/storage/Storage.java b/src/main/java/org/traccar/storage/Storage.java index 3726fbf50..22c48cae0 100644 --- a/src/main/java/org/traccar/storage/Storage.java +++ b/src/main/java/org/traccar/storage/Storage.java @@ -1,5 +1,6 @@ package org.traccar.storage; +import org.traccar.model.Permission; import org.traccar.storage.query.Request; import java.util.List; @@ -14,6 +15,13 @@ public abstract class Storage { public abstract void removeObject(Class<?> clazz, Request request) throws StorageException; + public abstract List<Permission> getPermissions( + Class<?> ownerClass, Class<?> propertyClass) throws StorageException; + + public abstract void addPermission(Permission permission) throws StorageException; + + public abstract void removePermission(Permission permission) throws StorageException; + public <T> T getObject(Class<T> clazz, Request request) throws StorageException { var objects = getObjects(clazz, request); return objects.isEmpty() ? null : objects.get(0); |