diff options
author | Renaud Allard <renaud@allard.it> | 2016-10-20 08:31:57 +0200 |
---|---|---|
committer | Renaud Allard <renaud@allard.it> | 2016-10-20 08:31:57 +0200 |
commit | 1104c2738e19579c5865db5c030388e1e320f0b4 (patch) | |
tree | a6571455b6f5d88581c8034bd1059968d506ed01 /src/org/traccar/reports | |
parent | 0d347cd7aac2ebfad01c1635f3893964be2e67ae (diff) | |
parent | 4f7356cc1c6962f46ca522246d5b63dc06a2d268 (diff) | |
download | trackermap-server-1104c2738e19579c5865db5c030388e1e320f0b4.tar.gz trackermap-server-1104c2738e19579c5865db5c030388e1e320f0b4.tar.bz2 trackermap-server-1104c2738e19579c5865db5c030388e1e320f0b4.zip |
Merge branch 'master' of https://github.com/tananaev/traccar
Diffstat (limited to 'src/org/traccar/reports')
-rw-r--r-- | src/org/traccar/reports/Events.java | 95 | ||||
-rw-r--r-- | src/org/traccar/reports/Route.java | 60 | ||||
-rw-r--r-- | src/org/traccar/reports/Summary.java | 29 | ||||
-rw-r--r-- | src/org/traccar/reports/Trips.java | 66 | ||||
-rw-r--r-- | src/org/traccar/reports/model/DeviceReport.java | 49 | ||||
-rw-r--r-- | src/org/traccar/reports/model/TripReport.java | 48 |
6 files changed, 315 insertions, 32 deletions
diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java index ed7e9e411..77d995f56 100644 --- a/src/org/traccar/reports/Events.java +++ b/src/org/traccar/reports/Events.java @@ -16,16 +16,34 @@ */ 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.HashMap; +import java.util.Iterator; +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.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 { @@ -38,25 +56,80 @@ public final class Events { JsonArrayBuilder json = Json.createArrayBuilder(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - for (String type : types) { - for (Event event : Context.getDataManager().getEvents(deviceId, type, from, to)) { - json.add(JsonConverter.objectToJson(event)); + Collection<Event> events = Context.getDataManager().getEvents(deviceId, from, to); + boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS); + for (Event event : events) { + if (all || types.contains(event.getType())) { + long geofenceId = event.getGeofenceId(); + if (geofenceId == 0 || Context.getGeofenceManager().checkGeofence(userId, geofenceId)) { + json.add(JsonConverter.objectToJson(event)); + } } } } 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); - for (String type : types) { - csv.addArray(Context.getDataManager().getEvents(deviceId, type, from, to)); + Collection<Event> events = Context.getDataManager().getEvents(deviceId, from, to); + boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS); + for (Iterator<Event> iterator = events.iterator(); iterator.hasNext();) { + Event event = iterator.next(); + if (all || types.contains(event.getType())) { + long geofenceId = event.getGeofenceId(); + if (geofenceId != 0) { + if (Context.getGeofenceManager().checkGeofence(userId, geofenceId)) { + Geofence geofence = Context.getGeofenceManager().getGeofence(geofenceId); + if (geofence != null) { + geofenceNames.put(geofenceId, geofence.getName()); + } + } else { + iterator.remove(); + } + } + } else { + iterator.remove(); + } + } + 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(events); + devicesEvents.add(deviceEvents); + } + String templatePath = Context.getConfig().getString("report.templatesPath", + "templates/export/"); + try (InputStream inputStream = new FileInputStream(templatePath + "/events.xlsx")) { + 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..6992c89cc 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,45 @@ 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); + 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.templatesPath", + "templates/export/"); + try (InputStream inputStream = new FileInputStream(templatePath + "/route.xlsx")) { + 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..14f8b7839 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.templatesPath", + "templates/export/"); + try (InputStream inputStream = new FileInputStream(templatePath + "/summary.xlsx")) { + 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..705644849 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,44 @@ 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); + 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(trips); + devicesTrips.add(deviceTrips); + } + String templatePath = Context.getConfig().getString("report.templatesPath", + "templates/export/"); + try (InputStream inputStream = new FileInputStream(templatePath + "/trips.xlsx")) { + 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) { |