Hashmap and Arraylist in Velocity

Posts: 12
Joined: Wed Feb 08, 2017 10:16 am

Hashmap and Arraylist in Velocity

Postby dbosman » Mon Feb 27, 2017 9:19 am



an example is showed how to use maps and lists in Velocity:

#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList
#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map

Unfortunately this does not work in 2016 SR1.

1. Which Velocity version is Polarion 2016 SR1 using?

I tried the following workaround:

1.doc: $document <BR>
2.dc: $document.getClass() <BR>
3.class: $document.getClass().forName("java.util.ArrayList") <BR>
4.lst: $document.getClass().forName("java.util.ArrayList").newInstance() <BR>

Unfortunately step 3 fails.

How does one create a ArrayList or HashMap in Velocity?

Posts: 3
Joined: Tue Sep 23, 2014 8:40 am

Re: Hashmap and Arraylist in Velocity

Postby enkelmann » Fri Mar 16, 2018 6:56 pm

Polarion needs a bit of extra work, but what you want can be done.

To get maps work, you need to install the "Wiki Scripting Tools Extension" http://extensions.polarion.com/extensions/83-wiki-scripting-tools.

With that in place, you can do:

Code: Select all

#set ($monkeySay = ["Not", $me, "fault"] ) ## ArrayList
#set ($monkeyMap = $objectFactory.newMap() )  ## Map
#set ($trash = $monkeyMap.put("banana", "good"))
#set ($trash = $monkeyMap.put( "roast beef", "bad"))

$monkeySay.class - $monkeySay<br>
$monkeyMap.class - $monkeyMap<br>

Which gives the desired result:

Code: Select all

class java.util.ArrayList - Test.Not, admin, fault
class java.util.HashMap - {banana=good, roast beef=bad}


Posts: 80
Joined: Wed Apr 27, 2016 10:34 am
Location: Austria

Re: Hashmap and Arraylist in Velocity

Postby ALMighty » Wed Mar 21, 2018 4:09 pm

dbosman wrote:On
1. Which Velocity version is Polarion 2016 SR1 using?

Polarion 2016 uses Velocity version 1.4, in this reply I have posted in the thread stop a #foreach sentence you will see proof for that. :wink:

Here is the Veloicty guide for version 1.4: https://velocity.apache.org/engine/1.4/user-guide.html

Here is btw an example of an Array probably this already fulfills your needs. In that example I have a String array to which I add another String and I add also a wokr item. Yep, that is possible in Velocity that you have an Array which contains value sof different types. :D

Code: Select all

#set($testArray = ["v1", "v2", "v3", "v4", "v5", "v6"])
#set($temp = $testArray.add("v7")) ## adding a value, the $temp variable is a boolean variable and the assignment is alas necessary

## In the next three rows we add now a work item to the String array. Yes that is possible because Velocity has no type security
#set($projectID = "$page.space.projectId") ## get the ID of the current project
#set($testWorkItem = $trackerService.getWorkItem($projectID, "Random-1") ) ## place here a work item ID of of your project
#set($temp = $testArray.add($testWorkItem)) ## adding another value

<h2> $temp </h2>
<h2> $testWorkItem </h2>

#foreach($currentValue in $testArray)
<H1> $currentValue </H1> ## testoutput of the currently selected value
Playing a good game is never lame. :D

Return to “Polarion Application Lifecycle Management (ALM)”

Who is online

Users browsing this forum: Google [Bot] and 7 guests