Polarion – Create Work Item with Wiki page

Problem

  • You want to create a polarion work item with a wiki page

Solution

First, if you have no wiki page, create one.

In Polarion you have to create a work item stub in the first step. Subsequent, fill the needed fields and finally save the object.

## Get project id
#set($projectId = $doc.getProject($doc.getSpace()))
## Get project object
#set(pro = $projectService.getProject($tprojectId))
## get trackerproject to create stub
#set($tpro = $trackerService.getTrackerProject($pro))

## Open transaction
$transactionService.beginTx()
## Create stub
#set($newWI = $tpro.createWorkItem(“requirement”))
## Set whatever you want
$newWI.set…
$newWI.set…
$newWI.set…
$newWI.set…
$newWI.setCustomField(…
$newWI.setCustomField(…
$newWI.setCustomField(…
$newWI.setCustomField(…

## Dont forget to save
$newI.save()
## Close transaction
$transactionService.commitTx()

Fin

Advertisements

Polarion – Update Work Item with Wiki page

Problem

  • You want to update a polarion work item with a wiki page
  • (You want to update several work items)

Solution

First, if you have no wiki page, create one.

The following code i use regularly to update a bunch of work items. In this example i update a custom field “test” (type String).

## Get project id
#set($projectId = $doc.getProject($doc.getSpace()))
## Get project object
#set($pro = $projectService.getProject($projectId))
## Get the objects
#set($objects = $trackerService.queryWorkItems($pro, “type:requirement”, “id”)
## Iteration
#foreach($entry in $objects)
## Open Transaction
$transactionService.beginTx()
## Set field
$entry.setCustomField(“test”, $texta)
## Don’t forget to save
$entry.save()
## Transaction commit
$transactionService.commitTx()
#end

Fin

Polarion – Export Work Items as CSV

Problem

  • You want to export objects in csv format

Solution

First, create a classic wiki page.

## Get Project-Id
#set($projectId = $doc.getProject($doc.getSpace()))
## Get Project as object
#set($pro = $projectService.getProject($projectId))
## Query Work Items (here requirements ordered by id)
#set($objects = $trackerService.queryWorkItems($pro, “type:requirement”, “id”))
##Iterate over list
#foreach($entry in $objects)
##Display on page. Seperator is ;
$entry.getId();$entry.getTitle();$entry.getSeverity().getName()\\
#end

Copy the content of the page to Excel or whatever.

Fin.

Collections in Polarion

Problem

  • For some reason you need a java.util.Set (f.i. HashSet) or a simple java.util.List (f.i. ArrayList).
  • In Polarion Classic Wiki Pages you can’t instantiate Java Collections by your own.

Solution

To obtain a collection you have to steal it. Since you can’t write

#set($test = new java.util.ArrayList())

you have to use a trick to get a fresh collection. Some of the Polarion services and objects are returning Java collections. Thus you only have to force them return an empty collection. Below you will find two examples (HashSet and ArrayList) for this solution.

Obtain a clean java.util.HashSet<IWorkItem>

In some cases i need a free of duplicates list by simply using a Set. To get a Set/HashSet I use the method filterWorkItems from IModule. For a clean HashSet I use a senseless lucene query. For a valid instance of a IModule I just search for a WorkItem.

##Example Code for retrieving a workitem
#set($proId = "ExampleProjectID")
#set($wiId = "WorkItemID")
#set($pro = $projectService.getProject($proId))
#set($tpro = $trackerService.getTrackerProject($pro))
#set($WIobject = $tpro.getWorkItem($wiId))

##Getting a HashSet
#set($hs = $WIobject.getModule().filterWorkItems(“status:YYY”))

##Add an item to the HashSet
#set($bla = $hs.add($someWorkItem))

Obtain a clean java.util.ArrayList<IWorkItem>


##Getting an ArrayList
#set($al = $WIobject.getModule().getExternalWorkItems())

Other instances of Collection

If you need Collections with different generic types you should simply use Google (or your prefered search engine).

Display a project tree in Polarion

Problem

  • You want to show a project tree in a “classic” wiki page
  • It has to be user specific. Every user should only see projects he is assigned to (at least read permissions).

The expected result should look like this:
polarion_project_tree

Setting

  • Polarion 3.7+ (tested also on 3.9)

Repository, ProjectGroup, Project

In Polarion you have a file system like project structure. The repository is the root, which can contain projects and projectgroups. In the case of our project tree, the project is the leaf and project groups are the branches in the tree, containing other groups or projects.

Solution

The solution is to run from the root (repository) recursive over all children until we find projects (leafs). Thus I developed a velocity macro showProjectTree containing several other macros:

  • checkPermissionForGroup: Checks if a user can get access to (read) at least one sub projectgroup or the projectgroup itself. If true toDisplay=1, if false toDisplay=1
  • displayContainedProjects: Displays all projects of a specific projectgroup, if the user has read permissions.
  • displayContainedProjectGroup: The recursion. Displays a projectgroup, calling displayContainedProjects and calling itself with all sub projectgroups (if user has read permissions)

The code (please read the inline comments to get the sense of the variables)

#macro (showProjectTree)

  ##Getting current user
  #set($user = $securityService.getCurrentUser())
  ##Variable for the check permission (read)
  #set($projectPermission = $securityService.constructPermission("com.polarion.persistence.object.Project.read"))
  ##The Repository
  #set($root = $projectService.getRootProjectGroup())

  ##Pictures from the default Polarion skin for the tree
  #set($PICTURE_PROJECT_GROUP = "/polarion/ria/images/projectlist/project_group.gif")
  #set($PICTURE_PROJECT_REPO = "/polarion/ria/images/projectlist/repository.gif")
  #set($PICTURE_PROJECT_OPEN = "/polarion/ria/images/projectlist/project_opened.gif")
  
  ##Temp Variable
  #set($toDisplay = 0)
  
  #macro(displayContainedProjectGroup $rootGroup)


<li> <img src=$PICTURE_PROJECT_GROUP>*$rootGroup.getName()*</li>




<ol style="list-style: none;">
    	#foreach($pgx in $rootGroup.getSortedContainedGroups())
    		#checkPermissionForGroup($pgx)
    		#if($toDisplay > 0)
    			#set($toDisplay = 0)
    			#displayContainedProjectGroup($pgx)
    		#else
    			#set($toDisplay = 0)
    		#end
    	#end
    	#displayContainedProjects($rootGroup)
    </ol>


  #end
    
  #macro (checkPermissionForGroup $checkGroup)
    #if(!$checkGroup.getSortedContainedProjects().isEmpty())
    	#foreach($prx in $checkGroup.getSortedContainedProjects())
    		#if($securityService.hasPermission($user,$projectPermission,$prx.getContextId()))
    			#set($toDisplay = 1)
    		#end
    	#end
    #end
    #if($toDisplay < 1)
    	#foreach($pgx in $checkGroup.getSortedContainedGroups())
    		#checkPermissionForGroup($pgx)
    	#end
    #end
  #end
    
  #macro( displayContainedProjects $projectGroup )
    #if(!$projectGroup.getSortedContainedProjects().isEmpty())
  		#foreach($prx in $projectGroup.getSortedContainedProjects())
  			#if($securityService.hasPermission($user,$projectPermission,$prx.getContextId()))


<li><img src=$PICTURE_PROJECT_OPEN> {project: $prx.getId() | display=long}</li>


  			#end
  		#end
    #end
  #end
    


<ol style="list-style: none;">
  	#foreach($goupsUnderRoot in $root.getSortedContainedGroups())


<li><img src=$PICTURE_PROJECT_REPO>*Repository*</li>




<ol style="list-style: none;">
  			#foreach($group in $goupsUnderRoot.getSortedContainedGroups())
  				#checkPermissionForGroup($group)
  				#if($toDisplay > 0)
  					#set($toDisplay = 0)
  					#displayContainedProjectGroup($group)
  				#else
  					#set($toDisplay = 0)
  				#end
  			#end
  			#displayContainedProjects($goupsUnderRoot)
  		</ol>


  	#end
  </ol>


#end

#showProjectTree()