<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:key name="objet" match="orgUnit | program | subProgram | course" use="@id"/>
	<xsl:output method="xml"/>
	<xsl:param name="cmd">extractMentionIDs</xsl:param>
	<xsl:param name="ID">program-400001</xsl:param>
	<xsl:param name="referentiel">constantes</xsl:param>
	<xsl:variable name="codes" select="document(concat('file://',$referentiel,'.xml'))/constantes"/>
	<!-- calcule le ident de l'orgunit universite : c'est forcement une orgunit de premier niveau /CM/orgunit avec normalement orgType='universitet' ou alors on chosit l'element de premiere position ! -->
	<xsl:variable name="universiteID" select="/CDM/orgUnit[1]/@id"/>
	<!-- traitement principal -->
	<xsl:template match="/">
		<xsl:choose>
			<xsl:when test="$cmd='extractDossier'">
				<xsl:call-template name="extractDossier"/>
			</xsl:when>
			<xsl:when test="$cmd='extractMentionIDs'">
				<xsl:call-template name="extractMentionIDs"/>
			</xsl:when>
			<xsl:when test="$cmd='extractCourseIDs'">
				<xsl:call-template name="extractCourseIDs"/>
			</xsl:when>
			<xsl:when test="$cmd='extractParcoursIDs'">
				<xsl:call-template name="extractParcoursIDs"/>
			</xsl:when>
			<xsl:when test="$cmd='extractOaiIDs'">
				<xsl:call-template name="extractOaiIDs"/>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="extractDossier">
		<CDM language="fr-FR">
			<!-- copy universite -->
			<xsl:copy-of select="/CDM/orgUnit[1]"/>
			<!-- copy des orgunit qui referencent le program -->
			<xsl:copy-of select="/CDM/orgUnit[not(@id=$universiteID)][.//refProgram/@idRef=$ID]"/>
			<!-- copy du program -->
			<xsl:copy-of select="key('objet', $ID)"/>
			<!-- copy des program qui sont references par le program -->
			<xsl:call-template name="program">
				<xsl:with-param name="id" select="$ID"/>
			</xsl:call-template>
			<!-- copy de tous les course -->
			<xsl:copy-of select="/CDM/course"/>
			<!-- copy des person qui sont references par le program ou les orgunit copies dans le dossier-->
			<xsl:copy-of select="/CDM/person"/>
		</CDM>
	</xsl:template>
	<xsl:template name="program">
		<xsl:param name="id"/>
		<xsl:for-each select="key('objet', $id)/programStructure//refProgram">
			<xsl:copy-of select="key('objet', @ref)"/>
			<xsl:call-template name="program">
				<xsl:with-param name="id" select="@ref"/>
			</xsl:call-template>
		</xsl:for-each>
	</xsl:template>
	<xsl:template name="extractMentionIDs">
		<IDs>
			<xsl:for-each select="/CDM/program">
				<ID>
					<xsl:value-of select="@id"/>
				</ID>
			</xsl:for-each>
		</IDs>
	</xsl:template>
	<xsl:template name="extractOaiIDs">
		<xsl:variable name="typeObjetSOF">
			<xsl:for-each select="$codes/oai/records/typeObjet">-<xsl:value-of select="."/></xsl:for-each>
		</xsl:variable>
		<IDs>
			<xsl:choose>
				<!--  On s'appuie sur SOF pour déterminer quels sont les type d'objets qui donnent lieu à une publication dans un enregistrement oai-->
				<xsl:when test="$codes/oai/records/typeObjet">
					<xsl:for-each select="//subProgram[contains($typeObjetSOF, infoBlock/extension/*/typeObjet)][string-length(infoBlock/extension/*/typeObjet)&gt;0]">
							<ID nb="{$typeObjetSOF}">
								<xsl:value-of select="@id"/>
							</ID>
					</xsl:for-each>
					<xsl:if test="count(//subProgram[contains($typeObjetSOF, infoBlock/extension/*/typeObjet)][string-length(infoBlock/extension/*/typeObjet)&gt;0])=0">
						<xsl:for-each select="//program[contains($typeObjetSOF, infoBlock/extension/*/typeObjet)][string-length(infoBlock/extension/*/typeObjet)&gt;0]">
								<ID nb="{$typeObjetSOF}">
									<xsl:value-of select="@id"/>
								</ID>
						</xsl:for-each>
					</xsl:if>
				</xsl:when>
				<!--  il faut régler ici la génération des enregistrements oai -->
				<xsl:otherwise>
					<xsl:for-each select="/CDM/program">
						<ID nb="{$typeObjetSOF}">
							<xsl:value-of select="@id"/>
						</ID>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
		</IDs>
	</xsl:template>
	<xsl:template name="extractParcoursIDs">
		<IDs>
			<xsl:for-each select="/CDM/program[/CDM/program/programStructure//refProgram/@idRef=@id]">
				<ID>
					<xsl:value-of select="@id"/>
				</ID>
			</xsl:for-each>
		</IDs>
	</xsl:template>
	<xsl:template name="extractCourseIDs">
		<IDs>
			<xsl:for-each select="//course[not(courseCode[@codeSet='natureUE-EC']/text()='EC')]">
				<ID>
					<xsl:value-of select="@id"/>
				</ID>
			</xsl:for-each>
		</IDs>
	</xsl:template>
</xsl:stylesheet>
