Dynamic Dates For JasperServer

 Adding download for jrxml and jar file. 

Here is what the report should look like:

Clone bitbucket repo to get code:

Https: git clone https://brooneyx@bitbucket.org/brooneyx/js-dynamic-dates.git
SSh: git clone git@bitbucket.org:brooneyx/js-dynamic-dates.git

 

Many of the reports I design require a predefined set of dates for the user to choose. In addition I also like to allow the user to pick any start and end date they would like. The ability to choose a set of dates is especially important when we want to schedule reports, a static start and end date do not cut in this case. Out of the box, JasperServer does not have this functionality and I have been through several revisions on the way to my current implementation of  this feature.

First you need to come up with the list of dates that you would like to provide to the user. In my case I use the following:

When the user chooses “User Defined” we then use the following to allow for user settable start and end dates.

Setting up these input selectors is the easy part, the real work comes in the back-end code that interprets the choices and sets the appropriate start and end dates for the chosen time slice. In my case I decided to use Java to create a class that will return a date given a string that corresponds to the users pick list displayed above.

The prototypes for the functions are:

The Date parameter in the second set is there for the case when the user chooses “User Defined”, in this case I send in the date that they picked in order to have a single call to cover all choices.

The next step is to implement the  java code to calculate the dates from the user picks. Here is a snippet of the getStartDate and getEndDate methods for “Previous Month”:

Okay so now we know how to calculate a date given a string representation of that time slice. How do we integrate this into a JasperReport ? This is the real challenge that is needed to make it work.

First we need to create a set of parameters in the jrxml file that help us calculate and store the dates.

  • PickDate – (String) This will be filled in with the date choice (“Yesterday”, This Week”, etc).
  • StartDate – (Date) This is the users set start date (“User Defined”)
  • EndDate-  (Date) This is the users set end date  (“User Defined”)
  • StartDateChoice – (String) This is where we call the getStartDate method.
  • EndDateChoice – (String) This is where we call the getEndDate method.

For StartDateChoice we will set the “Default Value Expression” to: com.reptics.dateutils.DateByString.getStartDate($P{DateChoice},$P{StartDate})

We do something similar with EndDateChoice and then we need to use $P(EndDateChoice} in the query when looking for a date. This replaces strings like “This Year” with the date we calculated from the Java code.

To make this all work you need to define the correct input selectors in JasperServer for DateChoice, StartDate and EndDate.

 

25 thoughts on “Dynamic Dates For JasperServer

  1. This is very interesting. Is there any way we could get a copy of the code and some details as to how this is implemented in Jasper?

    Thanks!

  2. Nice read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch! “Without friends no one would choose to live, though he had all other goods.” by Aristotle.

  3. This is exactly what I need, but I’m not sure where the jsp code goes. (Sorry, I’m a newbie) Can you provide additional guidance on where the code goes?

  4. it works great in ireports, but i cant get it work in jasperserver. i see no input controls in the report. i think i have to set the jar file on the right position on the server, but where?

  5. Hi,

    This is great, thanks. However, could you please provide a link to the .jar file ? and to the .java file ?

    Thanks, link is broken.

  6. Hi

    I’ve tried to clone the project but git ask me a password…
    Is it possible to have the source files or the jar?

    Thanks in advance

    Bozz

  7. This looks really interesting. Can you give some instructions on how to make this work on JasperReports Server, esp. for a scheduled report?

    I have defined the input controls and they get initially populated with their defined default values, yet (a) the report shows empty and (b) when selecting another entry in the DateChoice combo, the other Input Controls don’t reflect the new selection.

    Sorry, a newbie here.

  8. Hi Tom,
    You need to drop the jar file into the servers lib directory. Then follow the instructions in the article for adding parameters. You need all of them to get it working in a report. Ping me back if you need a bit more help.

  9. Thank you for this post!
    If I upload the jrxml file to the jasperserver and create the needed parameters(the same as in the report) I’ m getting an empty report(it doesn’t display anything)
    I have also added the JAR file to the server lib directory…
    I’m very confused! Please help me 🙁

    1. Try making it work in iReport first to rule any issue with the server. I will try it out tonight on the newest server to make sure it works.

  10. Hello, I downloaded your zip and opened it in Jasper Studio 6.0 and get the following error: Caused by: groovy.lang.MissingPropertyException: No such property: com for class: RepticDates_1426611995083_476875

    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)

    at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:86)

    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)

    at RepticDates_1426611995083_476875.evaluate(calculator_RepticDates_1426611995083_476875:154)

    at net.sf.jasperreports.engine.fill.JREvaluator.evaluate(JREvaluator.java:250)

    … 8 more

    Any ideas what could be going on?

  11. Hey – I seem to be having the same issue as others. The report works inside iReport, but does not work on the server. The jar was added to the lib directory, no errors appear the only thing I see is:

    RepticDates

    The report is empty.

    These are the input controls on the server as follows:
    DateChoice (included from upload, type string)
    StartDate (added, type date)
    EndDate (added, type date)
    StartDateChoice (added, type date)
    EndDateChoice (added, type date)

    When first being prompted for the parameters, StartDateChoice and EndDateChoice get the right date values from the default “DateChoice” value of “Last Month”

    However, when changing the DateChoice selection the StartDateChoice and EndDateChoice do not update.

    Through all of this, I only see “RepticDates
    The report is empty.” in the back. Are you able to replicate this?

    1. My Jasper instance stopped working on my mac. If I get a chance to install a new one I will look at this. If you have a instance I can test on please send along the info.

Leave a Reply to brooneyx Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.