<xsl:stylesheet version="1.0" 
xmlns:query="http://www.novell.com/nxsl/java/com.novell.nds.dirxml.driver.XdsQueryProcessor" 
xmlns:ncs="http://www.novell.com/nxsl/java/com.novell.ncs.dirxml.utilities.Utils" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- =======================================================================  -->
<!--	== Driver: 			Delimited text-file Driver on WindowsXP professional 			     	== -->
<!--	== Channel: 		Publisher													      	== -->
<!--	== Stylesheet:		PUB - Event Transformation							      			== -->
<!--	== Author:		       DirXML believer, Netherlands						       	          	== -->
<!--	== Customer:		Personal - XP-eDirectory								      			== -->
<!--	== Versions:		1.01 (115- lines)											      		== -->
<!--	== Updated:		       04.04.2002 - 15:56											      	== -->
<!--	== Description:		Delimited Text File Driver:										== -->
<!-- == Functionality:				- Creates Users	& Groups								== -->
<!-- ==									- Checks for group objects to associate the user with 	== -->
<!-- ==									- Adds the users to a eDirectory group object	 	== -->
<!--	======================================================================= -->

<!--	===================================================================== -->
<!-- == This is for testing the stylesheet outside of DirXML so things are pretty to look at == -->
<!--	===================================================================== -->
	<xsl:strip-space elements="*"/>
	<xsl:preserve-space elements="value component"/>
	<xsl:output indent="yes" method="xml"/>

<!-- ================================================================== -->
<!-- == Query processor variables are needed to perform queries in the stylesheet  ==-->
<!-- ================================================================== -->
	<xsl:param name="srcQueryProcessor"/>
	<xsl:param name="destQueryProcessor"/>

<!-- ==================================================================== -->
<!-- == Match add events and check the action attribute what to do with the event.   	==  -->
<!-- ==================================================================== -->
	<xsl:template match="add">

<!-- ======================================================================= -->
<!-- == Defining the "attribute" variables we are going to use in the rest of the stylesheet	== -->
<!-- ======================================================================= -->
	<xsl:variable name="FirstName"><xsl:value-of select="string(./add-attr[@attr-name='Given Name'])"/></xsl:variable>
	<xsl:variable name="LastName"><xsl:value-of select="string(./add-attr[@attr-name='Surname'])"/></xsl:variable>
	<xsl:variable name="UserName"><xsl:value-of select="concat($FirstName,' ',$LastName)"/></xsl:variable>

<!-- ==========================================================-->
<!-- == Variables needed to create a src-dn for the group & a group name == -->
<!-- ========================================================== -->
	<xsl:variable name="Title"><xsl:value-of select="string(./add-attr[@attr-name='Title'])"/></xsl:variable>
	<xsl:variable name="DirXMLgroup"><xsl:value-of select="concat('\EDIR-XP\COM\NOVELL\Groups\',$Title)"/></xsl:variable>
	
<!-- ======================================================================================= -->
<!-- == This will perform a query to check for existing groups based on the 'title' attribute of the user		== -->
<!-- ======================================================================================= -->
	<xsl:variable name="check-for-Groups">
		<xsl:call-template name="check-Groups">
			<xsl:with-param name="data" select="$Title"/>
			<xsl:with-param name="query-attr" select="'CN'"/>
		</xsl:call-template>
	</xsl:variable>

<!-- =============================================================================== -->
<!-- == This will supply us the content of the old subjectnummer and performs a count of the results	== -->
<!-- =============================================================================== -->
	<xsl:variable name="result-check-for-Groups" select="query:query($destQueryProcessor,$check-for-Groups)//instance/association"/>
	<xsl:variable name="result-count-for-Groups" select="count($result-check-for-Groups)"/>

<!-- =========================================== -->
<!-- == When there is a Group found , do nothing !		== -->
<!-- =========================================== -->
	<xsl:choose>
	<xsl:when test="$result-count-for-Groups=1">
		<xsl:message>No action is taken , the Group did exist in eDirectory !</xsl:message>
	</xsl:when>

<!-- =============================================================== -->
<!-- == When there is no Group found , create the group and add the user to it	== -->
<!-- =============================================================== -->
	<xsl:when test="$result-count-for-Groups=0">
		<xsl:message>Action is taken , Group will be created in eDirectory !</xsl:message>

		<add class-name="Group" src-dn="{$DirXMLgroup}">
			<add-attr attr-name="CN">
				<value type="string">
					<xsl:value-of select="$Title"/>
				</value>
			</add-attr>
		</add>

	</xsl:when>
	</xsl:choose>	
	
<!--===============================================================-->
<!-- == Copy any attributes through except for the src-dn 						== -->
<!--=============================================================== -->
	<xsl:copy>
	<xsl:apply-templates select="@*[name!='src-dn']"/>

<!-- =================== -->
<!-- == Setup src-dn		== -->
<!-- =================== -->
	<xsl:attribute name="src-dn"><xsl:value-of select="$UserName"/></xsl:attribute>

<!-- ========================= -->
<!-- == Setup the CN of the User	== -->
<!-- ========================= -->
	<add-attr attr-name="CN">
		<value type="string">
			<xsl:value-of select="$UserName"/>
		</value>
	</add-attr>
	
<!-- =============================================================================================== -->
<!-- == Add the Users to a eDirectory group based on the 'title' attribute of the user that is created earlier on !!! 	== -->
<!-- =============================================================================================== -->
	<add-attr attr-name="Group Membership">
		<add-value>
			<value type="dn">
				<xsl:value-of select="$DirXMLgroup"/>
			</value>
		</add-value>
	</add-attr>
	
<!--================================================================== -->
<!-- == Copy the rest of the stuff through, except for what we have already copied == -->
<!--================================================================== -->
	<xsl:apply-templates select="* | comment() | processing-instruction() | text()"/>

	</xsl:copy>
	</xsl:template>

<!-- ==================================== -->
<!-- == Template query for existing Groups	== -->
<!-- ==================================== -->
	<xsl:template name="check-Groups">
		<xsl:param name="data"/>
		<xsl:param name="query-attr"/>
		<nds dtdversion="1.0" ndsversion="8.5">
			<input>
				<query>
					<search-class class-name="Group"/>
					<search-attr attr-name="{$query-attr}">
						<value>
							<xsl:value-of select="$data"/>
						</value>
					</search-attr>
					<read-attr/>
				</query>
			</input>
		</nds>
	</xsl:template>
	
<!-- ============================================================ -->
<!-- == identity transform for everything we don't want to mess with 		  == -->
<!-- ============================================================ -->
	<xsl:template match="node()|@* ">
		<xsl:copy>
			<xsl:apply-templates select="node()|@*"/>
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>
