From 7af6139961fdd03d5e0340c05e804e5462c48e07 Mon Sep 17 00:00:00 2001 From: Melloware Date: Fri, 4 Oct 2024 18:39:18 -0400 Subject: [PATCH] DevUI: add report files (#95) --- .../deployment/JasperReportsProcessor.java | 47 +++++++++++++++--- .../deployment/ReportFileBuildItem.java | 49 +++++++++++++++++++ .../devui/JasperReportsDevUIProcessor.java | 20 +++++++- 3 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 deployment/src/main/java/io/quarkiverse/jasperreports/deployment/ReportFileBuildItem.java diff --git a/deployment/src/main/java/io/quarkiverse/jasperreports/deployment/JasperReportsProcessor.java b/deployment/src/main/java/io/quarkiverse/jasperreports/deployment/JasperReportsProcessor.java index ef8754e..71683bf 100644 --- a/deployment/src/main/java/io/quarkiverse/jasperreports/deployment/JasperReportsProcessor.java +++ b/deployment/src/main/java/io/quarkiverse/jasperreports/deployment/JasperReportsProcessor.java @@ -1,5 +1,12 @@ package io.quarkiverse.jasperreports.deployment; +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; @@ -20,12 +27,13 @@ import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedPackageBuildItem; import io.quarkus.deployment.pkg.builditem.UberJarMergedResourceBuildItem; +import io.quarkus.logging.Log; class JasperReportsProcessor { private static final String FEATURE = "jasperreports"; private static final String EXTENSIONS_FILE = "jasperreports_extension.properties"; - private static final String DEFAULT_ROOT_PATH = ""; + private static final String DEFAULT_ROOT_PATH = "src"; @BuildStep FeatureBuildItem feature() { @@ -127,23 +135,46 @@ void registerFonts(BuildProducer nativeIma @BuildStep(onlyIf = IsDevelopment.class) ReportRootBuildItem defaultReportRoot() { + return new ReportRootBuildItem(DEFAULT_ROOT_PATH); } @BuildStep(onlyIf = IsDevelopment.class) void watchReportFiles(BuildProducer watchedPaths, + BuildProducer reportFiles, List reportRoots) { - watchedPaths.produce(HotDeploymentWatchedFileBuildItem.builder().setLocationPredicate(path -> { - for (ReportRootBuildItem root : reportRoots) { + + for (ReportRootBuildItem reportRoot : reportRoots) { + Path startDir = Paths.get(reportRoot.getPath()); // Specify your starting directory + List foundFiles = new ArrayList<>(); + + try { // reports - .jrxml // styles - .jrtx - if (path.startsWith(root.getPath()) && (path.endsWith(".jrxml") || path.endsWith(".jrtx"))) { - return true; - } + // compiled - .jasper + Files.walkFileTree(startDir, new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { + // Check if the file has one of the desired extensions + for (String ext : ReportFileBuildItem.EXTENSIONS) { + if (file.toString().endsWith(ext)) { + foundFiles.add(file); + break; + } + } + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + Log.error("Error looking for report files.", e); } - return false; - }).build()); + // Print the found files + foundFiles.forEach((file) -> { + reportFiles.produce(new ReportFileBuildItem(file)); + watchedPaths.produce(new HotDeploymentWatchedFileBuildItem(file.toString())); + }); + } } private List collectClassesInPackage(CombinedIndexBuildItem combinedIndex, String packageName) { diff --git a/deployment/src/main/java/io/quarkiverse/jasperreports/deployment/ReportFileBuildItem.java b/deployment/src/main/java/io/quarkiverse/jasperreports/deployment/ReportFileBuildItem.java new file mode 100644 index 0000000..18e1f10 --- /dev/null +++ b/deployment/src/main/java/io/quarkiverse/jasperreports/deployment/ReportFileBuildItem.java @@ -0,0 +1,49 @@ +package io.quarkiverse.jasperreports.deployment; + +import java.nio.file.Path; +import java.util.List; +import java.util.Locale; + +import org.apache.commons.io.FilenameUtils; + +import io.quarkus.builder.item.MultiBuildItem; + +/** + * This build item represents a single watched Report file either .jrxml, .jasper, or .jrtx + */ +public final class ReportFileBuildItem extends MultiBuildItem { + + public final static String EXT_REPORT = "jrxml"; + public final static String EXT_STYLE = "jrtx"; + public final static String EXT_COMPILED = "jasper"; + public final static List EXTENSIONS = List.of("." + EXT_REPORT, "." + EXT_COMPILED, "." + EXT_STYLE); + + private final Path path; + + public ReportFileBuildItem(Path path) { + this.path = path; + } + + public Path getPath() { + return path; + } + + public String getFileName() { + return path.getFileName().toString(); + } + + public String getParent() { + return path.getParent().toString(); + } + + public String getType() { + String extension = FilenameUtils.getExtension(path.getFileName().toString()).toLowerCase(Locale.ROOT); + return switch (extension) { + case EXT_COMPILED -> "COMPILED"; + case EXT_REPORT -> "REPORT"; + case EXT_STYLE -> "STYLES"; + default -> "???"; + }; + } + +} \ No newline at end of file diff --git a/deployment/src/main/java/io/quarkiverse/jasperreports/deployment/devui/JasperReportsDevUIProcessor.java b/deployment/src/main/java/io/quarkiverse/jasperreports/deployment/devui/JasperReportsDevUIProcessor.java index 27f319a..4e18f55 100644 --- a/deployment/src/main/java/io/quarkiverse/jasperreports/deployment/devui/JasperReportsDevUIProcessor.java +++ b/deployment/src/main/java/io/quarkiverse/jasperreports/deployment/devui/JasperReportsDevUIProcessor.java @@ -1,17 +1,23 @@ package io.quarkiverse.jasperreports.deployment.devui; +import java.util.List; + +import io.quarkiverse.jasperreports.deployment.ReportFileBuildItem; import io.quarkus.deployment.IsDevelopment; import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; import io.quarkus.devui.spi.page.CardPageBuildItem; import io.quarkus.devui.spi.page.ExternalPageBuilder; import io.quarkus.devui.spi.page.Page; +import io.quarkus.devui.spi.page.PageBuilder; +import io.quarkus.devui.spi.page.TableDataPageBuilder; import net.sf.jasperreports.engine.JasperReport; public class JasperReportsDevUIProcessor { @BuildStep(onlyIf = IsDevelopment.class) - void createVersion(BuildProducer cardPageBuildItemBuildProducer) { + void createVersion(BuildProducer cardPageBuildItemBuildProducer, + List reportFiles) { final CardPageBuildItem card = new CardPageBuildItem(); final ExternalPageBuilder versionPage = Page.externalPageBuilder("JasperReports Version") @@ -22,6 +28,18 @@ void createVersion(BuildProducer cardPageBuildItemBuildProduc card.addPage(versionPage); + final PageBuilder reportsPage = Page.tableDataPageBuilder("Reports") + .icon("font-awesome-solid:file-pdf") + .staticLabel(String.valueOf(reportFiles.size())) + .showColumn("fileName") + .showColumn("type") + .showColumn("parent") + .buildTimeDataKey("reports"); + + card.addPage(reportsPage); + + card.addBuildTimeData("reports", reportFiles); + card.setCustomCard("qwc-jasperreports-card.js"); cardPageBuildItemBuildProducer.produce(card);