Skip to content

Instantly share code, notes, and snippets.

@robertoneto-senior
Created October 4, 2024 14:34
Show Gist options
  • Save robertoneto-senior/11bcdc283658a9b185c40fba370fa574 to your computer and use it in GitHub Desktop.
Save robertoneto-senior/11bcdc283658a9b185c40fba370fa574 to your computer and use it in GitHub Desktop.

To register a custom RepositoryService in JasperReports, you'll need to follow these steps:

1. Create a Custom RepositoryService Implementation

First, you need to implement the RepositoryService interface (or extend one of the existing implementations). This is the interface JasperReports uses to interact with various report repository sources (like files, databases, etc.).

Example:

import net.sf.jasperreports.repo.RepositoryService;
import net.sf.jasperreports.repo.RepositoryContext;
import net.sf.jasperreports.repo.Resource;
import net.sf.jasperreports.repo.InputStreamResource;
import net.sf.jasperreports.repo.ResourceInfo;

import java.io.InputStream;

public class CustomRepositoryService implements RepositoryService {

    @Override
    public <K extends Resource> K getResource(RepositoryContext context, String location, Class<K> resourceType) {
        // Implement logic to locate and return the requested resource
        if (resourceType.equals(InputStreamResource.class)) {
            InputStream inputStream = getInputStreamFromCustomSource(location);
            return resourceType.cast(new InputStreamResource(inputStream));
        }
        return null;
    }

    @Override
    public ResourceInfo getResourceInfo(RepositoryContext context, String location) {
        // Optionally implement to provide resource metadata
        return null;
    }

    private InputStream getInputStreamFromCustomSource(String location) {
        // Implement logic to retrieve resource from your custom source (e.g., database, cloud, etc.)
        return null;
    }
}

2. Register the Custom RepositoryService

Once your custom RepositoryService is implemented, you need to register it in JasperReports so that the engine knows about your new repository source. This can be done in the JasperReports context configuration by providing a custom RepositoryService instance.

To do this programmatically, you can use the SimpleRepositoryContext and JasperReportsContext classes to register your service.

Example:

import net.sf.jasperreports.engine.JasperReportsContext;
import net.sf.jasperreports.repo.RepositoryService;
import net.sf.jasperreports.repo.SimpleRepositoryService;

public class CustomRepositoryServiceConfigurer {

    public static void configure(JasperReportsContext jasperReportsContext) {
        // Create an instance of your custom RepositoryService
        RepositoryService customRepositoryService = new CustomRepositoryService();

        // Add the custom RepositoryService to the JasperReports context
        SimpleRepositoryService repositoryService = new SimpleRepositoryService(jasperReportsContext);
        repositoryService.setRepository(customRepositoryService);
        jasperReportsContext.setExtensions(RepositoryService.class, repositoryService);
    }
}

3. Use Your Custom RepositoryService in JasperReports

Finally, when initializing JasperReports (for instance, when filling reports or exporting them), ensure that the custom repository service is configured and added to the context:

Example of setting up the custom repository service during report filling:

import net.sf.jasperreports.engine.JasperReportsContext;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class ReportFiller {

    public void fillReport() throws Exception {
        // Create a JasperReportsContext (or get an existing one)
        JasperReportsContext jasperReportsContext = DefaultJasperReportsContext.getInstance();

        // Configure the custom repository service
        CustomRepositoryServiceConfigurer.configure(jasperReportsContext);

        // Fill the report
        JasperPrint jasperPrint = JasperFillManager.getInstance(jasperReportsContext).fillReport(
            "your_report_template.jasper", 
            null, 
            yourDataSource
        );
        
        // Use jasperPrint for further processing
    }
}

4. Ensure Compatibility

When working with custom repositories, it's essential to make sure that the format and types of resources returned by your custom RepositoryService are compatible with what JasperReports expects.

5. Optional: Configure via Spring (if using Spring)

If you are using Spring in your application, you can register the CustomRepositoryService as a bean in your Spring configuration and inject it into the JasperReports context, making it easier to manage in a Spring-based project.


Summary

  • Implement the RepositoryService interface to create a custom repository service.
  • Register your service programmatically by modifying the JasperReportsContext.
  • Use the custom repository service when filling or generating reports.

This allows you to seamlessly integrate custom repositories, such as a database, cloud storage, or any other data source, into JasperReports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment