1
1
package org .traccar .api .resource ;
2
2
3
+ import java .io .ByteArrayOutputStream ;
4
+ import java .io .IOException ;
3
5
import java .sql .SQLException ;
4
6
import java .util .List ;
5
7
23
25
@ Consumes (MediaType .APPLICATION_JSON )
24
26
public class ReportResource extends BaseResource {
25
27
26
- public static final String TEXT_CSV = "text/csv " ;
27
- public static final String CONTENT_DISPOSITION_VALUE = "attachment; filename=report.csv " ;
28
+ private static final String XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet " ;
29
+ private static final String CONTENT_DISPOSITION_VALUE_XLSX = "attachment; filename=report.xlsx " ;
28
30
29
31
@ Path ("route" )
30
32
@ GET
@@ -38,14 +40,16 @@ public Response getRouteJson(
38
40
39
41
@ Path ("route" )
40
42
@ GET
41
- @ Produces (TEXT_CSV )
42
- public Response getRouteCsv (
43
+ @ Produces (XLSX )
44
+ public Response getRouteExcel (
43
45
@ QueryParam ("deviceId" ) final List <Long > deviceIds , @ QueryParam ("groupId" ) final List <Long > groupIds ,
44
- @ QueryParam ("from" ) String from , @ QueryParam ("to" ) String to ) throws SQLException {
45
- return Response .ok (Route .getCsv (getUserId (), deviceIds , groupIds ,
46
- JsonConverter .parseDate (from ), JsonConverter .parseDate (to )))
47
- .header (HttpHeaders .CONTENT_DISPOSITION , CONTENT_DISPOSITION_VALUE )
48
- .build ();
46
+ @ QueryParam ("from" ) String from , @ QueryParam ("to" ) String to ) throws SQLException , IOException {
47
+ ByteArrayOutputStream stream = new ByteArrayOutputStream ();
48
+ Route .getExcel (stream , getUserId (), deviceIds , groupIds ,
49
+ JsonConverter .parseDate (from ), JsonConverter .parseDate (to ));
50
+
51
+ return Response .ok (stream .toByteArray ())
52
+ .header (HttpHeaders .CONTENT_DISPOSITION , CONTENT_DISPOSITION_VALUE_XLSX ).build ();
49
53
}
50
54
51
55
@ Path ("events" )
@@ -61,15 +65,17 @@ public Response getEventsJson(
61
65
62
66
@ Path ("events" )
63
67
@ GET
64
- @ Produces (TEXT_CSV )
65
- public Response getEventsCsv (
68
+ @ Produces (XLSX )
69
+ public Response getEventsExcel (
66
70
@ QueryParam ("deviceId" ) final List <Long > deviceIds , @ QueryParam ("groupId" ) final List <Long > groupIds ,
67
71
@ QueryParam ("type" ) final List <String > types ,
68
- @ QueryParam ("from" ) String from , @ QueryParam ("to" ) String to ) throws SQLException {
69
- return Response .ok (Events .getCsv (getUserId (), deviceIds , groupIds ,
70
- types , JsonConverter .parseDate (from ), JsonConverter .parseDate (to )))
71
- .header (HttpHeaders .CONTENT_DISPOSITION , CONTENT_DISPOSITION_VALUE )
72
- .build ();
72
+ @ QueryParam ("from" ) String from , @ QueryParam ("to" ) String to ) throws SQLException , IOException {
73
+ ByteArrayOutputStream stream = new ByteArrayOutputStream ();
74
+ Events .getExcel (stream , getUserId (), deviceIds , groupIds , types ,
75
+ JsonConverter .parseDate (from ), JsonConverter .parseDate (to ));
76
+
77
+ return Response .ok (stream .toByteArray ())
78
+ .header (HttpHeaders .CONTENT_DISPOSITION , CONTENT_DISPOSITION_VALUE_XLSX ).build ();
73
79
}
74
80
75
81
@ Path ("summary" )
@@ -84,14 +90,16 @@ public Response getSummaryJson(
84
90
85
91
@ Path ("summary" )
86
92
@ GET
87
- @ Produces (TEXT_CSV )
88
- public Response getSummaryCsv (
93
+ @ Produces (XLSX )
94
+ public Response getSummaryExcel (
89
95
@ QueryParam ("deviceId" ) final List <Long > deviceIds , @ QueryParam ("groupId" ) final List <Long > groupIds ,
90
- @ QueryParam ("from" ) String from , @ QueryParam ("to" ) String to ) throws SQLException {
91
- return Response .ok (Summary .getCsv (getUserId (), deviceIds , groupIds ,
92
- JsonConverter .parseDate (from ), JsonConverter .parseDate (to )))
93
- .header (HttpHeaders .CONTENT_DISPOSITION , CONTENT_DISPOSITION_VALUE )
94
- .build ();
96
+ @ QueryParam ("from" ) String from , @ QueryParam ("to" ) String to ) throws SQLException , IOException {
97
+ ByteArrayOutputStream stream = new ByteArrayOutputStream ();
98
+ Summary .getExcel (stream , getUserId (), deviceIds , groupIds ,
99
+ JsonConverter .parseDate (from ), JsonConverter .parseDate (to ));
100
+
101
+ return Response .ok (stream .toByteArray ())
102
+ .header (HttpHeaders .CONTENT_DISPOSITION , CONTENT_DISPOSITION_VALUE_XLSX ).build ();
95
103
}
96
104
97
105
@ Path ("trips" )
@@ -106,14 +114,16 @@ public Response getTripsJson(
106
114
107
115
@ Path ("trips" )
108
116
@ GET
109
- @ Produces (TEXT_CSV )
110
- public Response getTripsCsv (
117
+ @ Produces (XLSX )
118
+ public Response getTripsExcel (
111
119
@ QueryParam ("deviceId" ) final List <Long > deviceIds , @ QueryParam ("groupId" ) final List <Long > groupIds ,
112
- @ QueryParam ("from" ) String from , @ QueryParam ("to" ) String to ) throws SQLException {
113
- return Response .ok (Trips .getCsv (getUserId (), deviceIds , groupIds ,
114
- JsonConverter .parseDate (from ), JsonConverter .parseDate (to )))
115
- .header (HttpHeaders .CONTENT_DISPOSITION , CONTENT_DISPOSITION_VALUE )
116
- .build ();
120
+ @ QueryParam ("from" ) String from , @ QueryParam ("to" ) String to ) throws SQLException , IOException {
121
+ ByteArrayOutputStream stream = new ByteArrayOutputStream ();
122
+ Trips .getExcel (stream , getUserId (), deviceIds , groupIds ,
123
+ JsonConverter .parseDate (from ), JsonConverter .parseDate (to ));
124
+
125
+ return Response .ok (stream .toByteArray ())
126
+ .header (HttpHeaders .CONTENT_DISPOSITION , CONTENT_DISPOSITION_VALUE_XLSX ).build ();
117
127
}
118
128
119
129
}
0 commit comments