<!--                                                                  -->
<!--  Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved     -->
<!--  $Id$  -->
<!--                                                                  -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

	<xsl:template match="Root">
		<xsl:call-template name="jobsInQueueSection" />
		<xsl:call-template name="jobsRunningSection" />
		<xsl:call-template name="jobsFinishedSection" />
		<br />
	</xsl:template>
	
	<xsl:template name="jobsInQueueSection">
		<xsl:variable name="jobsInQueue" select="JobQueue/Jobs[StartDate = '0000-00-00 00:00:00']" />
		<fieldset>
			<legend>In Queue</legend>
			<xsl:choose>
				<xsl:when test="$jobsInQueue">
					<table class="textChart">
						<tr>
							<th>Priority</th>
							<th>Job ID</th>
							<th>Queued</th>
							<th>Host</th>
							<th>Client</th>
							<th>Class</th>
							<th>Subclass</th>
                            <th>On Hold</th>
						</tr>
						<xsl:for-each select="$jobsInQueue">
							<xsl:sort select="Priority" />
							<xsl:call-template name="jobInQueueRow" />
						</xsl:for-each>
					</table>
				</xsl:when>
				<xsl:otherwise>
					<p class="emptyMessage">No jobs are currently in the queue.</p>
					<br />
				</xsl:otherwise>
			</xsl:choose>
		</fieldset>
		<br />
	</xsl:template>

	<xsl:template name="jobInQueueRow">
		<tr class="row{position() mod 2}">
			<td><xsl:value-of select="Priority" /></td>
			<td><xsl:value-of select="JobID" /></td>
			<td><xsl:value-of select="QueueDate" /></td>
			<td><xsl:value-of select="HostName" /></td>
            <td>
				<xsl:call-template name="clientIDToName">
					<xsl:with-param name="clientID" select="ClientID" />
				</xsl:call-template>
            </td>
			<td><xsl:value-of select="Class" /></td>
			<td><xsl:value-of select="Subclass" /></td>
		    <xsl:if test="OnHold = 1"><td class="error">ON HOLD</td></xsl:if>
		    <xsl:if test="OnHold = 0"><td> </td></xsl:if>
		</tr>
	</xsl:template>
	
	<xsl:template name="jobsRunningSection">
		<xsl:variable name="jobsRunning" select="JobQueue/Jobs[StartDate != '0000-00-00 00:00:00' and EndDate = '0000-00-00 00:00:00']" />
		<fieldset>
			<legend>Running</legend>
			<xsl:choose>
				<xsl:when test="$jobsRunning">
					<table class="textChart">
						<tr>
							<th>Job ID</th>
							<th>Host</th>
							<th>Client</th>
							<th>Class</th>
							<th>Subclass</th>
							<th>Started</th>
							<th>Last Heartbeat</th>
						</tr>
						<xsl:for-each select="$jobsRunning">
							<xsl:call-template name="jobRunningRow" />
						</xsl:for-each>
					</table>
				</xsl:when>
				<xsl:otherwise>
					<p class="emptyMessage">No jobs are currently running.</p>
					<br />
				</xsl:otherwise>
			</xsl:choose>
		</fieldset>
		<br />
	</xsl:template>
	
	<xsl:template name="jobRunningRow">
		<tr class="row{position() mod 2}">
			<td><xsl:value-of select="JobID" /></td>
			<td><xsl:value-of select="HostName" /></td>
            <td>
				<xsl:call-template name="clientIDToName">
					<xsl:with-param name="clientID" select="ClientID" />
				</xsl:call-template>
            </td>
			<td><xsl:value-of select="Class" /></td>
			<td><xsl:value-of select="Subclass" /></td>
			<td><xsl:value-of select="StartDate" /></td>
			<td><xsl:value-of select="Heartbeat" /></td>
		</tr>
	</xsl:template>
	
	<xsl:template name="jobsFinishedSection">
		<xsl:variable name="jobsFinished" select="JobQueue/Jobs[EndDate != '0000-00-00 00:00:00']" />
		<fieldset>
			<legend>Completed</legend>
			<xsl:call-template name="jobsFinishedFilter" />
			<xsl:choose>
				<xsl:when test="$jobsFinished">
					<table class="textChart">
						<tr>
							<th>Job ID</th>
							<th>End Date</th>
							<th>Host</th>
							<th>Client</th>
							<th>Class</th>
							<th>Subclass</th>
							<th>Exit Code</th>
							<xsl:if test="Access/admin/joblog/read = 1">
								<th>Log</th>
							</xsl:if>
						</tr>
						<xsl:for-each select="$jobsFinished">
							<xsl:sort select="JobID" data-type="number" order="descending" />
							<xsl:call-template name="jobFinishedRow" />
						</xsl:for-each>
					</table>
				</xsl:when>
				<xsl:otherwise>
					<p class="emptyMessage">No jobs have been completed in the selected time frame.</p>
					<br />
				</xsl:otherwise>
			</xsl:choose>
		</fieldset>
		<br />
	</xsl:template>	
	
	<xsl:template name="jobFinishedRow">
		<tr class="row{position() mod 2}">
			<td><xsl:value-of select="JobID" /></td>
			<td><xsl:value-of select="EndDate" /></td>
			<td><xsl:value-of select="HostName" /></td>
            <td>
				<xsl:call-template name="clientIDToName">
					<xsl:with-param name="clientID" select="ClientID" />
				</xsl:call-template>
            </td>
			<td><xsl:value-of select="Class" /></td>
			<td><xsl:value-of select="Subclass" /></td>
			<td><xsl:value-of select="ExitCode" /></td>
			<xsl:if test="/Root/Access/admin/joblog/read = 1">
				<td>
					<a href="https://{Log/HostName}.royaltyshare.com/admin/joblog?c=read&amp;page=1&amp;log_id={Log/JobLogID}">View</a>
				</td>
			</xsl:if>
		</tr>
	</xsl:template>
	
	<xsl:template name="jobsFinishedFilter">
		<form name="showCompleted" id="showCompleted" action="/admin/jobqueue" method="get">
			Show only jobs completed in the last <select name="age" id="age">
				<option value="24">
					<xsl:if test="Params/age = 24">
						<xsl:attribute name="selected">selected</xsl:attribute>
					</xsl:if>
					Day
				</option>
				<option value="168">
					<xsl:if test="Params/age = 168">
						<xsl:attribute name="selected">selected</xsl:attribute>
					</xsl:if>
					Week
				</option>
				<option value="720">
					<xsl:if test="Params/age = 720">
						<xsl:attribute name="selected">selected</xsl:attribute>
					</xsl:if>
					Month
				</option>
				<option value="8760">
					<xsl:if test="Params/age = 8760">
						<xsl:attribute name="selected">selected</xsl:attribute>
					</xsl:if>
					Year
				</option>
			</select><xsl:text> </xsl:text><input type="submit" value="Update" />
		</form>	
		<br />
	</xsl:template>
	
	<xsl:template name="statusTranslator">
		<xsl:param name="statusCode" />
		<xsl:choose>
			<xsl:when test="$statusCode = 'DONE'">Completed</xsl:when>
			<xsl:when test="$statusCode = 'ABRT'">Aborted</xsl:when>
			<xsl:when test="$statusCode = 'ERR'">Error</xsl:when>
			<xsl:when test="$statusCode = 'CNLD'">Cancelled</xsl:when>
			<xsl:when test="$statusCode = 'DEAD'">Deleted</xsl:when>
			<xsl:otherwise><xsl:value-of select="$statusCode" /></xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="clientIDToName">
		<xsl:param name="clientID" />
        <xsl:value-of select="/Root/ClientList/Client[ClientID = $clientID]/ClientName" />
	</xsl:template>

</xsl:stylesheet> 
