﻿<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:import href="TeamFoundation.xsl" />
  <xsl:output method="html" indent="yes" doctype-public="-//W3C/DTD HTML 3.2 FINAL//EN" />

  <xsl:template name="vstfbuild_statusImage">
    <xsl:param name="status" />
    <xsl:param name="baseUrl" select="/Report/BaseUrl" />
    <img border="0" width="16" height="16">
      <xsl:attribute name="src">
        <xsl:choose>
          <xsl:when test="$status = 'Succeeded'">
            <xsl:value-of select="concat($baseUrl, '/v2.0/images/reportpass.gif')" />
          </xsl:when>
          <xsl:when test="$status = 'Failed'">
            <xsl:value-of select="concat($baseUrl, '/v2.0/images/reportfail.gif')" />
          </xsl:when>
          <xsl:when test="$status = 'Stopped'">
            <xsl:value-of select="concat($baseUrl, '/v2.0/images/abort.gif')" />
          </xsl:when>
          <xsl:when test="$status = 'PartiallySucceeded'">
            <xsl:value-of select="concat($baseUrl, '/v2.0/images/reportpartial.gif')" />
          </xsl:when>
          <xsl:when test="$status = 'InProgress'">
            <xsl:value-of select="concat($baseUrl, '/v2.0/images/reportinprogress.gif')" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="concat($baseUrl, '/v2.0/images/alert.gif')" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
    </img>
  </xsl:template>

  <xsl:template name="vstfbuild_activityTrackingImage">
    <xsl:param name="executionStatus" />
    <xsl:param name="baseUrl" select="/Report/BaseUrl" />
    <xsl:if test="$executionStatus = 'Executing'">
      <img border="0" width="16" height="16">
        <xsl:attribute name="src">
          <xsl:value-of select="concat($baseUrl, '/v2.0/images/reportinprogress.gif')"/>
        </xsl:attribute>
      </img>
    </xsl:if>
  </xsl:template>

  <!-- The main XSL template for the report when an exception occurs during processing. -->
  <xsl:template match="/Exception">
    <xsl:call-template name="style" />
    <xsl:call-template name="Exception">
      <xsl:with-param name="format" select="'html'" />
      <xsl:with-param name="Exception" select="." />
    </xsl:call-template>
  </xsl:template>

  

  <!-- The main XSL template for the report. This drives the overall creation of the web page. -->
  <xsl:template match="/Report">
    <html>
      <head>
        <meta http-equiv="Content-Language" content="en-us"/>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
        <title _locID="Title">Build Details</title>
        <style type="text/css">
          .title {
          }
          .title td {
              color: #666666;
              font-weight: bold;
          }
          div.buildNumber {
              color: #666666;
              font-size: 1.5em;
              font-weight: bold;
          }
          .heading th, div {
              border-bottom: 1px solid #F3F1E6;
              font-weight: bold;
              padding-top: 16 px;
              text-align: left;
              vertical-align: bottom;
          }
          .heading td {
              border-bottom: 1px solid #F3F1E6;
              height: 17px;
              vertical-align: top;
          }
          .content th {
              background-color: #F3F1E6;
              border: 0px;
              font-weight: normal;
              height: 17px;
              padding-left: 4 px;
              padding-top: 0 px;
              text-align: left;
              vertical-align: top;
          }
          .content td {
              border-bottom: 1px solid #F3F1E6;
              border-top: 0px;
              height: 17px;
              padding-left: 4 px;
              vertical-align: top;
          }
          .information {
              font-weight: normal;
              font-size: .8em;
              border-top: 0em;
              border-bottom: 0em;
              margin-top: 0em;
              margin-left: 0em;
              margin-bottom: .1em;
              padding-bottom: 0em;
              padding-top: 0em;
              position: relative;
          }
          .information div.information {
              margin-left: 2em;
              font-size: 1em;
          }
          div.information span {
              font-size: 1em;
              font-weight: bold;
          }
          div.information div.property {
              border-bottom: 0em;
              padding-top: .05em;
              padding-left: 1.5em;
          }
          div.property span {
              font-style: italic;
              font-weight: normal;
          }
        </style>
        <xsl:call-template name="style" />
        <script type="text/javascript" language="javascript">
            function navLink(elem)
            {
            try {
            location.href=elem.innerHTML;
            }
            catch (err) {
              alert("Cannot open file URI from current zone.");
            }
            return false;
          }
        </script>
      </head>
      <body bgcolor="#ffffff" topmargin="10" leftmargin="10" rightmargin="10" bottommargin="10" marginwidth="10" marginheight="10">

        <!-- Format the general information about the build -->
        <xsl:apply-templates select="BuildDetail" />

        <br/>

        <!-- Format the information nodes. -->
        <div class="content">
          <xsl:apply-templates select="BuildDetail/Information/BuildInformationNode" />
        </div>

        <xsl:call-template name="footer">
          <xsl:with-param name="format" select="'html'" />
          <xsl:with-param name="timeZoneOffset" select="TimeZoneOffset" />
          <xsl:with-param name="timeZoneName" select="TimeZoneName" />
        </xsl:call-template>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="BuildDetail">
    <xsl:variable name="controllerUri" select="@BuildControllerUri" />
    <xsl:variable name="definitionUri" select="@BuildDefinitionUri" />

    <div class="buildNumber" _locID="Build">
        Build <xsl:value-of select="@BuildNumber" />
    </div>

    <table class="heading" cellspacing="0" cellpadding="0" width="100%">
      <tr>
        <th width="25%" _locID="Summary">Summary</th>
        <td style="vertical-align: bottom">
          <xsl:call-template name="vstfbuild_statusImage">
            <xsl:with-param name="status" select="@Status" />
          </xsl:call-template>
          <xsl:value-of select="concat(' ', @DisplayStatus)"/>
        </td>
      </tr>
      <tr>
        <td width="20%" _locID="BuildName">Build name:</td>
        <td>
          <xsl:choose>
            <xsl:when test="string-length(@DropLocation) > 0">
              <!-- IE doesn't interpret Unicode in a path very well. This is
                   the work around, which places the link without escaping into
                   an invisible span, then uses that in javascript to nav. -->
              <span id="dllink" style="display:none;">
                <xsl:value-of select="concat('file:///', @DropLocation)" disable-output-escaping="yes"/>
              </span>
              <a onclick="return navLink(document.getElementById('dllink'));">
                <xsl:attribute name="href">
                  <xsl:value-of select="concat('file:///', @DropLocation)" />
                </xsl:attribute>
                <xsl:value-of select="@BuildNumber"/>
              </a>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="@BuildNumber"/>
            </xsl:otherwise>
          </xsl:choose>
        </td>
      </tr>
      <tr>
        <td _locID="RequestedBy">Requested by:</td>
        <td>
          <xsl:value-of select="@RequestedFor" />
        </td>
      </tr>
      <tr>
        <td _locID="TeamProject">Team project:</td>
        <td>
          <xsl:value-of select="../BuildDefinition[@Uri=$definitionUri]/@TeamProject" />
        </td>
      </tr>
      <tr>
        <td _locID="DefinitionName">Definition name:</td>
        <td>
          <xsl:value-of select="../BuildDefinition[@Uri=$definitionUri]/@Name" />
        </td>
      </tr>
      <tr>
        <td _locID="ControllerName">Controller name:</td>
        <td>
          <xsl:value-of select="../BuildController[@Uri=$controllerUri]/@Name" />
        </td>
      </tr>
      <tr>
        <td _locID="ProcessParameters">Process parameters:</td>
        <td>
          <xsl:apply-templates select="ProcessParameters" />
        </td>
      </tr>
      <tr>
        <td _locID="StartedOn">Started on:</td>
        <td>
          <xsl:value-of select="@StartTime" />
        </td>
      </tr>
      <tr>
        <td _locID="CompletedOn">Completed on:</td>
        <td>
          <xsl:value-of select="@FinishTime" />
        </td>
      </tr>
      <tr>
        <td _locID="LastChangedBy">Last changed by:</td>
        <td>
          <xsl:value-of select="@LastChangedBy" />
        </td>
      </tr>
      <tr>
        <td _locID="LastChangedOn">Last changed on:</td>
        <td>
          <xsl:value-of select="@LastChangedOn" />
        </td>
      </tr>
      <tr>
        <td _locID="Quality">Quality:</td>
        <td>
          <xsl:value-of select="@Quality"/>
        </td>
      </tr>
      <tr>
        <td _locID="WorkItemsOpened">Work items opened:</td>
        <xsl:choose>
          <xsl:when test="count(Information//BuildInformationNode[@Type='OpenedWorkItem']) > 0">
            <td>
              <xsl:choose>
                <xsl:when test="Information//BuildInformationNode[@Type='OpenedWorkItem']/InformationField[Name='ExternalUrl']/Value">
                  <a>
                    <xsl:attribute name="href">
                      <xsl:value-of select="Information//BuildInformationNode[@Type='OpenedWorkItem']/InformationField[Name='ExternalUrl']/Value"/>
                    </xsl:attribute>
                    <xsl:value-of select="Information//BuildInformationNode[@Type='OpenedWorkItem']/InformationField[Name='WorkItemId']/Value"/>
                  </a>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="Information//BuildInformationNode[@Type='OpenedWorkItem']/InformationField[Name='WorkItemId']/Value"/>
                </xsl:otherwise>
              </xsl:choose>
            </td>
          </xsl:when>
          <xsl:otherwise>
            <td _locID="NotAvailable">Not available</td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
      <tr>
        <td _locID="SourceControlVersion">Source control version:</td>
        <td>
          <xsl:value-of select="@SourceGetVersion" />
        </td>
      </tr>
      <tr>
        <td _locID="ShelvesetName">Shelveset name:</td>
        <td>
          <xsl:value-of select="@ShelvesetName" />
        </td>
      </tr>
      <tr>
        <td _locID="Log">Log:</td>
        <td>
          <span id="lllink" style="display:none;">
            <xsl:choose>
              <xsl:when test="contains(@LogLocation, '://')">
                <xsl:value-of select="@LogLocation" disable-output-escaping="yes" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="concat('file:///', @LogLocation)" disable-output-escaping="yes" />
              </xsl:otherwise>
            </xsl:choose>
          </span>
          <a onclick="return navLink(document.getElementById('lllink'));">
            <xsl:attribute name="href">
              <xsl:choose>
                <xsl:when test="contains(@LogLocation, '://')">
                  <xsl:value-of select="@LogLocation" />
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="concat('file:///', @LogLocation)" />
                </xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:value-of select="@LogLocation" />  
          </a>
        </td>
      </tr>
    </table>
  </xsl:template>

  <xsl:template match="ProcessParameters">
    <xsl:for-each select="//*[local-name() = 'Dictionary' and namespace-uri() = 'clr-namespace:System.Collections.Generic;assembly=mscorlib']/*">
      <xsl:value-of select="@*[local-name() = 'Key']" />: <xsl:value-of select="text()" /><br />
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="BuildInformationNode[@Type='ActivityTracking']" name="vstfbuild_activityTrackingNode">
    <div class="information">
      <xsl:call-template name="vstfbuild_activityTrackingImage">
        <xsl:with-param name="executionStatus" select="InformationField[Name='ExecutionStatus']/Value" />
      </xsl:call-template>

      <span _locID="Activity">
        <xsl:value-of select="InformationField[Name='DisplayText']/Value"/>
      </span>

      <xsl:for-each select="BuildInformationNode[@Type='ActivityProperties'][last()]">
        <xsl:for-each select="InformationField[Value!='']">
          <xsl:sort select="Name"/>
          <div class="property">
            <span>
              <xsl:value-of select="Name" />: <xsl:value-of select="Value" />
            </span>
          </div>
        </xsl:for-each>
      </xsl:for-each>
      <xsl:apply-templates select="BuildInformationNode[@Type!='ActivityProperties']"/>
    </div>
  </xsl:template>

  <xsl:template match="BuildInformationNode[@Type='AgentScopeActivityTracking']" name="vstfbuild_agentScopeActivityTrackingNode">
    <div class="information">
      <xsl:call-template name="vstfbuild_activityTrackingImage">
        <xsl:with-param name="executionStatus" select="InformationField[Name='ExecutionStatus']/Value" />
      </xsl:call-template>

      <span _locID="AgentScopeActivity">
          <xsl:value-of select="InformationField[Name='DisplayText']/Value"/>
      </span>

      <div class="property">
        <span _locID="ReservationStatus">
            Reservation Status: <xsl:value-of select="InformationField[Name='ReservationStatus']/Value"/>
        </span>
      </div>
      <div class="property">
        <span _locID="ReservedAgent">
            Reserved Agent: <xsl:value-of select="InformationField[Name='ReservedAgentName']/Value"/> (<xsl:value-of select="InformationField[Name='ReservedAgentUri']/Value"/>)
        </span>
      </div>

      <xsl:for-each select="BuildInformationNode[@Type='ActivityProperties'][last()]">
        <xsl:for-each select="InformationField[Value!='']">
          <xsl:sort select="Name"/>
          <div class="property">
            <span>
              <xsl:value-of select="Name" />: <xsl:value-of select="Value" />
            </span>
          </div>
        </xsl:for-each>
      </xsl:for-each>
      <xsl:apply-templates select="BuildInformationNode[@Type!='ActivityProperties']"/>
    </div>
  </xsl:template>

  <xsl:template match="BuildInformationNode[@Type='BuildMessage']" name="vstfbuild_messageNode">
    <div class="information" _locID="BuildMessage">
      <xsl:value-of select="InformationField[Name='Message']/Value"/>
      <xsl:apply-templates select="BuildInformationNode[@Type!='ActivityProperties']"/>
    </div>
  </xsl:template>

  <xsl:template match="BuildInformationNode[@Type='BuildError']" name="vstfbuild_errorNode">
    <div class="information" _locID="BuildError">
      <xsl:call-template name="vstfbuild_statusImage">
        <xsl:with-param name="status" select="'Failed'" />
      </xsl:call-template>
      <b>Error</b>: <xsl:value-of select="InformationField[Name='File']/Value"/>(<xsl:value-of select="InformationField[Name='LineNumber']/Value"/>): <xsl:value-of select="InformationField[Name='Code']/Value"/>: <xsl:value-of select="InformationField[Name='Message']/Value"/>
      <xsl:apply-templates select="BuildInformationNode[@Type!='ActivityProperties']"/>
    </div>
  </xsl:template>

  <xsl:template match="BuildInformationNode[@Type='BuildWarning']" name="vstfbuild_warningNode">
    <div class="information" _locID="BuildWarning">
      <xsl:call-template name="vstfbuild_statusImage">
        <xsl:with-param name="status" select="'Warning'" />
      </xsl:call-template>
      <b>Warning</b>: <xsl:value-of select="InformationField[Name='File']/Value"/>(<xsl:value-of select="InformationField[Name='LineNumber']/Value"/>): <xsl:value-of select="InformationField[Name='Code']/Value"/>: <xsl:value-of select="InformationField[Name='Message']/Value"/>
      <xsl:apply-templates select="BuildInformationNode[@Type!='ActivityProperties']"/>
    </div>
  </xsl:template>

  <xsl:template match="BuildInformationNode[@Type='BuildProject']" name="vstfbuild_buildProjectNode">
    <div class="information">
      <span _locID="BuildingProject">
        <xsl:choose>
          <xsl:when test="string-length(InformationField[Name='ServerPath']/Value) > 0">
            Built <xsl:value-of select="InformationField[Name='ServerPath']/Value"/>
          </xsl:when>
          <xsl:otherwise>
            Built <xsl:value-of select="InformationField[Name='LocalPath']/Value"/>
          </xsl:otherwise>
        </xsl:choose>
      </span>
      <xsl:apply-templates select="BuildInformationNode[@Type!='ActivityProperties']"/>
    </div>
  </xsl:template>

  <xsl:template match="BuildInformationNode" name="vstfbuild_unknownNode" />
</xsl:stylesheet>
