Two Items Template

This tutorial is targeting Add On version 5.1.3.

Requirements:

You have completed the Duration Template, Period Template and Hello World tutorial.

You have a basic knowledge about Java, Velocity Template Language (VTL), html and the Jira java API.

For javadoc references, see http://jplugs.bitbucket.org/ttb/5.1.3/

Result

We will create a template that will display total time logged for each user with logged worklogs (or users specified in the gadget configuration screen) arranged for each project.

Logged time will be displayed in intervals like day, week, month or year.

 

Tutorial

Like in the other tutorials, obtain the items (Projects and users) from TimeTrackingItemHolder class.

(See http://jplugs.bitbucket.org/ttb/5.1.3/com/ja/jira/plugin/ttb/model/worklog/package-summary.html)

Like

#set( $item = $itemHolder.getItemWorklog())
#set( $users = $itemHolder.getItemUser())
#set( $projects = $itemHolder.getItemProject())
#set($periods = $item.getTimeSpentRollOnDateUnit($calendarConstant))

 

$iprojects.getItems() returns a map of items, where the map values are the unique items - in this case Project objects.

Use the foreach statement to iterate over the map values.

Add a inner loop that iterates over Users and an inner inner loop that iterates over the Period objects.

Like

#foreach($project in $projects.getItems())
     $project.getName()
 
     ## Use this later as a holder for worklogs
     #set($mapWorklogs = $tools.getMap())
 
     #foreach($user in $users)
         ## Use this to add timespent for each user
         #set($timeSpentSum = 0)
         #foreach ($period in $periods)
             ## get the worklogs for this period and project. Store it in the map so it can be reused for each user
             #if(!$mapWorklogs.containsKey($period) && $mapWorklogs.put($period,$period.getWorklogs($project))) #end
             
             ## get the user time for the collection of worklogs.
             #set( $usersPeriod = $itemHolder.getItemUser($mapWorklogs.get($period)))
             
             ## the time spent for this user in this period for this user
             #set($timeSpent = $usersPeriod.getTimeSpent($user))
 
             ## Add to the $timeSpentSum to get the total time spent for this user for this project (for all periods).
             #set($timeSpentSum = $tools.getSum($timeSpentSum,$timeSpent))
         #end
     #end
#end

 

Putting it all together with a little mark up as well.

 

#set( $item = $itemHolder.getItemWorklog())
#set( $users = $itemHolder.getItemUser())
#set( $projects = $itemHolder.getItemProject())
#set($periods = $item.getTimeSpentRollOnDateUnit($calendarConstant))
<table class="grid" style="width:100%">
	<tr class="rowAlternate">
		<td><b>$i18n.getText("common.words.user")</b></td>
		#foreach ($period in $periods)
			<td><b>$tools.getCalendarConstantPretty($calendarConstant,$period.getStartDate())</b></td>
			#set( $colspan = $velocityCount+1)
		#end
		<td><b>$i18n.getText("common.words.total")</b></td>
	</tr>
	##Iterate over all available projects
	#foreach ($project in $projects.getItems())
		<tr #if ($velocityCount % 2 == 1) class="rowNormal" #else class="rowAlternate" #end >
			<td colspan="$colspan"><a href="$baseurl/browse/$project.getKey()">$project.getName()</a> <small>($project.getKey())</small></td>
			<td class="ttbright" nowrap><b>$tools.getTimePretty($projects.getTimeSpent($project))</b></td>
		</tr>
		#set($mapWorklogs = $tools.getMap())
		## Innerloop: Iterate over all available users
		#foreach ($user in $users.getItems())
			##holder for timespent sum for the user
			#set($timeSpentSum = 0)
			<tr #if ($velocityCount % 2 == 1) class="rowNormal" #else class="rowAlternate" #end >
				<td width="1%" nowrap>$user.getDisplayName() <small>($user.getName())</small>:</td>
				## Inner-Innerloop: Iterate over all "periods"
				#foreach ($period in $periods)
					## Get all worklogs for this project in this period, and then make a new itemUser object that only contains the periods worklogs
					#if(!$mapWorklogs.containsKey($period) && $mapWorklogs.put($period,$period.getWorklogs($project))) #end
					#set( $usersPeriod = $itemHolder.getItemUser($mapWorklogs.get($period)))
					#set($timeSpent = $usersPeriod.getTimeSpent($user))
					<td class="ttbright" nowrap>$tools.getTimePretty($timeSpent)</td>
					#set($timeSpentSum = $tools.getSum($timeSpentSum,$timeSpent))
				#end
				<td class="ttbright" nowrap><b>$tools.getTimePretty($timeSpentSum)</b></td>
			</tr>
		#end
	#end
	<tr class="rowNormal">
		<td class="ttbright"><b>$i18n.getText("common.words.total"):</b></td>
		#foreach ($period in $periods)
			<td class="ttbright" nowrap><b>$tools.getTimePretty($period.getSumTimeSpent())</b></td>
		#end
		<td class="ttbright"><b>$tools.getTimePretty($itemHolder.getItemWorklog().getTimeSpent())</b></td>
	</tr>
</table>