<?xml version="1.0" encoding="UTF-8" ?>
<!--=================================================
================= script header =====================
=====================================================

	script version 1.2
	supported iteraplan versions 6.1
	last update Jan 18 2018
	
-->
<xsl:stylesheet version="2.0" 
	xmlns="http://graphml.graphdrawing.org/xmlns" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns:iteraplan="urn:iteraplan"	
	xmlns:y="http://www.yworks.com/xml/graphml" 
	xmlns:g="http://graphml.graphdrawing.org/xmlns" 
	xmlns:dn="myPersonalNamespace">
	
	<xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>

<!--=================================================
================ global variables ===================
==================================================-->
	<!-- Load full iteraplan model -->
	<xsl:variable name="fullModel" select="/iteraplan:Container" />
	<xsl:variable name="buildingBlocks" select="/iteraplan:Container/contents" />
	<xsl:variable name="baseURL" select="'https://YourURL/TOLUY/'" />
	<xsl:variable name="filteredProcessId" select="$buildingBlocks[@name='Customer Strategy']/@id" />  

<!--=================================================
============= document processing ===================
==================================================-->

	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Start processing the full model by matching the root node "iteraplan:Container"
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<xsl:template match="iteraplan:Container">
      [INFO] Content was written to fullModLink.graphml which can be loaded from the file list on the right!
  	  <xsl:result-document href="fullModLink.graphml">
	  
	  <!-- Write the graphml header with all necessary configuration of the graphml file (keys) -->
	  <!-- NOTE: no dynamic keys for public demo scripts -->
	  <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xmlns:yed="http://www.yworks.com/xml/yed/3" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd">
	   <key id="d0" for="graph" attr.name="Beschreibung" attr.type="string"/>
	   <key id="d1" for="port" yfiles.type="portgraphics"/>
	   <key id="d2" for="port" yfiles.type="portgeometry"/>
	   <key id="d3" for="port" yfiles.type="portuserdata"/>
	   <key id="d4" for="node" attr.name="nodeID" attr.type="string">
	      <default>-1</default>
	   </key>
	   <key id="d5" for="node" attr.name="nodeType" attr.type="string"/>
	   <key id="d6" for="node" attr.name="url" attr.type="string"/>
	   <key id="d7" for="node" attr.name="description" attr.type="string"/>
	   <key id="d8" for="node" yfiles.type="nodegraphics"/>
	   <key id="d9" for="edge" attr.name="edgeID" attr.type="string">
	      <default>-1</default>
	   </key>
	   <key id="d10" for="edge" attr.name="edgeType" attr.type="string"/>
	   <key id="d11" for="edge" attr.name="url" attr.type="string"/>
	   <key id="d12" for="edge" attr.name="description" attr.type="string"/>
	   <key id="d13" for="edge" yfiles.type="edgegraphics"/>
	   <key id="d14" for="graphml" yfiles.type="resources"/>
		<graph edgedefault="directed" id="G">
		    <!-- starting point, top level process "Management" -->
		    <xsl:apply-templates select="contents[@xsi:type='iteraplan:BusinessProcess' and @name='Management']" />
		    
		    <!-- continue with systems supporting sub process "Customer Strategy" -->
		    <xsl:apply-templates select="contents[concat('#', @id) = $buildingBlocks[@businessProcess=concat('#', $filteredProcessId)]/@informationSystem]" />
		</graph>
	  </graphml>
	  </xsl:result-document>
	</xsl:template>


	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Process business processes with children
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<xsl:template match="contents[@xsi:type='iteraplan:BusinessProcess' and @children]">
	  <xsl:variable name="parameters">
	  	  <dn:fillColor type="open">#88BBDD</dn:fillColor>
	  	  <dn:fillColor type="closed">#DDDDDD</dn:fillColor>
	  </xsl:variable>
	  <xsl:copy-of select="dn:createGroupNode(., $parameters)" />
	</xsl:template>


	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Process business processes without children.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<xsl:template match="contents[@xsi:type='iteraplan:BusinessProcess' and not(@children)]">
	  <!-- render GraphML node -->
	  <xsl:variable name="parameters">
	  	  <dn:fillColor>#FFFFBB</dn:fillColor>
	  </xsl:variable>
	  <xsl:copy-of select="dn:createNode(., $parameters)" />
	</xsl:template>


	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Process information systems.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<xsl:template match="contents[@xsi:type='iteraplan:InformationSystem']">
	  <!-- render GraphML node and edge -->
	  <xsl:variable name="parameters">
	  	  <dn:fillColor>#BBFFBB</dn:fillColor>
	  </xsl:variable>
	  <xsl:copy-of select="dn:createNode(., $parameters)" />
	  <xsl:copy-of select="dn:createEdge($filteredProcessId, @id)" />
	  
	  <!-- continue with supporting technologies -->
	  <xsl:variable name="ISID" select="@id" />
	  <xsl:apply-templates select="$buildingBlocks[concat('#', @id) = $buildingBlocks[@informationSystem = concat('#', $ISID)]/@technicalComponent]">
	    <xsl:with-param name="sourceNodeId" select="@id" />
	  </xsl:apply-templates>
	</xsl:template>


	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Process information systems.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<xsl:template match="contents[@xsi:type='iteraplan:TechnicalComponent']">
	  <xsl:param name="sourceNodeId" />
	  
	  <!-- render GraphML node and edge -->
	  <xsl:variable name="parameters">
	  	  <dn:fillColor>#A3E0FF</dn:fillColor>
	  </xsl:variable>
	  <xsl:copy-of select="dn:createNode(., $parameters)" />
	  <xsl:copy-of select="dn:createEdge($sourceNodeId, @id)" />
	  
	  <!-- continue with supporting infrastructure -->
	  <xsl:variable name="TCID" select="@id" />
	  <xsl:apply-templates select="$buildingBlocks[concat('#', @id) = $buildingBlocks[@technicalComponent = concat('#', $TCID)]/@infrastructureElement]">
	    <xsl:with-param name="sourceNodeId" select="@id" />
	  </xsl:apply-templates>
	</xsl:template>


	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Process information systems.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<xsl:template match="contents[@xsi:type='iteraplan:InfrastructureElement']">
	  <xsl:param name="sourceNodeId" />
	  
	  <!-- render GraphML node and edge -->
	  <xsl:variable name="parameters">
	  	  <dn:fillColor>#BBBBBB</dn:fillColor>
	  </xsl:variable>
	  <xsl:copy-of select="dn:createNode(., $parameters)" />
	  <xsl:copy-of select="dn:createEdge($sourceNodeId, @id)" />
	</xsl:template>


<!--=================================================
======== additional templates / functions ===========
==================================================-->	
	
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Default template, do nothing.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
	<xsl:template match="contents" />
	
	
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Returns a GraphML group node and continues processing with children.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->	
	<xsl:function name="dn:createGroupNode">
	  <xsl:param name="node" />
	  <xsl:param name="parameters" />
	  
	  <node id="{$node/@id}" yfiles.foldertype="group">
	  	<data key="d4"><xsl:value-of select="$node/@id" /></data>
	  	<data key="d5"><xsl:value-of select="$node/@xsi:type" /></data>
	  	<data key="d6"><xsl:value-of select="dn:getURLByNode($node)" /></data>
	  	<data key="d7"><xsl:value-of select="$node/@description" /></data>
	  	<data key="d8">
	      <y:ProxyAutoBoundsNode>
			<y:Realizers active="0">
	  	  
            <y:GroupNode>
              <y:Geometry height="150" width="150" x="0" y="0"/>
              <y:Fill color="{$parameters/dn:fillColor[@type='open']}" transparent="false"/>
              <y:BorderStyle color="#000000" type="line" width="3.0"/>
              <y:NodeLabel alignment="center" autoSizePolicy="node_width" borderDistance="0.0" bottomInset="0" configuration="CroppingLabel" fontFamily="Calibri" fontSize="16" fontStyle="bold" hasBackgroundColor="false" hasLineColor="false" height="26.53125" leftInset="22" modelName="internal" modelPosition="t" rightInset="22" textColor="#000000" topInset="5" visible="true" width="452.9508928571429" x="0.0" y="0.0">
				<xsl:value-of select="$node/@name" />
			  </y:NodeLabel>
              <y:Shape type="rectangle"/>
              <y:State closed="false" closedHeight="50.0" closedWidth="50.0" innerGraphDisplayEnabled="false"/>
              <y:Insets bottom="15" bottomF="15.0" left="15" leftF="15.0" right="15" rightF="15.0" top="15" topF="15.0"/>
              <y:BorderInsets bottom="17" bottomF="17.0" left="0" leftF="0.0" right="0" rightF="0.0" top="40" topF="40.0"/>
            </y:GroupNode>
            
            <y:GroupNode>
              <y:Geometry height="150.0" width="150.0" x="0.0" y="0.0"/>
              <y:Fill color="{$parameters/dn:fillColor[@type='closed']}" transparent="false"/>
              <y:BorderStyle color="#000000" type="line" width="1.0"/>
              <y:NodeLabel alignment="left" autoSizePolicy="node_width" backgroundColor="#DDDDDD" borderDistance="0.0" bottomInset="0" configuration="CroppingLabel" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasLineColor="false" height="32.1875" leftInset="22" modelName="internal" modelPosition="t" rightInset="0" textColor="#000000" topInset="0" visible="true" width="190.0" x="0.0" y="0.0">
				<xsl:value-of select="concat($node/@name, ' (closed)')" />
              </y:NodeLabel>
              <y:Shape type="rectangle"/>
              <y:State closed="false" closedHeight="50.0" closedWidth="50.0" innerGraphDisplayEnabled="false"/>
              <y:Insets bottom="15" bottomF="15.0" left="15" leftF="15.0" right="15" rightF="15.0" top="15" topF="15.0"/>
              <y:BorderInsets bottom="0" bottomF="0.0" left="0" leftF="0.0" right="0" rightF="0.0" top="11" topF="11.0"/>
            </y:GroupNode>
			  
			</y:Realizers>
		  </y:ProxyAutoBoundsNode>
	  	</data>
		<graph id="{concat('graph', $node/@id)}" edgedefault="directed">
	  	
	  	<xsl:apply-templates select="$buildingBlocks[@parent=concat('#', $node/@id)]" />
 
		</graph>
      </node>
	</xsl:function>
	
	
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Returns a GraphML leaf node.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->	
	<xsl:function name="dn:createNode">
	  <xsl:param name="node" />
	  <xsl:param name="parameters" />
	  
	  <node id="{$node/@id}">
	  	<data key="d4"><xsl:value-of select="$node/@id" /></data>
	  	<data key="d5"><xsl:value-of select="$node/@xsi:type" /></data>
	  	<data key="d6"><xsl:value-of select="dn:getURLByNode($node)" /></data>
	  	<data key="d7"><xsl:value-of select="$node/@description" /></data>
	  	<data key="d8">

          <y:ShapeNode>
           	<y:Geometry height="100" width="100" x="0" y="0"/>
           	<y:Fill color="{$parameters/dn:fillColor}" transparent="false"/>
           	<y:BorderStyle color="#000000" type="line" width="1.0"/>
           	<y:NodeLabel alignment="center" autoSizePolicy="node_size" configuration="CroppingLabel" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="40.0" modelName="internal" modelPosition="c" textColor="#000000" visible="true" width="40.0" x="0.0" y="0.0"><xsl:value-of select="$node/@name" /></y:NodeLabel>
           	<y:Shape type="rectangle"/>
          </y:ShapeNode>

	  	</data>
	  </node>
	</xsl:function>

	
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Returns a simple GraphML edge.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->	
	<xsl:function name="dn:createEdge">
	  <xsl:param name="sourceNodeId" />
	  <xsl:param name="targetNodeId" />
	  
	  <edge id="{concat($sourceNodeId, $targetNodeId)}" source="{$sourceNodeId}" target="{$targetNodeId}">
	  	<data key="d9"><xsl:value-of select="concat($sourceNodeId, '-', $targetNodeId)" /></data>
	  	<data key="d13">
          <y:PolyLineEdge>
          	<y:LineStyle color="#000000" type="line" width="1.0"/>
          	<y:Arrows source="none" target="none"/>
          	<y:BendStyle smoothed="false"/>
          </y:PolyLineEdge>
	  	</data>
	  </edge>
	  
	</xsl:function>
	
	
	<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Returns the URL for the single item view in LUY based on a contents
	element.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->	
	<xsl:function name="dn:getURLByNode">
	  <xsl:param name="node" />
	  
	  <xsl:value-of select="concat($baseURL, 'client/#/single/', substring-after($node/@xsi:type,':'), '/', $node/@id)" />
	</xsl:function>
	
</xsl:stylesheet>