I'm trying to generate the event report in just one tab, in fact, putting an option to generate it in separate tabs or in just one tab, in just one tab/spreadsheet, makes it easier to create a filter for analyzing multiple devices. I made some changes and it is working in parts, it is not generating the report with all the times and dates. Would anyone like to help with this? Thanks!
EventsReportProvider.java
public void getExcel(
OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds,
Collection<String> types, Date from, Date to, boolean singleSheet) throws StorageException, IOException {
reportUtils.checkPeriodLimit(from, to);
ArrayList<DeviceReportSection> devicesEvents = new ArrayList<>();
ArrayList<String> sheetNames = new ArrayList<>();
HashMap<Long, String> geofenceNames = new HashMap<>();
HashMap<Long, String> maintenanceNames = new HashMap<>();
HashMap<Long, Position> positions = new HashMap<>();
boolean allEvents = types.isEmpty() || types.contains(Event.ALL_EVENTS);
for (Device device : DeviceUtil.getAccessibleDevices(storage, userId, deviceIds, groupIds)) {
Collection<Event> events = getEvents(device.getId(), from, to);
events.removeIf(event ->
{
try {
return !(allEvents || types.contains(event.getType())) ||
(!checkGeofenceAndMaintenance(userId, event, geofenceNames, maintenanceNames));
} catch (StorageException e) {
throw new RuntimeException(e);
}
}
);
// Populate positions for events with a position
for (Event event : events) {
long positionId = event.getPositionId();
if (positionId > 0) {
Position position = storage.getObject(Position.class, new Request(
new Columns.All(), new Condition.Equals("id", positionId)));
positions.put(positionId, position);
}
}
DeviceReportSection deviceEvents = new DeviceReportSection();
deviceEvents.setDeviceName(device.getName());
if (singleSheet) {
sheetNames.add("All Devices");
} else if (!singleSheet) {
sheetNames.add(WorkbookUtil.createSafeSheetName(device.getName()));
}
if (device.getGroupId() > 0) {
Group group = storage.getObject(Group.class, new Request(
new Columns.All(), new Condition.Equals("id", device.getGroupId())));
if (group != null) {
deviceEvents.setGroupName(group.getName());
}
}
deviceEvents.setObjects(events);
devicesEvents.add(deviceEvents);
// if (singleSheet) {
// break; // Only process first device for single sheet mode
// }
}
File file = Paths.get(config.getString(Keys.TEMPLATES_ROOT), "export", "events.xlsx").toFile();
try (InputStream inputStream = new FileInputStream(file)) {
var context = reportUtils.initializeContext(userId);
context.putVar("devices name", devicesEvents.stream().map(DeviceReportSection::getDeviceName));
context.putVar("devices", devicesEvents);
context.putVar("sheetNames", sheetNames);
context.putVar("geofenceNames", geofenceNames);
context.putVar("maintenanceNames", maintenanceNames);
context.putVar("positions", positions);
context.putVar("from", from);
context.putVar("to", to);
context.putVar("singleSheet", singleSheet);
reportUtils.processTemplateWithSheets(inputStream, outputStream, context);
}
}
// Helper method for checking geofence and maintenance
private boolean checkGeofenceAndMaintenance(
long userId,
Event event,
HashMap<Long, String> geofenceNames,
HashMap<Long, String> maintenanceNames) throws StorageException {
long geofenceId = event.getGeofenceId();
long maintenanceId = event.getMaintenanceId();
if (geofenceId != 0) {
Geofence geofence = reportUtils.getObject(userId, Geofence.class, geofenceId);
if (geofence != null) {
geofenceNames.put(geofenceId, geofence.getName());
return true;
}
return false;
} else if (maintenanceId != 0) {
Maintenance maintenance = reportUtils.getObject(userId, Maintenance.class, maintenanceId);
if (maintenance != null) {
maintenanceNames.put(maintenanceId, maintenance.getName());
return true;
}
return false;
}
return true;
}
ReportResource.java
@Path("events")
@GET
@Produces(EXCEL)
public Response getEventsExcel(
@QueryParam("deviceId") List<Long> deviceIds,
@QueryParam("groupId") List<Long> groupIds,
@QueryParam("type") List<String> types,
@QueryParam("from") Date from,
@QueryParam("to") Date to,
@QueryParam("mail") boolean mail) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
return executeReport(getUserId(), mail, stream -> {
LogAction.logReport(getUserId(), false, "events", from, to, deviceIds, groupIds);
eventsReportProvider.getExcel(stream, getUserId(), deviceIds, groupIds, types, from, to, true);
});
}
TaskReport.java
case "events":
var eventsReportProvider = injector.getInstance(EventsReportProvider.class);
reportMailer.sendAsync(user.getId(), stream -> eventsReportProvider.getExcel(
stream, user.getId(), deviceIds, groupIds, List.of(), from, to, true));
break;
I'm trying to generate the event report in just one tab, in fact, putting an option to generate it in separate tabs or in just one tab, in just one tab/spreadsheet, makes it easier to create a filter for analyzing multiple devices. I made some changes and it is working in parts, it is not generating the report with all the times and dates. Would anyone like to help with this? Thanks!
EventsReportProvider.java
ReportResource.java
TaskReport.java