From 16771c045a3d6fdffad322e55b6d346c93a03f71 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 8 Aug 2016 11:55:27 +0500 Subject: - Used @Consumes and @Produces instead of direct header access - Renamed General to Summary - Other changes --- src/org/traccar/api/BaseResource.java | 9 --- src/org/traccar/api/resource/ReportResource.java | 85 +++++++++++++----------- src/org/traccar/reports/Events.java | 6 +- src/org/traccar/reports/General.java | 66 ------------------ src/org/traccar/reports/ReportUtils.java | 2 +- src/org/traccar/reports/Route.java | 6 +- src/org/traccar/reports/Summary.java | 66 ++++++++++++++++++ src/org/traccar/reports/model/GeneralReport.java | 42 ------------ src/org/traccar/reports/model/SummaryReport.java | 42 ++++++++++++ src/org/traccar/web/CsvBuilder.java | 2 +- 10 files changed, 164 insertions(+), 162 deletions(-) delete mode 100644 src/org/traccar/reports/General.java create mode 100644 src/org/traccar/reports/Summary.java delete mode 100644 src/org/traccar/reports/model/GeneralReport.java create mode 100644 src/org/traccar/reports/model/SummaryReport.java (limited to 'src/org') diff --git a/src/org/traccar/api/BaseResource.java b/src/org/traccar/api/BaseResource.java index 8259f8868..567b9735a 100644 --- a/src/org/traccar/api/BaseResource.java +++ b/src/org/traccar/api/BaseResource.java @@ -15,7 +15,6 @@ */ package org.traccar.api; -import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.SecurityContext; public class BaseResource { @@ -23,9 +22,6 @@ 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) { @@ -33,9 +29,4 @@ 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 6c5772584..8682070b1 100644 --- a/src/org/traccar/api/resource/ReportResource.java +++ b/src/org/traccar/api/resource/ReportResource.java @@ -3,72 +3,83 @@ package org.traccar.api.resource; import java.sql.SQLException; import java.util.List; +import javax.ws.rs.Consumes; 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.MediaType; 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.General; +import org.traccar.reports.Summary; import org.traccar.reports.Route; import org.traccar.web.JsonConverter; @Path("reports") +@Consumes("application/json") public class ReportResource extends BaseResource { @Path("route") @GET - public Response getRoute( + @Produces("application/json") + public Response getRouteJson( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - if (getAcceptHeader().equals("application/ms-excel")) { - ResponseBuilder response = Response.ok(Route.getCsv(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))); - response.type("application/ms-excel"); - return response.build(); - } - ResponseBuilder response = Response.ok(Route.getJson(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))); - response.type(MediaType.APPLICATION_JSON); - return response.build(); + return Response.ok(Route.getJson(getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); + } + + @Path("route") + @GET + @Produces("application/ms-excel") + public Response getRouteCsv( + @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List 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(); + } + + @Path("events") + @GET + @Produces("application/json") + public Response getEventsJson( + @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 { + return Response.ok(Events.getJson(getUserId(), deviceIds, groupIds, types, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); } @Path("events") @GET - public Response getEvents( + @Produces("application/ms-excel") + public Response getEventsCsv( @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(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(); + return Response.ok(Events.getCsv(getUserId(), deviceIds, groupIds, + types, JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); + } + + @Path("summary") + @GET + @Produces("application/json") + public Response getSummaryJson( + @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, + @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { + return Response.ok(Summary.getJson(getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); } - @Path("general") + @Path("summary") @GET - public Response getGeneral( + @Produces("application/ms-excel") + public Response getSummaryCsv( @QueryParam("deviceId") final List deviceIds, @QueryParam("groupId") final List groupIds, @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException { - if (getAcceptHeader().equals("application/ms-excel")) { - ResponseBuilder response = Response.ok(General.getCsv(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))); - response.type("application/ms-excel"); - return response.build(); - } - ResponseBuilder response = Response.ok(General.getJson(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to))); - response.type(MediaType.APPLICATION_JSON); - return response.build(); + return Response.ok(Summary.getCsv(getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))).build(); } } diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index de4354371..9e14bd3db 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -20,7 +20,7 @@ public final class 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)) { + 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() @@ -34,12 +34,12 @@ public final class Events { Collection types, Date from, Date to) throws SQLException { CsvBuilder csv = new CsvBuilder(); csv.addHeaderLine(new Event()); - for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { + for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); for (String type : types) { csv.addArray(Context.getDataManager().getEvents(deviceId, type, from, to)); } } - return csv.get(); + return csv.build(); } } diff --git a/src/org/traccar/reports/General.java b/src/org/traccar/reports/General.java deleted file mode 100644 index 8a2b3940b..000000000 --- a/src/org/traccar/reports/General.java +++ /dev/null @@ -1,66 +0,0 @@ -package org.traccar.reports; - -import java.math.BigDecimal; -import java.math.RoundingMode; -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.helper.DistanceCalculator; -import org.traccar.model.Position; -import org.traccar.reports.model.GeneralReport; -import org.traccar.web.CsvBuilder; -import org.traccar.web.JsonConverter; - -public final class General { - - private General() { - } - - private static GeneralReport calculateGeneralResult(long deviceId, Date from, Date to) throws SQLException { - GeneralReport result = new GeneralReport(); - Collection 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) { - if (previousPosition != null) { - result.addDistance(DistanceCalculator.distance(previousPosition.getLatitude(), - previousPosition.getLongitude(), position.getLatitude(), position.getLongitude())); - } - previousPosition = position; - speedSum += position.getSpeed(); - result.setMaxSpeed(position.getSpeed()); - } - result.setAverageSpeed(speedSum / positions.size()); - result.setDistance(new BigDecimal(result.getDistance()).setScale(2, RoundingMode.HALF_UP).doubleValue()); - } - return result; - } - - public static String getJson(long userId, Collection deviceIds, Collection groupIds, - Date from, Date to) throws SQLException { - JsonObjectBuilder json = Json.createObjectBuilder(); - for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { - Context.getPermissionsManager().checkDevice(userId, deviceId); - json.add(String.valueOf(deviceId), JsonConverter.objectToJson(calculateGeneralResult(deviceId, from, to))); - } - return json.build().toString(); - } - - public static String getCsv(long userId, Collection deviceIds, Collection groupIds, - Date from, Date to) throws SQLException { - CsvBuilder csv = new CsvBuilder(); - csv.addHeaderLine(new GeneralReport()); - for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { - Context.getPermissionsManager().checkDevice(userId, deviceId); - csv.addLine(calculateGeneralResult(deviceId, from, to)); - } - return csv.get(); - } -} diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java index c4c63e525..5041871f7 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -10,7 +10,7 @@ public final class ReportUtils { private ReportUtils() { } - public static Collection getReportedDevices(Collection deviceIds, Collection groupIds) { + public static Collection getDeviceList(Collection deviceIds, Collection groupIds) { Collection result = new ArrayList<>(); result.addAll(deviceIds); for (long groupId : groupIds) { diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java index a77e3c5ff..7abe5c194 100644 --- a/src/org/traccar/reports/Route.java +++ b/src/org/traccar/reports/Route.java @@ -20,7 +20,7 @@ public final class Route { public static String getJson(long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws SQLException { JsonObjectBuilder json = Json.createObjectBuilder(); - for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { + 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))); @@ -32,10 +32,10 @@ public final class Route { Date from, Date to) throws SQLException { CsvBuilder csv = new CsvBuilder(); csv.addHeaderLine(new Position()); - for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { + for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); csv.addArray(Context.getDataManager().getPositions(deviceId, from, to)); } - return csv.get(); + return csv.build(); } } diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java new file mode 100644 index 000000000..474562002 --- /dev/null +++ b/src/org/traccar/reports/Summary.java @@ -0,0 +1,66 @@ +package org.traccar.reports; + +import java.math.BigDecimal; +import java.math.RoundingMode; +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.helper.DistanceCalculator; +import org.traccar.model.Position; +import org.traccar.reports.model.SummaryReport; +import org.traccar.web.CsvBuilder; +import org.traccar.web.JsonConverter; + +public final class Summary { + + private Summary() { + } + + private static SummaryReport calculateGeneralResult(long deviceId, Date from, Date to) throws SQLException { + SummaryReport result = new SummaryReport(); + Collection 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) { + if (previousPosition != null) { + result.addDistance(DistanceCalculator.distance(previousPosition.getLatitude(), + previousPosition.getLongitude(), position.getLatitude(), position.getLongitude())); + } + previousPosition = position; + speedSum += position.getSpeed(); + result.setMaxSpeed(position.getSpeed()); + } + result.setAverageSpeed(speedSum / positions.size()); + result.setDistance(new BigDecimal(result.getDistance()).setScale(2, RoundingMode.HALF_UP).doubleValue()); + } + return result; + } + + public static String getJson(long userId, Collection deviceIds, Collection groupIds, + Date from, Date to) throws SQLException { + JsonObjectBuilder json = Json.createObjectBuilder(); + for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { + Context.getPermissionsManager().checkDevice(userId, deviceId); + json.add(String.valueOf(deviceId), JsonConverter.objectToJson(calculateGeneralResult(deviceId, from, to))); + } + return json.build().toString(); + } + + public static String getCsv(long userId, Collection deviceIds, Collection groupIds, + Date from, Date to) throws SQLException { + CsvBuilder csv = new CsvBuilder(); + csv.addHeaderLine(new SummaryReport()); + for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { + Context.getPermissionsManager().checkDevice(userId, deviceId); + csv.addLine(calculateGeneralResult(deviceId, from, to)); + } + return csv.build(); + } +} diff --git a/src/org/traccar/reports/model/GeneralReport.java b/src/org/traccar/reports/model/GeneralReport.java deleted file mode 100644 index f60bb7238..000000000 --- a/src/org/traccar/reports/model/GeneralReport.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.traccar.reports.model; - -public class GeneralReport { - - private String deviceName; - public String getDeviceName() { - return deviceName; - } - public void setDeviceName(String deviceName) { - this.deviceName = deviceName; - } - - private double distance = 0; - public double getDistance() { - return distance; - } - public void setDistance(double distance) { - this.distance = distance; - } - - public void addDistance(double distance) { - this.distance += distance; - } - - private double averageSpeed = 0; - public double getAverageSpeed() { - return averageSpeed; - } - public void setAverageSpeed(double averageSpeed) { - this.averageSpeed = averageSpeed; - } - - private double maxSpeed = 0; - public double getMaxSpeed() { - return maxSpeed; - } - public void setMaxSpeed(double maxSpeed) { - if (maxSpeed > this.maxSpeed) { - this.maxSpeed = maxSpeed; - } - } -} diff --git a/src/org/traccar/reports/model/SummaryReport.java b/src/org/traccar/reports/model/SummaryReport.java new file mode 100644 index 000000000..aed3915f1 --- /dev/null +++ b/src/org/traccar/reports/model/SummaryReport.java @@ -0,0 +1,42 @@ +package org.traccar.reports.model; + +public class SummaryReport { + + private String deviceName; + public String getDeviceName() { + return deviceName; + } + public void setDeviceName(String deviceName) { + this.deviceName = deviceName; + } + + private double distance; + public double getDistance() { + return distance; + } + public void setDistance(double distance) { + this.distance = distance; + } + + public void addDistance(double distance) { + this.distance += distance; + } + + private double averageSpeed; + public double getAverageSpeed() { + return averageSpeed; + } + public void setAverageSpeed(double averageSpeed) { + this.averageSpeed = averageSpeed; + } + + private double maxSpeed; + public double getMaxSpeed() { + return maxSpeed; + } + public void setMaxSpeed(double maxSpeed) { + if (maxSpeed > this.maxSpeed) { + this.maxSpeed = maxSpeed; + } + } +} diff --git a/src/org/traccar/web/CsvBuilder.java b/src/org/traccar/web/CsvBuilder.java index d45d12873..fc0336513 100644 --- a/src/org/traccar/web/CsvBuilder.java +++ b/src/org/traccar/web/CsvBuilder.java @@ -161,7 +161,7 @@ public class CsvBuilder { } } - public String get() { + public String build() { return builder.toString(); } } -- cgit v1.2.3