<?xml version="1.0" encoding="UTF-8"?>
<maxdQL version="0.1" name="MeasurementReport" >
	
	<description>Gives useful information about a measurement.</description>
	
	<arguments type="user" >
		<var name="MeasurementNames" comment="the measurement names, * for all" />
	</arguments>
	
	<!-- 
	<xfunction id="formatBrowseResult" comment="formats a list of entries from a browse output">
		<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
			<xsl:output method="xml" encoding="ISO-8859-1" indent="no" omit-xml-declaration="yes"  media-type="text/html" />
			<xsl:template match="/">
				<P>
					<xsl:for-each select="//browse/*">
						<h3><xsl:value-of select="name()"/>: <xsl:value-of select="@Name"/></h3>
							<table border="1"  style="margin-left:30px;" >
								<xsl:for-each select="attribute">
									<tr>
										<td><xsl:value-of select="@Name"/></td>
										<td><xsl:value-of select="."/></td>										
									</tr>
								</xsl:for-each>	
								
							</table>
								
					</xsl:for-each>
				</P>
			</xsl:template>
		</xsl:stylesheet>
	</xfunction>
	
	<xfunction id="formatLinkResult" comment="formats a list of entries from a link output">
		<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
			<xsl:output method="xml" encoding="ISO-8859-1" indent="no" omit-xml-declaration="yes"  media-type="text/html" />
			<xsl:template match="/">
				<P>
					<xsl:for-each select="//link/*">
						<h3><xsl:value-of select="name()"/>: <xsl:value-of select="@Name"/></h3>
							<table border="1"  style="margin-left:30px;" >
								<xsl:for-each select="attribute">
									<tr>
										<td><xsl:value-of select="@Name"/></td>
										<td><xsl:value-of select="."/></td>										
									</tr>
								</xsl:for-each>	
								
							</table>
								
					</xsl:for-each>
				</P>
			</xsl:template>
		</xsl:stylesheet>
	</xfunction>
	
	 -->
	
	<pfunction id="formatLinkResult" comment="formats a list of entries from a link output" method="xml" >
			<![CDATA[
				$output = formatLinkResult($input, "Measurement");
			]]>
	</pfunction>
	
	<pfunction id="formatEnumerateResult" comment="formats a list of entries from an enumeration output" method="xml" >
			<![CDATA[
				
				// create a new dom
				$domNew = new DOMDocument();	
				$root = $domNew->createElement("result");
				$rootDom = $domNew->appendChild($root);
				
				$p = $domNew->createElement("p");
				$pDom = $rootDom->appendChild($p);
				
				foreach ($input->firstChild->childNodes as $childNode)
				{
					
					if ($childNode->nodeType == 1)
					{
						$name = $childNode->getAttribute("Name");
						//print "\n".$name;
						
						$h3 = $domNew->createElement("h3");
						$h3Dom = $p->appendChild($h3);
						$text = $domNew->createTextNode($name);
						$text = $h3Dom->appendChild($text);
			
						$table = $domNew->createElement("table");
						$tableDom = $pDom->appendChild($table);
						$tableDom->setAttribute("border", "1");		
						
						$columnsDone = false;
						foreach ($childNode->childNodes as $enumeration) {
							if ($enumeration->nodeType == 1)
							{
								//print "\t!". $enumeration->getAttribute("ID");
								
								$tr = $domNew->createElement("tr");
								$trDom = $tableDom->appendChild($tr);

								if ($columnsDone != true)
								{
									foreach ($enumeration->childNodes as $attribute)
									{
										if ($attribute->nodeType == 1)
										{
											$colName = $attribute->getAttribute("Name");
											
											$td = $domNew->createElement("td");
											$tdDom = $trDom->appendChild($td);
											$tdDom->setAttribute("style", "font-weight:bold");
											
											$text = $domNew->createTextNode($colName);
											$text = $tdDom->appendChild($text);
										}
									}
									$columnsDone = true;
								}				

								$tr = $domNew->createElement("tr");
								$trDom = $tableDom->appendChild($tr);
				
								foreach ($enumeration->childNodes as $attribute)
								{
									if ($attribute->nodeType == 1)
									{
										$colVal = $attribute->nodeValue;

										$td = $domNew->createElement("td");
										$tdDom = $trDom->appendChild($td);
				
										$text = $domNew->createTextNode($colVal);
										$text = $tdDom->appendChild($text);
									}
								}

							}
						}
					}
				}
				
				//print $domNew->saveXML();die();
				//die();
				$output = $domNew;
			
			]]>
	</pfunction>		
	
	<!-- 
	<query id="1" name="BrowseMeasurements" comment="browsing measurement table" type="maxdBrowse" >
		<names uref="user" name="MeasurementNames" />
		<action>browse</action>
		<table>Measurement</table>
		<attributes>yes</attributes>
	</query>	
		 -->
	<query id="1" name="EnumerateMeasurementMetadada" comment="enumerating measurements to get column types" type="maxdBrowse" >
		<names uref="user" name="MeasurementNames" />
		
		<action>enumerate</action>
		<table>Measurement</table>
	</query>
		
	<query id="2" name="LinkMeasurements" comment="browsing measurement table" type="maxdBrowse" >
		<names uref="user" name="MeasurementNames" />
		<action>link</action>
		<table>Measurement</table>
		<attributes>yes</attributes>
		<desiredTable>Measurement,Source,TreatedSample,Hybridisation,Array</desiredTable>
		<displayTable>Measurement,Source,TreatedSample,Hybridisation,Array</displayTable>
		<format>array</format>
	</query>
 
	<query id="4" name="PresentMyResults" comment="now take the results and present them" type="export" >
		<h1>Query results</h1>
		
		<h2>Data Column Headings</h2>
		<enumerateResult pref="formatEnumerateResult" processResultID="1" />
		
		<h2>Measurement metadata</h2>
		<!-- 
		<browseResult xref="formatBrowseResult" processResultID="1" />
		
		<h2>Linked metadata</h2>
		 -->		
		<linkResult pref="formatLinkResult" processResultID="2" />
		
		
	</query>


</maxdQL>
