aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2017-03-16 19:01:05 +1300
committerGitHub <noreply@github.com>2017-03-16 19:01:05 +1300
commit308d00b7f72b7455c58a98013fff06e227360097 (patch)
tree55367dae3f7e85c65432dfbf91adc4496df16254 /src
parent2f782faaf27ffdae3753e06a6386c1cd49b2ed16 (diff)
parentf7a668371769a7b4c176e57626f23314e3838e1c (diff)
downloadtrackermap-server-308d00b7f72b7455c58a98013fff06e227360097.tar.gz
trackermap-server-308d00b7f72b7455c58a98013fff06e227360097.tar.bz2
trackermap-server-308d00b7f72b7455c58a98013fff06e227360097.zip
Merge pull request #3011 from Abyss777/reports_timezone
Use timezone from preferences for excel reports
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/api/resource/ReportResource.java8
-rw-r--r--src/org/traccar/helper/DateUtil.java6
-rw-r--r--src/org/traccar/reports/Events.java29
-rw-r--r--src/org/traccar/reports/ReportUtils.java38
-rw-r--r--src/org/traccar/reports/Route.java29
-rw-r--r--src/org/traccar/reports/Summary.java11
-rw-r--r--src/org/traccar/reports/Trips.java28
7 files changed, 57 insertions, 92 deletions
diff --git a/src/org/traccar/api/resource/ReportResource.java b/src/org/traccar/api/resource/ReportResource.java
index a1b35d64e..a0f686e80 100644
--- a/src/org/traccar/api/resource/ReportResource.java
+++ b/src/org/traccar/api/resource/ReportResource.java
@@ -51,7 +51,7 @@ public class ReportResource extends BaseResource {
@QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Route.getExcel(stream, getUserId(), deviceIds, groupIds,
- DateUtil.parseDateTime(from), DateUtil.parseDateTime(to));
+ DateUtil.parseDate(from), DateUtil.parseDate(to));
return Response.ok(stream.toByteArray())
.header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build();
@@ -76,7 +76,7 @@ public class ReportResource extends BaseResource {
@QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Events.getExcel(stream, getUserId(), deviceIds, groupIds, types,
- DateUtil.parseDateTime(from), DateUtil.parseDateTime(to));
+ DateUtil.parseDate(from), DateUtil.parseDate(to));
return Response.ok(stream.toByteArray())
.header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build();
@@ -99,7 +99,7 @@ public class ReportResource extends BaseResource {
@QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Summary.getExcel(stream, getUserId(), deviceIds, groupIds,
- DateUtil.parseDateTime(from), DateUtil.parseDateTime(to));
+ DateUtil.parseDate(from), DateUtil.parseDate(to));
return Response.ok(stream.toByteArray())
.header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build();
@@ -123,7 +123,7 @@ public class ReportResource extends BaseResource {
@QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Trips.getExcel(stream, getUserId(), deviceIds, groupIds,
- DateUtil.parseDateTime(from), DateUtil.parseDateTime(to));
+ DateUtil.parseDate(from), DateUtil.parseDate(to));
return Response.ok(stream.toByteArray())
.header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build();
diff --git a/src/org/traccar/helper/DateUtil.java b/src/org/traccar/helper/DateUtil.java
index de36d4420..30bb1b2fa 100644
--- a/src/org/traccar/helper/DateUtil.java
+++ b/src/org/traccar/helper/DateUtil.java
@@ -18,7 +18,6 @@ package org.traccar.helper;
import java.util.Calendar;
import java.util.Date;
-import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
@@ -64,9 +63,4 @@ public final class DateUtil {
public static Date parseDate(String value) {
return DATE_FORMAT.parseDateTime(value).toDate();
}
-
- public static DateTime parseDateTime(String value) {
- return DATE_FORMAT.withOffsetParsed().parseDateTime(value);
- }
-
}
diff --git a/src/org/traccar/reports/Events.java b/src/org/traccar/reports/Events.java
index d2255684f..0706f1382 100644
--- a/src/org/traccar/reports/Events.java
+++ b/src/org/traccar/reports/Events.java
@@ -26,17 +26,8 @@ import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
-import java.util.List;
import org.apache.poi.ss.util.WorkbookUtil;
-import org.joda.time.DateTime;
-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;
@@ -70,13 +61,13 @@ public final class Events {
public static void getExcel(OutputStream outputStream,
long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
- Collection<String> types, DateTime from, DateTime to) throws SQLException, IOException {
+ 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);
- Collection<Event> events = Context.getDataManager().getEvents(deviceId, from.toDate(), to.toDate());
+ 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();
@@ -112,25 +103,13 @@ public final class Events {
String templatePath = Context.getConfig().getString("report.templatesPath",
"templates/export/");
try (InputStream inputStream = new FileInputStream(templatePath + "/events.xlsx")) {
- org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext();
+ org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId);
jxlsContext.putVar("devices", devicesEvents);
jxlsContext.putVar("sheetNames", sheetNames);
jxlsContext.putVar("geofenceNames", geofenceNames);
jxlsContext.putVar("from", from);
jxlsContext.putVar("to", to);
- jxlsContext.putVar("distanceUnit", ReportUtils.getDistanceUnit(userId));
- jxlsContext.putVar("speedUnit", ReportUtils.getSpeedUnit(userId));
- jxlsContext.putVar("timezone", from.getZone());
- 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();
+ ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
}
}
}
diff --git a/src/org/traccar/reports/ReportUtils.java b/src/org/traccar/reports/ReportUtils.java
index ed97c6857..e2a743b2f 100644
--- a/src/org/traccar/reports/ReportUtils.java
+++ b/src/org/traccar/reports/ReportUtils.java
@@ -16,14 +16,27 @@
*/
package org.traccar.reports;
+import org.apache.velocity.tools.generic.DateTool;
+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.helper.Log;
import org.traccar.model.Position;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
import java.util.TimeZone;
public final class ReportUtils {
@@ -96,4 +109,29 @@ public final class ReportUtils {
return "-";
}
+ public static org.jxls.common.Context initializeContext(long userId) {
+ org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext();
+ jxlsContext.putVar("distanceUnit", getDistanceUnit(userId));
+ jxlsContext.putVar("speedUnit", getSpeedUnit(userId));
+ jxlsContext.putVar("webUrl", Context.getVelocityEngine().getProperty("web.url"));
+ jxlsContext.putVar("dateTool", new DateTool());
+ jxlsContext.putVar("timezone", getTimezone(userId));
+ jxlsContext.putVar("locale", Locale.getDefault());
+ jxlsContext.putVar("bracketsRegex", "[\\{\\}\"]");
+ return jxlsContext;
+ }
+
+ public static void processTemplateWithSheets(InputStream templateStream, OutputStream targetStream,
+ org.jxls.common.Context jxlsContext) throws IOException {
+ Transformer transformer = TransformerFactory.createTransformer(templateStream, targetStream);
+ 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();
+ }
+
}
diff --git a/src/org/traccar/reports/Route.java b/src/org/traccar/reports/Route.java
index c1acaf322..aa6b7105b 100644
--- a/src/org/traccar/reports/Route.java
+++ b/src/org/traccar/reports/Route.java
@@ -24,17 +24,8 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
-import java.util.List;
import org.apache.poi.ss.util.WorkbookUtil;
-import org.joda.time.DateTime;
-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;
@@ -58,13 +49,13 @@ public final class Route {
public static void getExcel(OutputStream outputStream,
long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
- DateTime from, DateTime to) throws SQLException, IOException {
+ 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);
Collection<Position> positions = Context.getDataManager()
- .getPositions(deviceId, from.toDate(), to.toDate());
+ .getPositions(deviceId, from, to);
DeviceReport deviceRoutes = new DeviceReport();
Device device = Context.getIdentityManager().getDeviceById(deviceId);
deviceRoutes.setDeviceName(device.getName());
@@ -81,24 +72,12 @@ public final class Route {
String templatePath = Context.getConfig().getString("report.templatesPath",
"templates/export/");
try (InputStream inputStream = new FileInputStream(templatePath + "/route.xlsx")) {
- org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext();
+ org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId);
jxlsContext.putVar("devices", devicesRoutes);
jxlsContext.putVar("sheetNames", sheetNames);
jxlsContext.putVar("from", from);
jxlsContext.putVar("to", to);
- jxlsContext.putVar("distanceUnit", ReportUtils.getDistanceUnit(userId));
- jxlsContext.putVar("speedUnit", ReportUtils.getSpeedUnit(userId));
- jxlsContext.putVar("timezone", from.getZone());
- 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();
+ ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
}
}
}
diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java
index cacb4d1d6..abe0277da 100644
--- a/src/org/traccar/reports/Summary.java
+++ b/src/org/traccar/reports/Summary.java
@@ -25,8 +25,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
-import org.joda.time.DateTime;
-import org.jxls.transform.poi.PoiTransformer;
import org.jxls.util.JxlsHelper;
import org.traccar.Context;
import org.traccar.model.Position;
@@ -79,18 +77,15 @@ public final class Summary {
public static void getExcel(OutputStream outputStream,
long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
- DateTime from, DateTime to) throws SQLException, IOException {
- Collection<SummaryReport> summaries = getObjects(userId, deviceIds, groupIds, from.toDate(), to.toDate());
+ Date from, Date to) throws SQLException, IOException {
+ Collection<SummaryReport> summaries = getObjects(userId, deviceIds, groupIds, 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();
+ org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId);
jxlsContext.putVar("summaries", summaries);
jxlsContext.putVar("from", from);
jxlsContext.putVar("to", to);
- jxlsContext.putVar("distanceUnit", ReportUtils.getDistanceUnit(userId));
- jxlsContext.putVar("speedUnit", ReportUtils.getSpeedUnit(userId));
- jxlsContext.putVar("timezone", from.getZone());
JxlsHelper.getInstance().setUseFastFormulaProcessor(false)
.processTemplate(inputStream, outputStream, jxlsContext);
}
diff --git a/src/org/traccar/reports/Trips.java b/src/org/traccar/reports/Trips.java
index 45b52b63c..eeb2f2681 100644
--- a/src/org/traccar/reports/Trips.java
+++ b/src/org/traccar/reports/Trips.java
@@ -24,17 +24,8 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
-import java.util.List;
import org.apache.poi.ss.util.WorkbookUtil;
-import org.joda.time.DateTime;
-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;
@@ -193,12 +184,12 @@ public final class Trips {
public static void getExcel(OutputStream outputStream,
long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
- DateTime from, DateTime to) throws SQLException, IOException {
+ 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);
- Collection<TripReport> trips = detectTrips(deviceId, from.toDate(), to.toDate());
+ Collection<TripReport> trips = detectTrips(deviceId, from, to);
DeviceReport deviceTrips = new DeviceReport();
Device device = Context.getIdentityManager().getDeviceById(deviceId);
deviceTrips.setDeviceName(device.getName());
@@ -215,23 +206,12 @@ public final class Trips {
String templatePath = Context.getConfig().getString("report.templatesPath",
"templates/export/");
try (InputStream inputStream = new FileInputStream(templatePath + "/trips.xlsx")) {
- org.jxls.common.Context jxlsContext = PoiTransformer.createInitialContext();
+ org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId);
jxlsContext.putVar("devices", devicesTrips);
jxlsContext.putVar("sheetNames", sheetNames);
jxlsContext.putVar("from", from);
jxlsContext.putVar("to", to);
- jxlsContext.putVar("distanceUnit", ReportUtils.getDistanceUnit(userId));
- jxlsContext.putVar("speedUnit", ReportUtils.getSpeedUnit(userId));
- jxlsContext.putVar("timezone", from.getZone());
- 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();
+ ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
}
}