diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-08-10 14:06:39 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-10 14:06:39 +0300 |
commit | 40e26cbf38a236f3c92e50dff619b1a29bd413de (patch) | |
tree | 450026b9cda50c8cafdf3a15ce07ed3bbccfbc3c /src/org/traccar | |
parent | 78f8d278276f0826eb302a08213609b083570760 (diff) | |
parent | ee5af32f8a9bc1edd7af02dfe3f243ebfdfb8641 (diff) | |
download | trackermap-server-40e26cbf38a236f3c92e50dff619b1a29bd413de.tar.gz trackermap-server-40e26cbf38a236f3c92e50dff619b1a29bd413de.tar.bz2 trackermap-server-40e26cbf38a236f3c92e50dff619b1a29bd413de.zip |
Merge pull request #2197 from Abyss777/reports_web
Initial implementation Report API for web
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/api/resource/ReportResource.java | 31 | ||||
-rw-r--r-- | src/org/traccar/reports/Events.java | 9 | ||||
-rw-r--r-- | src/org/traccar/reports/Route.java | 9 | ||||
-rw-r--r-- | src/org/traccar/reports/Summary.java | 9 | ||||
-rw-r--r-- | src/org/traccar/reports/model/SummaryReport.java | 8 |
5 files changed, 44 insertions, 22 deletions
diff --git a/src/org/traccar/api/resource/ReportResource.java b/src/org/traccar/api/resource/ReportResource.java index 8682070b1..e87d6401c 100644 --- a/src/org/traccar/api/resource/ReportResource.java +++ b/src/org/traccar/api/resource/ReportResource.java @@ -8,6 +8,8 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.traccar.api.BaseResource; @@ -17,12 +19,15 @@ import org.traccar.reports.Route; import org.traccar.web.JsonConverter; @Path("reports") -@Consumes("application/json") +@Consumes(MediaType.APPLICATION_JSON) public class ReportResource extends BaseResource { + public static final String TEXT_CSV = "text/csv"; + public static final String CONTENT_DISPOSITION_VALUE = "attachment; filename=report.csv"; + @Path("route") @GET - @Produces("application/json") + @Produces(MediaType.APPLICATION_JSON) public Response getRouteJson( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { @@ -32,17 +37,19 @@ public class ReportResource extends BaseResource { @Path("route") @GET - @Produces("application/ms-excel") + @Produces(TEXT_CSV) public Response getRouteCsv( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { return Response.ok(Route.getCsv(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); + JsonConverter.parseDate(from), JsonConverter.parseDate(to))) + .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE) + .build(); } @Path("events") @GET - @Produces("application/json") + @Produces(MediaType.APPLICATION_JSON) public Response getEventsJson( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, @QueryParam("type") final List<String> types, @@ -53,18 +60,20 @@ public class ReportResource extends BaseResource { @Path("events") @GET - @Produces("application/ms-excel") + @Produces(TEXT_CSV) public Response getEventsCsv( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, @QueryParam("type") final List<String> types, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { return Response.ok(Events.getCsv(getUserId(), deviceIds, groupIds, - types, JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); + types, JsonConverter.parseDate(from), JsonConverter.parseDate(to))) + .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE) + .build(); } @Path("summary") @GET - @Produces("application/json") + @Produces(MediaType.APPLICATION_JSON) public Response getSummaryJson( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { @@ -74,12 +83,14 @@ public class ReportResource extends BaseResource { @Path("summary") @GET - @Produces("application/ms-excel") + @Produces(TEXT_CSV) public Response getSummaryCsv( @QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { return Response.ok(Summary.getCsv(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); + JsonConverter.parseDate(from), JsonConverter.parseDate(to))) + .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE) + .build(); } } diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index 9e14bd3db..fbbd3131b 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -5,7 +5,7 @@ import java.util.Collection; import java.util.Date; import javax.json.Json; -import javax.json.JsonObjectBuilder; +import javax.json.JsonArrayBuilder; import org.traccar.Context; import org.traccar.model.Event; @@ -19,12 +19,13 @@ public final class Events { public static String getJson(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Collection<String> types, Date from, Date to) throws SQLException { - JsonObjectBuilder json = Json.createObjectBuilder(); + JsonArrayBuilder json = Json.createArrayBuilder(); for (long deviceId: ReportUtils.getDeviceList(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))); + for (Event event : Context.getDataManager().getEvents(deviceId, type, from, to)) { + json.add(JsonConverter.objectToJson(event)); + } } } return json.build().toString(); diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java index 7abe5c194..9622151e7 100644 --- a/src/org/traccar/reports/Route.java +++ b/src/org/traccar/reports/Route.java @@ -5,7 +5,7 @@ import java.util.Collection; import java.util.Date; import javax.json.Json; -import javax.json.JsonObjectBuilder; +import javax.json.JsonArrayBuilder; import org.traccar.Context; import org.traccar.model.Position; @@ -19,11 +19,12 @@ public final class Route { public static String getJson(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws SQLException { - JsonObjectBuilder json = Json.createObjectBuilder(); + JsonArrayBuilder json = Json.createArrayBuilder(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - json.add(String.valueOf(deviceId), JsonConverter.arrayToJson(Context.getDataManager() - .getPositions(deviceId, from, to))); + for (Position position : Context.getDataManager().getPositions(deviceId, from, to)) { + json.add(JsonConverter.objectToJson(position)); + } } return json.build().toString(); } diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index 474562002..e0da1c87e 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -7,7 +7,7 @@ import java.util.Collection; import java.util.Date; import javax.json.Json; -import javax.json.JsonObjectBuilder; +import javax.json.JsonArrayBuilder; import org.traccar.Context; import org.traccar.helper.DistanceCalculator; @@ -23,9 +23,10 @@ public final class Summary { private static SummaryReport calculateGeneralResult(long deviceId, Date from, Date to) throws SQLException { SummaryReport result = new SummaryReport(); + result.setDeviceId(deviceId); + result.setDeviceName(Context.getDeviceManager().getDeviceById(deviceId).getName()); Collection<Position> positions = Context.getDataManager().getPositions(deviceId, from, to); if (positions != null && !positions.isEmpty()) { - result.setDeviceName(Context.getDeviceManager().getDeviceById(deviceId).getName()); Position previousPosition = null; double speedSum = 0; for (Position position : positions) { @@ -45,10 +46,10 @@ public final class Summary { public static String getJson(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Date from, Date to) throws SQLException { - JsonObjectBuilder json = Json.createObjectBuilder(); + JsonArrayBuilder json = Json.createArrayBuilder(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - json.add(String.valueOf(deviceId), JsonConverter.objectToJson(calculateGeneralResult(deviceId, from, to))); + json.add(JsonConverter.objectToJson(calculateGeneralResult(deviceId, from, to))); } return json.build().toString(); } diff --git a/src/org/traccar/reports/model/SummaryReport.java b/src/org/traccar/reports/model/SummaryReport.java index aed3915f1..e42727eaf 100644 --- a/src/org/traccar/reports/model/SummaryReport.java +++ b/src/org/traccar/reports/model/SummaryReport.java @@ -2,6 +2,14 @@ package org.traccar.reports.model; public class SummaryReport { + private long deviceId; + public long getDeviceId() { + return deviceId; + } + public void setDeviceId(long deviceId) { + this.deviceId = deviceId; + } + private String deviceName; public String getDeviceName() { return deviceName; |