Archive

Archive for the ‘JASPER REPORTS’ Category

IReport / Jasper Reports – Passing collection parameter in a drill down report

November 4, 2009 8 comments

Well, it definitely seems i am playing on grounds never touched before. At least that’s how it looks like, after investing quite a lot of time  in this matter.

I was trying to pass a collection parameter (java.util.collection in Ireport) to a drill-down report. The collection was defined by a “Multiple Select Values” based on an sql query, that returned a list of id#s, something like:

select id from performance

This parameter was to be used in my main report, as an input control, for selecting the id’s for which i wanted to generate my report.

So, the first step was to add the repository based input control both to my main report, and to my drill-down report. I also had to add the local parameter, let’s call it “myid“, of type java.util.collection.

All well, my main report runs smoothly, and it returns the data based on the id’s i have selected from my collection type input control.

Now, i wanted to drill-down in the second report (to be read. NOT SUBREPORT ), using the same values. So, after defining the parameter “myid” in the second report, i went back to my first report, and used the “myid” parameter as a hyperlink. Again, you have to define the type that is to be sent, and the value.

myid: $P{myid} – of type collection

Theoretically, my drill down report should now accept the collection, and use it in a query, something like

select test from tests where $X{IN,test_id,myid}

Well, it doesn’t, and i am 100 % sure about this. I have tried all possible combinations…changing variable names, creating local parameters containing the collection data, functions, variables…None of them worked. So after chatting with a dev, i decided to try using the toString() method in Java, and see what comes out.

I added a string parameter, called “$P{coltostring}“, with the default expression of “$P{myid}.toString()“, and used this parameter as a hyperlink:

coltostring: $P{myid}.toString() - of type java.lang.String

Little was i amazed to see that the url now changed, from not including my collection values, to something like:

http://jasperserver:8080/jasperserver/flow.html?_flowId=viewReportFlow&reportUnit=%2Freports%2FPerformance%2FClassloading&coltostring=[25261,+25260]&reportLocale=en

So my collection was now having a form…just not the one that i needed. The problem here was the collection was printed with the brackets before and after ( ‘[',']‘), and was breaking my Jasper Server report.

The pluses won’t make a difference, mysql ignores them, so i was able to leave them out. I had to remove the brackets somehow. From here it was pretty simple:

I decided to use the String methods of Java, in this case the string.replace(char,char) method. So what i did, was to send the parameter in the hyperlink as a formatted string like this:

coltostring: (($P{myid}.toString()).replace(‘[',' ')).replace(']‘,’ ‘) – type set to java.lang.string

where coltostring is the name of the parameter sent, and (($P{myid}.toString()).replace(‘[',' ')).replace(']‘,’ ‘) is its expression.

I was now able to see the url being properly formated like:

http://jasperserver:8080/jasperserver/flow.html?_flowId=viewReportFlow&reportUnit=%2Freports%2FPerformance%2FClassloading&coltostring=+25261%2C+25260%2C+25259+&reportLocale=en

As you see, my collection in this case is comprised of three values

Using the “coltostring” parameter in my drill down report, i was now able to run my query using the IN statement, instead of the $X(IN,var1,var2), which is used for collections and lists:

select test from tests where test_id in ( $P!{coltostring} )


So using some simple toString, and string.replace methods, i was able to create a drill down report, with a parameter containing multiple values, all selected by the user in the main report.

An alternative solution could be the one suggested to me (which i did not have the time to try yet) by a support user on the JasperServer Forums, right here:

http://jasperforge.org/plugins/espforum/view.php?group_id=83&forumid=101&topicid=66194

I hope this will help some other people trying to do the same things.

One more note before putting an end to this… collections WORK IN SUBREPORTS ( but not in drill down reports)

IReport / Jasper Reports – Passing parameters between reports using IReport and Jasper Server

October 22, 2009 8 comments

How to send parameter to another in jasper ireport

After i struggled hard for almost a couple of days somewhere in august, for making a connection between two JasperReports (passing parameters from one report to another), these days i was facing the same problem again…so i thought i should write this down before i loose any other minute in the future on the same subject.

The topic is:

Having a Jasper Report, generated with IReport, running on a JasperServer, we want to run a “subreport” by passing a runtime parameter, that was generated in the first report.

As my environment is made up of IReport and JasperServer, running with a JasperServer repository, i was facing two problems:

  1. How to pass the parameter between two JasperReport reports
  2. Local vs. Repository Input Controls needed

Point 1 – Passing parameter between reports – Prepare the first report

After you created your first report using IReport (and updated it in the repository), suppose you want to build in a hyperlink, that should run a second report (Report2), by using this parameter as a field in a query for example.

These are the steps to perform:

  1. In your first report, Report1, right click on the text field containing the book value. Select “hyperlink
  2. Select “Hyperlink Type = Report Execution
  3. Go to “Link Parameter“, the fourth tab.
  4. You will have to add two parameters here, both of them of type string:
    • _report : “/reports/MyReports/Report2″ - where `_report` is the name of the parameter, and `/reports/MyReports/Report2` is your relative path in the repository, where Report2 is saved. Just replace the path with your own path
    • paramrep2 : $F{paramrep1} - where `paramrep2` is the name of the parameter, and `$F{paramrep1}` is the field in your first report, containing the value that you want to send to the second report
  5. You do not need to add these parameters as parameters in your first report. But you do have to add them in the second, and that takes me to point 2

Point 2 – Preparing the second report for the values passed by the first report

Because we are working with a repository, please try to avoid defining local input controls or datasources.

The parameters sent by Report 1 have to be integrated in Report2 with the help of “input controls”

Define your parameter as an input control in the repository, of the type that you need. Remember it has to have EXACTLY the same name as the parameter defined in Point 1, that being `paramrep2` . After that, refresh the report in IReport, so that you make sure the newly created input control is in the report (check it under the `Input controls` node)

Once you have this settled, you can modify your query accordingly, by using the passed parameter, something like:

where classname=$P{paramrep2}

There it is. It looks straightforward, but we do forget don’t we? And the trick with the repository is a thing to remember (and no, the other way around won’t do it, not with my version of IReport at least, which is 3.5.0)

Follow

Get every new post delivered to your Inbox.

Join 32 other followers