Jump to content
Changes to the Jaspersoft community edition download ×

changing a parameter in a Scriptlet


moujabz

Recommended Posts

  • Replies 13
  • Created
  • Last Reply

Top Posters In This Topic

Hi,

 

You can pass any object as parameter to a report.

It could be an instance of, let's say, MyValueHolder.

In the scriptlet, you could get this instance and call something like setValue() on it.

After the report is done, when you call getValue() on your instance, you would see what you set with the scriptlet during report execution.

 

I hope this helps.

Teodor

Link to comment
Share on other sites

  • 2 months later...

I know this post is outdated, however, My question remains.

 

Is it possible to modify the main report's query using a scriptlet BERORE the query is executed?

 

 

For example, if flag = 1, $P!{WHERECLAUSE} = "AND field1 > 12" else if flag = 0, $P!{WHERECLAUSE} = null

 

I really need this type of functionality, if someone has an answer for me, PLEASE by all means spill it. This would make my life so much easier...Thanks in advance.

Link to comment
Share on other sites

codyjasperForge wrote:

Is it possible to modify the main report's query using a scriptlet BERORE the query is executed?

 

It is, bit it's more of a hack:

Code:

class MyScriptlet extends JRAbstractScriptlet {
public void setData(Map parameters, Map fields, Map variables, JRFillGroup[] groups) {
super.setData(parameters, fields, variables, groups);

JRFillParameter parameter = (JRFillParameter) parameters.get("MyParam"«»);
parameter.setValue(..);
}
}

 

However, this can also be done without using a scriptlet. It's not clear where the flag values comes from, but let's say you pass it to the report as a param:

Code:
[code]
<parameter name="WHERECLAUSE" isForPrompting="false">
<defaultValueExpression>$P{flag}.booleanValue() ? " AND field1 > 12" : ""</defaultValueExpression>
</parameter>

 

HTH,

Lucian

Link to comment
Share on other sites

Yes, Lucianc it is quite funny that it is not the same question :laugh:

 

However, I found this question by searching the forum for similar entries...

 

Anyway thank you for the response, I can't begin to express how much time you may have just saved me. The flag scenario is just an example, but my real problem is very similar.

 

I am attempting to convert 'unmarshalled' xml report data (from a different more expen$ive tool) into jaspers' xml structure, but there is some extra baggage that's coming with the old xml. (a few functions that are executed before the query is executed to manipulate the 'Where' and 'order by' clauses, depending on parameter values)

 

I had previously tried using the beforeReportInit() function of the internal scriptlet editor to call the necessary functions that do the manipulation of the "where" and "order by" clauses. I found out though, that the query gets executed before this function is called, therefore I was at a stand still...

 

Once again, thanks for creating such a useful tool, and thank you for you timely response(s)... Keep up the good work!

 

iReport 2.0 freakin rocks!

Link to comment
Share on other sites

One additional question,

 

I noticed in your code example that you are extending JRAbstractScriptlet. Is this a requirement in order to meet my expectations, or can I extend IReportScriptlet. (I am using iReport 2.0)

 

Also

It is, bit it's more of a hack:

 

Is this a hack because of overloading/overriding?

 

How is it that you know that this will execute before the main report's query?

 

Sorry for the repeated questions, but I need a little clarification... Thanks

Link to comment
Share on other sites

codyjasperForge wrote:

I noticed in your code example that you are extending JRAbstractScriptlet. Is this a requirement in order to meet my expectations, or can I extend IReportScriptlet. (I am using iReport 2.0)

 

You don't need to directly extend JRAbstractScriptlet; IReportScriptlet extends JRAbstractScriptlet so it would work just as fine.

 

Is this a hack because of overloading/overriding?

 

No, it's a hack because the setData method was meant to initialize the scriptlet, and using it to change a parameter value is not something one would usually do.

 

How is it that you know that this will execute before the main report's query?

 

I know the code and the scriptlet is initialized before the query gets executed. Note that I haven't actually tried any of this, but from what I know it should work.

 

Regards,

Lucian

Link to comment
Share on other sites

Do I need to import anything else?

Because it keep getting this error: (47 times)

 

Code:

C:Program FilesJasperSoftreportsseeker_services_type_officeScriptlet.java:31: cannot find symbol
symbol : class JRFillGroup
location: class seeker_services_type_officeScriptlet
public void setData(Map parameters, Map fields, Map variables, JRFillGroup[] groups) {

C:Program FilesJasperSoftreportsseeker_services_type_officeScriptlet.java:37: cannot find symbol
symbol : class JRFillParameter
location: class seeker_services_type_officeScriptlet
JRFillParameter office_id_param = (JRFillParameter) parameters.get("SELECT_OFFICE_ID");






...

 

I have 'jasperReports-1.3.4.jar' included in my classpath, as well as the 'tools.jar' file from my jdk(1.5), my import statement is there and correct, why does iReport not find JRFillParameter?

 

Do I need to include additional jars for this to execute? I'm a little confused...

 

Thank you again.

Post edited by: codyjasperForge, at: 2007/07/24 16:11

Link to comment
Share on other sites

I have more questions...:blush:

 

I included my import statements, and the scriptlet compiled...

 

I am curious now, how exactly I should call setData(). I do not know how to pass the arguments to setData() either. I am able to get the 'parameters Map' but, where do the 'field' and 'variable' maps come from? Are these only accessible in 'JasperReports', or can I access them in iReport? I'm quite confident that the scriptlet code will execute like it is supposed to, but I'm not sure how/where to call it. If anyone has ideas, feel free to share, because I'm stuck.

Link to comment
Share on other sites

codyjasperForge wrote:

I am curious now, how exactly I should call setData().

 

You do not need not call setData() yourself, the JR fill code calls this method on the report scriptlet.

 

However, the bad news is that the hack does not actually work in this form. Sorry, my bad, I should have tested it. It doesn't work because after the scriptlet is initialized, all the parameter values are overwritten. There is one more hack that will fix this, but I think we had enough of this.

 

What I would propose you now is a more natural approach.

 

Do you actually need to set the parameter value in the scriptlet? You could also reverse this logic by making the parameter ask the scriptlet for its value:

Code:

// the scriptlet
public class MyScriptlet extends .. {
public String getMyParameterValue() {
..compute and return the param value..
}
}

<!-- the report JRXML -->
<parameter name="MyParameter" isForPrompting="false">
<defaultValueExpression>$P{REPORT_SCRIPTLET}.getMyParameterValue()</defaultValueExpression>
</parameter>

 

Would this work in your case?

 

Regards,

Lucian

Link to comment
Share on other sites

Lucianc wrote

Do you actually need to set the parameter value in the scriptlet?

 

Yes, ideally this is what I would like to do.

 

The only problem with the 'natural approach' is the amount of data manipulation that occurs inside of the functions that I am using. I am converting PL/SQL functions/procedures into java code equivalent, to do preprocessing on my variable/parameter values.

 

For example, I have the following parameters:

 

serviceTypes 1-5 (Integers)

Office id (Integer)

sortBy (String)

status (String)

start date (date)

end date (date)

where clause (string)

order by(string)

 

I have the following variables:

 

order by -> depends on sortBy param value

 

Where clause -> depends on (status, office id,

 

servicetype, start date param values)

 

display_status -> depends on status param value

 

display_officeName -> depends on office id value

 

display_serviceType -> depends on which service type parameter is used (1-5)

 

The main function calls two procedures depending on the values of the input params:

 

if sortBy == "value1"

orderBy += "new Value"

 

if officeid!=null

call MakeOfficeNames()

whereclause += "another value"

 

if serviceType1!=null

call MakeServiceLists()

whereclause += "yet another value"

 

etc.

 

I have came to the conclusion that it would be best to avoid using the scriptlet to do the preprocessing, but it was a courageous effort in trying.

 

I want to say thanks for you patience with my posts, and for all of you help, as I have a new found appreciation for scriptlets in iReport! :)

 

Thanks again Lucianc...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...