<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	exclude-result-prefixes="xsi xsl">
	<xsl:output method="xml" standalone="yes" />
	<xsl:key name="objet" match="orgUnit | program | subProgram | course | person" use="@id"/>
	<xsl:param name="referentiel">constantes.sample</xsl:param>
	<xsl:param name="_lang">fr-FR</xsl:param>
	<xsl:variable name="defaultLang">fr-FR</xsl:variable>
	<xsl:variable name="codes"
		select="document(concat('file://',$referentiel,'.xml'))/constantes" />
	<!-- La version actuelle ne prend pas en charge le multilinguisme.
		Il fallait aussi distinguer aisément les subProgram qui sont des options ou des mentions d'un program des subProgram qui ne sont que des découpages en semestres : un codeSet uniform portant la valeur diplome est utilisé à cet effect dans l'élément programCode
		A FAIRE : Les codes de typeDiplome        ****************************************************************************************************************************************** -->
	<!-- traitement par défaut : copie des noeuds -->
	<xsl:template match="* | @* | comment() | text()">
		<xsl:copy>
			<xsl:apply-templates select="* | @* | comment() |text()" />
		</xsl:copy>
	</xsl:template>
		<xsl:template name="copy.attributes">
		<xsl:for-each
			select="@*[not(name()='id')  or not(name()='ident')or not(name()='idRef')or not(name()='ref') ]">
			<xsl:apply-templates select="." />
		</xsl:for-each>
		<xsl:choose>
			<xsl:when test="@ident">
				<xsl:attribute name="id">
					<xsl:value-of select="@ident" />
				</xsl:attribute>
				<xsl:attribute name="ident">
					<xsl:value-of select="@ident" />
				</xsl:attribute>
			</xsl:when>
			<xsl:when test="@id">
				<xsl:attribute name="id">
					<xsl:value-of select="@id" />
				</xsl:attribute>
				<xsl:attribute name="ident">
					<xsl:value-of select="@id" />
				</xsl:attribute>
			</xsl:when>
		</xsl:choose>

		<xsl:choose>
			<xsl:when test="@ref">
				<xsl:attribute name="ref">
					<xsl:value-of select="@ref" />
				</xsl:attribute>
				<xsl:attribute name="idRef">
					<xsl:value-of select="@ref" />
				</xsl:attribute>
			</xsl:when>
			<xsl:when test="@idRef">
				<xsl:attribute name="ref">
					<xsl:value-of select="@idRef" />
				</xsl:attribute>
				<xsl:attribute name="idRef">
					<xsl:value-of select="@idRef" />
				</xsl:attribute>
			</xsl:when>
		</xsl:choose>

	</xsl:template>
	<xsl:template match="properties/infoBlock">
		<xsl:copy>
			<xsl:apply-templates select="@*|*" />
			<xsl:if test="count(extension)=0">
				<extension>
					<uniform/>
					<hierarchy>
						<xsl:apply-templates select="/CDM/program[1]" mode="hierarchy"><xsl:with-param name="niveau" select="'1'"/><xsl:with-param name="typeObjet" select="'program'"/></xsl:apply-templates>
					</hierarchy>
				</extension>
			</xsl:if>
			
		</xsl:copy>
	</xsl:template>
    <xsl:template match="properties">
        <xsl:copy>
            <xsl:apply-templates select="@*|*" />
            <xsl:if test="count(infoBlock)=0">
                <infoBlock>
                    <extension>
                        <uniform/>
                        <hierarchy>
                            <xsl:apply-templates select="/CDM/program[1]" mode="hierarchy"><xsl:with-param name="niveau" select="'1'"/></xsl:apply-templates>
                        </hierarchy>
                    </extension>
                </infoBlock>
            </xsl:if>
            
        </xsl:copy>
    </xsl:template>
	<xsl:template match="properties/infoBlock/extension">
		<xsl:copy>
			<xsl:apply-templates select="@*|*" />
			<xsl:if test="count(uniform)=0">
					<uniform/>
			</xsl:if>
			<hierarchy primaryDomainId="{/CDM/properties/infoBlock/extension/uniform/constantes/domaineDiplome[value[@lang='fr-FR']=/CDM/habilitation/field/@primaryDomainName]/@id}" otherDomainId="{/CDM/properties/infoBlock/extension/uniform/constantes/domaineDiplome[value[@lang='fr-FR']=/CDM/habilitation/field/@otherDomainName]/@id}">
				<xsl:apply-templates select="/CDM/program[1]" mode="hierarchy"><xsl:with-param name="niveau" select="'1'"/><xsl:with-param name="typeObjet" select="'program'"/></xsl:apply-templates>
			</hierarchy>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="/CDM">
		<xsl:copy>
			<xsl:call-template name="copy.attributes" />
			<xsl:choose>
				<xsl:when test="not(/CDM/@language)">
					<xsl:attribute name="language">
						<xsl:value-of select="$defaultLang" />
					</xsl:attribute>
				</xsl:when>
			</xsl:choose>
			<xsl:apply-templates select="habilitation | properties | orgUnit |  text()" />
			<xsl:for-each select="program">
				<xsl:variable name="id" select="@id"/>
				<xsl:if test="not(preceding-sibling::program/@id=$id)">
					<xsl:apply-templates select="." />
				</xsl:if>
			</xsl:for-each>
			<!-- eliminer les course non references-->
			<xsl:for-each select="//refCourse">
				<xsl:variable name="ref">
					<xsl:choose>
						<xsl:when test="@ref">
							<xsl:value-of select="@ref" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="@idRef" />
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:if
					test="not(preceding::refCourse/@ref=$ref or preceding::refCourse/@idRef=$ref )">
					<xsl:apply-templates
						select="/CDM/course[@ident=$ref][1]" />
				</xsl:if>
			</xsl:for-each>
			<!-- eliminer les person non references-->
			<xsl:for-each select="//refPerson">
				<xsl:variable name="ref">
					<xsl:choose>
						<xsl:when test="@ref">
							<xsl:value-of select="@ref" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="@idRef" />
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:if
					test="not(preceding::refPerson/@ref=$ref or preceding::refPerson/@idRef=$ref )">
					<xsl:apply-templates
						select="/CDM/person[@ident=$ref][1]" />
				</xsl:if>
			</xsl:for-each>
			
		</xsl:copy>
	</xsl:template>
	<xsl:template match="program" mode="hierarchy">
		<xsl:param name="niveau"/>
		<xsl:param name="typeObjet">program</xsl:param>
		<noeud niveau="{$niveau}" id="{@id}" nid="{generate-id()}" libelle="{programName/text[@language=$_lang or not(@language)]}" typeObjet="{$typeObjet}" affichable="{count(contacts)+count(learningObjectives)+count(aip)+count(programIntroduction)}">
			<xsl:apply-templates select="programStructure/refProgram|programStructure/refCourse" mode="hierarchy">
				<xsl:with-param name="niveau" select="$niveau+1"/>
			</xsl:apply-templates>
		</noeud>
	</xsl:template>
	<xsl:template match="course" mode="hierarchy">
		<xsl:param name="niveau"/>
		<xsl:param name="typeObjet">course</xsl:param>
		<noeud niveau="{$niveau}" id="{@id}"  nid="{generate-id()}" libelle="{courseName}" typeObjet="{$typeObjet}" affichable="{count(courseDescription)}">
			<xsl:apply-templates select="courseDescription/courseContentsEC/refCourse" mode="hierarchy">
				<xsl:with-param name="niveau" select="$niveau+1"/>
			</xsl:apply-templates>
		</noeud>
	</xsl:template>
	<xsl:template match="refProgram|refCourse|refProgram|refCourse" mode="hierarchy">
		<xsl:param name="niveau"/>
		<xsl:variable name="ref" select="@ref"/>
		<refProgram id="{key('objet', $ref)[1]/@id}"/>
		<xsl:apply-templates select="key('objet', $ref)[1]" mode="hierarchy">
			<xsl:with-param name="niveau" select="$niveau"/>
		</xsl:apply-templates>
	</xsl:template>
</xsl:stylesheet>
