<?xml version="1.0" encoding="UTF-8"?>
<!--
Sample XSL stylesheet for processing XML files generated by ccollab admin review-xml command.
For simplicity it does not process some nodes and attributes.
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <head>
<style>
* { margin: 0; padding: 5px;}
h2 { background-color: lightblue;}
h3 { background-color: lightseagreen;}
div.section { border: 1px solid lightseagreen; border-radius: 10px 10px 10px 10px; margin-top: 1em;}
table { border-collapse: collapse;}
table, td, th { text-align: left; border: 1px solid lightgray;}
tr:nth-child(even) { background-color: lightcyan;}
</style>
</head>
  <body>
  <h1>Reviews</h1>
  <xsl:apply-templates/>
  </body>
  </html>
</xsl:template>

<xsl:template match="review">
  <div class="review">
  <xsl:apply-templates select="general"/>
  <xsl:apply-templates select="custom-review-fields"/>  
  <xsl:apply-templates select="checklist"/>  
  <xsl:apply-templates select="participants"/>
  <xsl:apply-templates select="participant-custom-fields"/>
  <xsl:apply-templates select="metrics"/>
  <xsl:apply-templates select="artifacts"/>
  <xsl:apply-templates select="defects"/>
  <xsl:apply-templates select="conversations"/>
  </div>
</xsl:template>

<xsl:template match="general">
	<h2>Review #<xsl:value-of select="reviewId"/>: <xsl:value-of select="title"/></h2>
	<xsl:if test="child::*">
	<div class="section general">
	<h3>General Info</h3>
	<table>		
		<tbody>
			<tr><td><b>Title:</b></td><td><xsl:value-of select="title"/></td></tr>
			<tr><td><b>Created:</b></td><td><xsl:value-of select="creationDate"/> by <xsl:value-of select="creator"/> (<xsl:value-of select="creator/@userLogin"/>)</td></tr>
			<tr><td><b>Current phase:</b></td><td><xsl:value-of select="phase"/></td></tr>
			<tr><td><b>Template:</b></td><td><xsl:value-of select="template"/></td></tr>
			<tr><td><b>Group:</b></td><td><xsl:value-of select="group/name"/> (<xsl:value-of select="group/@guid"/>)</td></tr>
		</tbody>
	</table>
  </div>  
</xsl:if>  
</xsl:template>
<xsl:template match="custom-review-fields">
<xsl:if test="child::*">
	<div class="section custom-review-fields">
	<h3>Custom Review Fields</h3>
	<table>		
		<tbody>
			 <xsl:for-each select="child::*">
			 	 <xsl:choose>
					  <xsl:when test="@title">
							<tr><td><b><xsl:value-of select="@title"/>:</b></td><td><xsl:value-of select="."/></td></tr>
					  </xsl:when>
					  <xsl:otherwise>
							<tr><td><b><xsl:value-of select="name(.)"/>:</b></td><td><xsl:value-of select="."/></td></tr>
					  </xsl:otherwise>
				</xsl:choose> 
			 </xsl:for-each>
		</tbody>
	</table>
  </div>
</xsl:if>    
</xsl:template>

<xsl:template match="checklist">
<xsl:if test="child::*">
	<div class="section checklist">
	<h3>Checklist</h3>	
  <table>		
		<tbody>
			 <xsl:for-each select="checklist-item">
			 <tr><td><b><xsl:value-of select="@title"/>:</b></td><td><xsl:value-of select="@value"/></td></tr>
			 </xsl:for-each>
		</tbody>
	</table>	
  </div>
</xsl:if>    
</xsl:template>

<xsl:template match="participants">
<xsl:if test="child::*">
	<div class="section participants">
	<h3>Participants</h3>
	<table>
		<thead>
			<tr>
				<th>ID</th><th>Name</th><th>Role</th><th>State</th><th>Review Pool Group</th><th>Time in Review (man hours)</th><th># of comments</th><th># of defects</th>
			</tr>
		</thead>	
		<tbody>
			 <xsl:for-each select="participant">
			 <tr>
				 <td><xsl:value-of select="@userId"/></td>
				 <td><xsl:value-of select="name"/></td>
				 <td><xsl:value-of select="role"/></td>
				 <td><xsl:value-of select="state"/></td>
				 <td><xsl:value-of select="reviewPoolGroup"/></td>
				 <td><xsl:value-of select="man-hours"/></td>
				 <td><xsl:value-of select="num-comments"/></td>
				 <td><xsl:value-of select="num-defects"/></td>
			 </tr>
			 </xsl:for-each>
		</tbody>
	</table>
  </div>  
</xsl:if>  
</xsl:template>

<xsl:template match="participant-custom-fields">
<xsl:if test="user/child::*">
	<div class="section participant-custom-fields">
	<h3>Participant Custom Fields</h3>
	<xsl:for-each select="user">
		<xsl:if test="child::*">
			<xsl:variable name="currentUserId" select="@userId"/>
			<h4>Custom Fields of <xsl:value-of select="//participant[@userId=$currentUserId]/name"/></h4>
				<table>		
					<tbody>
						 <xsl:for-each select="child::*">
							 <xsl:choose>
								  <xsl:when test="@title">
										<tr><td><b><xsl:value-of select="@title"/>:</b></td><td><xsl:value-of select="."/></td></tr>
								  </xsl:when>
							  <xsl:otherwise>
										<tr><td><b><xsl:value-of select="name(.)"/>:</b></td><td><xsl:value-of select="."/></td></tr>
							  </xsl:otherwise>
							</xsl:choose> 
						 </xsl:for-each>
					</tbody>
				</table>
		</xsl:if>
	</xsl:for-each>	
  </div>  
</xsl:if>  
</xsl:template>

<xsl:template match="metrics">
<xsl:if test="child::*">
	<div class="section metrics">
	<h3>Metrics</h3>
	<table>		
		<tbody>
			 <xsl:for-each select="child::*">
			 <tr><td><b><xsl:value-of select="name(.)"/>:</b></td><td><xsl:value-of select="."/></td></tr>
			 </xsl:for-each>
		</tbody>
	</table>
  </div>  
</xsl:if>  
</xsl:template>

<xsl:template match="artifacts">
<xsl:if test="child::*">
	<div class="section artifacts">
	<h3>Review Materials</h3>
	<table>
		<thead>
			<tr>
				<th>Path</th><th>Change type</th><th>SCM ID</th><th>SCM Type</th><th># of comments</th><th># of defects</th><th>LOC added</th><th>LOC changed</th><th>LOC deleted</th><th>LOC</th><th>MD5</th>
			</tr>
		</thead>	
		<tbody>
			 <xsl:for-each select="artifact">
			 <tr>
				 <td><xsl:value-of select="path"/></td>				 
				 <xsl:choose>					  
					  <xsl:when test="change-type='?'">
							<td>Unknown</td>
					  </xsl:when>
					  <xsl:when test="change-type='A'">
							<td>Added</td>
					  </xsl:when>
					  <xsl:when test="change-type='B'">
							<td>Branched</td>
					  </xsl:when>
					  <xsl:when test="change-type='D'">
							<td>Deleted</td>
					  </xsl:when>
					  <xsl:when test="change-type='I'">
							<td>Integrated</td>
					  </xsl:when>
					  <xsl:when test="change-type='M'">
							<td>Modified</td>
					  </xsl:when>
					  <xsl:when test="change-type='R'">
							<td>Reverted</td>
					  </xsl:when>
					  <xsl:when test="change-type='U'">
							<td>Uploaded</td>
					  </xsl:when>
					  <xsl:otherwise>
							<td><xsl:value-of select="change-type"/></td>
					  </xsl:otherwise>
				</xsl:choose> 				 
    			 <td><xsl:value-of select="scm/@type"/></td>
				 <td><xsl:value-of select="scm/@id"/></td>  	 			 
				 <td><xsl:value-of select="num-comments"/></td>
				 <td><xsl:value-of select="num-defects"/></td>
				 <td><xsl:value-of select="@lines-added"/></td>				 
				 <td><xsl:value-of select="@lines-changed"/></td>
				 <td><xsl:value-of select="@lines-deleted"/></td>
				 <td><xsl:value-of select="@lines"/></td>
				 <td><xsl:value-of select="md5sum"/></td>				 
			 </tr>
			 </xsl:for-each>
		</tbody>
	</table>
  </div>  
</xsl:if>  
</xsl:template>

<xsl:template match="review/defects">
<xsl:if test="child::*">
	<div class="section defects">
	<h3>Defects</h3>	
	<table>
		<thead>
			<tr>
				<th>ID</th><th>Text</th><th>Status</th><th>File</th><th>Line</th><th>Custom Defect Fields</th><th>Creator</th><th>Created on</th>
			</tr>
		</thead>	
		<tbody>
			 <xsl:for-each select="defect">
			 <tr>
				 <td><xsl:value-of select="@defectId"/></td>
				 <td><xsl:value-of select="text"/></td>
				 <td><xsl:value-of select="status"/></td>
				 <td><xsl:value-of select="file"/></td>
				 <td><xsl:value-of select="line"/></td>				 
				 <td>
					<xsl:for-each select="custom-defect-fields/child::*">
						 <xsl:value-of select="@title"/>: <xsl:value-of select="."/>&#160;
					</xsl:for-each>
				 </td>
 				 <td><xsl:value-of select="creator"/></td>
				 <td><xsl:value-of select="creationDate"/></td>
			 </tr>
			 </xsl:for-each>
		</tbody>
	</table>	
  </div>
</xsl:if>    
</xsl:template>

<xsl:template match="review/conversations">
<xsl:if test="child::*">
	<div class="section conversations">
	<h3>Conversations</h3>	
		<xsl:for-each select="conversation">
			<xsl:choose>
				<xsl:when test="@file-version='' and location/@type='Overall'">
					<h4>Overall Review Conversation</h4>
				</xsl:when>
				<xsl:when test="location/@type='Overall'">
					<h4>Overall Conversation for <xsl:value-of select="@file-path"/></h4>
				</xsl:when>
				<xsl:when test="location/@type='Coordinate'">
					<h4>Conversation for <xsl:value-of select="@file-path"/> at Page <xsl:value-of select="location/page"/> on Pin <xsl:value-of select="location/pinNumber"/> [<xsl:value-of select="location/x"/>, <xsl:value-of select="location/y"/>]</h4>
				</xsl:when>
				<xsl:when test="location/@type='Cell'">					
					<h4>Conversation for <xsl:value-of select="@file-path"/> on <xsl:value-of select="location/sheet"/> at Cell <xsl:value-of select="location/column-name"/> <xsl:value-of select="location/row+1"/></h4>
				</xsl:when>
				<xsl:when test="location/@type='Line'">
					<h4>Conversation for <xsl:value-of select="@file-path"/> at Line <xsl:value-of select="location/line-number"/></h4>
				</xsl:when>
				<xsl:when test="location/@type='Label'">
					<h4>Conversation for <xsl:value-of select="@file-path"/> at <xsl:value-of select="location/label"/></h4>
				</xsl:when>
				<xsl:otherwise>
					<h4>Conversation for <xsl:value-of select="@file-path"/></h4>
				</xsl:otherwise>
			</xsl:choose>
			<table>
		<thead>
			<tr>
				<th>ID</th><th>Text</th><th>Creator</th><th>Created on</th>
			</tr>
		</thead>	
		<tbody>
			 <xsl:for-each select="comments/comment">
			 <tr>
				 <td><xsl:value-of select="@id"/></td>
				 <td><xsl:value-of select="."/></td>
				 <xsl:variable name="creatorLogin" select="@creator"/>			
 				 <td><xsl:value-of select="//participant[login=$creatorLogin]/name"/></td>
				 <td><xsl:value-of select="@created-on"/></td>
			 </tr>
			 </xsl:for-each>
		</tbody>
	</table>
		</xsl:for-each>
  </div>
</xsl:if>    
</xsl:template>
</xsl:stylesheet>
