<?xml version="1.0" encoding="UTF-8"?>

<!-- 	**************************************************************************
		**	Dataset overview 	 for ABCD 1.2									**
		**																		**
		**	This template provides a simple overview of an ABCD document.		**
		**	It needs to															**
		**	include the en_ABCD1.2_CommonTemplates.xslt file, but if it is being 	**
		**	called by the All_parts XSLT then it is already included before		**
		**	and should be mantained commented									**
		**																		**
		**	Author: Javier de la Torre, Markus Döring							**
		**************************************************************************
-->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2004/07/xpath-functions" xmlns:xdt="http://www.w3.org/2004/07/xpath-datatypes" xmlns:n1="http://www.tdwg.org/schemas/abcd/1.2" exclude-result-prefixes="xsl xs fn xdt n1">
	<xsl:output version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" media-type="text/html"/>
	<xsl:include href="en_ABCD1.2_CommonTemplates.xslt" />
	<!-- if no dsa is passed use relative anchor links instead of querytool unit links  -->
	<xsl:param name="dsa"/>
	<xsl:param name="unitlink"/>
	
	<xsl:template name="DatasetOverview" match="/">

		<h3 class="background">Dataset(s) summary:</h3>
		<p>Total datasets: <xsl:value-of select="count(//n1:DataSets/n1:DataSet)"/> | Total units: <xsl:value-of select="count(//n1:DataSets/n1:DataSet/n1:Units/n1:Unit)"/> | Georeferenced Units: <xsl:value-of select="count(/n1:DataSets/n1:DataSet/n1:Units/n1:Unit/n1:Gathering/n1:GatheringSite/n1:SiteCoordinateSets/n1:SiteCoordinates/n1:CoordinatesLatLong/n1:LongitudeDecimal[text()])"/> |
					<!-- General map -->
		<xsl:if test="n1:DataSets/n1:DataSet/n1:Units/n1:Unit/n1:Gathering/n1:GatheringSite/n1:SiteCoordinateSets/n1:SiteCoordinates">
			<a href="Javascript:document.GeneralMap.submit()">Create Map!</a>
				<form action="http://linuxgurrl.agr.gc.ca/mapdata/itis/itisrosa.php" method="POST" target="_blank" name="GeneralMap">
						<input type="hidden" name="xml">
							<xsl:attribute name="value">%3C%3Fxml+version%3D%271.0%27+encoding%3D%27iso-8859-1%27%3F%3E%3Cresponse%3E%3Cheader%3E%3Cauthor%3E%3C%2Fauthor%3E%3Cboundingbox%3E-180%2C-90%2C180%2C90%2CWorld%3C%2Fboundingbox%3E%3Cdescription%3EThis+Generic+Point+Mapper+is+a+service+provided+by+the+Canadian+Biological+Information+Facility%3C%2Fdescription%3E%3Cifx%3E%3C%2Fifx%3E%3Clanguage%3Een%3C%2Flanguage%3E%3Cprojection%3Elatlong%3C%2Fprojection%3E%3Crecordcount%3E%3C%2Frecordcount%3E%3Ctimestamp%3E2004-11-30+11%3A10%3A58.043%3C%2Ftimestamp%3E%3Ctitle%3ESimple+access+to+ABCD+providers+-+Point+Location+Data%3C%2Ftitle%3E%3Curl%3Ehttp%3A%2F%2Fwww.cbif.gc.ca%2Fmc%2Findex_e.php%3C%2Furl%3E%3C%2Fheader%3E%3Crecords<xsl:for-each select="n1:DataSets/n1:DataSet/n1:Units/n1:Unit/n1:Gathering/n1:GatheringSite/n1:SiteCoordinateSets/n1:SiteCoordinates">%3E%3Crecord<xsl:for-each select="n1:CoordinatesLatLong"><xsl:for-each select="n1:LatitudeDecimal">%3E%3Clatitude%3E<xsl:apply-templates/>%3C%2Flatitude</xsl:for-each><xsl:for-each select="n1:LongitudeDecimal">%3E%3Clongitude%3E<xsl:apply-templates/>%3C%2Flongitude</xsl:for-each></xsl:for-each>%3E%3Crecordurl%3E%3C%2Frecordurl%3E%3C%2Frecord</xsl:for-each>%3E%3C%2Frecords%3E%3C%2Fresponse%3E</xsl:attribute>
						</input>
				</form>
		</xsl:if>
		<!-- Finish General Map -->
		</p>
		<ul>
			<xsl:for-each select="n1:DataSets">
				<xsl:for-each select="n1:DataSet">
					<xsl:variable name="DataSetId" select="position()"/>
					<xsl:for-each select="n1:OriginalSource">
						<li><a href="#{$DataSetId}"><xsl:value-of select="n1:SourceName"/> (<xsl:value-of select="n1:SourceInstitutionCode"/>)</a></li>
					</xsl:for-each>
				</xsl:for-each>
			</xsl:for-each>
		</ul>
		<p>Click on UnitId to get details. </p>
		<xsl:for-each select="n1:DataSets">
			<xsl:for-each select="n1:DataSet">
			<xsl:variable name="DataSetId" select="position()"/>
				<xsl:for-each select="n1:OriginalSource">
					<h3>
						<xsl:if test="../n1:DatasetDerivations/n1:DatasetDerivation/n1:Statements/n1:LogoURL">
							<img border="0" src="{../n1:DatasetDerivations/n1:DatasetDerivation/n1:Statements/n1:LogoURL}" align="right" height="45"/>
						</xsl:if>
						<!-- resource Identifier -->
						<a name="{$DataSetId}" class="linktarget"><xsl:for-each select="n1:SourceName"><xsl:apply-templates/></xsl:for-each> (<xsl:for-each select="n1:SourceInstitutionCode"><xsl:apply-templates/></xsl:for-each>)</a>
					</h3>
					<p>
						<span class="label">Last update: </span>
						<xsl:value-of select="n1:SourceLastUpdatedDate"/>|<xsl:if test="n1:SourceWebAddress"> <a href="{n1:SourceWebAddress}" target ="_blank">URL</a>|</xsl:if>
						<xsl:if test="../n1:DatasetDerivations/n1:DatasetDerivation/n1:Supplier/n1:EmailAddresses/n1:EmailAddress"> <a href="mailto:{../n1:DatasetDerivations/n1:DatasetDerivation/n1:Supplier/n1:EmailAddresses/n1:EmailAddress}">EMAIL</a></xsl:if>
					<xsl:if test="../n1:Units/n1:Unit/n1:Gathering/n1:GatheringSite/n1:SiteCoordinateSets">
						| <a><xsl:attribute name="href">Javascript:document.form<xsl:value-of select="$DataSetId"/>.submit()</xsl:attribute>Create Map!</a>
					</xsl:if>
				<!-- Form to create a map, only if there is data on it-->
				<xsl:if test="../n1:Units/n1:Unit/n1:Gathering/n1:GatheringSite/n1:SiteCoordinateSets">
					<form action="http://linuxgurrl.agr.gc.ca/mapdata/itis/itisrosa.php" method="POST" target="_blank">
						<xsl:attribute name="name">form<xsl:value-of select="$DataSetId"/></xsl:attribute>
						<input><xsl:attribute name="value">%3C%3Fxml+version%3D%271.0%27+encoding%3D%27iso-8859-1%27%3F%3E%3Cresponse%3E%3Cheader%3E%3Cauthor%3E%3C%2Fauthor%3E%3Cboundingbox%3E-180%2C-90%2C180%2C90%2CWorld%3C%2Fboundingbox%3E%3Cdescription%3EThis+Generic+Point+Mapper+is+a+service+provided+by+the+Canadian+Biological+Information+Facility%3C%2Fdescription%3E%3Cifx%3E%3C%2Fifx%3E%3Clanguage%3Een%3C%2Flanguage%3E%3Cprojection%3Elatlong%3C%2Fprojection%3E%3Crecordcount%3E%3C%2Frecordcount%3E%3Ctimestamp%3E2004-11-30+11%3A10%3A58.043%3C%2Ftimestamp%3E%3Ctitle%3ESimple+access+to+ABCD+providers+-+Point+Location+Data%3C%2Ftitle%3E%3Curl%3Ehttp%3A%2F%2Fwww.cbif.gc.ca%2Fmc%2Findex_e.php%3C%2Furl%3E%3C%2Fheader%3E%3Crecords<xsl:for-each select="../n1:Units/n1:Unit/n1:Gathering/n1:GatheringSite/n1:SiteCoordinateSets/n1:SiteCoordinates">%3E%3Crecord<xsl:for-each select="n1:CoordinatesLatLong"><xsl:for-each select="n1:LatitudeDecimal">%3E%3Clatitude%3E<xsl:apply-templates/>%3C%2Flatitude</xsl:for-each><xsl:for-each select="n1:LongitudeDecimal">%3E%3Clongitude%3E<xsl:apply-templates/>%3C%2Flongitude</xsl:for-each></xsl:for-each>%3E%3Crecordurl%3E%3C%2Frecordurl%3E%3C%2Frecord</xsl:for-each>%3E%3C%2Frecords%3E%3C%2Fresponse%3E</xsl:attribute>
						<xsl:attribute name="type">hidden</xsl:attribute>
						<xsl:attribute name="name">xml</xsl:attribute>
						</input>
					</form>
				</xsl:if>
				<!-- /For to create a map-->
					</p>
				</xsl:for-each>
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<th width="125">UnitId</th>
						<th>Prefered Name(s) </th>
						<xsl:if test="n1:Units/n1:Unit/n1:RecordBasis">
							<th>Record Basis </th>
						</xsl:if>
						<xsl:if test="n1:Units/n1:Unit/n1:Gathering/n1:GatheringSite/n1:LocalityText">
							<th>Locality</th>
						</xsl:if>
						<xsl:if test="n1:Units/n1:Unit/n1:Gathering/n1:GatheringSite/n1:Country">
							<th width="120">Country</th>
						</xsl:if>
					</tr>
					<xsl:for-each select="n1:Units">
						<xsl:for-each select="n1:Unit">
							<xsl:variable name="UnitID" select="concat($DataSetId,'_',position())"/>
							<tr>
								<td>
									<!-- to link to the unit details we have to use the SourceInstitutionCode + SourceName + UnitID as GUID -->
									<a>
										<xsl:choose>
											<xsl:when test="string($dsa)='' ">
												<!-- relative link -->
												<xsl:attribute name="href">#<xsl:value-of select="$UnitID"/></xsl:attribute>
											</xsl:when>
											<xsl:otherwise>
												<!-- its a querytool link -->
												<xsl:attribute name="href"><xsl:value-of select="$unitlink"/>&amp;inst=<xsl:value-of select="normalize-space(../../n1:OriginalSource/n1:SourceInstitutionCode)"/>&amp;col=<xsl:value-of select="normalize-space(../../n1:OriginalSource/n1:SourceName)"/>&amp;cat=<xsl:value-of select="normalize-space(n1:UnitID)"/></xsl:attribute>
											</xsl:otherwise>
										</xsl:choose>
										<xsl:for-each select="n1:UnitID">
											<xsl:apply-templates/>
										</xsl:for-each>
									</a>
								</td>
								<td>
									<!-- ***********************************************************************+NAME****************************************************** -->
									<xsl:for-each select="n1:Identifications/n1:Identification">
										<!--General rule for names -->
										<!-- Avoid execution if there are no Identifications-->
										<!--Differenciate if there is only one identification o more -->
										<xsl:choose>
											<xsl:when test="count(../n1:Identification) = 1">
												<!--Only one identification, is shown as non prefered -->
												<span class="style2">
													<xsl:value-of select="n1:TaxonIdentified/n1:NameAuthorYearString"/>
												</span>
											</xsl:when>
											<!-- more than 1 identifications -->
											<xsl:otherwise>
												<!--Check if there is a preferedflag -->
												<xsl:choose>
													<xsl:when test="not(@PreferredIdentificationFlag)">
														<!-- Identification without prefered flag -->
														<xsl:value-of select="n1:TaxonIdentified/n1:NameAuthorYearString"/> |
													</xsl:when>
													<xsl:otherwise>
														<xsl:choose>
															<xsl:when test="@PreferredIdentificationFlag= '1' or @PreferredIdentificationFlag='true'">
																<!--Multiple identifications, prefered one -->
																<span class="style2">
																	<xsl:value-of select="n1:TaxonIdentified/n1:NameAuthorYearString"/>
																</span>
															</xsl:when>
															<xsl:otherwise>
																<!--Multiple identifications, non prefered one -->
															</xsl:otherwise>
														</xsl:choose>
													</xsl:otherwise>
												</xsl:choose>
											</xsl:otherwise>
										</xsl:choose>
									</xsl:for-each>
									<!-- ***********************************************************************+NAME****************************************************** -->
								</td>
								<xsl:if test="../../n1:Units/n1:Unit/n1:RecordBasis">
									<td>
										<xsl:if test="n1:RecordBasis">
											<xsl:value-of select="n1:RecordBasis"/>
										</xsl:if>
									</td>
								</xsl:if>
								<xsl:if test="../../n1:Units/n1:Unit/n1:Gathering/n1:GatheringSite/n1:LocalityText">
									<td>
										<xsl:if test="n1:Gathering/n1:GatheringSite/n1:LocalityText">
											<xsl:value-of select="n1:Gathering/n1:GatheringSite/n1:LocalityText"/>
										</xsl:if>
									</td>
								</xsl:if>
								<xsl:if test="../../n1:Units/n1:Unit/n1:Gathering/n1:GatheringSite/n1:Country">
									<td>
										<xsl:if test="n1:Gathering/n1:GatheringSite/n1:Country">
											<xsl:value-of select="n1:Gathering/n1:GatheringSite/n1:Country/n1:CountryName"/> <xsl:if test="string(n1:Gathering/n1:GatheringSite/n1:Country/n1:ISO2Letter)"> (<xsl:value-of select="n1:Gathering/n1:GatheringSite/n1:Country/n1:ISO2Letter"/>)</xsl:if> <xsl:if test="string(n1:Gathering/n1:GatheringSite/n1:Country/n1:ISO3Letter)"> (<xsl:value-of select="n1:Gathering/n1:GatheringSite/n1:Country/n1:ISO3Letter"/>)</xsl:if>
										</xsl:if>
									</td>
								</xsl:if>
							</tr>
						</xsl:for-each>
					</xsl:for-each>
				</table>
				<xsl:for-each select="n1:DatasetDerivations/n1:DatasetDerivation/n1:Rights">
					<xsl:call-template name="RightsTable"/>
				</xsl:for-each>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>