<xsl:stylesheet version="1.0" 
xmlns:query="http://www.novell.com/nxsl/java/com.novell.nds.dirxml.driver.XdsQueryProcessor" 
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										== -->
<!-- ==							- Checks for uniqueness of the Users in eDirectory 	== -->
<!--	======================================================================= -->

<!--	===================================================================== -->
<!-- == 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>
	<xsl:variable name="PassWord"><xsl:value-of select="string(./add-attr[@attr-name='Internet EMail Address'])"/></xsl:variable>
	
	<xsl:message>This will show the content of the Given Name: -><xsl:value-of select="$FirstName"/></xsl:message>
	<xsl:message>This will show the content of the Surname: -><xsl:value-of select="$LastName"/></xsl:message>
	<xsl:message>This will show the content of the new User Name: -><xsl:value-of select="$UserName"/></xsl:message>
	
<!-- ================================================= -->
<!-- == This will perform a query to check for existing users	== -->
<!-- ================================================= -->
	<xsl:variable name="check-for-Users">
		<xsl:call-template name="check-Users">
			<xsl:with-param name="data" select="$UserName"/>
			<xsl:with-param name="query-attr" select="'CN'"/>
		</xsl:call-template>
	</xsl:variable>

<!-- ======================================================================================= -->
<!-- == This will supply us the content of the query for the users and performs a count of the results	== -->
<!-- ======================================================================================= -->
	<xsl:variable name="result-check-for-Users" select="query:query($destQueryProcessor,$check-for-Users)//instance/association"/>
	<xsl:variable name="result-count-for-Users" select="count($result-check-for-Users)"/>
	
	<xsl:message>Number of Users found by the query: <xsl:value-of select="$result-count-for-Users"/></xsl:message>

	<xsl:choose>

<!-- ================================================= -->
<!-- == When there is a User found , do nothing !	== -->
<!-- ================================================= -->
	<xsl:when test="$result-count-for-Users=1">
		<xsl:message>No action is taken , the User did exist in eDirectory !</xsl:message>
	</xsl:when>

<!-- =========================================== -->
<!-- == When there is no user found, create the user !  == -->
<!-- =========================================== -->
	<xsl:when test="$result-count-for-Users=0">
		<xsl:message>Action is taken , the User did not exist in eDirectory !</xsl:message>
		<xsl:copy>
		
<!--===============================================================-->
<!-- == Copy any attributes through except for the src-dn 						== -->
<!-- == we'll construct the src-dn below so that the placement rule will work	== -->
<!--=============================================================== -->
	<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>

<!--================================================================== -->
<!-- == Copy the rest of the stuff through, except for what we have already copied == -->
<!--================================================================== -->
	<xsl:apply-templates select="* | comment() | processing-instruction() | text()"/>

<!-- =================================================================== -->
<!-- == This will generate  a password based on the Internet email address attribute   == -->
<!-- =================================================================== -->
	<password>
		<xsl:value-of select="$PassWord"/>
	</password>

	</xsl:copy>

	</xsl:when>
	</xsl:choose>
	</xsl:template>

<!-- ========================================== -->
<!-- == Template query for existing eDirectory users	== -->
<!-- ========================================== -->
	<xsl:template name="check-Users">
		<xsl:param name="data"/>
		<xsl:param name="query-attr"/>
		<nds dtdversion="1.0" ndsversion="8.5">
			<input>
				<query>
					<search-class class-name="User"/>
					<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>
