Importing and Exporting Reports
The Jmix Reports add-on provides features to handle the export and import of report definitions directly from the UI and programmatically.
Manually
The Reports add-on allows you to manually import and export reports directly from the Reports view by clicking the Import and Export buttons. See Report List View for more information.
Using ReportImportExport
The Jmix Reports add-on provides the ReportImportExport
API which allows you to import ZIP files with exported reports.
Consider the example of automated report deployment within a Jmix project. The reports have been manually exported and saved as ZIP archives to the project’s resources directory.
@Component
public class DemoDataInitializer { (1)
@Autowired
private DataManager dataManager;
@Autowired
private Resources resources;
@Autowired
protected ReportImportExport reportImportExport;
private final static String REPORT_LOCATION = "com/company/library/reports/";
@EventListener
@Authenticated
public void onApplicationStarted(ApplicationStartedEvent event) { (2)
importReport();
}
private void importReport(){
InitFlags initFlags = dataManager.load(InitFlags.class) (3)
.id(1)
.lockMode(LockModeType.PESSIMISTIC_WRITE)
.optional()
.orElseGet(() -> {
InitFlags entity = dataManager.create(InitFlags.class);
entity.setId(1);
return entity;
});
if (!Boolean.TRUE.equals(initFlags.getReportsInitialized())) {
importReport("Book Items location.zip");
importReport("Book Record.zip");
importReport("Publication details.zip");
importReport("Publications grouped by types and books.zip");
importReport("Recently added book items.zip");
initFlags.setReportsInitialized(true);
dataManager.save(initFlags);
}
}
private void importReport(String reportFileName) {
String location = REPORT_LOCATION + reportFileName;
log.info("Initializing report from " + location);
try (InputStream stream = resources.getResourceAsStream(location)) {
if (stream != null) {
reportImportExport.importReports(IOUtils.toByteArray(stream)); (4)
} else {
log.info("Not found: " + location);
}
} catch (IOException e) {
log.error("Unable to initialize reports", e);
}
}
}
1 | The DemoDataInitializer bean is responsible for triggering the report import on application startup. |
2 | This method is automatically invoked when the ApplicationStartedEvent is published. We’ll put our initialization logic in here. ApplicationStartedEvent is a Spring event that is published after the entire application has been initialized and is ready to process requests. It signals the successful startup of your Jmix application. |
3 | DemoDataInitializer utilizes an InitFlags entity to persist a flag that’s set to true after the initial import. This flag uses a pessimistic database lock to avoid concurrent execution when multiple servers in a cluster are starting up. Moreover, you have the flexibility to define alternative conditions for report creation/updates or even trigger ReportImportExport via a manual command in the application’s user interface. |
4 | The ReportImportExport bean handles the actual report import. |
Was this page helpful?
Thank you for your feedback