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') 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