<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	xmlns:user="http://mycompany.com/mynamespace" xmlns:js="urn:the-xml-files:xslt-csharp" xmlns:ntd='http://www.ntd.ru/ntd/'
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<xsl:output method="text" />

	<xsl:template match="text()"/>
	<!-- Postponed Delete Entity Settings -->
	<xsl:template match="IES.POSTPONED_DELETE_ENTITY_group">
		<xsl:for-each select="IES.POSTPONED_DELETE_ENTITY">
			Delete from IES.T_POSTPONED_DELETE_ENTITY tp where tp.Name = '<xsl:value-of select="Name" />';
			Insert into IES.T_POSTPONED_DELETE_ENTITY
				(PostponedDeleteEntityID
				,Name
				,ParentTriggerPaths
				,IsSystem
				,"order"
				,IsActive
				,DependentPaths)
			Values ( 
				gen_random_uuid()
				,'<xsl:value-of select="Name"/>'
				,'<xsl:value-of select="ParentTriggerPaths"/>'
				,'<xsl:value-of select="IsSystem"/>'
				,'<xsl:value-of select="Order"/>'
				,'<xsl:value-of select="IsActive"/>'
				,'<xsl:value-of select="DependentPaths"/>');
		</xsl:for-each>
	</xsl:template>
	<!--  criteria group Entity Import Filters -->
	<xsl:template match="IES.CRITERIA_GROUP_group">
		<xsl:for-each select="IES.CRITERIA_GROUP">
			do
			$$
			declare
			criteriaGroupId uuid;
			criteriaItemId uuid;
			begin
			criteriaGroupId = gen_random_uuid();
			Insert into IES.T_CRITERIA_GROUP
			(CriteriaGroupID
			,Name
			,ExpertiseType
			,Condition
			,Priority
			,IsActive
			,Entities
			,ItemEntity
			<xsl:if test="F006ControlType/ArrayOfUniquenessField/UniquenessField/@Value != ''">
				,F006ControlType
			</xsl:if>)
			Values(criteriaGroupId
			,'<xsl:value-of select="Name"/>'
			,'<xsl:value-of select="ExpertiseType"/>'
			,'<xsl:call-template name="serializeNodeToString">
				<xsl:with-param name="node" select="Condition/*"/>
			</xsl:call-template>'
			,'<xsl:value-of select="Priority"/>'
			,'<xsl:value-of select="IsActive"/>'
			,(select e1.EntityID from VCLib.T_ENTITIES e1 where
			e1.Schema = '<xsl:value-of select="substring-before(Entities, '.')"/>' and
			e1.EntName = '<xsl:value-of select="substring-after(Entities, '.')"/>')
			,(select e1.EntityID from VCLib.T_ENTITIES e1 where
			e1.Schema = '<xsl:value-of select="substring-before(ItemEntity, '.')"/>' and
			e1.EntName = '<xsl:value-of select="substring-after(ItemEntity, '.')"/>')
			<xsl:if test="F006ControlType/ArrayOfUniquenessField/UniquenessField/@Value != ''">
				,(select F006ControlTypeID from IES.T_F006_CONTROL_TYPE  
					where 1=1  <xsl:for-each select="F006ControlType/ArrayOfUniquenessField/UniquenessField">
									<xsl:if test="@Value != ''"> 
						and <xsl:value-of select="@StepString"/> = '<xsl:value-of select="@Value"/>'
									</xsl:if>
					</xsl:for-each> limit 1)
			</xsl:if>);
			<xsl:for-each select="CRITERIA_ITEM">
				<xsl:for-each select="IES.CRITERIA_ITEM_CODE">
					begin
					criteriaItemId = select gen_random_uuid();
					Insert into IES.T_CRITERIA_ITEM
					(CriteriaItemID
					,CriteriaGroup
					,DateBegin
					<xsl:if test="DateEnd != ''">
					    ,DateEnd
				    </xsl:if>
					,Description
					,type_
					,Priority)
					Values(criteriaItemId
					,criteriaGroupId
					,'<xsl:value-of select="DateBegin"/>'
					<xsl:if test="DateEnd != ''">
						,'<xsl:value-of select="DateEnd"/>'
					</xsl:if>
					,'<xsl:value-of select="Description"/>'
					,(select e1.EntityID from VCLib.T_ENTITIES e1 where
					e1.Schema ||'.'|| e1.EntName = '<xsl:value-of select="local-name()"/>')
					,'<xsl:value-of select="Priority"/>');
					Insert into IES.T_CRITERIA_ITEM_CODE
					(Source
					,CriteriaItemID)
					Values('<xsl:value-of select="Source"/>'
					,criteriaItemId);
					end;
				</xsl:for-each>
				<xsl:for-each select="IES.CRITERIA_ITEM_LEXEM">
					begin
					criteriaItemId = gen_random_uuid();
					Insert into IES.T_CRITERIA_ITEM
					(CriteriaItemID
					,CriteriaGroup
					,DateBegin
					<xsl:if test="DateEnd != ''">
					    ,DateEnd
				    </xsl:if>
					,Description
					,type_
					,Priority)
					Values(criteriaItemId
					,criteriaGroupId
					,'<xsl:value-of select="DateBegin"/>'
					<xsl:if test="DateEnd != ''">
						,'<xsl:value-of select="DateEnd"/>'
					</xsl:if>
					,'<xsl:value-of select="Description"/>'
					,(select e1.EntityID from VCLib.T_ENTITIES e1 where
					e1.Schema ||'.'|| e1.EntName = '<xsl:value-of select="local-name()"/>')
					,'<xsl:value-of select="Priority"/>');
					Insert into IES.T_CRITERIA_ITEM_LEXEM
						(Condition
					,ExpertiseError
					<xsl:if test="MakeCode != ''">
						,MakeCode
					</xsl:if>
					,CriteriaItemID
					<xsl:if test="DescriptionCondition != ''">
					    ,DescriptionCondition
					</xsl:if>
					<xsl:if test="LinksCondition != ''">
					    ,LinksCondition
				    </xsl:if>
					<xsl:if test="Test != ''">
						,Test
					</xsl:if>)
					Values('<xsl:call-template name="serializeNodeToString">
					            <xsl:with-param name="node" select="Condition/*"/>
				            </xsl:call-template>'
					,'<xsl:value-of select="ExpertiseError"/>'
					<xsl:if test="MakeCode != ''">
					    ,'<xsl:value-of select="MakeCode"/>'
					</xsl:if>
					,criteriaItemId
					<xsl:if test="DescriptionCondition != ''">
						,'<xsl:value-of select="DescriptionCondition"/>'
					</xsl:if>
					<xsl:if test="LinksCondition != ''">
						,'<xsl:value-of select="LinksCondition"/>'
					</xsl:if>
					<xsl:if test="Test != ''">
						,'<xsl:value-of select="Test"/>'
					</xsl:if>);
					end;
				</xsl:for-each>
			</xsl:for-each>
			end;
			$$;
		</xsl:for-each>
	</xsl:template>
	<!-- Load Settigs-->
	<xsl:template match="IES.LOAD_SETTINGS_group">
		<xsl:for-each select="IES.LOAD_SETTINGS">
			Insert into IES.LOAD_SETTINGS
			(LoadSettingsID
			,Name
			,CreateData)
			Values ( 
			gen_random_uuid(),
			'<xsl:value-of select="Name"/>',
			'<xsl:value-of select="CreateData"/>');
		</xsl:for-each>
	</xsl:template>
	<!-- Verification Pattern -->
	<xsl:template match="IES.VERIFICATION_PATTERN_group">
		<xsl:for-each select="IES.VERIFICATION_PATTERN">
			do
			$$
			declare
			verificationPatternID uuid
			begin
			verificationPatternID := select gen_random_uuid();
			Insert into IES.T_VERIFICATION_PATTERN
			(VerificationPatternID,
			Name,
			Descr,
			Version,
			IgnoreFileCount,
			Procedure,
			IsBulk,
			IsAdditional)
			Values(
			verificationPatternID
			,'<xsl:value-of select="Name"/>'
			,'<xsl:value-of select="Descr"/>'
			,'<xsl:value-of select="Version"/>'
			,'<xsl:value-of select="IgnoreFileCount"/>'
			,'<xsl:value-of select="Procedure"/>'
			,'<xsl:value-of select="IsBulk"/>'
			,'<xsl:value-of select="IsAdditional"/>');
			<xsl:for-each select="FILE_PATTERN">
				<xsl:for-each select="IES.FILE_PATTERN">
					insert into IES.T_FILE_PATTERN
					(FilePatternID,
					VerificationPattern,
					Pattern,
					XsdSchema,
					LoadSchema,
					Procedure,
					Version,
					IsRequired)
					Values(
					gen_random_uuid()
					,verificationPatternID
					,'<xsl:value-of select="Pattern"/>'
					,'<xsl:value-of select="XsdSchema"/>'
					,'<xsl:value-of select="LoadSchema"/>'
					,'<xsl:value-of select="Procedure"/>'
					,'<xsl:value-of select="Version"/>'
					,'<xsl:value-of select="IsRequired"/>');
				</xsl:for-each>
			</xsl:for-each>
			end;
			$$;
		</xsl:for-each>
	</xsl:template>
	<!-- Load Settings Pattern-->
	<xsl:template match="IES.LOAD_SETTINGS_PATTERNS_group">
		<xsl:for-each select="IES.LOAD_SETTINGS_PATTERNS">
			Insert into IES.T_LOAD_SETTINGS_PATTERNS
			(LoadSettingsPatternsID,
			LoadSettings,
			VerificationPattern)
			Values(
				gen_random_uuid()
				,'<xsl:value-of select="LoadSettings"/>'
				,'<xsl:value-of select="VerificationPattern"/>');
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template name="serializeNodeToString">
		<xsl:param name="node" />
		<xsl:variable name="name" select="name($node)" />

		<xsl:text>&lt;</xsl:text>
		<xsl:value-of select="$name" />
		<xsl:for-each select="$node/@*">
			<xsl:text> </xsl:text>
			<xsl:value-of select="name()" />
			<xsl:text>=&quot;</xsl:text>
			<xsl:call-template name ="replaceSpecCharacters">
				<xsl:with-param name="text" select="."/>
			</xsl:call-template>
			<xsl:text>&quot;</xsl:text>
		</xsl:for-each>
		<xsl:if test="namespace-uri($node)">
			<xsl:text> xmlns:</xsl:text>
			<xsl:value-of select="substring-before(name($node),':')"/>
			<xsl:text>="</xsl:text>
			<xsl:value-of select="namespace-uri($node)"/>
			<xsl:text>"</xsl:text>
		</xsl:if>
		<xsl:text>&gt;</xsl:text>
		<xsl:if test="$node/text()">
			<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
			<xsl:value-of select="$node/text()" />
			<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
		</xsl:if>
		<xsl:for-each select="$node/*">
			<xsl:call-template name="serializeNodeToString">
				<xsl:with-param name="node" select="."/>
			</xsl:call-template>
		</xsl:for-each>

		<xsl:text>&lt;/</xsl:text>
		<xsl:value-of select="$name" />
		<xsl:text>&gt;</xsl:text>
	</xsl:template>
	<xsl:template name="replaceSpecCharacters">
		<xsl:param name="text" />

		<xsl:call-template name ="replace">
			<xsl:with-param name="text">
				<xsl:call-template name ="replace">
					<xsl:with-param name="text">
						<xsl:call-template name ="replace">
							<xsl:with-param name="text">
								<xsl:call-template name ="replace">
									<xsl:with-param name="text">
										<xsl:call-template name ="replace">
											<xsl:with-param name="text" select="$text"/>
											<xsl:with-param name="replace">&amp;</xsl:with-param>
											<xsl:with-param name="by">&amp;amp;</xsl:with-param>
										</xsl:call-template>
									</xsl:with-param>
									<xsl:with-param name="replace">&lt;</xsl:with-param>
									<xsl:with-param name="by">&amp;lt;</xsl:with-param>
								</xsl:call-template>
							</xsl:with-param>
							<xsl:with-param name="replace">&gt;</xsl:with-param>
							<xsl:with-param name="by">&amp;gt;</xsl:with-param>
						</xsl:call-template>
					</xsl:with-param>
					<xsl:with-param name="replace">&apos;</xsl:with-param>
					<xsl:with-param name="by">&amp;apos;</xsl:with-param>
				</xsl:call-template>
			</xsl:with-param>
			<xsl:with-param name="replace">&quot;</xsl:with-param>
			<xsl:with-param name="by">&amp;quot;</xsl:with-param>
		</xsl:call-template>
	</xsl:template>
	
	<xsl:template name ="replace">
		<xsl:param name="text"/>
		<xsl:param name="replace"/>
		<xsl:param name="by"/>
		<xsl:choose>
			<xsl:when test="contains($text, $replace)">
				<xsl:value-of select="substring-before($text,$replace)" />
				<xsl:value-of select="$by" />
				<xsl:call-template name="replace">
					<xsl:with-param name="text" select="substring-after($text,$replace)" />
					<xsl:with-param name ="replace" select="$replace"/>
					<xsl:with-param name ="by" select="$by"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$text" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>