Merge pull request #5458 from CpdnCristiano/add-sort

Implement Sorting Functionality
This commit is contained in:
Anton Tananaev 2024-10-31 16:03:35 -07:00 committed by GitHub
commit c8459a8352
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 47 additions and 30 deletions

View File

@ -23,6 +23,7 @@ import org.traccar.model.User;
import org.traccar.storage.StorageException;
import org.traccar.storage.query.Columns;
import org.traccar.storage.query.Condition;
import org.traccar.storage.query.Order;
import org.traccar.storage.query.Request;
import jakarta.ws.rs.GET;
@ -32,8 +33,15 @@ import java.util.LinkedList;
public class ExtendedObjectResource<T extends BaseModel> extends BaseObjectResource<T> {
private final String sortField;
public ExtendedObjectResource(Class<T> baseClass) {
this(baseClass, null);
}
public ExtendedObjectResource(Class<T> baseClass, String sortField) {
super(baseClass);
this.sortField = sortField;
}
@GET
@ -65,7 +73,9 @@ public class ExtendedObjectResource<T extends BaseModel> extends BaseObjectResou
conditions.add(new Condition.Permission(Device.class, deviceId, baseClass).excludeGroups());
}
return storage.getObjects(baseClass, new Request(new Columns.All(), Condition.merge(conditions)));
return storage.getObjects(baseClass, new Request(new Columns.All(), Condition.merge(conditions),
sortField != null ? new Order(sortField) : null));
}
}

View File

@ -21,19 +21,29 @@ import org.traccar.model.User;
import org.traccar.storage.StorageException;
import org.traccar.storage.query.Columns;
import org.traccar.storage.query.Condition;
import org.traccar.storage.query.Order;
import org.traccar.storage.query.Request;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.QueryParam;
import java.util.Collection;
import java.util.LinkedList;
public class SimpleObjectResource<T extends BaseModel> extends BaseObjectResource<T> {
private final String sortField;
public SimpleObjectResource(Class<T> baseClass) {
super(baseClass);
this(baseClass, null);
}
public SimpleObjectResource(Class<T> baseClass, String sortField) {
super(baseClass);
this.sortField = sortField;
}
@GET
public Collection<T> get(
@QueryParam("all") boolean all, @QueryParam("userId") long userId) throws StorageException {
@ -53,7 +63,8 @@ public class SimpleObjectResource<T extends BaseModel> extends BaseObjectResourc
conditions.add(new Condition.Permission(User.class, userId, baseClass));
}
return storage.getObjects(baseClass, new Request(new Columns.All(), Condition.merge(conditions)));
return storage.getObjects(baseClass, new Request(new Columns.All(), Condition.merge(conditions),
sortField != null ? new Order(sortField) : null));
}
}

View File

@ -50,7 +50,7 @@ public class AttributeResource extends ExtendedObjectResource<Attribute> {
private ComputedAttributesHandler.Late computedAttributesHandler;
public AttributeResource() {
super(Attribute.class);
super(Attribute.class, "description");
}
@POST

View File

@ -30,7 +30,7 @@ import org.traccar.model.Calendar;
public class CalendarResource extends SimpleObjectResource<Calendar> {
public CalendarResource() {
super(Calendar.class);
super(Calendar.class, "name");
}
}

View File

@ -69,7 +69,7 @@ public class CommandResource extends ExtendedObjectResource<Command> {
private ServerManager serverManager;
public CommandResource() {
super(Command.class);
super(Command.class, "description");
}
private BaseProtocol getDeviceProtocol(long deviceId) throws StorageException {

View File

@ -33,6 +33,7 @@ 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.Order;
import org.traccar.storage.query.Request;
import jakarta.inject.Inject;
@ -129,7 +130,8 @@ public class DeviceResource extends BaseObjectResource<Device> {
}
}
return storage.getObjects(baseClass, new Request(new Columns.All(), Condition.merge(conditions)));
return storage.getObjects(baseClass, new Request(new Columns.All(), Condition.merge(conditions),
new Order("name")));
}
}

View File

@ -30,7 +30,7 @@ import org.traccar.model.Driver;
public class DriverResource extends ExtendedObjectResource<Driver> {
public DriverResource() {
super(Driver.class);
super(Driver.class, "name");
}
}

View File

@ -29,7 +29,7 @@ import jakarta.ws.rs.core.MediaType;
public class GeofenceResource extends ExtendedObjectResource<Geofence> {
public GeofenceResource() {
super(Geofence.class);
super(Geofence.class, "name");
}
}

View File

@ -29,7 +29,7 @@ import jakarta.ws.rs.core.MediaType;
public class GroupResource extends SimpleObjectResource<Group> {
public GroupResource() {
super(Group.class);
super(Group.class, "name");
}
}

View File

@ -30,7 +30,7 @@ import org.traccar.model.Maintenance;
public class MaintenanceResource extends ExtendedObjectResource<Maintenance> {
public MaintenanceResource() {
super(Maintenance.class);
super(Maintenance.class, "name");
}
}

View File

@ -61,7 +61,7 @@ public class NotificationResource extends ExtendedObjectResource<Notification> {
private NotificatorManager notificatorManager;
public NotificationResource() {
super(Notification.class);
super(Notification.class, "description");
}
@GET

View File

@ -29,7 +29,7 @@ import jakarta.ws.rs.core.MediaType;
public class OrderResource extends SimpleObjectResource<Order> {
public OrderResource() {
super(Order.class);
super(Order.class, "description");
}
}

View File

@ -85,7 +85,7 @@ public class ReportResource extends SimpleObjectResource<Report> {
private ReportMailer reportMailer;
public ReportResource() {
super(Report.class);
super(Report.class, "description");
}
private Response executeReport(long userId, boolean mail, ReportExecutor executor) {

View File

@ -33,6 +33,7 @@ import org.traccar.model.User;
import org.traccar.storage.StorageException;
import org.traccar.storage.query.Columns;
import org.traccar.storage.query.Condition;
import org.traccar.storage.query.Order;
import org.traccar.storage.query.Request;
import jakarta.annotation.security.PermitAll;
@ -66,26 +67,19 @@ public class UserResource extends BaseObjectResource<User> {
@GET
public Collection<User> get(
@QueryParam("userId") long userId, @QueryParam("deviceId") long deviceId) throws StorageException {
var conditions = new LinkedList<Condition>();
if (userId > 0) {
permissionsService.checkUser(getUserId(), userId);
return storage.getObjects(baseClass, new Request(
new Columns.All(),
new Condition.Permission(User.class, userId, ManagedUser.class).excludeGroups()));
} else if (deviceId > 0) {
permissionsService.checkManager(getUserId());
var conditions = new LinkedList<Condition>();
conditions.add(new Condition.Permission(User.class, Device.class, deviceId).excludeGroups());
if (permissionsService.notAdmin(getUserId())) {
conditions.add(new Condition.Permission(User.class, getUserId(), ManagedUser.class).excludeGroups());
}
return storage.getObjects(baseClass, new Request(new Columns.All(), Condition.merge(conditions)));
conditions.add(new Condition.Permission(User.class, userId, ManagedUser.class).excludeGroups());
} else if (permissionsService.notAdmin(getUserId())) {
return storage.getObjects(baseClass, new Request(
new Columns.All(),
new Condition.Permission(User.class, getUserId(), ManagedUser.class).excludeGroups()));
} else {
return storage.getObjects(baseClass, new Request(new Columns.All()));
conditions.add(new Condition.Permission(User.class, getUserId(), ManagedUser.class).excludeGroups());
}
if (deviceId > 0) {
permissionsService.checkManager(getUserId());
conditions.add(new Condition.Permission(User.class, Device.class, deviceId).excludeGroups());
}
return storage.getObjects(baseClass,
new Request(new Columns.All(), Condition.merge(conditions), new Order("name")));
}
@Override