aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-18 07:35:32 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-18 07:35:32 -0700
commit9fa2fc7091041a5cffa9092318d5d2f5a1a367a2 (patch)
treed91a41e5356a32781df682efbda88a555697b37e
parent316ef36ca8bf6425df5a6186da2e7901dcb23c0e (diff)
downloadtrackermap-server-9fa2fc7091041a5cffa9092318d5d2f5a1a367a2.tar.gz
trackermap-server-9fa2fc7091041a5cffa9092318d5d2f5a1a367a2.tar.bz2
trackermap-server-9fa2fc7091041a5cffa9092318d5d2f5a1a367a2.zip
Migrate device resource
-rw-r--r--src/main/java/org/traccar/api/resource/DeviceResource.java101
-rw-r--r--src/main/java/org/traccar/database/DeviceManager.java43
-rw-r--r--src/main/java/org/traccar/database/PermissionsManager.java11
3 files changed, 80 insertions, 75 deletions
diff --git a/src/main/java/org/traccar/api/resource/DeviceResource.java b/src/main/java/org/traccar/api/resource/DeviceResource.java
index cd5ebf0c5..2509c9003 100644
--- a/src/main/java/org/traccar/api/resource/DeviceResource.java
+++ b/src/main/java/org/traccar/api/resource/DeviceResource.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 - 2022 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,13 +15,18 @@
*/
package org.traccar.api.resource;
-import org.traccar.Context;
+import org.traccar.Main;
import org.traccar.api.BaseObjectResource;
-import org.traccar.database.DeviceManager;
import org.traccar.helper.LogAction;
import org.traccar.model.Device;
import org.traccar.model.DeviceAccumulators;
+import org.traccar.model.Position;
+import org.traccar.model.User;
+import org.traccar.session.cache.CacheManager;
import org.traccar.storage.StorageException;
+import org.traccar.storage.query.Columns;
+import org.traccar.storage.query.Condition;
+import org.traccar.storage.query.Request;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@@ -32,9 +37,8 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Collection;
-import java.util.HashSet;
+import java.util.LinkedList;
import java.util.List;
-import java.util.Set;
@Path("devices")
@Produces(MediaType.APPLICATION_JSON)
@@ -50,38 +54,46 @@ public class DeviceResource extends BaseObjectResource<Device> {
@QueryParam("all") boolean all, @QueryParam("userId") long userId,
@QueryParam("uniqueId") List<String> uniqueIds,
@QueryParam("id") List<Long> deviceIds) throws StorageException {
- DeviceManager deviceManager = Context.getDeviceManager();
- Set<Long> result;
- if (all) {
- if (Context.getPermissionsManager().getUserAdmin(getUserId())) {
- result = deviceManager.getAllItems();
- } else {
- Context.getPermissionsManager().checkManager(getUserId());
- result = deviceManager.getUserItems(getUserId());
- }
- } else if (uniqueIds.isEmpty() && deviceIds.isEmpty()) {
- if (userId == 0) {
- userId = getUserId();
- }
- permissionsService.checkUser(getUserId(), userId);
- if (Context.getPermissionsManager().getUserAdmin(getUserId())) {
- result = deviceManager.getAllUserItems(userId);
- } else {
- result = deviceManager.getUserItems(userId);
- }
- } else {
- result = new HashSet<>();
+
+ if (!uniqueIds.isEmpty() || !deviceIds.isEmpty()) {
+
+ List<Device> result = new LinkedList<>();
for (String uniqueId : uniqueIds) {
- Device device = deviceManager.getByUniqueId(uniqueId);
- permissionsService.checkPermission(Device.class, getUserId(), device.getId());
- result.add(device.getId());
+ result.addAll(storage.getObjects(Device.class, new Request(
+ new Columns.All(),
+ new Condition.And(
+ new Condition.Equals("uniqueId", "uniqueId", uniqueId),
+ new Condition.Permission(User.class, getUserId(), Device.class)))));
}
for (Long deviceId : deviceIds) {
- permissionsService.checkPermission(Device.class, getUserId(), deviceId);
- result.add(deviceId);
+ result.addAll(storage.getObjects(Device.class, new Request(
+ new Columns.All(),
+ new Condition.And(
+ new Condition.Equals("id", "id", deviceId),
+ new Condition.Permission(User.class, getUserId(), Device.class)))));
+ }
+ return result;
+
+ } else {
+
+ var conditions = new LinkedList<Condition>();
+
+ if (all) {
+ if (permissionsService.notAdmin(getUserId())) {
+ conditions.add(new Condition.Permission(User.class, getUserId(), baseClass));
+ }
+ } else {
+ if (userId == 0) {
+ conditions.add(new Condition.Permission(User.class, getUserId(), baseClass));
+ } else {
+ permissionsService.checkUser(getUserId(), userId);
+ conditions.add(new Condition.Permission(User.class, userId, baseClass).excludeGroups());
+ }
}
+
+ return storage.getObjects(baseClass, new Request(new Columns.All(), Condition.merge(conditions)));
+
}
- return deviceManager.getItems(result);
}
@Path("{id}/accumulators")
@@ -91,7 +103,30 @@ public class DeviceResource extends BaseObjectResource<Device> {
permissionsService.checkManager(getUserId());
permissionsService.checkPermission(Device.class, getUserId(), entity.getDeviceId());
}
- Context.getDeviceManager().resetDeviceAccumulators(entity);
+
+ Position position = storage.getObject(Position.class, new Request(
+ new Columns.All(), new Condition.LatestPositions(entity.getDeviceId())));
+ if (position != null) {
+ if (entity.getTotalDistance() != null) {
+ position.getAttributes().put(Position.KEY_TOTAL_DISTANCE, entity.getTotalDistance());
+ }
+ if (entity.getHours() != null) {
+ position.getAttributes().put(Position.KEY_HOURS, entity.getHours());
+ }
+ position.setId(storage.addObject(position, new Request(new Columns.Exclude("id"))));
+
+ Device device = new Device();
+ device.setId(position.getDeviceId());
+ device.setPositionId(position.getId());
+ storage.updateObject(device, new Request(
+ new Columns.Include("positionId"),
+ new Condition.Equals("id", "id")));
+
+ Main.getInjector().getInstance(CacheManager.class).updatePosition(position);
+ } else {
+ throw new IllegalArgumentException();
+ }
+
LogAction.resetDeviceAccumulators(getUserId(), entity.getDeviceId());
return Response.noContent().build();
}
diff --git a/src/main/java/org/traccar/database/DeviceManager.java b/src/main/java/org/traccar/database/DeviceManager.java
index bd100245c..81043fd7a 100644
--- a/src/main/java/org/traccar/database/DeviceManager.java
+++ b/src/main/java/org/traccar/database/DeviceManager.java
@@ -15,32 +15,30 @@
*/
package org.traccar.database;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicLong;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.traccar.Main;
-import org.traccar.config.Config;
import org.traccar.Context;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Command;
import org.traccar.model.Device;
-import org.traccar.session.ConnectionManager;
-import org.traccar.session.DeviceState;
-import org.traccar.model.DeviceAccumulators;
import org.traccar.model.Group;
import org.traccar.model.Position;
import org.traccar.model.Server;
+import org.traccar.session.ConnectionManager;
+import org.traccar.session.DeviceState;
import org.traccar.session.cache.CacheManager;
import org.traccar.storage.StorageException;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+
public class DeviceManager extends BaseObjectManager<Device> implements IdentityManager {
private static final Logger LOGGER = LoggerFactory.getLogger(DeviceManager.class);
@@ -378,23 +376,6 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
return result;
}
- public void resetDeviceAccumulators(DeviceAccumulators deviceAccumulators) throws StorageException {
- Position last = positions.get(deviceAccumulators.getDeviceId());
- if (last != null) {
- if (deviceAccumulators.getTotalDistance() != null) {
- last.getAttributes().put(Position.KEY_TOTAL_DISTANCE, deviceAccumulators.getTotalDistance());
- }
- if (deviceAccumulators.getHours() != null) {
- last.getAttributes().put(Position.KEY_HOURS, deviceAccumulators.getHours());
- }
- getDataManager().addObject(last);
- updateLatestPosition(last);
- Main.getInjector().getInstance(CacheManager.class).updatePosition(last);
- } else {
- throw new IllegalArgumentException();
- }
- }
-
public DeviceState getDeviceState(long deviceId) {
DeviceState deviceState = deviceStates.get(deviceId);
if (deviceState == null) {
diff --git a/src/main/java/org/traccar/database/PermissionsManager.java b/src/main/java/org/traccar/database/PermissionsManager.java
index 4ac27c717..9d1e5aadf 100644
--- a/src/main/java/org/traccar/database/PermissionsManager.java
+++ b/src/main/java/org/traccar/database/PermissionsManager.java
@@ -179,17 +179,6 @@ public class PermissionsManager {
return user != null && user.getAdministrator();
}
- public boolean getUserManager(long userId) {
- User user = getUser(userId);
- return user != null && user.getUserLimit() != 0;
- }
-
- public void checkManager(long userId) throws SecurityException {
- if (!getUserManager(userId)) {
- throw new SecurityException("Manager access required");
- }
- }
-
public void refreshPermissions(Permission permission) {
if (permission.getOwnerClass().equals(User.class)) {
if (permission.getPropertyClass().equals(Device.class)