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()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s