<?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/'>
	<xsl:output method="text" />

	<xsl:template match="text()"/>
	<xsl:template match="root/Schemas/Schema">
		<xsl:for-each select ="users/user">
			-- USER

			do
			$$
			declare
			entid integer;
			usrid uuid;
			identId uuid;
			lognId uuid;
			type integer;
			_salt uuid;
			rc uuid;
			contid integer;
			divid uuid;
			child_schema varchar(100);
			begin
			if exists(select * from VCLib.T_DIVISIONS)
			then
			divid := (select DivisionID from VCLib.T_DIVISIONS order by TreeLevel limit 1);
			else
			begin
			divid := gen_random_uuid();
			insert into VCLib.T_DIVISIONS ( DivisionID, Name, FullName )
			values ( divid, 'Main', 'Main');
			end;
			end if;

			type := case when '<xsl:value-of select="../../../Schema/@Name" />' != ''
			then (SELECT EntityID FROM VCLib.T_ENTITIES WHERE EntName = 'USERS' and schema = '<xsl:value-of select="../../../Schema/@Name" />')
			else (SELECT EntityID FROM VCLib.T_ENTITIES WHERE EntName = 'USERS')
			end;
			
			_salt := gen_random_uuid();

			identId := (select i.IdentityID from VCLib.T_IDENTITY i join VCLib.T_LOGIN l on l.Identity = i.IdentityID where l.Login = '<xsl:value-of select="@Login" />' limit 1);

			if (identId is null)
			then
			begin
			identId := gen_random_uuid();
			lognId := gen_random_uuid();

			insert into VCLib.T_IDENTITY (IdentityID, IdentityName)
			values (identId, '<xsl:value-of select="@UserName" />');

			insert into VCLib.T_LOGIN (LoginID, Login, AuthenticationType, Password, Salt, Flags, Identity, Divisions, IsBlocked)
			values (lognId, '<xsl:value-of select="@Login" />', 0, replace(encode(digest('<xsl:value-of select="@Password" />' || _salt::text, 'sha512'), 'base64'), E'\n', ''), _salt::text, 0, identId, divid, false);
			end;
			else
			begin

			update VCLib.T_IDENTITY
			set IdentityName = '<xsl:value-of select="@UserName" />'
			where IdentityID = identId;

			end;
			end if;

			usrid := (select UserID from VCLib.T_USERS where Identity = identId limit 1);

			if (usrid is null)
			then
			begin
			usrid := gen_random_uuid();
			insert into VCLib.T_USERS (UserID, Flags, DivisionID, Identity, type_)
			values (usrid, 1, divid, identId, @type);

			insert into VCCC.T_USERS (UserID)
			values (usrid);

			insert into <xsl:value-of select="../../@Name" />.T_USERS (UserID)
			values (usrid);
			end;
			end if;

			update VCLib.T_users
			set Flags = case when <xsl:value-of select="@Active" /> = 1 then Flags | 1 else Flags &amp; ~1 end
			where userId = usrid;

			<xsl:if test="@Remote">
				update VCLib.T_users
				set Flags = case when <xsl:value-of select="@Remote" /> = 1 then Flags | 4 else Flags &amp; ~4 end
				where userId = usrid;
			</xsl:if>

			<xsl:for-each select="UserSearches">
				-- user searches
				<xsl:for-each select="UserSearch">
					set rc = gen_random_uuid();
					EntID := (select EntityID from VCLib.T_ENTITIES WHERE EntName='<xsl:value-of select="@Entity" />');
					INSERT INTO VCLib.T_USER_SEARCHES(SearchID,EntityID, UserID, SearchName, FilterValue, SimpleFilter)
					VALUES(rc, EntID, usrId, '<xsl:value-of select="@SearchName" />', '<xsl:value-of select="." />', <xsl:value-of select="@SimpleFilter" />)
				</xsl:for-each>
			</xsl:for-each>
			end;
			$$;
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
