diff options
Diffstat (limited to 'src/org/traccar/reports/Summary.java')
-rw-r--r-- | src/org/traccar/reports/Summary.java | 61 |
1 files changed, 34 insertions, 27 deletions
diff --git a/src/org/traccar/reports/Summary.java b/src/org/traccar/reports/Summary.java index d4171f644..cacb4d1d6 100644 --- a/src/org/traccar/reports/Summary.java +++ b/src/org/traccar/reports/Summary.java @@ -1,6 +1,6 @@ /* - * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) - * Copyright 2016 Andrey Kunitsyn (abyss@fox5.ru) + * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,18 +16,21 @@ */ 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.joda.time.DateTime; +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 { @@ -47,12 +50,8 @@ public final class Summary { if (firstPosition == null) { firstPosition = position; } - if (previousPosition != null - && position.getAttributes().get(Position.KEY_IGNITION) != null - && Boolean.parseBoolean(position.getAttributes().get(Position.KEY_IGNITION).toString()) - && previousPosition.getAttributes().get(Position.KEY_IGNITION) != null - && Boolean.parseBoolean(previousPosition.getAttributes() - .get(Position.KEY_IGNITION).toString())) { + if (previousPosition != null && position.getBoolean(Position.KEY_IGNITION) + && previousPosition.getBoolean(Position.KEY_IGNITION)) { result.addEngineHours(position.getFixTime().getTime() - previousPosition.getFixTime().getTime()); } @@ -61,31 +60,39 @@ public final class Summary { result.setMaxSpeed(position.getSpeed()); } boolean ignoreOdometer = Context.getDeviceManager() - .lookupConfigBoolean(deviceId, "report.ignoreOdometer", false); + .lookupAttributeBoolean(deviceId, "report.ignoreOdometer", false, true); result.setDistance(ReportUtils.calculateDistance(firstPosition, previousPosition, !ignoreOdometer)); result.setAverageSpeed(speedSum / positions.size()); } return result; } - public static String getJson(long userId, Collection<Long> deviceIds, Collection<Long> groupIds, - Date from, Date to) throws SQLException { - JsonArrayBuilder json = Json.createArrayBuilder(); + public static Collection<SummaryReport> getObjects(long userId, Collection<Long> deviceIds, + Collection<Long> groupIds, Date from, Date to) throws SQLException { + ArrayList<SummaryReport> result = new ArrayList<>(); for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { Context.getPermissionsManager().checkDevice(userId, deviceId); - json.add(JsonConverter.objectToJson(calculateSummaryResult(deviceId, from, to))); + result.add(calculateSummaryResult(deviceId, from, to)); } - return json.build().toString(); + return result; } - 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()); - for (long deviceId: ReportUtils.getDeviceList(deviceIds, groupIds)) { - Context.getPermissionsManager().checkDevice(userId, deviceId); - csv.addLine(calculateSummaryResult(deviceId, from, to)); + 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()); + 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); + 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); } - return csv.build(); } } |