aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2016-10-17 14:02:23 +0500
committerAbyss777 <abyss@fox5.ru>2016-10-17 14:02:23 +0500
commitc613143105d9f7c898dd3fb236d683b76cf8a42c (patch)
tree0facff2ac16b5d0067fc651fd5024aa2b668ebaf
parent83ca0476167d91f16f18ae3cc532d56cd97f743e (diff)
downloadtraccar-server-c613143105d9f7c898dd3fb236d683b76cf8a42c.tar.gz
traccar-server-c613143105d9f7c898dd3fb236d683b76cf8a42c.tar.bz2
traccar-server-c613143105d9f7c898dd3fb236d683b76cf8a42c.zip
- Implement export reports to excel
- Migrate csv export to /api/positions
-rw-r--r--pom.xml10
-rw-r--r--src/org/traccar/api/resource/PositionResource.java24
-rw-r--r--src/org/traccar/api/resource/ReportResource.java70
-rw-r--r--src/org/traccar/reports/Events.java87
-rw-r--r--src/org/traccar/reports/Route.java62
-rw-r--r--src/org/traccar/reports/Summary.java29
-rw-r--r--src/org/traccar/reports/Trips.java68
-rw-r--r--src/org/traccar/reports/model/DeviceReport.java49
-rw-r--r--src/org/traccar/reports/model/TripReport.java48
-rw-r--r--templates/export/events.xlsxbin0 -> 14120 bytes
-rw-r--r--templates/export/route.xlsxbin0 -> 14505 bytes
-rw-r--r--templates/export/summary.xlsxbin0 -> 14413 bytes
-rw-r--r--templates/export/trips.xlsxbin0 -> 14666 bytes
13 files changed, 387 insertions, 60 deletions
diff --git a/pom.xml b/pom.xml
index e99d8a2e1..24c3b442e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -117,6 +117,16 @@
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
+ <dependency>
+ <groupId>org.jxls</groupId>
+ <artifactId>jxls</artifactId>
+ <version>2.3.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jxls</groupId>
+ <artifactId>jxls-poi</artifactId>
+ <version>1.0.11</version>
+ </dependency>
</dependencies>
diff --git a/src/org/traccar/api/resource/PositionResource.java b/src/org/traccar/api/resource/PositionResource.java
index e00e06e7a..92b62c0a6 100644
--- a/src/org/traccar/api/resource/PositionResource.java
+++ b/src/org/traccar/api/resource/PositionResource.java
@@ -18,6 +18,7 @@ package org.traccar.api.resource;
import org.traccar.Context;
import org.traccar.api.BaseResource;
import org.traccar.model.Position;
+import org.traccar.web.CsvBuilder;
import org.traccar.web.JsonConverter;
import javax.ws.rs.Consumes;
@@ -25,17 +26,23 @@ 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 java.sql.SQLException;
import java.util.Collection;
@Path("positions")
-@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PositionResource extends BaseResource {
+ public static final String TEXT_CSV = "text/csv";
+ public static final String CONTENT_DISPOSITION_VALUE_CSV = "attachment; filename=positions.csv";
+
@GET
- public Collection<Position> get(
+ @Produces(MediaType.APPLICATION_JSON)
+ public Collection<Position> getJson(
@QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to)
throws SQLException {
if (deviceId == 0) {
@@ -47,4 +54,17 @@ public class PositionResource extends BaseResource {
}
}
+ @GET
+ @Produces(TEXT_CSV)
+ public Response getCsv(
+ @QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to)
+ throws SQLException {
+ Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
+ CsvBuilder csv = new CsvBuilder();
+ csv.addHeaderLine(new Position());
+ csv.addArray(Context.getDataManager().getPositions(
+ deviceId, JsonConverter.parseDate(from), JsonConverter.parseDate(to)));
+ return Response.ok(csv.build()).header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_CSV).build();
+ }
+
}
diff --git a/src/org/traccar/api/resource/ReportResource.java b/src/org/traccar/api/resource/ReportResource.java
index 0dd0452ff..709eef377 100644
--- a/src/org/traccar/api/resource/ReportResource.java
+++ b/src/org/traccar/api/resource/ReportResource.java
@@ -1,5 +1,7 @@
package org.traccar.api.resource;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@@ -23,8 +25,8 @@ import org.traccar.web.JsonConverter;
@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";
+ private static final String XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
+ private static final String CONTENT_DISPOSITION_VALUE_XLSX = "attachment; filename=report.xlsx";
@Path("route")
@GET
@@ -38,14 +40,16 @@ public class ReportResource extends BaseResource {
@Path("route")
@GET
- @Produces(TEXT_CSV)
- public Response getRouteCsv(
+ @Produces(XLSX)
+ public Response getRouteExcel(
@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)))
- .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE)
- .build();
+ @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException {
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ Route.getExcel(stream, getUserId(), deviceIds, groupIds,
+ JsonConverter.parseDate(from), JsonConverter.parseDate(to));
+
+ return Response.ok(stream.toByteArray())
+ .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build();
}
@Path("events")
@@ -61,15 +65,17 @@ public class ReportResource extends BaseResource {
@Path("events")
@GET
- @Produces(TEXT_CSV)
- public Response getEventsCsv(
+ @Produces(XLSX)
+ public Response getEventsExcel(
@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)))
- .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE)
- .build();
+ @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException {
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ Events.getExcel(stream, getUserId(), deviceIds, groupIds, types,
+ JsonConverter.parseDate(from), JsonConverter.parseDate(to));
+
+ return Response.ok(stream.toByteArray())
+ .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build();
}
@Path("summary")
@@ -84,14 +90,16 @@ public class ReportResource extends BaseResource {
@Path("summary")
@GET
- @Produces(TEXT_CSV)
- public Response getSummaryCsv(
+ @Produces(XLSX)
+ public Response getSummaryExcel(
@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)))
- .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE)
- .build();
+ @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException {
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ Summary.getExcel(stream, getUserId(), deviceIds, groupIds,
+ JsonConverter.parseDate(from), JsonConverter.parseDate(to));
+
+ return Response.ok(stream.toByteArray())
+ .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build();
}
@Path("trips")
@@ -106,14 +114,16 @@ public class ReportResource extends BaseResource {
@Path("trips")
@GET
- @Produces(TEXT_CSV)
- public Response getTripsCsv(
+ @Produces(XLSX)
+ public Response getTripsExcel(
@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(Trips.getCsv(getUserId(), deviceIds, groupIds,
- JsonConverter.parseDate(from), JsonConverter.parseDate(to)))
- .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE)
- .build();
+ @QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException {
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ Trips.getExcel(stream, getUserId(), deviceIds, groupIds,
+ JsonConverter.parseDate(from), JsonConverter.parseDate(to));
+
+ return Response.ok(stream.toByteArray())
+ .header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build();
}
}
diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java
index ed7e9e411..9f58e1bf4 100644
--- a/src/org/traccar/reports/Events.java
+++ b/src/org/traccar/reports/Events.java
@@ -16,16 +16,36 @@
*/
package org.traccar.reports;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.Comparator;
import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.SortedSet;
+import java.util.TreeSet;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
+import org.jxls.area.Area;
+import org.jxls.builder.xls.XlsCommentAreaBuilder;
+import org.jxls.common.CellRef;
+import org.jxls.formula.StandardFormulaProcessor;
+import org.jxls.transform.Transformer;
+import org.jxls.transform.poi.PoiTransformer;
+import org.jxls.util.TransformerFactory;
import org.traccar.Context;
+import org.traccar.model.Device;
import org.traccar.model.Event;
-import org.traccar.web.CsvBuilder;
+import org.traccar.model.Geofence;
+import org.traccar.model.Group;
+import org.traccar.reports.model.DeviceReport;
import org.traccar.web.JsonConverter;
public final class Events {
@@ -47,16 +67,69 @@ public final class Events {
return json.build().toString();
}
- public static String getCsv(long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
- Collection<String> types, Date from, Date to) throws SQLException {
- CsvBuilder csv = new CsvBuilder();
- csv.addHeaderLine(new Event());
+ public static void getExcel(OutputStream outputStream,
+ long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
+ Collection<String> types, Date from, Date to) throws SQLException, IOException {
+ ArrayList<DeviceReport> devicesEvents = new ArrayList<>();
+ ArrayList<String> sheetNames = new ArrayList<>();
+ HashMap<Long, String> geofenceNames = new HashMap<>();
for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
+ SortedSet<Event> eventReports = new TreeSet<Event>(new Comparator<Event>() {
+ @Override
+ public int compare(Event e1, Event e2) {
+ return e1.getServerTime().compareTo(e2.getServerTime());
+ }
+ });
for (String type : types) {
- csv.addArray(Context.getDataManager().getEvents(deviceId, type, from, to));
+ Collection<Event> events = Context.getDataManager().getEvents(deviceId, type, from, to);
+ for (Event event : events) {
+ long geofenceId = event.getGeofenceId();
+ if (geofenceId != 0) {
+ Geofence geofence = Context.getGeofenceManager().getGeofence(geofenceId);
+ if (geofence != null) {
+ geofenceNames.put(geofenceId, geofence.getName());
+ } else {
+ geofenceNames.put(geofenceId, Long.toString(geofenceId));
+ }
+ }
+ eventReports.add(event);
+ }
+ }
+ if (!eventReports.isEmpty()) {
+ DeviceReport deviceEvents = new DeviceReport();
+ Device device = Context.getIdentityManager().getDeviceById(deviceId);
+ deviceEvents.setDeviceName(device.getName());
+ sheetNames.add(deviceEvents.getDeviceName());
+ if (device.getGroupId() != 0) {
+ Group group = Context.getDeviceManager().getGroupById(device.getGroupId());
+ if (group != null) {
+ deviceEvents.setGroupName(group.getName());
+ }
+ }
+ deviceEvents.setObjects(eventReports);
+ devicesEvents.add(deviceEvents);
+ }
+ }
+ String templatePath = Context.getConfig().getString("report.events.template.excel",
+ "templates/export/events.xlsx");
+ try (InputStream inputStream = new FileInputStream(templatePath)) {
+ org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext();
+ jxlsContext.putVar("devices", devicesEvents);
+ jxlsContext.putVar("sheetNames", sheetNames);
+ jxlsContext.putVar("geofenceNames", geofenceNames);
+ jxlsContext.putVar("from", from);
+ jxlsContext.putVar("to", to);
+ jxlsContext.putVar("bracketsRegex", "[\\{\\}\"]");
+ Transformer transformer = TransformerFactory.createTransformer(inputStream, outputStream);
+ List<Area> xlsAreas = new XlsCommentAreaBuilder(transformer).build();
+ for (Area xlsArea : xlsAreas) {
+ xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), jxlsContext);
+ xlsArea.setFormulaProcessor(new StandardFormulaProcessor());
+ xlsArea.processFormulas();
}
+ transformer.deleteSheet(xlsAreas.get(0).getStartCellRef().getSheetName());
+ transformer.write();
}
- return csv.build();
}
}
diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java
index 45b81cd8c..1bb7b564a 100644
--- a/src/org/traccar/reports/Route.java
+++ b/src/org/traccar/reports/Route.java
@@ -16,16 +16,31 @@
*/
package org.traccar.reports;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
+import java.util.List;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
+import org.jxls.area.Area;
+import org.jxls.builder.xls.XlsCommentAreaBuilder;
+import org.jxls.common.CellRef;
+import org.jxls.formula.StandardFormulaProcessor;
+import org.jxls.transform.Transformer;
+import org.jxls.transform.poi.PoiTransformer;
+import org.jxls.util.TransformerFactory;
import org.traccar.Context;
+import org.traccar.model.Device;
+import org.traccar.model.Group;
import org.traccar.model.Position;
-import org.traccar.web.CsvBuilder;
+import org.traccar.reports.model.DeviceReport;
import org.traccar.web.JsonConverter;
public final class Route {
@@ -45,14 +60,47 @@ public final class Route {
return json.build().toString();
}
- public static String getCsv(long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
- Date from, Date to) throws SQLException {
- CsvBuilder csv = new CsvBuilder();
- csv.addHeaderLine(new Position());
+ public static void getExcel(OutputStream outputStream,
+ long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
+ Date from, Date to) throws SQLException, IOException {
+ ArrayList<DeviceReport> devicesRoutes = new ArrayList<>();
+ ArrayList<String> sheetNames = new ArrayList<>();
for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
- csv.addArray(Context.getDataManager().getPositions(deviceId, from, to));
+ Collection<Position> positions = Context.getDataManager().getPositions(deviceId, from, to);
+ if (positions != null && !positions.isEmpty()) {
+ DeviceReport deviceRoutes = new DeviceReport();
+ Device device = Context.getIdentityManager().getDeviceById(deviceId);
+ deviceRoutes.setDeviceName(device.getName());
+ sheetNames.add(deviceRoutes.getDeviceName());
+ if (device.getGroupId() != 0) {
+ Group group = Context.getDeviceManager().getGroupById(device.getGroupId());
+ if (group != null) {
+ deviceRoutes.setGroupName(group.getName());
+ }
+ }
+ deviceRoutes.setObjects(positions);
+ devicesRoutes.add(deviceRoutes);
+ }
+ }
+ String templatePath = Context.getConfig().getString("report.route.template.excel",
+ "templates/export/route.xlsx");
+ try (InputStream inputStream = new FileInputStream(templatePath)) {
+ org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext();
+ jxlsContext.putVar("devices", devicesRoutes);
+ jxlsContext.putVar("sheetNames", sheetNames);
+ jxlsContext.putVar("from", from);
+ jxlsContext.putVar("to", to);
+ jxlsContext.putVar("bracketsRegex", "[\\{\\}\"]");
+ Transformer transformer = TransformerFactory.createTransformer(inputStream, outputStream);
+ List<Area> xlsAreas = new XlsCommentAreaBuilder(transformer).build();
+ for (Area xlsArea : xlsAreas) {
+ xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), jxlsContext);
+ xlsArea.setFormulaProcessor(new StandardFormulaProcessor());
+ xlsArea.processFormulas();
+ }
+ transformer.deleteSheet(xlsAreas.get(0).getStartCellRef().getSheetName());
+ transformer.write();
}
- return csv.build();
}
}
diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java
index d4171f644..e7f41bcd6 100644
--- a/src/org/traccar/reports/Summary.java
+++ b/src/org/traccar/reports/Summary.java
@@ -16,17 +16,23 @@
*/
package org.traccar.reports;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
+import org.jxls.transform.poi.PoiTransformer;
+import org.jxls.util.JxlsHelper;
import org.traccar.Context;
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 {
@@ -78,14 +84,23 @@ public final class Summary {
return json.build().toString();
}
- public static String getCsv(long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
- Date from, Date to) throws SQLException {
- CsvBuilder csv = new CsvBuilder();
- csv.addHeaderLine(new SummaryReport());
+ public static void getExcel(OutputStream outputStream,
+ long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
+ Date from, Date to) throws SQLException, IOException {
+ ArrayList<SummaryReport> summaries = new ArrayList<>();
for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
- csv.addLine(calculateSummaryResult(deviceId, from, to));
+ summaries.add(calculateSummaryResult(deviceId, from, to));
+ }
+ String templatePath = Context.getConfig().getString("report.summary.template.excel",
+ "templates/export/summary.xlsx");
+ try (InputStream inputStream = new FileInputStream(templatePath)) {
+ org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext();
+ jxlsContext.putVar("summaries", summaries);
+ jxlsContext.putVar("from", from);
+ jxlsContext.putVar("to", to);
+ JxlsHelper.getInstance().setUseFastFormulaProcessor(false)
+ .processTemplate(inputStream, outputStream, jxlsContext);
}
- return csv.build();
}
}
diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java
index f0a10edbd..c912dc651 100644
--- a/src/org/traccar/reports/Trips.java
+++ b/src/org/traccar/reports/Trips.java
@@ -16,18 +16,32 @@
*/
package org.traccar.reports;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
+import java.util.List;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
+import org.jxls.area.Area;
+import org.jxls.builder.xls.XlsCommentAreaBuilder;
+import org.jxls.common.CellRef;
+import org.jxls.formula.StandardFormulaProcessor;
+import org.jxls.transform.Transformer;
+import org.jxls.transform.poi.PoiTransformer;
+import org.jxls.util.TransformerFactory;
import org.traccar.Context;
+import org.traccar.model.Device;
+import org.traccar.model.Group;
import org.traccar.model.Position;
+import org.traccar.reports.model.DeviceReport;
import org.traccar.reports.model.TripReport;
-import org.traccar.web.CsvBuilder;
import org.traccar.web.JsonConverter;
public final class Trips {
@@ -50,16 +64,24 @@ public final class Trips {
}
TripReport trip = new TripReport();
+
long tripDuration = endTrip.getFixTime().getTime() - positions.get(startIndex).getFixTime().getTime();
long deviceId = startTrip.getDeviceId();
trip.setDeviceId(deviceId);
trip.setDeviceName(Context.getIdentityManager().getDeviceById(deviceId).getName());
+
trip.setStartPositionId(startTrip.getId());
+ trip.setStartLat(startTrip.getLatitude());
+ trip.setStartLon(startTrip.getLongitude());
trip.setStartTime(startTrip.getFixTime());
trip.setStartAddress(startTrip.getAddress());
+
trip.setEndPositionId(endTrip.getId());
+ trip.setEndLat(endTrip.getLatitude());
+ trip.setEndLon(endTrip.getLongitude());
trip.setEndTime(endTrip.getFixTime());
trip.setEndAddress(endTrip.getAddress());
+
boolean ignoreOdometer = Context.getDeviceManager()
.lookupConfigBoolean(deviceId, "report.ignoreOdometer", false);
trip.setDistance(ReportUtils.calculateDistance(startTrip, endTrip, !ignoreOdometer));
@@ -157,14 +179,46 @@ public final class Trips {
return json.build().toString();
}
- public static String getCsv(long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
- Date from, Date to) throws SQLException {
- CsvBuilder csv = new CsvBuilder();
- csv.addHeaderLine(new TripReport());
+ public static void getExcel(OutputStream outputStream,
+ long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
+ Date from, Date to) throws SQLException, IOException {
+ ArrayList<DeviceReport> devicesTrips = new ArrayList<>();
+ ArrayList<String> sheetNames = new ArrayList<>();
for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
- csv.addArray(detectTrips(deviceId, from, to));
+ Collection<TripReport> trips = detectTrips(deviceId, from, to);
+ if (!trips.isEmpty()) {
+ DeviceReport deviceTrips = new DeviceReport();
+ Device device = Context.getIdentityManager().getDeviceById(deviceId);
+ deviceTrips.setDeviceName(device.getName());
+ sheetNames.add(deviceTrips.getDeviceName());
+ if (device.getGroupId() != 0) {
+ Group group = Context.getDeviceManager().getGroupById(device.getGroupId());
+ if (group != null) {
+ deviceTrips.setGroupName(group.getName());
+ }
+ }
+ deviceTrips.setObjects(detectTrips(deviceId, from, to));
+ devicesTrips.add(deviceTrips);
+ }
+ }
+ String templatePath = Context.getConfig().getString("report.trips.template.excel",
+ "templates/export/trips.xlsx");
+ try (InputStream inputStream = new FileInputStream(templatePath)) {
+ org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext();
+ jxlsContext.putVar("devices", devicesTrips);
+ jxlsContext.putVar("sheetNames", sheetNames);
+ jxlsContext.putVar("from", from);
+ jxlsContext.putVar("to", to);
+ Transformer transformer = TransformerFactory.createTransformer(inputStream, outputStream);
+ List<Area> xlsAreas = new XlsCommentAreaBuilder(transformer).build();
+ for (Area xlsArea : xlsAreas) {
+ xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), jxlsContext);
+ xlsArea.setFormulaProcessor(new StandardFormulaProcessor());
+ xlsArea.processFormulas();
+ }
+ transformer.deleteSheet(xlsAreas.get(0).getStartCellRef().getSheetName());
+ transformer.write();
}
- return csv.build();
}
}
diff --git a/src/org/traccar/reports/model/DeviceReport.java b/src/org/traccar/reports/model/DeviceReport.java
new file mode 100644
index 000000000..c072de231
--- /dev/null
+++ b/src/org/traccar/reports/model/DeviceReport.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2016 Andrey Kunitsyn (abyss@fox5.ru)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.reports.model;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class DeviceReport {
+
+ private String deviceName;
+ public String getDeviceName() {
+ return deviceName;
+ }
+ public void setDeviceName(String deviceName) {
+ this.deviceName = deviceName;
+ }
+
+ private String groupName = "";
+ public String getGroupName() {
+ return groupName;
+ }
+ public void setGroupName(String groupName) {
+ this.groupName = groupName;
+ }
+
+ private List<?> objects;
+ public Collection<?> getObjects() {
+ return objects;
+ }
+ public void setObjects(Collection<?> objects) {
+ this.objects = new ArrayList<>(objects);
+ }
+
+}
diff --git a/src/org/traccar/reports/model/TripReport.java b/src/org/traccar/reports/model/TripReport.java
index 3a77b02ca..d726195cb 100644
--- a/src/org/traccar/reports/model/TripReport.java
+++ b/src/org/traccar/reports/model/TripReport.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2016 Andrey Kunitsyn (abyss@fox5.ru)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
package org.traccar.reports.model;
import java.util.Date;
@@ -20,6 +36,38 @@ public class TripReport extends BaseReport {
this.endPositionId = endPositionId;
}
+ private double startLat;
+ public double getStartLat() {
+ return startLat;
+ }
+ public void setStartLat(double startLat) {
+ this.startLat = startLat;
+ }
+
+ private double startLon;
+ public double getStartLon() {
+ return startLon;
+ }
+ public void setStartLon(double startLon) {
+ this.startLon = startLon;
+ }
+
+ private double endLat;
+ public double getEndLat() {
+ return endLat;
+ }
+ public void setEndLat(double endLat) {
+ this.endLat = endLat;
+ }
+
+ private double endLon;
+ public double getEndLon() {
+ return endLon;
+ }
+ public void setEndLon(double endLon) {
+ this.endLon = endLon;
+ }
+
private Date startTime;
public Date getStartTime() {
if (startTime != null) {
diff --git a/templates/export/events.xlsx b/templates/export/events.xlsx
new file mode 100644
index 000000000..007265496
--- /dev/null
+++ b/templates/export/events.xlsx
Binary files differ
diff --git a/templates/export/route.xlsx b/templates/export/route.xlsx
new file mode 100644
index 000000000..c22e37268
--- /dev/null
+++ b/templates/export/route.xlsx
Binary files differ
diff --git a/templates/export/summary.xlsx b/templates/export/summary.xlsx
new file mode 100644
index 000000000..6e22343ba
--- /dev/null
+++ b/templates/export/summary.xlsx
Binary files differ
diff --git a/templates/export/trips.xlsx b/templates/export/trips.xlsx
new file mode 100644
index 000000000..136291181
--- /dev/null
+++ b/templates/export/trips.xlsx
Binary files differ