aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-10-13 10:31:54 -0700
committerAnton Tananaev <anton@traccar.org>2022-10-13 10:31:54 -0700
commit4c8e98ed409df6e12128591eb1d8d0cf0856deb7 (patch)
tree83da3517bb14977ab4809aecf3681b777785e83d
parentacedd28b8584a18022d9e2343558f9acbad1666f (diff)
downloadtrackermap-server-4c8e98ed409df6e12128591eb1d8d0cf0856deb7.tar.gz
trackermap-server-4c8e98ed409df6e12128591eb1d8d0cf0856deb7.tar.bz2
trackermap-server-4c8e98ed409df6e12128591eb1d8d0cf0856deb7.zip
Refactor database code (fix #4961)
-rw-r--r--src/main/java/org/traccar/MainEventHandler.java2
-rw-r--r--src/main/java/org/traccar/api/BaseObjectResource.java11
-rw-r--r--src/main/java/org/traccar/api/MediaFilter.java2
-rw-r--r--src/main/java/org/traccar/api/resource/CommandResource.java2
-rw-r--r--src/main/java/org/traccar/api/resource/DeviceResource.java8
-rw-r--r--src/main/java/org/traccar/api/resource/EventResource.java2
-rw-r--r--src/main/java/org/traccar/api/resource/PasswordResource.java7
-rw-r--r--src/main/java/org/traccar/api/resource/PositionResource.java2
-rw-r--r--src/main/java/org/traccar/api/resource/ServerResource.java2
-rw-r--r--src/main/java/org/traccar/api/resource/SessionResource.java2
-rw-r--r--src/main/java/org/traccar/api/resource/UserResource.java3
-rw-r--r--src/main/java/org/traccar/api/security/LoginService.java7
-rw-r--r--src/main/java/org/traccar/api/security/PermissionsService.java4
-rw-r--r--src/main/java/org/traccar/database/CommandsManager.java8
-rw-r--r--src/main/java/org/traccar/database/DeviceLookupService.java2
-rw-r--r--src/main/java/org/traccar/handler/FilterHandler.java2
-rw-r--r--src/main/java/org/traccar/handler/events/GeofenceEventHandler.java3
-rw-r--r--src/main/java/org/traccar/handler/events/MotionEventHandler.java2
-rw-r--r--src/main/java/org/traccar/handler/events/OverspeedEventHandler.java2
-rw-r--r--src/main/java/org/traccar/helper/model/PositionUtil.java2
-rw-r--r--src/main/java/org/traccar/model/Device.java7
-rw-r--r--src/main/java/org/traccar/reports/EventsReportProvider.java4
-rw-r--r--src/main/java/org/traccar/reports/GpxExportProvider.java2
-rw-r--r--src/main/java/org/traccar/reports/KmlExportProvider.java2
-rw-r--r--src/main/java/org/traccar/reports/RouteReportProvider.java2
-rw-r--r--src/main/java/org/traccar/reports/StopsReportProvider.java2
-rw-r--r--src/main/java/org/traccar/reports/TripsReportProvider.java2
-rw-r--r--src/main/java/org/traccar/reports/common/ReportUtils.java4
-rw-r--r--src/main/java/org/traccar/session/ConnectionManager.java4
-rw-r--r--src/main/java/org/traccar/session/cache/CacheManager.java12
-rw-r--r--src/main/java/org/traccar/storage/DatabaseStorage.java25
-rw-r--r--src/main/java/org/traccar/storage/QueryBuilder.java54
-rw-r--r--src/main/java/org/traccar/storage/query/Columns.java3
-rw-r--r--src/main/java/org/traccar/storage/query/Condition.java8
34 files changed, 102 insertions, 104 deletions
diff --git a/src/main/java/org/traccar/MainEventHandler.java b/src/main/java/org/traccar/MainEventHandler.java
index 17bcad0dd..877f03ae7 100644
--- a/src/main/java/org/traccar/MainEventHandler.java
+++ b/src/main/java/org/traccar/MainEventHandler.java
@@ -90,7 +90,7 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter {
updatedDevice.setPositionId(position.getId());
storage.updateObject(updatedDevice, new Request(
new Columns.Include("positionId"),
- new Condition.Equals("id", "id")));
+ new Condition.Equals("id", updatedDevice.getId())));
cacheManager.updatePosition(position);
connectionManager.updatePosition(true, position);
diff --git a/src/main/java/org/traccar/api/BaseObjectResource.java b/src/main/java/org/traccar/api/BaseObjectResource.java
index 0ec2bfeaa..904781e54 100644
--- a/src/main/java/org/traccar/api/BaseObjectResource.java
+++ b/src/main/java/org/traccar/api/BaseObjectResource.java
@@ -56,7 +56,7 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour
public Response getSingle(@PathParam("id") long id) throws StorageException {
permissionsService.checkPermission(baseClass, getUserId(), id);
T entity = storage.getObject(baseClass, new Request(
- new Columns.All(), new Condition.Equals("id", "id", id)));
+ new Columns.All(), new Condition.Equals("id", id)));
if (entity != null) {
return Response.ok(entity).build();
} else {
@@ -86,7 +86,7 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour
if (entity instanceof User) {
User before = storage.getObject(User.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", entity.getId())));
+ new Columns.All(), new Condition.Equals("id", entity.getId())));
permissionsService.checkUserUpdate(getUserId(), before, (User) entity);
} else if (entity instanceof Group) {
Group group = (Group) entity;
@@ -97,12 +97,13 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour
storage.updateObject(entity, new Request(
new Columns.Exclude("id"),
- new Condition.Equals("id", "id")));
+ new Condition.Equals("id", entity.getId())));
if (entity instanceof User) {
User user = (User) entity;
if (user.getHashedPassword() != null) {
storage.updateObject(entity, new Request(
- new Columns.Include("hashedPassword", "salt"), new Condition.Equals("id", "id")));
+ new Columns.Include("hashedPassword", "salt"),
+ new Condition.Equals("id", entity.getId())));
}
}
cacheManager.updateOrInvalidate(true, entity);
@@ -117,7 +118,7 @@ public abstract class BaseObjectResource<T extends BaseModel> extends BaseResour
permissionsService.checkEdit(getUserId(), baseClass, false);
permissionsService.checkPermission(baseClass, getUserId(), id);
- storage.removeObject(baseClass, new Request(new Condition.Equals("id", "id", id)));
+ storage.removeObject(baseClass, new Request(new Condition.Equals("id", id)));
cacheManager.invalidate(baseClass, id);
LogAction.remove(getUserId(), baseClass, id);
diff --git a/src/main/java/org/traccar/api/MediaFilter.java b/src/main/java/org/traccar/api/MediaFilter.java
index 6d95c66a8..ab75bdc5d 100644
--- a/src/main/java/org/traccar/api/MediaFilter.java
+++ b/src/main/java/org/traccar/api/MediaFilter.java
@@ -84,7 +84,7 @@ public class MediaFilter implements Filter {
String[] parts = path != null ? path.split("/") : null;
if (parts != null && parts.length >= 2) {
Device device = storage.getObject(Device.class, new Request(
- new Columns.All(), new Condition.Equals("uniqueId", "uniqueId", parts[1])));
+ new Columns.All(), new Condition.Equals("uniqueId", parts[1])));
if (device != null) {
permissionsServiceProvider.get().checkPermission(Device.class, userId, device.getId());
chain.doFilter(request, response);
diff --git a/src/main/java/org/traccar/api/resource/CommandResource.java b/src/main/java/org/traccar/api/resource/CommandResource.java
index 92804e725..80b9fd18f 100644
--- a/src/main/java/org/traccar/api/resource/CommandResource.java
+++ b/src/main/java/org/traccar/api/resource/CommandResource.java
@@ -110,7 +110,7 @@ public class CommandResource extends ExtendedObjectResource<Command> {
permissionsService.checkPermission(Command.class, getUserId(), entity.getId());
long deviceId = entity.getDeviceId();
entity = storage.getObject(baseClass, new Request(
- new Columns.All(), new Condition.Equals("id", "id", entity.getId())));
+ new Columns.All(), new Condition.Equals("id", entity.getId())));
entity.setDeviceId(deviceId);
} else {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getLimitCommands);
diff --git a/src/main/java/org/traccar/api/resource/DeviceResource.java b/src/main/java/org/traccar/api/resource/DeviceResource.java
index 1d9bc20ec..c0b0cea0d 100644
--- a/src/main/java/org/traccar/api/resource/DeviceResource.java
+++ b/src/main/java/org/traccar/api/resource/DeviceResource.java
@@ -84,14 +84,14 @@ public class DeviceResource extends BaseObjectResource<Device> {
result.addAll(storage.getObjects(Device.class, new Request(
new Columns.All(),
new Condition.And(
- new Condition.Equals("uniqueId", "uniqueId", uniqueId),
+ new Condition.Equals("uniqueId", uniqueId),
new Condition.Permission(User.class, getUserId(), Device.class)))));
}
for (Long deviceId : deviceIds) {
result.addAll(storage.getObjects(Device.class, new Request(
new Columns.All(),
new Condition.And(
- new Condition.Equals("id", "id", deviceId),
+ new Condition.Equals("id", deviceId),
new Condition.Permission(User.class, getUserId(), Device.class)))));
}
return result;
@@ -142,7 +142,7 @@ public class DeviceResource extends BaseObjectResource<Device> {
device.setPositionId(position.getId());
storage.updateObject(device, new Request(
new Columns.Include("positionId"),
- new Condition.Equals("id", "id")));
+ new Condition.Equals("id", device.getId())));
try {
cacheManager.addDevice(position.getDeviceId());
@@ -169,7 +169,7 @@ public class DeviceResource extends BaseObjectResource<Device> {
Device device = storage.getObject(Device.class, new Request(
new Columns.All(),
new Condition.And(
- new Condition.Equals("id", "id", deviceId),
+ new Condition.Equals("id", deviceId),
new Condition.Permission(User.class, getUserId(), Device.class))));
if (device != null) {
String name = "device";
diff --git a/src/main/java/org/traccar/api/resource/EventResource.java b/src/main/java/org/traccar/api/resource/EventResource.java
index 3870e9af9..afdaf52b5 100644
--- a/src/main/java/org/traccar/api/resource/EventResource.java
+++ b/src/main/java/org/traccar/api/resource/EventResource.java
@@ -41,7 +41,7 @@ public class EventResource extends BaseResource {
@GET
public Event get(@PathParam("id") long id) throws StorageException {
Event event = storage.getObject(Event.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", id)));
+ new Columns.All(), new Condition.Equals("id", id)));
if (event == null) {
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build());
}
diff --git a/src/main/java/org/traccar/api/resource/PasswordResource.java b/src/main/java/org/traccar/api/resource/PasswordResource.java
index 625ff4cb1..ebf4e3b91 100644
--- a/src/main/java/org/traccar/api/resource/PasswordResource.java
+++ b/src/main/java/org/traccar/api/resource/PasswordResource.java
@@ -59,7 +59,7 @@ public class PasswordResource extends BaseResource {
throws StorageException, MessagingException, GeneralSecurityException, IOException {
User user = storage.getObject(User.class, new Request(
- new Columns.All(), new Condition.Equals("email", "email", email)));
+ new Columns.All(), new Condition.Equals("email", email)));
if (user != null) {
var velocityContext = textTemplateFormatter.prepareContext(permissionsService.getServer(), user);
velocityContext.put("token", tokenManager.generateToken(user.getId()));
@@ -78,11 +78,12 @@ public class PasswordResource extends BaseResource {
long userId = tokenManager.verifyToken(token);
User user = storage.getObject(User.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", userId)));
+ new Columns.All(), new Condition.Equals("id", userId)));
if (user != null) {
user.setPassword(password);
storage.updateObject(user, new Request(
- new Columns.Include("hashedPassword", "salt"), new Condition.Equals("id", "id")));
+ new Columns.Include("hashedPassword", "salt"),
+ new Condition.Equals("id", userId)));
return Response.ok().build();
}
return Response.status(Response.Status.NOT_FOUND).build();
diff --git a/src/main/java/org/traccar/api/resource/PositionResource.java b/src/main/java/org/traccar/api/resource/PositionResource.java
index 7d7921085..042dd1e23 100644
--- a/src/main/java/org/traccar/api/resource/PositionResource.java
+++ b/src/main/java/org/traccar/api/resource/PositionResource.java
@@ -67,7 +67,7 @@ public class PositionResource extends BaseResource {
var positions = new ArrayList<Position>();
for (long positionId : positionIds) {
Position position = storage.getObject(Position.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", positionId)));
+ new Columns.All(), new Condition.Equals("id", positionId)));
permissionsService.checkPermission(Device.class, getUserId(), position.getDeviceId());
positions.add(position);
}
diff --git a/src/main/java/org/traccar/api/resource/ServerResource.java b/src/main/java/org/traccar/api/resource/ServerResource.java
index e35cd7d95..e7f0b93ca 100644
--- a/src/main/java/org/traccar/api/resource/ServerResource.java
+++ b/src/main/java/org/traccar/api/resource/ServerResource.java
@@ -76,7 +76,7 @@ public class ServerResource extends BaseResource {
permissionsService.checkAdmin(getUserId());
storage.updateObject(entity, new Request(
new Columns.Exclude("id"),
- new Condition.Equals("id", "id")));
+ new Condition.Equals("id", entity.getId())));
cacheManager.updateOrInvalidate(true, entity);
LogAction.edit(getUserId(), entity);
return Response.ok(entity).build();
diff --git a/src/main/java/org/traccar/api/resource/SessionResource.java b/src/main/java/org/traccar/api/resource/SessionResource.java
index 05f492d73..1e984fbd0 100644
--- a/src/main/java/org/traccar/api/resource/SessionResource.java
+++ b/src/main/java/org/traccar/api/resource/SessionResource.java
@@ -122,7 +122,7 @@ public class SessionResource extends BaseResource {
public User get(@PathParam("id") long userId) throws StorageException {
permissionsService.checkAdmin(getUserId());
User user = storage.getObject(User.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", userId)));
+ new Columns.All(), new Condition.Equals("id", userId)));
request.getSession().setAttribute(USER_ID_KEY, user.getId());
LogAction.login(user.getId(), ServletHelper.retrieveRemoteAddress(request));
return user;
diff --git a/src/main/java/org/traccar/api/resource/UserResource.java b/src/main/java/org/traccar/api/resource/UserResource.java
index dd71de4c6..91875ef51 100644
--- a/src/main/java/org/traccar/api/resource/UserResource.java
+++ b/src/main/java/org/traccar/api/resource/UserResource.java
@@ -100,7 +100,8 @@ public class UserResource extends BaseObjectResource<User> {
entity.setId(storage.addObject(entity, new Request(new Columns.Exclude("id"))));
storage.updateObject(entity, new Request(
- new Columns.Include("hashedPassword", "salt"), new Condition.Equals("id", "id")));
+ new Columns.Include("hashedPassword", "salt"),
+ new Condition.Equals("id", entity.getId())));
LogAction.create(getUserId(), entity);
diff --git a/src/main/java/org/traccar/api/security/LoginService.java b/src/main/java/org/traccar/api/security/LoginService.java
index 32487f06b..88bafcfb5 100644
--- a/src/main/java/org/traccar/api/security/LoginService.java
+++ b/src/main/java/org/traccar/api/security/LoginService.java
@@ -58,7 +58,7 @@ public class LoginService {
}
long userId = tokenManager.verifyToken(token);
User user = storage.getObject(User.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", userId)));
+ new Columns.All(), new Condition.Equals("id", userId)));
if (user != null) {
checkUserEnabled(user);
}
@@ -66,11 +66,12 @@ public class LoginService {
}
public User login(String email, String password) throws StorageException {
+ email = email.trim();
User user = storage.getObject(User.class, new Request(
new Columns.All(),
new Condition.Or(
- new Condition.Equals("email", "email", email.trim()),
- new Condition.Equals("login", "email"))));
+ new Condition.Equals("email", email),
+ new Condition.Equals("login", email))));
if (user != null) {
if (ldapProvider != null && user.getLogin() != null && ldapProvider.login(user.getLogin(), password)
|| !forceLdap && user.isPasswordValid(password)) {
diff --git a/src/main/java/org/traccar/api/security/PermissionsService.java b/src/main/java/org/traccar/api/security/PermissionsService.java
index ddfaaab94..0d4877fdb 100644
--- a/src/main/java/org/traccar/api/security/PermissionsService.java
+++ b/src/main/java/org/traccar/api/security/PermissionsService.java
@@ -62,7 +62,7 @@ public class PermissionsService {
user = new ServiceAccountUser();
} else {
user = storage.getObject(
- User.class, new Request(new Columns.All(), new Condition.Equals("id", "id", userId)));
+ User.class, new Request(new Columns.All(), new Condition.Equals("id", userId)));
}
}
return user;
@@ -187,7 +187,7 @@ public class PermissionsService {
var object = storage.getObject(clazz, new Request(
new Columns.Include("id"),
new Condition.And(
- new Condition.Equals("id", "id", objectId),
+ new Condition.Equals("id", objectId),
new Condition.Permission(
User.class, userId, clazz.equals(User.class) ? ManagedUser.class : clazz))));
if (object == null) {
diff --git a/src/main/java/org/traccar/database/CommandsManager.java b/src/main/java/org/traccar/database/CommandsManager.java
index 53040ad53..764ea637b 100644
--- a/src/main/java/org/traccar/database/CommandsManager.java
+++ b/src/main/java/org/traccar/database/CommandsManager.java
@@ -69,9 +69,9 @@ public class CommandsManager implements BroadcastInterface {
throw new RuntimeException("SMS not configured");
}
Device device = storage.getObject(Device.class, new Request(
- new Columns.Include("positionId", "phone"), new Condition.Equals("id", "id", deviceId)));
+ new Columns.Include("positionId", "phone"), new Condition.Equals("id", deviceId)));
Position position = storage.getObject(Position.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", device.getPositionId())));
+ new Columns.All(), new Condition.Equals("id", device.getPositionId())));
if (position != null) {
BaseProtocol protocol = serverManager.getProtocol(position.getProtocol());
protocol.sendTextCommand(device.getPhone(), command);
@@ -101,12 +101,12 @@ public class CommandsManager implements BroadcastInterface {
try {
var commands = storage.getObjects(QueuedCommand.class, new Request(
new Columns.All(),
- new Condition.Equals("deviceId", "deviceId", deviceId),
+ new Condition.Equals("deviceId", deviceId),
new Order(false, "id"),
new Limit(count)));
for (var command : commands) {
storage.removeObject(QueuedCommand.class, new Request(
- new Condition.Equals("id", "id", command.getId())));
+ new Condition.Equals("id", command.getId())));
}
return commands.stream().map(QueuedCommand::toCommand).collect(Collectors.toList());
} catch (StorageException e) {
diff --git a/src/main/java/org/traccar/database/DeviceLookupService.java b/src/main/java/org/traccar/database/DeviceLookupService.java
index 9cf0899ee..28910c24a 100644
--- a/src/main/java/org/traccar/database/DeviceLookupService.java
+++ b/src/main/java/org/traccar/database/DeviceLookupService.java
@@ -108,7 +108,7 @@ public class DeviceLookupService {
for (String uniqueId : uniqueIds) {
if (!isThrottled(uniqueId)) {
device = storage.getObject(Device.class, new Request(
- new Columns.All(), new Condition.Equals("uniqueId", "uniqueId", uniqueId)));
+ new Columns.All(), new Condition.Equals("uniqueId", uniqueId)));
if (device != null) {
lookupSucceeded(uniqueId);
break;
diff --git a/src/main/java/org/traccar/handler/FilterHandler.java b/src/main/java/org/traccar/handler/FilterHandler.java
index f09cb16a3..3722f2a22 100644
--- a/src/main/java/org/traccar/handler/FilterHandler.java
+++ b/src/main/java/org/traccar/handler/FilterHandler.java
@@ -88,7 +88,7 @@ public class FilterHandler extends BaseDataHandler {
return storage.getObject(Position.class, new Request(
new Columns.All(),
new Condition.And(
- new Condition.Equals("deviceId", "deviceId", deviceId),
+ new Condition.Equals("deviceId", deviceId),
new Condition.Compare("fixTime", "<=", "time", date)),
new Order(true, "fixTime"),
new Limit(1)));
diff --git a/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java b/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java
index b1be7e8ad..9414f4b31 100644
--- a/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/GeofenceEventHandler.java
@@ -82,7 +82,8 @@ public class GeofenceEventHandler extends BaseEventHandler {
try {
storage.updateObject(device, new Request(
- new Columns.Include("geofenceIds"), new Condition.Equals("id", "id")));
+ new Columns.Include("geofenceIds"),
+ new Condition.Equals("id", device.getId())));
} catch (StorageException e) {
throw new RuntimeException("Update device geofences error", e);
}
diff --git a/src/main/java/org/traccar/handler/events/MotionEventHandler.java b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
index 3511cf682..1b9763c41 100644
--- a/src/main/java/org/traccar/handler/events/MotionEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/MotionEventHandler.java
@@ -76,7 +76,7 @@ public class MotionEventHandler extends BaseEventHandler {
try {
storage.updateObject(device, new Request(
new Columns.Include("motionState", "motionTime", "motionDistance"),
- new Condition.Equals("id", "id")));
+ new Condition.Equals("id", device.getId())));
} catch (StorageException e) {
LOGGER.warn("Update device motion error", e);
}
diff --git a/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java b/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
index 3928fcc88..4d6aa8857 100644
--- a/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
+++ b/src/main/java/org/traccar/handler/events/OverspeedEventHandler.java
@@ -113,7 +113,7 @@ public class OverspeedEventHandler extends BaseEventHandler {
try {
storage.updateObject(device, new Request(
new Columns.Include("overspeedState", "overspeedTime", "overspeedGeofenceId"),
- new Condition.Equals("id", "id")));
+ new Condition.Equals("id", device.getId())));
} catch (StorageException e) {
LOGGER.warn("Update device overspeed error", e);
}
diff --git a/src/main/java/org/traccar/helper/model/PositionUtil.java b/src/main/java/org/traccar/helper/model/PositionUtil.java
index 31f828947..6c380b81a 100644
--- a/src/main/java/org/traccar/helper/model/PositionUtil.java
+++ b/src/main/java/org/traccar/helper/model/PositionUtil.java
@@ -59,7 +59,7 @@ public final class PositionUtil {
return storage.getObjects(Position.class, new Request(
new Columns.All(),
new Condition.And(
- new Condition.Equals("deviceId", "deviceId", deviceId),
+ new Condition.Equals("deviceId", deviceId),
new Condition.Between("fixTime", "from", from, "to", to)),
new Order("fixTime")));
}
diff --git a/src/main/java/org/traccar/model/Device.java b/src/main/java/org/traccar/model/Device.java
index 147b0fd20..7728172cb 100644
--- a/src/main/java/org/traccar/model/Device.java
+++ b/src/main/java/org/traccar/model/Device.java
@@ -52,22 +52,22 @@ public class Device extends GroupedModel implements Disableable {
private String status;
+ @QueryIgnore
public String getStatus() {
return status != null ? status : STATUS_OFFLINE;
}
- @QueryIgnore
public void setStatus(String status) {
this.status = status != null ? status.trim() : null;
}
private Date lastUpdate;
+ @QueryIgnore
public Date getLastUpdate() {
return this.lastUpdate;
}
- @QueryIgnore
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
@@ -79,18 +79,17 @@ public class Device extends GroupedModel implements Disableable {
return positionId;
}
- @QueryIgnore
public void setPositionId(long positionId) {
this.positionId = positionId;
}
private List<Long> geofenceIds;
+ @QueryIgnore
public List<Long> getGeofenceIds() {
return geofenceIds;
}
- @QueryIgnore
public void setGeofenceIds(List<? extends Number> geofenceIds) {
if (geofenceIds != null) {
this.geofenceIds = geofenceIds.stream().map(Number::longValue).collect(Collectors.toList());
diff --git a/src/main/java/org/traccar/reports/EventsReportProvider.java b/src/main/java/org/traccar/reports/EventsReportProvider.java
index 878c0265d..d0d4fe8bf 100644
--- a/src/main/java/org/traccar/reports/EventsReportProvider.java
+++ b/src/main/java/org/traccar/reports/EventsReportProvider.java
@@ -64,7 +64,7 @@ public class EventsReportProvider {
return storage.getObjects(Event.class, new Request(
new Columns.All(),
new Condition.And(
- new Condition.Equals("deviceId", "deviceId", deviceId),
+ new Condition.Equals("deviceId", deviceId),
new Condition.Between("eventTime", "from", from, "to", to)),
new Order("eventTime")));
}
@@ -134,7 +134,7 @@ public class EventsReportProvider {
sheetNames.add(WorkbookUtil.createSafeSheetName(deviceEvents.getDeviceName()));
if (device.getGroupId() > 0) {
Group group = storage.getObject(Group.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", device.getGroupId())));
+ new Columns.All(), new Condition.Equals("id", device.getGroupId())));
if (group != null) {
deviceEvents.setGroupName(group.getName());
}
diff --git a/src/main/java/org/traccar/reports/GpxExportProvider.java b/src/main/java/org/traccar/reports/GpxExportProvider.java
index f1a0f292d..ccbd97fc3 100644
--- a/src/main/java/org/traccar/reports/GpxExportProvider.java
+++ b/src/main/java/org/traccar/reports/GpxExportProvider.java
@@ -42,7 +42,7 @@ public class GpxExportProvider {
OutputStream outputStream, long deviceId, Date from, Date to) throws StorageException {
var device = storage.getObject(Device.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", deviceId)));
+ new Columns.All(), new Condition.Equals("id", deviceId)));
var positions = PositionUtil.getPositions(storage, deviceId, from, to);
try (PrintWriter writer = new PrintWriter(outputStream)) {
diff --git a/src/main/java/org/traccar/reports/KmlExportProvider.java b/src/main/java/org/traccar/reports/KmlExportProvider.java
index e8b5c4278..24fcfb8ab 100644
--- a/src/main/java/org/traccar/reports/KmlExportProvider.java
+++ b/src/main/java/org/traccar/reports/KmlExportProvider.java
@@ -43,7 +43,7 @@ public class KmlExportProvider {
OutputStream outputStream, long deviceId, Date from, Date to) throws StorageException {
var device = storage.getObject(Device.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", deviceId)));
+ new Columns.All(), new Condition.Equals("id", deviceId)));
var positions = PositionUtil.getPositions(storage, deviceId, from, to);
var dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
diff --git a/src/main/java/org/traccar/reports/RouteReportProvider.java b/src/main/java/org/traccar/reports/RouteReportProvider.java
index 0f618822e..3ee651619 100644
--- a/src/main/java/org/traccar/reports/RouteReportProvider.java
+++ b/src/main/java/org/traccar/reports/RouteReportProvider.java
@@ -80,7 +80,7 @@ public class RouteReportProvider {
sheetNames.add(WorkbookUtil.createSafeSheetName(deviceRoutes.getDeviceName()));
if (device.getGroupId() > 0) {
Group group = storage.getObject(Group.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", device.getGroupId())));
+ new Columns.All(), new Condition.Equals("id", device.getGroupId())));
if (group != null) {
deviceRoutes.setGroupName(group.getName());
}
diff --git a/src/main/java/org/traccar/reports/StopsReportProvider.java b/src/main/java/org/traccar/reports/StopsReportProvider.java
index ba61ef6a1..ec3fd2215 100644
--- a/src/main/java/org/traccar/reports/StopsReportProvider.java
+++ b/src/main/java/org/traccar/reports/StopsReportProvider.java
@@ -87,7 +87,7 @@ public class StopsReportProvider {
sheetNames.add(WorkbookUtil.createSafeSheetName(deviceStops.getDeviceName()));
if (device.getGroupId() > 0) {
Group group = storage.getObject(Group.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", device.getGroupId())));
+ new Columns.All(), new Condition.Equals("id", device.getGroupId())));
if (group != null) {
deviceStops.setGroupName(group.getName());
}
diff --git a/src/main/java/org/traccar/reports/TripsReportProvider.java b/src/main/java/org/traccar/reports/TripsReportProvider.java
index 2d9bcdfbf..265811354 100644
--- a/src/main/java/org/traccar/reports/TripsReportProvider.java
+++ b/src/main/java/org/traccar/reports/TripsReportProvider.java
@@ -87,7 +87,7 @@ public class TripsReportProvider {
sheetNames.add(WorkbookUtil.createSafeSheetName(deviceTrips.getDeviceName()));
if (device.getGroupId() > 0) {
Group group = storage.getObject(Group.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", device.getGroupId())));
+ new Columns.All(), new Condition.Equals("id", device.getGroupId())));
if (group != null) {
deviceTrips.setGroupName(group.getName());
}
diff --git a/src/main/java/org/traccar/reports/common/ReportUtils.java b/src/main/java/org/traccar/reports/common/ReportUtils.java
index 1de774dab..120dadcf5 100644
--- a/src/main/java/org/traccar/reports/common/ReportUtils.java
+++ b/src/main/java/org/traccar/reports/common/ReportUtils.java
@@ -95,7 +95,7 @@ public class ReportUtils {
return storage.getObject(clazz, new Request(
new Columns.All(),
new Condition.And(
- new Condition.Equals("id", "id", objectId),
+ new Condition.Equals("id", objectId),
new Condition.Permission(User.class, userId, clazz))));
}
@@ -166,7 +166,7 @@ public class ReportUtils {
if (driverUniqueId != null) {
Driver driver = storage.getObject(Driver.class, new Request(
new Columns.All(),
- new Condition.Equals("uniqueId", "uniqueId", driverUniqueId)));
+ new Condition.Equals("uniqueId", driverUniqueId)));
if (driver != null) {
return driver.getName();
}
diff --git a/src/main/java/org/traccar/session/ConnectionManager.java b/src/main/java/org/traccar/session/ConnectionManager.java
index 9e50c9ead..37a42d827 100644
--- a/src/main/java/org/traccar/session/ConnectionManager.java
+++ b/src/main/java/org/traccar/session/ConnectionManager.java
@@ -215,7 +215,7 @@ public class ConnectionManager implements BroadcastInterface {
if (device == null) {
try {
device = storage.getObject(Device.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", deviceId)));
+ new Columns.All(), new Condition.Equals("id", deviceId)));
} catch (StorageException e) {
LOGGER.warn("Failed to get device", e);
}
@@ -265,7 +265,7 @@ public class ConnectionManager implements BroadcastInterface {
try {
storage.updateObject(device, new Request(
new Columns.Include("status", "lastUpdate"),
- new Condition.Equals("id", "id")));
+ new Condition.Equals("id", deviceId)));
} catch (StorageException e) {
LOGGER.warn("Update device status error", e);
}
diff --git a/src/main/java/org/traccar/session/cache/CacheManager.java b/src/main/java/org/traccar/session/cache/CacheManager.java
index ed67ed70e..8f2e7ba93 100644
--- a/src/main/java/org/traccar/session/cache/CacheManager.java
+++ b/src/main/java/org/traccar/session/cache/CacheManager.java
@@ -201,7 +201,7 @@ public class CacheManager implements BroadcastInterface {
public void invalidateObject(boolean local, Class<? extends BaseModel> clazz, long id) {
try {
var object = storage.getObject(clazz, new Request(
- new Columns.All(), new Condition.Equals("id", "id", id)));
+ new Columns.All(), new Condition.Equals("id", id)));
if (object != null) {
updateOrInvalidate(local, object);
} else {
@@ -286,7 +286,7 @@ public class CacheManager implements BroadcastInterface {
Map<Class<? extends BaseModel>, Set<Long>> links = new HashMap<>();
Device device = storage.getObject(Device.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", deviceId)));
+ new Columns.All(), new Condition.Equals("id", deviceId)));
if (device != null) {
addObject(deviceId, device);
@@ -294,7 +294,7 @@ public class CacheManager implements BroadcastInterface {
long groupId = device.getGroupId();
while (groupDepth < GROUP_DEPTH_LIMIT && groupId > 0) {
Group group = storage.getObject(Group.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", groupId)));
+ new Columns.All(), new Condition.Equals("id", groupId)));
links.computeIfAbsent(Group.class, k -> new LinkedHashSet<>()).add(group.getId());
addObject(deviceId, group);
groupId = group.getGroupId();
@@ -311,7 +311,7 @@ public class CacheManager implements BroadcastInterface {
var scheduled = (ScheduledModel) object;
if (scheduled.getCalendarId() > 0) {
var calendar = storage.getObject(Calendar.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", scheduled.getCalendarId())));
+ new Columns.All(), new Condition.Equals("id", scheduled.getCalendarId())));
links.computeIfAbsent(Notification.class, k -> new LinkedHashSet<>())
.add(calendar.getId());
addObject(deviceId, calendar);
@@ -336,7 +336,7 @@ public class CacheManager implements BroadcastInterface {
addObject(deviceId, notification);
if (notification.getCalendarId() > 0) {
var calendar = storage.getObject(Calendar.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", notification.getCalendarId())));
+ new Columns.All(), new Condition.Equals("id", notification.getCalendarId())));
links.computeIfAbsent(Notification.class, k -> new LinkedHashSet<>())
.add(calendar.getId());
addObject(deviceId, calendar);
@@ -348,7 +348,7 @@ public class CacheManager implements BroadcastInterface {
if (device.getPositionId() > 0) {
devicePositions.put(deviceId, storage.getObject(Position.class, new Request(
- new Columns.All(), new Condition.Equals("id", "id", device.getPositionId()))));
+ new Columns.All(), new Condition.Equals("id", device.getPositionId()))));
}
}
}
diff --git a/src/main/java/org/traccar/storage/DatabaseStorage.java b/src/main/java/org/traccar/storage/DatabaseStorage.java
index 8ca464147..884c8fca8 100644
--- a/src/main/java/org/traccar/storage/DatabaseStorage.java
+++ b/src/main/java/org/traccar/storage/DatabaseStorage.java
@@ -57,7 +57,7 @@ public class DatabaseStorage extends Storage {
if (request.getColumns() instanceof Columns.All) {
query.append('*');
} else {
- query.append(formatColumns(request.getColumns(), clazz, "get", c -> c));
+ query.append(formatColumns(request.getColumns().getColumns(clazz, "set"), c -> c));
}
query.append(" FROM ").append(getStorageName(clazz));
query.append(formatCondition(request.getCondition()));
@@ -76,16 +76,17 @@ public class DatabaseStorage extends Storage {
@Override
public <T> long addObject(T entity, Request request) throws StorageException {
+ List<String> columns = request.getColumns().getColumns(entity.getClass(), "get");
StringBuilder query = new StringBuilder("INSERT INTO ");
query.append(getStorageName(entity.getClass()));
query.append("(");
- query.append(formatColumns(request.getColumns(), entity.getClass(), "set", c -> c));
+ query.append(formatColumns(columns, c -> c));
query.append(") VALUES (");
- query.append(formatColumns(request.getColumns(), entity.getClass(), "set", c -> ':' + c));
+ query.append(formatColumns(columns, c -> ':' + c));
query.append(")");
try {
QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString(), true);
- builder.setObject(entity);
+ builder.setObject(entity, columns);
return builder.executeUpdate();
} catch (SQLException e) {
throw new StorageException(e);
@@ -94,14 +95,15 @@ public class DatabaseStorage extends Storage {
@Override
public <T> void updateObject(T entity, Request request) throws StorageException {
+ List<String> columns = request.getColumns().getColumns(entity.getClass(), "get");
StringBuilder query = new StringBuilder("UPDATE ");
query.append(getStorageName(entity.getClass()));
query.append(" SET ");
- query.append(formatColumns(request.getColumns(), entity.getClass(), "set", c -> c + " = :" + c));
+ query.append(formatColumns(columns, c -> c + " = :" + c));
query.append(formatCondition(request.getCondition()));
try {
QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString());
- builder.setObject(entity);
+ builder.setObject(entity, columns);
for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) {
builder.setValue(variable.getKey(), variable.getValue());
}
@@ -135,12 +137,10 @@ public class DatabaseStorage extends Storage {
query.append(Permission.getStorageName(ownerClass, propertyClass));
var conditions = new LinkedList<Condition>();
if (ownerId > 0) {
- conditions.add(new Condition.Equals(
- Permission.getKey(ownerClass), Permission.getKey(ownerClass), ownerId));
+ conditions.add(new Condition.Equals(Permission.getKey(ownerClass), ownerId));
}
if (propertyId > 0) {
- conditions.add(new Condition.Equals(
- Permission.getKey(propertyClass), Permission.getKey(propertyClass), propertyId));
+ conditions.add(new Condition.Equals(Permission.getKey(propertyClass), propertyId));
}
Condition combinedCondition = Condition.merge(conditions);
query.append(formatCondition(combinedCondition));
@@ -230,9 +230,8 @@ public class DatabaseStorage extends Storage {
return results;
}
- private String formatColumns(
- Columns columns, Class<?> clazz, String type, Function<String, String> mapper) {
- return columns.getColumns(clazz, type).stream().map(mapper).collect(Collectors.joining(", "));
+ private String formatColumns(List<String> columns, Function<String, String> mapper) {
+ return columns.stream().map(mapper).collect(Collectors.joining(", "));
}
private String formatCondition(Condition genericCondition) throws StorageException {
diff --git a/src/main/java/org/traccar/storage/QueryBuilder.java b/src/main/java/org/traccar/storage/QueryBuilder.java
index a58ebe2b4..fa71a8e8f 100644
--- a/src/main/java/org/traccar/storage/QueryBuilder.java
+++ b/src/main/java/org/traccar/storage/QueryBuilder.java
@@ -37,10 +37,12 @@ import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Set;
@SuppressWarnings("UnusedReturnValue")
public final class QueryBuilder {
@@ -283,36 +285,32 @@ public final class QueryBuilder {
return this;
}
- public QueryBuilder setObject(Object object) throws SQLException {
-
- Method[] methods = object.getClass().getMethods();
-
- for (Method method : methods) {
- if (method.getName().startsWith("get") && method.getParameterTypes().length == 0
- && !method.getName().equals("getClass")) {
- String name = method.getName().substring(3);
- try {
- if (method.getReturnType().equals(boolean.class)) {
- setBoolean(name, (Boolean) method.invoke(object));
- } else if (method.getReturnType().equals(int.class)) {
- setInteger(name, (Integer) method.invoke(object));
- } else if (method.getReturnType().equals(long.class)) {
- setLong(name, (Long) method.invoke(object), name.endsWith("Id"));
- } else if (method.getReturnType().equals(double.class)) {
- setDouble(name, (Double) method.invoke(object));
- } else if (method.getReturnType().equals(String.class)) {
- setString(name, (String) method.invoke(object));
- } else if (method.getReturnType().equals(Date.class)) {
- setDate(name, (Date) method.invoke(object));
- } else if (method.getReturnType().equals(byte[].class)) {
- setBlob(name, (byte[]) method.invoke(object));
- } else {
- setString(name, objectMapper.writeValueAsString(method.invoke(object)));
- }
- } catch (IllegalAccessException | InvocationTargetException | JsonProcessingException error) {
- LOGGER.warn("Get property error", error);
+ public QueryBuilder setObject(Object object, List<String> columns) throws SQLException {
+
+ try {
+ for (String column : columns) {
+ Method method = object.getClass().getMethod(
+ "get" + Character.toUpperCase(column.charAt(0)) + column.substring(1));
+ if (method.getReturnType().equals(boolean.class)) {
+ setBoolean(column, (Boolean) method.invoke(object));
+ } else if (method.getReturnType().equals(int.class)) {
+ setInteger(column, (Integer) method.invoke(object));
+ } else if (method.getReturnType().equals(long.class)) {
+ setLong(column, (Long) method.invoke(object), column.endsWith("Id"));
+ } else if (method.getReturnType().equals(double.class)) {
+ setDouble(column, (Double) method.invoke(object));
+ } else if (method.getReturnType().equals(String.class)) {
+ setString(column, (String) method.invoke(object));
+ } else if (method.getReturnType().equals(Date.class)) {
+ setDate(column, (Date) method.invoke(object));
+ } else if (method.getReturnType().equals(byte[].class)) {
+ setBlob(column, (byte[]) method.invoke(object));
+ } else {
+ setString(column, objectMapper.writeValueAsString(method.invoke(object)));
}
}
+ } catch (ReflectiveOperationException | JsonProcessingException e) {
+ LOGGER.warn("Set object error", e);
}
return this;
diff --git a/src/main/java/org/traccar/storage/query/Columns.java b/src/main/java/org/traccar/storage/query/Columns.java
index 545995b3c..a00400b36 100644
--- a/src/main/java/org/traccar/storage/query/Columns.java
+++ b/src/main/java/org/traccar/storage/query/Columns.java
@@ -17,6 +17,7 @@ package org.traccar.storage.query;
import org.traccar.storage.QueryIgnore;
+import java.beans.Introspector;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedList;
@@ -36,7 +37,7 @@ public abstract class Columns {
if (method.getName().startsWith(type) && method.getParameterTypes().length == parameterCount
&& !method.isAnnotationPresent(QueryIgnore.class)
&& !method.getName().equals("getClass")) {
- columns.add(method.getName().substring(3).toLowerCase());
+ columns.add(Introspector.decapitalize(method.getName().substring(3)));
}
}
return columns;
diff --git a/src/main/java/org/traccar/storage/query/Condition.java b/src/main/java/org/traccar/storage/query/Condition.java
index 136b0402b..08b199052 100644
--- a/src/main/java/org/traccar/storage/query/Condition.java
+++ b/src/main/java/org/traccar/storage/query/Condition.java
@@ -34,12 +34,8 @@ public interface Condition {
}
class Equals extends Compare {
- public Equals(String column, String variable) {
- this(column, variable, null);
- }
-
- public Equals(String column, String variable, Object value) {
- super(column, "=", variable, value);
+ public Equals(String column, Object value) {
+ super(column, "=", column, value);
}
}