How to create a sublist from List<Object> in apache velocity template .vm - velocity

I am new to apache velocity, I want to create a subList Object from the List Objects which are coming from some service call in .vm file.
We need to render the list based on some logic in parts, for that we want to create sublist from list.
$table.getBooks() //contains all the Books objects
Below is the sample code which I tried but it did not work.
#set($segregatedList = [])
#set($size = $table.getLineItems().size())
#foreach($index in [0..$size-1])
#set($value = $index + 4)
#set($minimum = $math.min($nItems,$value))
$segregatedList.add($table.getBooks().subList($index,$minimum)))
$index += 4
#end
I executed the code, while rendering $segregatedList is coming as null.
I verified $table.getBooks() contains the Objects as when I am passing this,Objects are getting rendered successfully.
Can someone please tell what I am doing wrong or how can I create a sublist ?

First you are increment index with 4 and can cause an IndexOutOfBoundsException, so need to change until size-5 (and therefore remove math minimum check)
Second you are adding single Element instead of all Elements using addAll
Third your size check if on wrong parameter - should be on relevant $table.getBooks()
And last make sure your list have more than 5 elements
#set($segregatedList = [])
#set($size = $table.getBooks().size())
#foreach($index in [0..$size-5])
#set($value = $index + 4)
$segregatedList.addAll($table.getBooks().subList($index, $value)))
$index += 4
#end

Related

Polarion Velocity Script adding a custom field integer

I'm new to Velocity scripting and made a few simple scripts and they work ok.
I'm now trying something else, which should be simple but I can't seem to get it to work.
I'm selecting a bunch of Work Items, reading a custom field (NumberPack) and I just want to sum them.
My script is as follow:
#set($PCR = $transaction.workItems.search.query("type:Paramrequest AND created:[20220101 TO 30000000] AND NumberPack.1:[00000000001 TO 02147483647]"))
#set($Total = 0)
#set($Pack = 0)
#set($x = 0)
#foreach($PCR in $PCR)
##set($Pack = $Pack.parseInt($PCR.fields.get("NumberPack")))
##set($x = $Total.add($Pack))
$PCR.fields.get("NumberPack").render ## this renders each NumberPack of each WI
#set($Pack = $PCR.fields.get("NumberPack"))
##set($x = $Total2.add($PCR.fields.get("NumberPack")))
##set($Total2 = $Total2 + 1)
#set($x = $math.add($x, 1))
#end
<br> Total: $Total
<br> $x
As you can see I tried a few methods but I keep getting the total 0.
Any ideas what I'm doing wrong?
Thanks
If you write
#set($Pack = $PCR.fields.get("NumberPack"))
Pack: $Pack <br>
the output is something like:
Pack: com.polarion.alm.server.api.model.fields.ProxyIntegerField#67807d51
In the API Javadoc (https://almdemo.polarion.com/polarion/sdk/doc/javadoc-rendering/com/polarion/alm/shared/api/model/fields/IntegerField.html), you'll find that api.model.fields IntegerField has a get() method, which gives you the value. Though I agree this is never explicitly stated in the documentation.
You need to write
#set($Pack = $PCR.fields.get("NumberPack").get())
to get the value. The following statement will give you the cumulative sum.
#set($Total = $math.add($Total, $PCR.fields.get("NumberPack").get()))
Also be careful with your #foreach statement. In this case it seems to work, but it would be safer to give your iterator variable a name differing from the collection you are iterating through. For example:
#foreach($PCR in $PCRs)

I need to go few websites and grab specific data set

Looking for a way to go to a few websites (10 sites) and grab a set of data, need a proper way to do since I am new to coding.
code:
WebUI.openBrowser('');
WebUI.navigateToUrl("www.site1.com");
WebUI.maximizeWindow();
String text1 = WebUI.getText(findTestObject("xpath"));
Do I need to do this for all the 10 websites?
Let's say you have the 10 URLs in an array. Create another empty array that will hold the text values:
WebUI.openBrowser('');
def mySites = ["www.site1.com", "www.site2.com", "www.site3.com", "www.site4.com", "www.site5.com", "www.site6.com", "www.site7.com", "www.site8.com", "www.site9.com", "www.site10.com"];
def myTexts = [];
Then loop your above code in a for loop, and add the text values to the array (note: xpath element name is the same for all the sites in this example, you should edit that according to your needs):
for(i=0; i<mySites.size; i++){
WebUI.navigateToUrl("www.site1.com");
WebUI.maximizeWindow();
myTexts << WebUI.getText(findTestObject("xpath"));
}

Adding to List Prints true in Velocity

I am trying to add some string values to a list in Velocity. When I run the code it works alright. But the line where it adds the value prints true. Is it always like that in Velocity? I am new to Velocity templates, so cant figure it out myself.
#set ($uniqueInterfaces = [])
#if($ipv4interfaceName == $ipv6interfaceName)
$uniqueInterfaces.add($ipv4interfaceName)
#end
Its part of larger code with a nested foreach. It has two matches in it, so the output is:
true
true
I do not need this true being printed at all!
Java's List#add method returns boolean, that's why this return value is printed in your html output.
You can hide it simply by assigning the output of the add method to a dummy variable:
#set ($uniqueInterfaces = [])
#if($ipv4interfaceName == $ipv6interfaceName)
#set ($swallow = $uniqueInterfaces.add($ipv4interfaceName))
#end

How do I loop thought each DB field to see if range is correct

I have this response in soapUI:
<pointsCriteria>
<calculatorLabel>Have you registered for inContact, signed up for marketing news from FNB/RMB Private Bank, updated your contact details and chosen to receive your statements</calculatorLabel>
<description>Be registered for inContact, allow us to communicate with you (i.e. update your marketing consent to 'Yes'), receive your statements via email and keep your contact information up to date</description>
<grades>
<points>0</points>
<value>No</value>
</grades>
<grades>
<points>1000</points>
<value>Yes</value>
</grades>
<label>Marketing consent given and Online Contact details updated in last 12 months</label>
<name>c21_mrktng_cnsnt_cntct_cmb_point</name>
</pointsCriteria>
There are many many many pointsCriteria and I use the below xquery to give me the DB value and Range of what that field is meant to be:
<return>
{
for $x in //pointsCriteria
return <DBRange>
<db>{data($x/name/text())}</db>
<points>{data($x//points/text())}</points>
</DBRange>
}
</return>
And i get the below response
<return><DBRange><db>c21_mrktng_cnsnt_cntct_cmb_point</db><points>0 1000</points></DBRange>
That last bit sits in a property transfer. I need SQL to bring back all rows where that DB field is not in that points range (field can only be 0 or 1000 in this case), my problem is I dont know how to loop through each DBRange/DBrange in this manner? please help
I'm not sure that I really understand your question, however I think that you want to make queries in your DB using specific table with a column name defined in your <db> field of your xml, and using as values the values defined in <points> field of the same xml.
So you can try using a groovy TestStep, first parse your Xml and get back your column name, and your points. To iterate over points if the values are separated with a blank space you can make a split(" ") to get a list and then use each() to iterate over the points on this list. Then using groovy.sql.Sql you can perform the queries in your DB.
Only one more thing, you need to put the JDBC drivers for your vendor DB in $SOAPUI_HOME/bin/ext and then restart SOAPUI in order that it can load the necessary driver classes.
So the follow code approach can achieve your goal:
import groovy.sql.Sql
import groovy.util.XmlSlurper
// soapui groovy testStep requires that first register your
// db vendor drivers, as example I use oracle drivers...
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver( "oracle.jdbc.driver.OracleDriver")
// connection properties db (example for oracle data base)
def db = [
url : 'jdbc:oracle:thin:#db_host:d_bport/db_name',
username : 'yourUser',
password : '********',
driver : 'oracle.jdbc.driver.OracleDriver'
]
// create the db instance
def sql = Sql.newInstance("${db.url}", "${db.username}", "${db.password}","${db.driver}")
def result = '''<return>
<DBRange>
<db>c21_mrktng_cnsnt_cntct_cmb_point</db>
<points>0 1000</points>
</DBRange>
</return>'''
def resXml = new XmlSlurper().parseText(result)
// get the field
def field = resXml.DBRange.db.text()
// get the points
def points = resXml.DBRange.points.text()
// points are separated by blank space,
// so split to get an array with the points
def pointList = points.split(" ")
// for each point make your query
pointList.each {
def sqlResult = sql.rows "select * from your_table where ${field} = ?",[it]
log.info sqlResult
}
sql.close();
Hope this helps,
Thanks again for your help #albciff, I had to add this into a multidimensional array (I renamed field to column and result is a large return from the Xquery above)
def resXml = new XmlSlurper().parseText(result)
//get the columns and points ranges
def Column = resXml.DBRange.db*.text()
def Points = resXml.DBRange.points*.text()
//sorting it all out into a multidimensional array (index per index)
count = 0
bigList = Column.collect
{
[it, Points[count++]]
}
//iterating through the array
bigList.each
{//creating two smaller lists and making it readable for sql part later
def column = it[0]
def points = it[1]
//further splitting the points to test each
pointList = points.split(" ")
pointList.each
{//test each points range per column
def sqlResult = sql.rows "select * from my_table where ${column} <> ",[it]
log.info sqlResult
}
}
sql.close();
return;

What is the best way to access an array inside Velocity?

I have a Java array such as:
String[] arr = new String[] {"123","doc","projectReport.doc"};
In my opinion the natural way to access would be:
#set($att_id = $arr[0])
#set($att_type = $arr[1])
#set($att_name = $arr[2])
But that it is not working. I have come with this workaround. But it a bit too much code for such an easy task.
#set($counter = 0)
#foreach($el in $arr)
#if($counter==0)
#set($att_id = $el)
#elseif($counter==1)
#set($att_type = $el)
#elseif($counter==2)
#set($att_name = $el)
#end
#set($counter = $counter + 1)
#end
Is there any other way?
You can use use Velocity 1.6: for an array named $array one can simply do $array.get($index).
In the upcoming Velocity 1.7, one will be able to do $array[$index] (as well as $list[$index] and $map[$key]).
You could wrap the array in a List using Arrays.asList(T... a). The new List object is backed by the original array so it doesn't wastefully allocate a copy. Even changes made to the new List will propagate back to the array.
Then you can use $list.get(int index) to get your objects out in Velocity.
If you need to get just one or two objects from an array, you can also use Array.get(Object array, int index)
to get an item from an array.
String[] arr = new String[] {"123", "doc", "projectReport.doc"};
In my opinion the natural way to access would be:
#set($att_id = $arr[0])
#set($att_type = $arr[1])
#set($att_name = $arr[2])
The value for this can be get by using $array.get("arr", 1) because there is no direct way to get the value from array like $att_id = $arr[0] in velocity.
Hope it works :)
Velocity 1.6
$myarray.isEmpty()
$myarray.size()
$myarray.get(2)
$myarray.set(1, 'test')
http://velocity.apache.org/engine/1.7/user-guide.html
there is an implicit counter $velocityCount which starts with value 1 so you do not have to create your own counter.
Brian's answer is indeed correct, although you might like to know that upcoming Velocity 1.6 has direct support for arrays; see the Velocity documentation for more information.
I ended up using the ListTool from the velocity-tools.jar. It has methods to access an array's elements and also get its size.
I has the same question and it got answered on another thread
#set ( $Page = $additionalParams.get('Page') )
#set ( $Pages = [] )
#if ( $Page != $null && $Page != "" )
#foreach($i in $Page.split(";"))
$Pages.add($i)
#end
#end
Array indexing in Confluence / Velocity templates