From 504a555b40ee0c2c14e8679ed320b8c2578f8610 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 3 Aug 2016 17:53:54 +0500 Subject: - Moved retrieving Accept to BaseResource - Added Events report - Removed deviceIds from csv --- src/org/traccar/api/BaseResource.java | 8 +++++ src/org/traccar/api/resource/ReportResource.java | 28 ++++++++++++----- src/org/traccar/reports/Events.java | 40 ++++++++++++++++++++++++ src/org/traccar/reports/Route.java | 4 +-- 4 files changed, 70 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/org/traccar/api/BaseResource.java b/src/org/traccar/api/BaseResource.java index 97f5c074d..8259f8868 100644 --- a/src/org/traccar/api/BaseResource.java +++ b/src/org/traccar/api/BaseResource.java @@ -15,6 +15,7 @@ */ package org.traccar.api; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.SecurityContext; public class BaseResource { @@ -22,6 +23,9 @@ public class BaseResource { @javax.ws.rs.core.Context private SecurityContext securityContext; + @javax.ws.rs.core.Context + private HttpHeaders headers; + protected long getUserId() { UserPrincipal principal = (UserPrincipal) securityContext.getUserPrincipal(); if (principal != null) { @@ -30,4 +34,8 @@ public class BaseResource { return 0; } + protected String getAcceptHeader() { + return headers.getRequestHeaders().getFirst("Accept"); + } + } diff --git a/src/org/traccar/api/resource/ReportResource.java b/src/org/traccar/api/resource/ReportResource.java index 598cbebf6..cf11cd4e4 100644 --- a/src/org/traccar/api/resource/ReportResource.java +++ b/src/org/traccar/api/resource/ReportResource.java @@ -6,14 +6,12 @@ import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; import org.traccar.api.BaseResource; +import org.traccar.reports.Events; import org.traccar.reports.ReportUtils; import org.traccar.reports.Route; import org.traccar.web.JsonConverter; @@ -23,12 +21,10 @@ public class ReportResource extends BaseResource { @Path("route") @GET - public Response getRoute(@Context HttpHeaders headers, + public Response getRoute( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - MultivaluedMap headerParams = headers.getRequestHeaders(); - String accept = headerParams.getFirst("Accept"); - if (accept.equals("application/ms-excel")) { + if (getAcceptHeader().equals("application/ms-excel")) { ResponseBuilder response = Response.ok(ReportUtils.getOut(Route.getCsv(getUserId(), deviceIds, groupIds, JsonConverter.parseDate(from), JsonConverter.parseDate(to)))); response.type("application/ms-excel"); @@ -40,4 +36,22 @@ public class ReportResource extends BaseResource { return response.build(); } + @Path("events") + @GET + public Response getEvents( + @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, + @QueryParam("type") final List types, + @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { + if (getAcceptHeader().equals("application/ms-excel")) { + ResponseBuilder response = Response.ok(ReportUtils.getOut(Events.getCsv(getUserId(), deviceIds, groupIds, + types, JsonConverter.parseDate(from), JsonConverter.parseDate(to)))); + response.type("application/ms-excel"); + return response.build(); + } + ResponseBuilder response = Response.ok(Events.getJson(getUserId(), deviceIds, groupIds, types, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))); + response.type(MediaType.APPLICATION_JSON); + return response.build(); + } + } diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index 1edeb0773..d3457e5a5 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -1,5 +1,45 @@ package org.traccar.reports; +import java.sql.SQLException; +import java.util.Collection; +import java.util.Date; + +import javax.json.Json; +import javax.json.JsonObjectBuilder; + +import org.traccar.Context; +import org.traccar.model.Event; +import org.traccar.web.CsvBuilder; +import org.traccar.web.JsonConverter; + public final class Events { + private Events() { + } + + public static String getJson(long userId, Collection deviceIds, Collection groupIds, + Collection types, Date from, Date to) throws SQLException { + JsonObjectBuilder json = Json.createObjectBuilder(); + for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { + Context.getPermissionsManager().checkDevice(userId, deviceId); + for (String type : types) { + json.add(String.valueOf(deviceId), JsonConverter.arrayToJson(Context.getDataManager() + .getEvents(deviceId, type, from, to))); + } + } + return json.build().toString(); + } + + public static byte[] getCsv(long userId, Collection deviceIds, Collection groupIds, + Collection types, Date from, Date to) throws SQLException { + CsvBuilder csv = new CsvBuilder(); + csv.addHeaderLine(new Event()); + for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { + Context.getPermissionsManager().checkDevice(userId, deviceId); + for (String type : types) { + csv.addArray(Context.getDataManager().getEvents(deviceId, type, from, to)); + } + } + return csv.get(); + } } diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java index 3602c4758..035526970 100644 --- a/src/org/traccar/reports/Route.java +++ b/src/org/traccar/reports/Route.java @@ -31,13 +31,11 @@ public final class Route { public static byte[] getCsv(long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws SQLException { CsvBuilder csv = new CsvBuilder(); + csv.addHeaderLine(new Position()); for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - csv.addLine(deviceId); - csv.addHeaderLine(new Position()); csv.addArray(Context.getDataManager().getPositions(deviceId, from, to)); } return csv.get(); } - } -- cgit v1.2.3