From a5c431069ca0dfe8240f0c8b43f0508189f8fc86 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 3 Aug 2016 10:05:40 +0500 Subject: - Added getGroupDevices to PermissionsManager - Implemented Route Report --- src/org/traccar/web/CsvBuilder.java | 150 ++++++++++++++++++++++++++++++++++++ src/org/traccar/web/WebServer.java | 3 +- 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 src/org/traccar/web/CsvBuilder.java (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/CsvBuilder.java b/src/org/traccar/web/CsvBuilder.java new file mode 100644 index 000000000..cfebec3b4 --- /dev/null +++ b/src/org/traccar/web/CsvBuilder.java @@ -0,0 +1,150 @@ +package org.traccar.web; + +import java.beans.Introspector; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Collection; +import java.util.Date; +import java.util.Map; + +import org.joda.time.DateTime; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; +import org.traccar.helper.Log; +import org.traccar.model.MiscFormatter; + +public class CsvBuilder { + + private static final String LINE_ENDING = "\r\n"; + private static final String SEPARATOR = ";"; + private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.dateTime(); + + private StringBuilder builder = new StringBuilder(); + + private void addLineEnding() { + builder.append(LINE_ENDING); + } + private void addSeparator() { + builder.append(SEPARATOR); + } + public void addLine(String value) { + if (value != null) { + builder.append(value); + } + addLineEnding(); + } + + public void addLine(long value) { + builder.append(value); + addLineEnding(); + } + + public void addLine(double value) { + builder.append(value); + addLineEnding(); + } + + public void addLine(boolean value) { + builder.append(value); + addLineEnding(); + } + + public void addField(String value) { + builder.append(value); + addSeparator(); + } + + public void addField(long value) { + builder.append(value); + addSeparator(); + } + + public void addField(int value) { + builder.append(value); + addSeparator(); + } + + public void addField(double value) { + builder.append(value); + addSeparator(); + } + + public void addField(boolean value) { + builder.append(value); + addSeparator(); + } + + public void addLine(Object object) { + + Method[] methods = object.getClass().getMethods(); + + for (Method method : methods) { + if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) { + try { + if (method.getReturnType().equals(boolean.class)) { + addField((Boolean) method.invoke(object)); + } else if (method.getReturnType().equals(int.class)) { + addField((Integer) method.invoke(object)); + } else if (method.getReturnType().equals(long.class)) { + addField((Long) method.invoke(object)); + } else if (method.getReturnType().equals(double.class)) { + addField((Double) method.invoke(object)); + } else if (method.getReturnType().equals(String.class)) { + addField((String) method.invoke(object)); + } else if (method.getReturnType().equals(Date.class)) { + Date value = (Date) method.invoke(object); + addField(DATE_FORMAT.print(new DateTime(value))); + } else if (method.getReturnType().equals(Map.class)) { + Map value = (Map) method.invoke(object); + if (value != null) { + addField(MiscFormatter.toJson(value).toString()); + } + } + } catch (IllegalAccessException | InvocationTargetException error) { + Log.warning(error); + } + } + } + addLineEnding(); + } + + public void addHeaderLine(Object object) { + Method[] methods = object.getClass().getMethods(); + + for (Method method : methods) { + if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) { + String name = Introspector.decapitalize(method.getName().substring(3)); + if (!name.equals("class")) { + addField(name); + } + } + } + addLineEnding(); + } + + public void addArray(Collection array) { + for (Object object : array) { + switch (object.getClass().getSimpleName().toLowerCase()) { + case "string": + addLine(object.toString()); + break; + case "long": + addLine((long) object); + break; + case "double": + addLine((double) object); + break; + case "boolean": + addLine((boolean) object); + break; + default: + addLine(object); + break; + } + } + } + + public byte[] get() { + return String.valueOf(builder).getBytes(); + } +} diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 4ef31b1df..e022a9285 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -44,6 +44,7 @@ import org.traccar.api.resource.GroupResource; import org.traccar.api.resource.NotificationResource; import org.traccar.api.resource.DeviceResource; import org.traccar.api.resource.PositionResource; +import org.traccar.api.resource.ReportResource; import org.traccar.api.resource.CommandTypeResource; import org.traccar.api.resource.DeviceGeofenceResource; import org.traccar.api.resource.EventResource; @@ -160,7 +161,7 @@ public class WebServer { GroupResource.class, DeviceResource.class, PositionResource.class, CommandTypeResource.class, EventResource.class, GeofenceResource.class, DeviceGeofenceResource.class, GeofencePermissionResource.class, GroupGeofenceResource.class, - NotificationResource.class); + NotificationResource.class, ReportResource.class); servletHandler.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*"); handlers.addHandler(servletHandler); -- cgit v1.2.3 From 933247b3d2ec24d65203fae39dcafbe2c9afb87e Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 5 Aug 2016 11:58:43 +0500 Subject: - Added distance rounding - Fixed charset --- src/org/traccar/reports/General.java | 3 +++ src/org/traccar/web/CsvBuilder.java | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/reports/General.java b/src/org/traccar/reports/General.java index bcf2312b2..75194bc13 100644 --- a/src/org/traccar/reports/General.java +++ b/src/org/traccar/reports/General.java @@ -1,5 +1,7 @@ package org.traccar.reports; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.sql.SQLException; import java.util.Collection; import java.util.Date; @@ -76,6 +78,7 @@ public final class General { result.setMaxSpeed(position.getSpeed()); } result.setAverageSpeed(speedSum / positions.size()); + result.setDistance(new BigDecimal(result.getDistance()).setScale(2, RoundingMode.HALF_UP).doubleValue()); } return result; } diff --git a/src/org/traccar/web/CsvBuilder.java b/src/org/traccar/web/CsvBuilder.java index cfebec3b4..3f93ee102 100644 --- a/src/org/traccar/web/CsvBuilder.java +++ b/src/org/traccar/web/CsvBuilder.java @@ -3,6 +3,7 @@ package org.traccar.web; import java.beans.Introspector; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Date; import java.util.Map; @@ -145,6 +146,6 @@ public class CsvBuilder { } public byte[] get() { - return String.valueOf(builder).getBytes(); + return String.valueOf(builder).getBytes(StandardCharsets.UTF_8); } } -- cgit v1.2.3 From 5a6e7582a7978341f3a2de9a81cb63252bca0bb7 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 5 Aug 2016 14:41:53 +0500 Subject: - Moved GeneralReport to separate file - Removed unnecessary getOut function - Added methods sorting --- src/org/traccar/api/resource/ReportResource.java | 13 +++--- src/org/traccar/reports/Events.java | 2 +- src/org/traccar/reports/General.java | 50 +++--------------------- src/org/traccar/reports/ReportUtils.java | 13 ------ src/org/traccar/reports/Route.java | 2 +- src/org/traccar/reports/model/GeneralReport.java | 42 ++++++++++++++++++++ src/org/traccar/web/CsvBuilder.java | 26 +++++++++--- 7 files changed, 76 insertions(+), 72 deletions(-) create mode 100644 src/org/traccar/reports/model/GeneralReport.java (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/api/resource/ReportResource.java b/src/org/traccar/api/resource/ReportResource.java index 0885a3638..6c5772584 100644 --- a/src/org/traccar/api/resource/ReportResource.java +++ b/src/org/traccar/api/resource/ReportResource.java @@ -13,7 +13,6 @@ 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.ReportUtils; import org.traccar.reports.Route; import org.traccar.web.JsonConverter; @@ -26,8 +25,8 @@ public class ReportResource extends BaseResource { @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(ReportUtils.getOut(Route.getCsv(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to)))); + ResponseBuilder response = Response.ok(Route.getCsv(getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))); response.type("application/ms-excel"); return response.build(); } @@ -44,8 +43,8 @@ public class ReportResource extends BaseResource { @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)))); + ResponseBuilder response = Response.ok(Events.getCsv(getUserId(), deviceIds, groupIds, + types, JsonConverter.parseDate(from), JsonConverter.parseDate(to))); response.type("application/ms-excel"); return response.build(); } @@ -61,8 +60,8 @@ public class ReportResource extends BaseResource { @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(ReportUtils.getOut(General.getCsv(getUserId(), deviceIds, groupIds, - JsonConverter.parseDate(from), JsonConverter.parseDate(to)))); + ResponseBuilder response = Response.ok(General.getCsv(getUserId(), deviceIds, groupIds, + JsonConverter.parseDate(from), JsonConverter.parseDate(to))); response.type("application/ms-excel"); return response.build(); } diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index d3457e5a5..de4354371 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -30,7 +30,7 @@ public final class Events { return json.build().toString(); } - public static byte[] getCsv(long userId, Collection deviceIds, Collection groupIds, + public static String getCsv(long userId, Collection deviceIds, Collection groupIds, Collection types, Date from, Date to) throws SQLException { CsvBuilder csv = new CsvBuilder(); csv.addHeaderLine(new Event()); diff --git a/src/org/traccar/reports/General.java b/src/org/traccar/reports/General.java index 75194bc13..595dd49f9 100644 --- a/src/org/traccar/reports/General.java +++ b/src/org/traccar/reports/General.java @@ -12,6 +12,7 @@ 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; @@ -20,49 +21,8 @@ public final class General { private General() { } - public static class GeneralResult { - - 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; - } - } - } - - private static GeneralResult calculateGeneralResult(long deviceId, Date from, Date to) throws SQLException { - GeneralResult result = new GeneralResult(); + 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()); @@ -93,12 +53,12 @@ public final class General { return json.build().toString(); } - public static byte[] getCsv(long userId, Collection deviceIds, Collection groupIds, + public static String getCsv(long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws SQLException { CsvBuilder csv = new CsvBuilder(); for (long deviceId: ReportUtils.getReportedDevices(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - csv.addHeaderLine(new GeneralResult()); + csv.addHeaderLine(new GeneralReport()); 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 e8048f1f2..c4c63e525 100644 --- a/src/org/traccar/reports/ReportUtils.java +++ b/src/org/traccar/reports/ReportUtils.java @@ -1,13 +1,8 @@ package org.traccar.reports; -import java.io.IOException; -import java.io.OutputStream; import java.util.ArrayList; import java.util.Collection; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.StreamingOutput; - import org.traccar.Context; public final class ReportUtils { @@ -24,12 +19,4 @@ public final class ReportUtils { return result; } - public static StreamingOutput getOut(final byte[] csvBytes) { - return new StreamingOutput() { - @Override - public void write(OutputStream out) throws IOException, WebApplicationException { - out.write(csvBytes); - } - }; - } } diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java index 035526970..a77e3c5ff 100644 --- a/src/org/traccar/reports/Route.java +++ b/src/org/traccar/reports/Route.java @@ -28,7 +28,7 @@ public final class Route { return json.build().toString(); } - public static byte[] getCsv(long userId, Collection deviceIds, Collection groupIds, + public static String getCsv(long userId, Collection deviceIds, Collection groupIds, Date from, Date to) throws SQLException { CsvBuilder csv = new CsvBuilder(); csv.addHeaderLine(new Position()); diff --git a/src/org/traccar/reports/model/GeneralReport.java b/src/org/traccar/reports/model/GeneralReport.java new file mode 100644 index 000000000..f60bb7238 --- /dev/null +++ b/src/org/traccar/reports/model/GeneralReport.java @@ -0,0 +1,42 @@ +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/web/CsvBuilder.java b/src/org/traccar/web/CsvBuilder.java index 3f93ee102..d45d12873 100644 --- a/src/org/traccar/web/CsvBuilder.java +++ b/src/org/traccar/web/CsvBuilder.java @@ -3,10 +3,13 @@ package org.traccar.web; import java.beans.Introspector; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.nio.charset.StandardCharsets; +import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; import java.util.Date; import java.util.Map; +import java.util.SortedSet; +import java.util.TreeSet; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormatter; @@ -75,9 +78,21 @@ public class CsvBuilder { addSeparator(); } + private SortedSet getSortedMethods(Object object) { + Method[] methodArray = object.getClass().getMethods(); + SortedSet methods = new TreeSet(new Comparator() { + @Override + public int compare(Method m1, Method m2) { + return m1.getName().compareTo(m2.getName()); + } + }); + methods.addAll(Arrays.asList(methodArray)); + return methods; + } + public void addLine(Object object) { - Method[] methods = object.getClass().getMethods(); + SortedSet methods = getSortedMethods(object); for (Method method : methods) { if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) { @@ -110,7 +125,8 @@ public class CsvBuilder { } public void addHeaderLine(Object object) { - Method[] methods = object.getClass().getMethods(); + + SortedSet methods = getSortedMethods(object); for (Method method : methods) { if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) { @@ -145,7 +161,7 @@ public class CsvBuilder { } } - public byte[] get() { - return String.valueOf(builder).getBytes(StandardCharsets.UTF_8); + public String get() { + return builder.toString(); } } -- cgit v1.2.3 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/traccar/web') 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 From e316afef8768c9c08d41cb9ae0a845a93a5fe95a Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 8 Aug 2016 12:22:37 +0500 Subject: Removed addLine and addField functions --- src/org/traccar/web/CsvBuilder.java | 82 +++++++++++-------------------------- 1 file changed, 24 insertions(+), 58 deletions(-) (limited to 'src/org/traccar/web') diff --git a/src/org/traccar/web/CsvBuilder.java b/src/org/traccar/web/CsvBuilder.java index fc0336513..73d5fac1d 100644 --- a/src/org/traccar/web/CsvBuilder.java +++ b/src/org/traccar/web/CsvBuilder.java @@ -31,52 +31,6 @@ public class CsvBuilder { private void addSeparator() { builder.append(SEPARATOR); } - public void addLine(String value) { - if (value != null) { - builder.append(value); - } - addLineEnding(); - } - - public void addLine(long value) { - builder.append(value); - addLineEnding(); - } - - public void addLine(double value) { - builder.append(value); - addLineEnding(); - } - - public void addLine(boolean value) { - builder.append(value); - addLineEnding(); - } - - public void addField(String value) { - builder.append(value); - addSeparator(); - } - - public void addField(long value) { - builder.append(value); - addSeparator(); - } - - public void addField(int value) { - builder.append(value); - addSeparator(); - } - - public void addField(double value) { - builder.append(value); - addSeparator(); - } - - public void addField(boolean value) { - builder.append(value); - addSeparator(); - } private SortedSet getSortedMethods(Object object) { Method[] methodArray = object.getClass().getMethods(); @@ -98,22 +52,29 @@ public class CsvBuilder { if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) { try { if (method.getReturnType().equals(boolean.class)) { - addField((Boolean) method.invoke(object)); + builder.append((Boolean) method.invoke(object)); + addSeparator(); } else if (method.getReturnType().equals(int.class)) { - addField((Integer) method.invoke(object)); + builder.append((Integer) method.invoke(object)); + addSeparator(); } else if (method.getReturnType().equals(long.class)) { - addField((Long) method.invoke(object)); + builder.append((Long) method.invoke(object)); + addSeparator(); } else if (method.getReturnType().equals(double.class)) { - addField((Double) method.invoke(object)); + builder.append((Double) method.invoke(object)); + addSeparator(); } else if (method.getReturnType().equals(String.class)) { - addField((String) method.invoke(object)); + builder.append((String) method.invoke(object)); + addSeparator(); } else if (method.getReturnType().equals(Date.class)) { Date value = (Date) method.invoke(object); - addField(DATE_FORMAT.print(new DateTime(value))); + builder.append(DATE_FORMAT.print(new DateTime(value))); + addSeparator(); } else if (method.getReturnType().equals(Map.class)) { Map value = (Map) method.invoke(object); if (value != null) { - addField(MiscFormatter.toJson(value).toString()); + builder.append(MiscFormatter.toJson(value).toString()); + addSeparator(); } } } catch (IllegalAccessException | InvocationTargetException error) { @@ -132,7 +93,8 @@ public class CsvBuilder { if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) { String name = Introspector.decapitalize(method.getName().substring(3)); if (!name.equals("class")) { - addField(name); + builder.append(name); + addSeparator(); } } } @@ -143,16 +105,20 @@ public class CsvBuilder { for (Object object : array) { switch (object.getClass().getSimpleName().toLowerCase()) { case "string": - addLine(object.toString()); + builder.append(object.toString()); + addLineEnding(); break; case "long": - addLine((long) object); + builder.append((long) object); + addLineEnding(); break; case "double": - addLine((double) object); + builder.append((double) object); + addLineEnding(); break; case "boolean": - addLine((boolean) object); + builder.append((boolean) object); + addLineEnding(); break; default: addLine(object); -- cgit v1.2.3