<?xml version="1.0" encoding="UTF-8"?>
<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 (352 - lines)										      					== -->
<!--	== Updated:		       04.25.2002 - 17:30														      	== -->
<!--	== Description:		Delimited Text File Driver:													== -->
<!-- == Functionality:				- Creates Users	& Organizational Units								== -->
<!-- == 							- Users are created based on the Title attribute						== -->
<!-- == 								- ACT = Create													== -->
<!-- == 								- UIT = Delete													== -->
<!-- == 								- ANN | DOU | OVG | OVR | PAC = Modify							== -->
<!-- == 							- When the 'Internet Email Address' field is used in a Modify event:				== -->
<!-- == 								- Move the user to the OU that corresponds to the 'Description' attribute			== -->
<!-- == 								- If this OU does not exist create it and perform the move				== -->
<!-- =======================================================================================================  -->

<!--	===================================================================== -->
<!-- == 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 'Title' attribute for what to do with the event.   ==  -->
<!-- ==================================================================== -->
	<xsl:template match="add">
		<xsl:variable name="StatusCheck" select="/nds/input/add/add-attr[@attr-name='Title']/value"/>
		<xsl:variable name="UserName" select="@src-dn"/>
		<xsl:choose>

<!-- ============================= -->
<!-- == When the  User status = ACT	== -->
<!-- ============================= -->
	<xsl:when test="$StatusCheck='ACT'">
	<xsl:message>ACT status works</xsl:message>

<!-- ================================================= -->
<!-- == This will perform a query to check for an existing User	== -->
<!-- ================================================= -->
	<xsl:variable name="check-for-Users-ACT">
		<xsl:call-template name="Check-Objects">
			<xsl:with-param name="class-name" select="'User'"/>
			<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 found user and performs a count of the results	== -->
<!-- =============================================================================== -->
	<xsl:variable name="result-check-for-User-ACT" select="query:query($destQueryProcessor,$check-for-Users-ACT)//instance/association"/>
	<xsl:variable name="result-check-for-src-dn-ACT" select="query:query($destQueryProcessor,$check-for-Users-ACT)//instance/@src-dn"/>
	<xsl:variable name="result-count-for-User-ACT" select="count($result-check-for-User-ACT)"/>
	<xsl:choose>

<!-- ==================================================================== -->
<!-- == When there is a User found , transform the add into a modify for all attributes !== -->
<!-- ==================================================================== -->
	<xsl:when test="$result-count-for-User-ACT=1">
	<xsl:message>User was found !!!!</xsl:message>

<!-- ================ -->
<!-- == Modify event !== -->
<!-- ================ -->
	<modify class-name="User" dest-dn="{$result-check-for-src-dn-ACT}">
		<association>
			<xsl:value-of select="$result-check-for-User-ACT"/>
		</association>
		<xsl:choose>

<!-- ======================================= -->
<!-- == Only modify specified attribute: Surname	== -->
<!-- ======================================= -->
	<xsl:when test="add-attr[@attr-name='Surname']/value">
		<modify-attr attr-name="Surname">
			<remove-all-values/>
			<add-value>
				<value type="string">
					<xsl:value-of select="add-attr[@attr-name='Surname']/value"/>
				</value>
			</add-value>
		</modify-attr>
	</xsl:when>

<!-- =============================== -->
<!-- == Only modify specified attribute: Title == -->
<!-- =============================== -->
	<xsl:when test="add-attr[@attr-name='Title']/value">
		<modify-attr attr-name="Title">
			<remove-all-values/>
			<add-value>
				<value type="string">
					<xsl:value-of select="add-attr[@attr-name='Title']/value"/>
				</value>
			</add-value>
		</modify-attr>
	</xsl:when>
	</xsl:choose>
	</modify>
	</xsl:when>

<!-- ================================================== -->
<!-- == When there is no User found simply create the user	== -->
<!-- ================================================= -->
	<xsl:when test="$result-count-for-User-ACT=0">
	<xsl:message>user was NOT found, user will be created !!!!</xsl:message>
		<xsl:copy>
			<xsl:apply-templates select="node()|@*"/>
		</xsl:copy>
	</xsl:when>
	</xsl:choose>
	</xsl:when>

<!-- ============================= -->
<!-- == When the  User status = UIT	== -->
<!-- ============================= -->
	<xsl:when test="$StatusCheck='UIT'">
	<xsl:message>UIT status works</xsl:message>

<!-- ================================================= -->
<!-- == This will perform a query to check for existing groups	== -->
<!-- ================================================= -->
	<xsl:variable name="check-for-Users-UIT">
		<xsl:call-template name="Check-Objects">
			<xsl:with-param name="class-name" select="'User'"/>
			<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 found users and performs a count of the results	== -->
<!-- =============================================================================== -->
	<xsl:variable name="result-check-for-User-UIT" select="query:query($destQueryProcessor,$check-for-Users-UIT)//instance/association"/>
	<xsl:variable name="result-check-for-src-dn-UIT" select="query:query($destQueryProcessor,$check-for-Users-UIT)//instance/@src-dn"/>
	<xsl:variable name="result-count-for-User-UIT" select="count($result-check-for-User-UIT)"/>
	<xsl:choose>

<!-- ==================================================================== -->
<!-- == When there is a User found , transform the add into a 'delete' for all attributes !== -->
<!-- ==================================================================== -->
	<xsl:when test="$result-count-for-User-UIT=1">

<!-- ==================================== -->
<!-- == User is found, delete the found user	== -->
<!-- ==================================== -->
	<delete class-name="User" dest-dn="{$result-check-for-src-dn-UIT}">
		<association>
			<xsl:value-of select="$result-check-for-User-UIT"/>
		</association>
	</delete>
	</xsl:when>

<!-- ========================================== -->
<!-- == User was not found and can not be deleted !	== -->
<!-- ========================================== -->
	<xsl:otherwise>
		<xsl:message>User not found so do nothing !!!! </xsl:message>
	</xsl:otherwise>
	</xsl:choose>
	</xsl:when>

<!-- ================================================= -->
<!-- == When the  User status = ANN | DOU | OVG | OVR | PAC == -->
<!-- ================================================= -->
	<xsl:when test="$StatusCheck=('ANN') or $StatusCheck=('DOU') or $StatusCheck=('OVG') or $StatusCheck=('OVR') or $StatusCheck=('PAC')">
	<xsl:message>ANN | DOU | OVG | OVR | PAC status works</xsl:message>

<!-- ================================================= -->
<!-- == This will perform a query to check for an existing User	== -->
<!-- ================================================= -->
	<xsl:variable name="check-for-Users-MOD">
		<xsl:call-template name="Check-Objects">
			<xsl:with-param name="class-name" select="'User'"/>
			<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 found user and performs a count of the results	== -->
<!-- =============================================================================== -->
	<xsl:variable name="result-check-for-User-MOD" select="query:query($destQueryProcessor,$check-for-Users-MOD)//instance/association"/>
	<xsl:variable name="result-check-for-src-dn-MOD" select="query:query($destQueryProcessor,$check-for-Users-MOD)//instance/@src-dn"/>
	<xsl:variable name="result-count-for-User-MOD" select="count($result-check-for-User-MOD)"/>
	
	<xsl:variable name="parent-src-dn" select="'\EDIR-XP\COM\NOVELL\Users\DirXML\Moved\'"/>

	<xsl:choose>

	<xsl:when test="$result-count-for-User-MOD=1">
<!-- ================================================================================= -->
<!-- == When there is a User found , check the Internet Email Address field if the user needs to move  !== -->
<!-- ================================================================================== -->
	<xsl:message>User was found !!!!</xsl:message>
	<xsl:choose>
	<xsl:when test="add-attr[@attr-name='Internet EMail Address']/value">

<!-- ================================================= -->
<!-- == This will perform a query to check for an existing User	== -->
<!-- ================================================= -->
	<xsl:variable name="check-for-OU">
		<xsl:call-template name="Check-Objects">
			<xsl:with-param name="class-name" select="'Organizational Unit'"/>
			<xsl:with-param name="data" select="add-attr[@attr-name='Description']/value"/>
			<xsl:with-param name="query-attr" select="'L'"/>
		</xsl:call-template>
	</xsl:variable>

<!-- ======================================================================================= -->
<!-- == This will supply us the content of the found Organizational Unit and performs a count of the results	== -->
<!-- ======================================================================================= -->
	<xsl:variable name="result-check-for-OU" select="query:query($destQueryProcessor,$check-for-OU)//instance"/>
	<xsl:variable name="result-check-for-OU-src-dn" select="query:query($destQueryProcessor,$check-for-OU)//instance/@src-dn"/>
	<xsl:variable name="result-check-for-OU-association" select="query:query($destQueryProcessor,$check-for-OU)//instance/association"/>

	<xsl:variable name="result-count-for-OU" select="count($result-check-for-OU-association)"/>
	<xsl:variable name="dest-src-dn" select="concat($result-check-for-OU-src-dn,'\',$UserName)"/>

	<xsl:variable name="new-src-dn" select="/nds/input/add/add-attr[@attr-name='Description']/value"/>
	<xsl:variable name="newOU-src-dn" select="concat('\EDIR-XP\COM\NOVELL\DirXML\',$new-src-dn)"/>
	
	<xsl:message><xsl:value-of select="$result-count-for-OU"/></xsl:message>

	<xsl:choose>

<!-- =========================================================== -->
<!-- == 0 - No Organizational Unit found = create OU and move User to it. 	== -->
<!--==  1 - Organizational Unit found, move the User to it					== -->
<!-- =========================================================== -->
		<xsl:when test="$result-count-for-OU=1">
			<move class-name="User" old-src-dn="{$result-check-for-src-dn-MOD}" src-dn="{$dest-src-dn}">
				<association>
					<xsl:value-of select="$result-check-for-User-MOD"/>
				</association>
				<parent src-dn="{$parent-src-dn}">
					<association>
						<xsl:value-of select="$result-check-for-OU-association"/>
					</association>
				</parent>
			</move>
		</xsl:when>

		<xsl:when test="$result-count-for-OU=0">
			<xsl:variable name="dest-src-dn-OU" select="concat($newOU-src-dn,'\',$UserName)"/>
			<xsl:variable name="UniqueAssociation" select="concat('Delimited-Text','-',$newOU-src-dn,'\',$UserName)"/>
			
<!-- ======================================================================= -->
<!-- == Create Organizational Unit with the name from the Description attribute			== -->
<!-- ======================================================================= -->
			<add class-name="Organizational Unit" src-dn="{$newOU-src-dn}">
				<association>
					<xsl:value-of select="$UniqueAssociation"/>
				</association>
				<add-attr attr-name="L">
					<value type="string">
						<xsl:value-of select="$new-src-dn"/>
					</value>
				</add-attr>
			</add>

<!-- ====================================================================================== -->
<!-- ==Move the User to the new Organizational Unit with the name from the Description attribute		== -->
<!-- ======================================================================================= -->
			<move class-name="User" old-src-dn="{$result-check-for-src-dn-MOD}" src-dn="{$dest-src-dn-OU}">
				<association>
					<xsl:value-of select="$result-check-for-User-MOD"/>
				</association>
				<parent src-dn="{$parent-src-dn}">
					<association>
						<xsl:value-of select="$UniqueAssociation"/>
					</association>
				</parent>
			</move>			

		</xsl:when>

	</xsl:choose>
	</xsl:when>
	</xsl:choose>
	</xsl:when>

<!-- ======================================= -->
<!-- == When there is no User found do nothing !	== -->
<!-- ======================================= -->
	<xsl:when test="$result-count-for-User-MOD=0">
		<xsl:message>user was NOT found, user will not be moved or modified !!!!</xsl:message>
	</xsl:when>
	</xsl:choose>
	</xsl:when>

<!-- ================================================================= -->
<!-- == When the  User status was not = ACT | ANN | DOU | OVG | OVR | PAC | UIT == -->
<!-- ================================================================= -->
	<xsl:when test="$StatusCheck!='ACT' or $StatusCheck!='ANN' or $StatusCheck!='DOU' or $StatusCheck!='OVG' or $StatusCheck!='OVR' or $StatusCheck!='PAC' or $StatusCheck!='UIT'">
		<xsl:message>Status was not: ACT | ANN | DOU | OVG | OVR | PAC | UIT</xsl:message>
	</xsl:when>
	</xsl:choose>
	</xsl:template>

<!-- ====================== -->
<!-- == Match modify events	==  -->
<!-- ====================== -->
	<xsl:template match="modify"/>

<!-- ======================= -->
<!-- == Match delete events	==  -->
<!-- ======================= -->
	<xsl:template match="delete"/>

<!-- ============================================================ -->
<!-- == 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>

<!-- ==================================== -->
<!-- == Template query for existing Objects	== -->
<!-- ==================================== -->
	<xsl:template name="Check-Objects">
		<xsl:param name="class-name"/>
		<xsl:param name="data"/>
		<xsl:param name="query-attr"/>
		<nds dtdversion="1.0" ndsversion="8.5">
			<input>
				<query>
					<search-class class-name="{$class-name}"/>
					<search-attr attr-name="{$query-attr}">
						<value>
							<xsl:value-of select="$data"/>
						</value>
					</search-attr>
					<read-attr/>
				</query>
			</input>
		</nds>
	</xsl:template>

</xsl:stylesheet>
