<?xml version="1.0" encoding="utf-8"?>
<!--

Copyright (c) 2002,2003 The Regenstrief Institute.  All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Written by Gunther Schadow.

$Id: spextract-html.xsl,v 1.1.1.1 2005/02/17 00:43:51 gschadow Exp $

-->
<xsl:transform version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:exclude-result-prefixes="xsl">

  <xsl:import href="xml-verbatim.xsl"/>
  <xsl:output method="xhtml" media-type="text/html" encoding="ascii"/>
  <xsl:strip-space elements="*"/>

  <xsl:param name="prog" select="'spexload-db'"/>
  <xsl:param name="show-original" select="'no'"/>
  <xsl:param name="batch" select="'no'"/>
  <xsl:param name="privacy" select="/.."/>

  <!-- deep null transform -->
  <xsl:template match="/|@*|node()">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:template>

  <!-- for ICD-O code review we need to use this -->
  <xsl:variable name="icdo2" select="document('icdo2.xml')"/>

  <xsl:template match="/*[spextract|text_report]">
    <html>
      <head>
        <style type="text/css">
	  <xsl:text>
@media print		  { font-size: 8pt; }
@media screen		  { font-size: normal; }
.xmlverb-default          { color: #333333; background-color: #ffffff;
                            font-family: serif; }
.xmlverb-element-name     { color: #990000; }
.xmlverb-element-nsprefix { color: #666600; }
.xmlverb-attr-name        { color: #660000; }
.xmlverb-attr-content     { color: #000099; font-weight: bold; }
.xmlverb-ns-name          { color: #666600; }
.xmlverb-ns-uri           { color: #330099; }
.xmlverb-text             { color: #000000; font-weight: bold; }
.xmlverb-comment          { color: #006600; font-style: italic; }
.xmlverb-pi-name          { color: #006600; font-style: italic; }
.xmlverb-pi-content       { color: #006666; font-style: italic; }
a			  { text-decoration: none; color: #000000; }
.ttext			  { font-family: monospace; 
			    word-wrap: break-word; }
.ttext-title		  { font-weight: bold; }
.ttext-verbosity	  { font-style: italic; }
.ttext-signature	  { font-style: italic; }
.skin			  { position: absolute;
			    width: 165px;
			    border-style: solid solid solid solid;
			    border-width: 1 1 1 1;
			    background-color: menu;
			    cursor: default;
			    font-family: sans-serif;
			    font-size: 80%;
			    z-index: 100;
			    visibility: hidden;
			  }
.skin hr		  { border-width: 1 0 0 0;
                            border-style: solid none none none; 
			    border-color: gray; }
.menuitem		  { padding: 1px, 10px, 1px, 10px; }
.control		  { font-family: sans-serif;
			    font-size: 90%;
			  }
td.marked		  { width: 650; }
td.formal		  { width: 450; vertical-align: middle; }  
table	                  { 
			    border-collapse: collapse; 
			    empty-cells: show; 
			    border-style: none none none none;
			    border-width: 0 0 0 0; }
tr			  { vertical-align: top; }
td			  { border-style: solid dotted solid dotted;
			    border-width: 1 1 1 1; }
hr			  { border-width: 1 0 0 0;
                            border-style: dotted none none none; 
			    border-color: gray; }
.false			  { text-decoration: underline; }
.false-harmless		  { text-decoration: underline; }
.data			  { z-index: 100;
			    visibility: hidden; }
</xsl:text>
<!-- table width { width: 1000; -->
	  <xsl:for-each 
	       select="$property-map/prop[(@element) and (@color)]">
	    <xsl:text>.</xsl:text>
	    <xsl:value-of select="@element"/>
	    <xsl:text>	  { background-color: </xsl:text>
	    <xsl:value-of select="@color"/>
	    <xsl:text>; }
</xsl:text>
          </xsl:for-each>
	</style>
        <script type="text/javascript" src="spextract.js"/>
      </head>
      <body>
        <xsl:apply-templates select="@*|node()"/>
	<xsl:if test="not($batch='yes')">
	  <textarea id="callMonitor" cols="136" rows="10"></textarea>
	  <div id="menu" class="skin" 
	      onmouseover="menuItemEnter(event)" 
	      onmouseout="menuItemLeave(event)" 
	      onclick="menuItemSelect(event)">
	    <div class="menuitem" item="false">Major False Mark</div>
	    <div class="menuitem" item="false-harmless">Minor False Mark</div>
	    <hr/>
	    <div class="menuitem" item="specimen">Specimen</div>
	    <div class="menuitem" item="tissue-type">Tissue Type</div>
	    <div class="menuitem" item="site-modifier">Site Modifier</div>
	    <div class="menuitem" 
			  item="collection-method">Collection Method</div>
	    <div class="menuitem" item="observation">Observation</div>
	    <div class="menuitem" item="ttext-title">Title</div>
	    <div class="menuitem" item="code">Observation Code</div>
	    <div class="menuitem" item="value">Observation Value</div>
	    <hr/>
	    <div class="menuitem" item="missing-code">Missing Code</div>
	    <div class="menuitem" item="spurious-code">Spurious Code</div>
	    <div class="menuitem" item="descriptive-normal">Descriptive Normal</div>
	    <hr/>
	    <div class="menuitem" item="unmark">Unmark</div>
	  </div>
	  <script type="text/javascript">init();</script>
	</xsl:if>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="text()[$privacy]" priority="1"/>
  <xsl:template match="text()[not($privacy) or matches(ancestor::text/@title,'\b(?i:gross|micro|diagnos)')]" priority="2">
    <xsl:sequence select="."/>
  </xsl:template>

  <!-- don't put issues/text into the output -->
  <xsl:template match="issues"/>

  <xsl:variable name="rating-scale">
    <rating value="excellent" accesskey="E">
      <label><u>e</u>xcellent</label>
      <definition>All interesting finidings are coded. If the UMLS
does not contain some (even benign) concepts mentioned, it is not 
excellent.</definition>
    </rating>
    <rating value="good" accesskey="G">
      <label><u>g</u>ood</label>
      <definition>All critical finidings are coded. Minor or benign
descriptive concepts need not be coded.</definition>
    </rating>
    <rating value="sufficient" accesskey="U">
      <label>s<u>u</u>fficient</label>
      <definition>Most critical finidings are coded. Some
critical findings may not be coded if they are represented by other
findings that are coded. This includes if one specimen part among
others has been missed.</definition>
    </rating>
    <rating value="defective" accesskey="D">
      <label><u>d</u>efective</label>
      <definition>Major findings are not coded or negations not
detected.</definition>
    </rating>
    <rating value="excluded" accesskey="X">
      <label>e<u>x</u>cluded</label>
      <definition>Not rated because the report does not contain
anything that the system is designed to detect. This includes
diagnoses mentioned without a specimen.</definition>
    </rating>
  </xsl:variable>

  <xsl:template match="spextract|text_report">
    <xsl:param name="current-spextract" select="."/>
    <xsl:if test="not($batch='yes')">
      <xml class="data" id="xmlData" cols="0" rows="0">
        <xsl:sequence select="."/>
      </xml>
    </xsl:if>
    <xsl:variable name="controls">
      <xsl:if test="not($batch='yes') and @id">
        <xsl:variable name="id" select="@id cast as xs:integer"/>
	<p class="control">Coding is
	  <xsl:for-each select="$rating-scale/rating">
	    <input name="rating-{$current-spextract/@id}" 
		   type="radio" class="control">
	      <xsl:sequence select="@value|@accesskey"/>
	      <xsl:if test="$current-spextract/rating/@rating = @value">
		<xsl:attribute name="checked"/>
	      </xsl:if>
	    </input>
	    <a title="{normalize-space(definition/text())}">
	      <xsl:sequence select="label/node()"/>
	    </a>
	  </xsl:for-each>. Comment: 
	  <input id="comment-{@id}" type="text" class="control" 
	      size="55" value="{$current-spextract/rating/@comment}"/>
	  <input type="button" class="control" 
	      value=" &lt;&lt; " accesskey="B" 
	      onclick="goTo('{$prog}?ref={$id - 10}')"/>
	  <input type="button" class="control" 
	      value=" &lt; " accesskey="P" 
	      onclick="goTo('{$prog}?ref={$id - 1}')"/>
	  <input type="button" class="control" 
	      value="Save" accesskey="S" spextractId="{@id}" 
	      onclick="doItem(selection.createRange,this,'save')"/>
	  <input type="button" class="control" 
	      value=" &gt; " accesskey="N" 
	      onclick="goTo('{$prog}?ref={$id + 1}')"/>
	  <input type="button" class="control" 
	      value=" &gt;&gt; " accesskey="F" 
	      onclick="goTo('{$prog}?ref={$id + 10}')"/>
	  <input type="button" class="control" 
	      value="Recode" accesskey="R" 
	      onclick="goTo('spextract-db?ref={$id}')"/>
	</p>
      </xsl:if>
    </xsl:variable>
    <xsl:sequence select="$controls"/>
    <xsl:if test="@site or @morphology">
      <p>
        <xsl:value-of select="position()"/>
	<xsl:text>-</xsl:text>
        <xsl:value-of select="@position"/>
	<xsl:text>-</xsl:text>
        <xsl:value-of select="@institutionId"/>
	<xsl:text>-</xsl:text>
        <xsl:value-of select="@physiologicTime"/>
	<xsl:text> </xsl:text>
	<xsl:value-of select="@site"/>
	<xsl:text> </xsl:text>
	<xsl:value-of select="$icdo2//concept[@code2=current()/@site]/@displayName"/>
        <xsl:text> (</xsl:text>
	<xsl:value-of select="@laterality"/>
        <xsl:text>) </xsl:text>
	<xsl:value-of select="@morphology"/>
	<xsl:text> </xsl:text>
	<xsl:value-of select="$icdo2//concept[@code2=current()/@morphology]/@displayName"/>
      </p>
    </xsl:if>
    <table id="tab-{@id}" spextractId="{@id}">
      <tr>
        <xsl:if test="$show-original='yes'">
	  <td class="orig">
	    <pre class="ttext">
	      <xsl:sequence select="text/node()"/>      
	    </pre>
	  </td>
	</xsl:if>
	<td class="marked" oncontextmenu="menuShow">
	  <pre class="ttext">
	    <xsl:apply-templates select="node() except text"/>    
	  </pre>
	</td>
	<td class="formal">
	  <xsl:variable name="formal-data">
            <xsl:apply-templates mode="formal" select="*"/>
	  </xsl:variable>
	  <div class="formal">
	    <xsl:apply-templates mode="xmlverb" select="$formal-data"/>
          </div>
	</td>
      </tr>
    </table>
  </xsl:template>

  <xsl:template match="hr|br">
    <xsl:copy/>
  </xsl:template>

  <xsl:template match="p">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text">
    <xsl:param name="no">
    <xsl:number count="text"/>
    </xsl:param>
    <br/>
    <a id="sect-{$no}">
      <span class="ttext-title">
        <xsl:value-of select="(@title|title)[1]"/>
      </span>
      <xsl:text>: </xsl:text>
      <xsl:apply-templates select="* except subject"/>
    </a>
  </xsl:template>

  <xsl:template match="verbosity">
    <span class="ttext-verbosity">
      <xsl:value-of select="@text"/>
      <xsl:text> </xsl:text><br/>
    </span>
  </xsl:template>

  <xsl:template match="signature">
    <span class="ttext-signature">
      <xsl:value-of select="@text"/>
    </span>
  </xsl:template>

  <xsl:variable name="property-map">
      <prop element="specimen" color="#D0D0D0" mark="SP"/>
      <prop element="tissue-type" color="#00F000" mark="tt"/>
      <prop element="site-modifier" color="#F0F000" mark="sm"/>
      <prop element="collection-method" color="#00F0F0" mark="cp"/>
      <prop element="observation" color="#C080F0" mark="obx"/>
      <prop element="code" color="#F000F0" mark="cd"/>
      <prop element="value" color="#F0F080" mark="val"/>
      <prop element="negationInd" color="#FF0000" mark="neg"/>
      <prop element="uncertaintyInd" color="#FFE0E0" mark="unc"/>
      <prop element="suspicionInd" color="#FF8080" mark="susp"/>  
      <prop element="missing-code" color="#F0F0F0" mark="mc"/>
      <prop element="descriptive-normal" color="#E0E0E0" mark="dn"/>
      <prop element="spurious-code" color="#F0F0F0" mark="mc"/>
      <prop element="false" color="#F0F0F0" mark="mc"/>
      <prop element="false-harmless" color="#E0E0E0" mark="mc"/>
      <prop color="#00A0A0"/>
  </xsl:variable>

  <xsl:template match="a[@id]">
    <xsl:variable name="target" 
      select="ancestor-or-self::text/subject//*[@ref=current()/@id]"/>
    <xsl:variable name="prop"
      select="$property-map/prop[@element=local-name($target) 
			      or not(@element)][1]"/>
    <xsl:if test="not($prop)">
      <xsl:message>
	<WARNING text="no property for" 
	  element="{local-name($target)}" id="{@id}"/>
      </xsl:message>
    </xsl:if>
    <xsl:choose>
      <xsl:when test="$target[(self::observation and observation)
	                      or (self::observation and not(value))]">
        <a nohref="#{@id}" ref="{@id}" id="txt-{@id}">
<!--	     onmouseover="document.getElementById('{@id}').style.textDecoration='underline';return true;"
	     onmouseout="document.getElementById('{@id}').style.textDecoration='none';return true;" -->
	  <xsl:attribute name="title">
	    <xsl:apply-templates mode="tooltip-text" select="$target"/>
	  </xsl:attribute>
          <xsl:text>[*</xsl:text>
	  <sup><xsl:value-of select="$prop/@mark"/></sup>
	  <xsl:apply-templates select="@*|node()"/>
          <xsl:text>]</xsl:text>
	  <xsl:sequence select="$prop/after/node()"/>
        </a>
      </xsl:when>
      <xsl:when test="$target">
	<span class="{local-name($target)}"> 
	  <xsl:text>[</xsl:text>
	  <sup><xsl:value-of select="$prop/@mark"/></sup>
	  <a nohref="#{@id}" ref="{@id}" id="txt-{@id}">
<!--		 onmouseover="document.getElementById('{@id}').style.textDecoration='underline';return true;"
		 onmouseout="document.getElementById('{@id}').style.textDecoration='none';return true;"-->
	    <xsl:attribute name="title">
	      <xsl:apply-templates mode="tooltip-text" select="$target"/>
	    </xsl:attribute>
	    <xsl:apply-templates select="@*|node()"/>
	  </a>
	  <xsl:text>]</xsl:text>
	</span>
	<xsl:sequence select="$prop/after/node()"/>
      </xsl:when>
      <xsl:otherwise>
	<span>
	  <xsl:apply-templates select="@*|node()"/>
	</span>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- MODE: tooltip-text -->
  <!-- based on deep null transform -->
  <xsl:template mode="tooltip-text" match="/|@*|node()">
    <xsl:apply-templates mode="tooltip-text" select="@*|node()"/>
  </xsl:template>
  <xsl:template mode="tooltip-text" match="observation[code]">
    <xsl:value-of select="code/@displayName"/>
    <xsl:text> [</xsl:text>
    <xsl:value-of select="code/@code"/>
    <xsl:text>] </xsl:text>
    <xsl:if test="uncertaintyInd[@value='true']">
      <xsl:text>UNCERTAIN </xsl:text>
    </xsl:if>
    <xsl:if test="suspicionInd[@value='true']">
      <xsl:text>SUSPECT </xsl:text>
    </xsl:if>
    <xsl:if test="negationInd[@value='true']">
      <xsl:text>NEGATIVE </xsl:text>
    </xsl:if>
    <xsl:apply-templates mode="tooltip-text" select="@*|node()"/>
  </xsl:template>  
  <xsl:template mode="tooltip-text" 
         match="observation/value[@code and @displayName]">
    <xsl:if test="../suspicionInd[@value='true']
                 |../negationInd[@value='true']">
      <xsl:text>FOR </xsl:text>
    </xsl:if>
    <xsl:value-of select="@displayName"/>
    <xsl:text> [</xsl:text>
    <xsl:value-of select="@code"/>
    <xsl:text>] </xsl:text>
  </xsl:template>  
  <xsl:template mode="tooltip-text" match="*[@code and @displayName
				            and not(parent::observation)]">
    <xsl:value-of select="@displayName"/>
    <xsl:text> [</xsl:text>
    <xsl:value-of select="@code"/>
    <xsl:text>] </xsl:text>
  </xsl:template>  

  <!-- MODE: formal - show formal parsed data -->
  <xsl:template mode="formal" match="/|@*|node()">
    <xsl:apply-templates mode="formal" select="@*|node()"/>
  </xsl:template>
  <xsl:template mode="formal" match="codething"/>
  <xsl:template mode="formal"
      match="specimen
            |tissue-type|tissue-type/@code|tissue-type/@displayName
	        |tissue-type/text|tissue-type/text/text()
	    |site-modifier|site-modifier/@code|site-modifier/@displayName
	        |site-modifier/text|site-modifier/text/text()
	    |collection-method|collection-method/@code
		|collection-method/@displayName
		|collection-method/text|collection-method/text/text()
            |observation
	    |observation/text|observation/text/text()
	    |observation/code|observation/code/@code
	        |observation/code/@displayName
	        |observation/code/text|observation/code/text/text()
	    |observation/value|observation/value/@code
	        |observation/value/@displayName|observation/value/@phrase
	        |observation/value/text|observation/value/text/text()
	    |observation/negationInd|observation/negationInd/@value
	    |observation/suspicionInd|observation/suspicionInd/@value
	    |observation/uncertaintyInd|observation/uncertaintyInd/@value
	    |observation/interpretationCode|observation/interpretationCode/@code
            |@item">
      <!-- |*[ancestor-or-self::concept]
	   |@*[ancestor::concept] -->
    <xsl:copy>
      <xsl:apply-templates mode="formal" select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template mode="formal"
      match="specimen/@ref
            |tissue-type/@ref
	    |site-modifier/@ref
	    |collection-method/@ref
	    |observation/@ref
	    |observation/code/@ref
	    |observation/value/@ref">
    <xsl:attribute name="id">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

</xsl:transform>
