Skip to content

Commit

Permalink
DevUI: add report files (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Oct 4, 2024
1 parent 23c5f4f commit 7af6139
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -127,23 +135,46 @@ void registerFonts(BuildProducer<NativeImageResourcePatternsBuildItem> nativeIma

@BuildStep(onlyIf = IsDevelopment.class)
ReportRootBuildItem defaultReportRoot() {

return new ReportRootBuildItem(DEFAULT_ROOT_PATH);
}

@BuildStep(onlyIf = IsDevelopment.class)
void watchReportFiles(BuildProducer<HotDeploymentWatchedFileBuildItem> watchedPaths,
BuildProducer<ReportFileBuildItem> reportFiles,
List<ReportRootBuildItem> 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<Path> 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<String> collectClassesInPackage(CombinedIndexBuildItem combinedIndex, String packageName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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 -> "???";
};
}

}
Original file line number Diff line number Diff line change
@@ -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<CardPageBuildItem> cardPageBuildItemBuildProducer) {
void createVersion(BuildProducer<CardPageBuildItem> cardPageBuildItemBuildProducer,
List<ReportFileBuildItem> reportFiles) {
final CardPageBuildItem card = new CardPageBuildItem();

final ExternalPageBuilder versionPage = Page.externalPageBuilder("JasperReports Version")
Expand All @@ -22,6 +28,18 @@ void createVersion(BuildProducer<CardPageBuildItem> cardPageBuildItemBuildProduc

card.addPage(versionPage);

final PageBuilder<TableDataPageBuilder> 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);
Expand Down

0 comments on commit 7af6139

Please sign in to comment.