Support

Expand all | Collapse all

How to use the "Date" report parameter in Velocity?

  • 1.  How to use the "Date" report parameter in Velocity?

    Posted 11-10-2015 12:19
    Hi Velocity-Gurus,

    I have set up a velocity report where I want to evaluate things based on a certain date the user choses when executing the report. Therefore I have added a report parameter of type "Date" to my report.
    What I get, when I display the contents of the variable is something like:

    Date: 1447110000000 (this is the result when I select 11/10/2015 as date)

    What do I need to do with it to make Velocity use this as a real date, so that I can do e.g. comparisons with it? I've tried e.g. convertStringToDate(), but I cannot get it to work. Has anyone solved that already and can show me how to do the trick?

    Thank you,
    Anna


  • 2.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-10-2015 14:35
    I haven't delved much into Velocity yet, but from my experimentation with BIRT, I believe Velocity is reporting the date in Java timestamp format -- i.e. the number of milliseconds since the UNIX epoch (https://en.wikipedia.org/wiki/Unix_time).

    ("UNIX time" measures from the same point, but in seconds; i.e. drop the 4 trailing zeroes)

    You can convert this into an appropriate date/time with online tools (e.g. http://www.epochconverter.com/), but you likely want something programmatic -- Hopefully someone with more experience at Velocity will recommend the best approach.


  • 3.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-11-2015 20:06
    Hi Anna,

    Can you let us know how you were trying to use the convertStringToDate() call and what the result was?


  • 4.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-12-2015 08:04
    Hi Chris,

    I created this simple script:

    ## name of date parameter in the report launch dialog: $myDate
    Report Dialog: $myDate <br>
    Converted to String: $myDate.convertStringToDate() <br>
    The output is the following:

    Report Dialog: 1447714800000
    Converted to String: $myDate.convertStringToDate()
    I was assuming that the date is passed as string (as the other report parameters are as well), so I don't know where it goes wrong.

    Anna

    P.S.: Is there any way - that is NOT guessing - to find out the which type a variable in a velocity script is?


  • 5.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-12-2015 23:02
    Hi Anna, Chris is out today so I am jumping in to see if I can be of any help. I suggest you check the article here which is on Velocity dateTool if you have not already. Tom also has an update in the comments where he has a code snippet on how to use dateTool to get the desired format for dates.


  • 6.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-13-2015 07:26
    Hi Shabnam,

    thank you. I am aware how to use the dateTool. But the format the report dialog sends the date to the actual report is not an object of type "date". This is why I cannot use the functions provided by the tool. I assume that it is handled as a string (as any other report parameters are as well), but I need to tranform ist to an actual date-object to work with it. And this is where I can't get it work.

    Anna


  • 7.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-13-2015 21:37
    Hi Anna - 
    I played with this a bit and have come to the conclusion that you really can't easily use the value Jama sends to Velocity. It sends it in milliseconds after the epoch, which is easy to handle with SQL queries. However, with velocity, I was unable to do find a way to convert it to a date. My recommendation would be to use the String parameter instead of the Date Parameter in Jama. You will probably have to put the desired format in the Parameter name so you can handle it correctly. Then convert the string to a date in the velocity code. I realize this isn't an optimal solution, but I believe it's the best one we have.

    Shawnna
    Reports Developer


  • 8.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-16-2015 08:54
    Hi Shawnna,

    thank you for digging into it. We have come to the same conclusion here and have also used the String parameter here already. It's sad that it's not working, but at least we have a workaround.
    Was the date parameter never tested against velocity? If you say that it is easy to handle with SQL-queries, was it designed to work with BIRT?
    If you decide to file this as a bug, would you mind putting me on the notify-list?

    Thank you,
    Anna


  • 9.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-16-2015 19:16
    Hi Anna - 
    I'm not sure this would be condsidered a bug in Jama. Velocity is a third party tool that we include with Jama, and as such, we are limited to what can be done with it. I will reach out to support, and will let you know if we do log it as a bug.

    Shawnna


  • 10.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-16-2015 19:28
    Anna - have you tried running the report as a "context-based" report, using a filter to narrow the data down by date before you run the report? That might help you as well.

    I have logged a ticket with support and asked them to notify you if they do fix this bug.


  • 11.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-17-2015 21:20
    Here's one way to turn a date criteria from Jama in milliseconds since the epoch in to an actual Date object:

    (My report is set up with a single criteria, with a type of "Date Parameter" and name of "report_dateParam". Also, it's hardcoded to use the test plan "CoveragePlus Release" in the Jama demo data)


    dateReport.vm (Velocity Report):

    The date parameter from Jama is: $report_dateParam with a type of $report_dateParam.getClass()<br>
    #set ($secondsFromEpoch = $mathTool.abs($report_dateParam))
    secondsFromEpoch is: $secondsFromEpoch with a type of $secondsFromEpoch.getClass();<br>
    #set ($dateObject = $dateTool.toDate($secondsFromEpoch))
    dateObject is: $dateObject with a type of $dateObject.getClass()<br>
    <br><hr><br>
    ## Test plan "CoveragePlus Release" in the Jama demo data
    #set ($testCycles = $testCycleDao.getTestCyclesForTestPlan(265))
    #foreach ($testCycle in $testCycles)
        #set ($endDate = $testCycle.getEndDate())
    $testCycle.getName() ends on $endDate
    #if ($endDate.compareTo($dateObject) < 0)
        (before $dateObject)<br>
    #else
    (after $dateObject)<br>
    #end
    #end


    Results (report_dateParam = Nov 15, 2015):

    The date parameter from Jama is: 1447574400000 with a type of class java.lang.String
    secondsFromEpoch is: 1447574400000 with a type of class java.lang.Long;
    dateObject is: Sun Nov 15 00:00:00 PST 2015 with a type of class java.util.Date


    Release 1.0 QA Build 1 ends on 2015-01-11 17:00:00.0 (before Sun Nov 15 00:00:00 PST 2015)
    Test Cycle #2 ends on 2015-11-24 17:00:00.0 (after Sun Nov 15 00:00:00 PST 2015)
    Test Cycle #3 ends on 2015-11-24 17:00:00.0 (after Sun Nov 15 00:00:00 PST 2015)



    Anna, does that help with what you're trying to do? 



    Jason


  • 12.  RE: Re: How to use the "Date" report parameter in Velocity?

    Posted 5 days ago
    Terrific solution! Works for me too.

    ------------------------------
    Theresa
    Fujifilm SonoSite
    WA United States
    ------------------------------



  • 13.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-18-2015 08:17
    Hi Jason,

    actually, your answer was a double hit.
    Now that I am aware of the function getClass() I understand what Velocity is doing and understand your solution. I adopted it to my script - and it works perfectly.

    May I suggest to document the getClass()-Function in your help-pages of the Velocity section, I don't think that anyone here was aware of it...

    So again - thank you!

    Anna


  • 14.  Re: How to use the "Date" report parameter in Velocity?

    Posted 11-18-2015 16:58
    Yay! I'm so glad Jason was able to find something that worked for you. And yeah, we'll see about getting this added to the Velocity documentation.