1

I'm generating a velocity report. I currently iterate through a collection of documents (IDs), for each of these I can get a list of relationships.

What I'd like to do is for each these IDs is to call the same function to see if there any further relationships:

I thought about a while loop but subsequently found out that is not supported.

## Loop through the selection of documents
#foreach( $vDoc in $documentList )

## for each document obtain a list of all upostream relationships
#foreach($h1 in $relDao.getUpstreamDocumentIds($vDoc.document.id))

## Need recursion in here....
## need to keep getting the upstream IDs until the size is zero and then return that ID

#end
#end
Andyww
  • 209
  • 3
  • 13

1 Answers1

0

Here is a way to achieve recursion in velocity. We can make use of macros, define a macro and use it recursively along with a base case

Snippet for the same. Modify as per the requirements.

## ***Call the macro for the first time***
#reportMacro($documentList) 

## ***This is the definition of the macro***
#macro(reportMacro $documentList)
{    
    ## Loop through the selection of documents
    #foreach( $vDoc in $documentList )

        ## for each document obtain a list of all upostream relationships
        #foreach($h1 in $relDao.getUpstreamDocumentIds($vDoc.document.id))
            #if("$h1" != ""  && ${h1.size()} > 0)
                ## ***Recursively call the macro ***
                #reportMacro($h1)
            #else
                this marks end of recursion do some operations here if needed ... 
            #end
        #end
    #end
}
#end 
Rathan Naik
  • 993
  • 12
  • 25