forked from traccar/traccar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
87 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,19 @@ | ||
/* | ||
* Copyright 2016 - 2018 Anton Tananaev ([email protected]) | ||
* Copyright 2016 - 2018 Andrey Kunitsyn ([email protected]) | ||
* | ||
* 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.api.resource; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
|
@@ -6,6 +22,10 @@ | |
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import javax.activation.DataHandler; | ||
import javax.mail.MessagingException; | ||
import javax.mail.internet.MimeBodyPart; | ||
import javax.mail.util.ByteArrayDataSource; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
@@ -15,6 +35,9 @@ | |
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.traccar.Context; | ||
import org.traccar.api.BaseResource; | ||
import org.traccar.helper.DateUtil; | ||
import org.traccar.model.Event; | ||
|
@@ -33,9 +56,43 @@ | |
@Consumes(MediaType.APPLICATION_JSON) | ||
public class ReportResource extends BaseResource { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ReportResource.class); | ||
|
||
private static final String XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; | ||
private static final String CONTENT_DISPOSITION_VALUE_XLSX = "attachment; filename=report.xlsx"; | ||
|
||
private interface ReportExecutor { | ||
void execute(ByteArrayOutputStream stream) throws SQLException, IOException; | ||
} | ||
|
||
private Response executeReport( | ||
long userId, boolean mail, ReportExecutor executor) throws SQLException, IOException { | ||
final ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
if (mail) { | ||
new Thread(() -> { | ||
try { | ||
executor.execute(stream); | ||
|
||
MimeBodyPart attachment = new MimeBodyPart(); | ||
|
||
attachment.setFileName("report.xlsx"); | ||
attachment.setDataHandler(new DataHandler(new ByteArrayDataSource( | ||
stream.toByteArray(), "application/octet-stream"))); | ||
|
||
Context.getMailManager().sendMessage( | ||
userId, "Report", "The report is in the attachment.", attachment); | ||
} catch (SQLException | IOException | MessagingException e) { | ||
LOGGER.warn("Report failed", e); | ||
} | ||
}).start(); | ||
return Response.noContent().build(); | ||
} else { | ||
executor.execute(stream); | ||
return Response.ok(stream.toByteArray()) | ||
.header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); | ||
} | ||
} | ||
|
||
@Path("route") | ||
@GET | ||
public Collection<Position> getRoute( | ||
|
@@ -50,13 +107,12 @@ public Collection<Position> getRoute( | |
@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, IOException { | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
Route.getExcel(stream, getUserId(), deviceIds, groupIds, | ||
DateUtil.parseDate(from), DateUtil.parseDate(to)); | ||
|
||
return Response.ok(stream.toByteArray()) | ||
.header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); | ||
@QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail) | ||
throws SQLException, IOException { | ||
return executeReport(getUserId(), mail, stream -> { | ||
Route.getExcel(stream, getUserId(), deviceIds, groupIds, | ||
DateUtil.parseDate(from), DateUtil.parseDate(to)); | ||
}); | ||
} | ||
|
||
@Path("events") | ||
|
@@ -75,13 +131,12 @@ public Collection<Event> getEvents( | |
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, IOException { | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
Events.getExcel(stream, getUserId(), deviceIds, groupIds, types, | ||
DateUtil.parseDate(from), DateUtil.parseDate(to)); | ||
|
||
return Response.ok(stream.toByteArray()) | ||
.header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); | ||
@QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail) | ||
throws SQLException, IOException { | ||
return executeReport(getUserId(), mail, stream -> { | ||
Events.getExcel(stream, getUserId(), deviceIds, groupIds, types, | ||
DateUtil.parseDate(from), DateUtil.parseDate(to)); | ||
}); | ||
} | ||
|
||
@Path("summary") | ||
|
@@ -98,13 +153,12 @@ public Collection<SummaryReport> getSummary( | |
@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, IOException { | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
Summary.getExcel(stream, getUserId(), deviceIds, groupIds, | ||
DateUtil.parseDate(from), DateUtil.parseDate(to)); | ||
|
||
return Response.ok(stream.toByteArray()) | ||
.header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); | ||
@QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail) | ||
throws SQLException, IOException { | ||
return executeReport(getUserId(), mail, stream -> { | ||
Summary.getExcel(stream, getUserId(), deviceIds, groupIds, | ||
DateUtil.parseDate(from), DateUtil.parseDate(to)); | ||
}); | ||
} | ||
|
||
@Path("trips") | ||
|
@@ -122,13 +176,12 @@ public Collection<TripReport> getTrips( | |
@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, IOException { | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
Trips.getExcel(stream, getUserId(), deviceIds, groupIds, | ||
DateUtil.parseDate(from), DateUtil.parseDate(to)); | ||
|
||
return Response.ok(stream.toByteArray()) | ||
.header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); | ||
@QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail) | ||
throws SQLException, IOException { | ||
return executeReport(getUserId(), mail, stream -> { | ||
Trips.getExcel(stream, getUserId(), deviceIds, groupIds, | ||
DateUtil.parseDate(from), DateUtil.parseDate(to)); | ||
}); | ||
} | ||
|
||
@Path("stops") | ||
|
@@ -146,14 +199,12 @@ public Collection<StopReport> getStops( | |
@Produces(XLSX) | ||
public Response getStopsExcel( | ||
@QueryParam("deviceId") final List<Long> deviceIds, @QueryParam("groupId") final List<Long> groupIds, | ||
@QueryParam("from") String from, @QueryParam("to") String to) throws SQLException, IOException { | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
Stops.getExcel(stream, getUserId(), deviceIds, groupIds, | ||
DateUtil.parseDate(from), DateUtil.parseDate(to)); | ||
|
||
return Response.ok(stream.toByteArray()) | ||
.header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_XLSX).build(); | ||
@QueryParam("from") String from, @QueryParam("to") String to, @QueryParam("mail") boolean mail) | ||
throws SQLException, IOException { | ||
return executeReport(getUserId(), mail, stream -> { | ||
Stops.getExcel(stream, getUserId(), deviceIds, groupIds, | ||
DateUtil.parseDate(from), DateUtil.parseDate(to)); | ||
}); | ||
} | ||
|
||
|
||
} |