Let End Users Upload Jasper Reports

From JRapid

Jump to: navigation, search

Introduction

JRapid provides an easy way to add Jasper Reports to your web applications during development. You define reports as elements of the application, include the corresponding Jasper file in your project and specify some way in which the end user can open it. This is all described in detail here.

But there are times when you need to provide the end users with means for uploading new Jasper Reports that are to be created in the future, and you need to do allow them to do it without the need of a re-deployment. The following simple steps will show you how you may achieve this.

Letting End Users Upload Jasper Reports

First, you need to define some JRapid elements in your projects.

    <report file="dynamic_report.jasper" location="repository" name="DynamicReport"/>

    <entity label="App Report" menu="Menu" name="AppReport">
        <property display="primary" label="Name" name="name"/>
        <property label="Jasper File" name="jasperFile" type="file"/>
    </entity>

    <entity label="Open Report" menu="Menu" name="OpenReport" transient="transient">
        <next report="DynamicReport" type="report"/>
        <property entity="AppReport" label="Select Report" name="appReport"/>
    </entity>

The DynamicReport is there so that the code generator creates the necessary classes to render Jasper Reports. The name used in the file attribute is of no importance as this file will never get used.

The AppReport entity is where the end user will create the reports and upload the Jasper file to be used for each of them.

OpenReport is a transient entity that will allow the users to select which report to open after they click OK.

The important part here is the overriding of the getInputStream() method in the DynamicReportReport.java file.

    @Override
    public InputStream getInputStream(File appDir, File repositoryDir,
            Map<String, Object> parameterMap) {
       
        String appReportId = ((String[]) parameterMap.get("appReport"))[0];
        System.out.println(parameterMap.get("appReport"));
        String file = AppReport.DAO().findById(appReportId).getJasperFile();
       
        try {
                return new FileInputStream(new File(repositoryDir, file));
        } catch (FileNotFoundException e) {
            throw new ReportServerException(ErrorCodes.REPORT_FILE_NOT_FOUND, 
                "Report file not found: " + file + " in location: " + "repository" + ". ", e);
        }
       
    }

This makes the DynamicReport render the file selected in the OpenReport form.


This is how the end user will be able to manage reports.


File:dyn_rep1.png


And this is how they will be able to open the desired report.


File:dyn_rep2.png


See also

Personal tools