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

  <!--<xsl:template match="Entity">
    <xsl:choose>
      <xsl:when test="@Index">
        IF NOT EXISTS(SELECT EntityID FROM T_ENTITIES WHERE EntityID=<xsl:value-of select="@Index" />)
        BEGIN
        INSERT INTO T_ENTITIES (EntityID, EntName, XML, ServerName, EntDesc, NeedHistory, BaseEntity)
        VALUES (<xsl:value-of select="@Index" />,'<xsl:value-of select="@Name" />',NULL,'Primary SQL Server','<xsl:value-of select="@Description" />',0, (select top 1 EntityID from T_ENTITIES where EntName = '<xsl:value-of select="@Inherits" />'))
        END
        ELSE BEGIN
        UPDATE T_ENTITIES SET EntName='<xsl:value-of select="@Name" />',EntDesc='<xsl:value-of select="@Description" />',BaseEntity=(select top 1 EntityID from T_ENTITIES where EntName = '<xsl:value-of select="@Inherits" />') WHERE EntityID=<xsl:value-of select="@Index" />
        END
        GO
        UPDATE T_ENTITIES SET XML='<xsl:call-template name="string-replace-all"><xsl:with-param name="text" select="$XmlText" /><xsl:with-param name="replace" select="$apos" /><xsl:with-param name="by" select="$apos2" /></xsl:call-template>' WHERE EntityID=<xsl:value-of select="@Index" />
        GO
      </xsl:when>
      <xsl:otherwise>
        IF NOT EXISTS(SELECT * FROM VCLib.T_ENTITIES WHERE [Schema]='<xsl:value-of select="@Schema" />' and EntName='<xsl:value-of select="@Name" />')
        BEGIN
        INSERT INTO VCLib.T_ENTITIES (EntityID, EntName, XML, ServerName, EntDesc, NeedHistory, BaseEntity, [Schema])
        VALUES ((isnull((select top 1 EntityID from VCLib.T_ENTITIES order by EntityID desc), 0) + 1),'<xsl:value-of select="@Name" />',NULL,'Primary SQL Server','<xsl:value-of select="@Description" />',0, (select top 1 EntityID from VCLib.T_ENTITIES where [Schema]='<xsl:value-of select="substring-before(@Inherits, '.')" />' and EntName = '<xsl:value-of select="substring-after(@Inherits, '.')" />'), '<xsl:value-of select="@Schema" />')
        END
        ELSE BEGIN
        UPDATE VCLib.T_ENTITIES SET EntDesc='<xsl:value-of select="@Description" />',BaseEntity=(select top 1 EntityID from VCLib.T_ENTITIES where [Schema]='<xsl:value-of select="substring-before(@Inherits, '.')" />' and EntName = '<xsl:value-of select="substring-after(@Inherits, '.')" />') WHERE [Schema]='<xsl:value-of select="@Schema" />' and EntName='<xsl:value-of select="@Name" />'
        END
        GO
        UPDATE VCLib.T_ENTITIES SET XML='<xsl:call-template name="string-replace-all"><xsl:with-param name="text" select="$XmlText" /><xsl:with-param name="replace" select="$apos" /><xsl:with-param name="by" select="$apos2" /></xsl:call-template>' WHERE [Schema]='<xsl:value-of select="@Schema" />' and EntName='<xsl:value-of select="@Name" />'
        GO
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>-->

  <xsl:template match="root/users/user">
    --RAW
    DECLARE entityid_ int;userid_ raw(16);type_ int;rc raw(16);exist int;typeus int;
    BEGIN

    SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'USERS';
    SELECT count(*) INTO exist FROM T_USERS WHERE Login = '<xsl:value-of select="@Login" />';
    IF exist = 0 THEN
    rc := SYS_GUID();
    INSERT INTO T_USERS (userID, Login, UserName, Active, type_)
    VALUES (rc, '<xsl:value-of select="@Login" />', '<xsl:value-of select="@UserName" />', 1, type_);
    userid_ := rc;


    <!--__AuthType:Basic-->
    <!--<xsl:variable name="salt" select="user:createSalt()" />-->
    INSERT INTO T_PA_USERS (UserID, Password, Salt)
    VALUES(rc, '<!--<xsl:value-of select="user:Hash( @Password, $salt )"/>-->', '<!--<xsl:value-of select="$salt" />-->');
    <!--__AuthType_END-->
    <!--__AuthType:SSPI-->
    INSERT INTO T_WA_USERS (UserID) VALUES (rc);
    <!--__AuthType_END-->


    ELSE
    SELECT userid INTO userid_ FROM T_USERS WHERE Login = '<xsl:value-of select="@Login" />';
    UPDATE t_users
    SET UserName = '<xsl:value-of select="@UserName"/>',
    Active = <xsl:value-of select="@Active" />
    WHERE userId = userid_;
    END IF;
    <xsl:for-each select="UserSearches">
      -- user searches
      SELECT EntityID INTO typeus FROM T_ENTITIES WHERE EntName = 'USER_SEARCHES';
      <xsl:for-each select="UserSearch">
        rc := SYS_GUID();
        SELECT EntityID INTO entityid_ from T_ENTITIES WHERE EntName='<xsl:value-of select="@Entity" />';
        INSERT INTO T_USER_SEARCHES(SearchID,EntityID, UserID, SearchName, FilterValue)
        VALUES(rc, entityid_, userid_, '<xsl:value-of select="@SearchName" />', '<xsl:value-of select="." />');
      </xsl:for-each>

    </xsl:for-each>
    COMMIT;
    END;
    --ENDRAW
  </xsl:template>
  <xsl:template match="root/client_modules/client_module">
    --RAW
    DECLARE /*type_ int; */id_ raw(16); priority int; exist int;
    BEGIN
    SELECT count(*) INTO exist FROM T_CLIENT_MODULES WHERE ModuleType = '<xsl:value-of select="@ModuleType" />';
    IF exist = 0 THEN
    /*SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'CLIENT_MODULES';*/
    priority := <xsl:choose>
      <xsl:when test="@Priority">
        <xsl:value-of select="@Priority" />
      </xsl:when>
      <xsl:otherwise>10</xsl:otherwise>
    </xsl:choose>;
    id_ := SYS_GUID();
    INSERT INTO T_CLIENT_MODULES (ClientModuleID, ModuleType, ModuleDescription, Priority/*, type_*/)
    VALUES (id_, '<xsl:value-of select="@ModuleType" />', '<xsl:value-of select="@ModuleDescription" />', priority/*, type_*/);
    COMMIT;
    END IF;
    COMMIT;
    END;
    --ENDRAW
  </xsl:template>
  <xsl:template match="root/CommonUserColumns">
    <xsl:for-each select="UserColumn">
      --RAW
      DECLARE
      EntityID_ INT;
      rc raw(16);
      Context_ varchar2(255);
      contId_ raw(16);
      type_ INT;
      /*typeuct INT;*/
      BEGIN
      SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'USER_COLUMNS';
      /*SELECT EntityID INTO typeuct FROM T_ENTITIES WHERE EntName = 'USER_CONTEXTS';*/
      SELECT EntityID INTO EntityID_ from T_ENTITIES WHERE EntName='<xsl:value-of select="@Entity" />';

      Context_ :=	<xsl:choose>
        <xsl:when test="@UserContext">
          '<xsl:value-of select="@UserContext" />'
        </xsl:when>
        <xsl:otherwise>NULL</xsl:otherwise>
      </xsl:choose>;

      if Context_ is NULL THEN
      begin
      SELECT UserContextID INTO contId_ FROM T_USER_CONTEXTS WHERE
      Context is NULL AND EntityID = EntityID_ AND UserID is NULL;
      exception
      when NO_DATA_FOUND then contId_ := NULL;
      end;
      else
      begin
      SELECT UserContextID INTO contId_ FROM T_USER_CONTEXTS WHERE
      Context = Context_ AND EntityID = EntityID_ AND UserID is NULL;
      exception
      when NO_DATA_FOUND then contId_ := NULL;
      end;
      end if;

      if contId_ is NULL then
      contId_ := SYS_GUID();
      INSERT INTO T_USER_CONTEXTS (UserContextID, EntityID, UserID, Context/*, type_*/)
      VALUES (contId_, EntityID_, null, Context_/*, typeuct*/);
      end if;

      rc := SYS_GUID();

      INSERT INTO T_USER_COLUMNS(UserColsId, AttributePath, DisplayName, ColumnIndex, ColumnWidth, Visible, SortingIndex, Sorting, UserContextID)
      VALUES(rc, '<xsl:value-of select="@AttributePath" />',
      '<xsl:value-of select="@DisplayName" />', <xsl:value-of select="@ColumnIndex" />,
      <xsl:value-of select="@ColumnWidth" />, 1, 0, 0, contId_);
      COMMIT;
      END;
      --ENDRAW
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="root/CommonUserSearches">
    --RAW
    DECLARE EntityID_ INT;
    rc raw(16);
    BEGIN
    <xsl:for-each select="UserSearch">
      rc := SYS_GUID();
      SELECT EntityID INTO EntityID_ from T_ENTITIES WHERE EntName='<xsl:value-of select="@Entity" />';
      INSERT INTO T_USER_SEARCHES(SearchId,EntityID, UserID, SearchName, FilterValue)
      VALUES(rc, EntityID_, null, '<xsl:value-of select="@SearchName" />', '<xsl:value-of select="." />');
    </xsl:for-each>
    COMMIT;
    END;
    --ENDRAW
  </xsl:template>
  <xsl:template match="EnumTypes">
    <xsl:apply-templates select="EnumType" />
    --RAW
    declare FieldName_ varchar(256);
    isexist int;
    BEGIN
    FieldName_ := 'PrimaryKey';
    begin
    select id into isexist from T_ENTITY_IDS where EntName = 'ENUM_TYPES' and FieldName = FieldName_;
    exception
    when NO_DATA_FOUND then isexist := NULL;
    end;

    if isexist is null then
    insert INTO T_ENTITY_IDS (id, EntName, FieldName) values(<xsl:value-of select="@next-enum-type-id" />, 'ENUM_TYPES', FieldName_);
    else
    UPDATE T_ENTITY_IDS SET id=<xsl:value-of select="@next-enum-type-id" />  WHERE EntName='ENUM_TYPES';
    end if;

    begin
    select id into isexist from T_ENTITY_IDS where EntName = 'ENUMS' and FieldName = 'PrimaryKey';
    exception
    when NO_DATA_FOUND then isexist := NULL;
    end;

    if isexist is null then
    insert INTO T_ENTITY_IDS (id, EntName, FieldName) values(<xsl:value-of select="@next-enum-id" />, 'ENUMS', FieldName_);
    else
    UPDATE T_ENTITY_IDS SET id=<xsl:value-of select="@next-enum-id" />
    WHERE EntName='ENUMS';
    end if;
    COMMIT;
    END;
    --ENDRAW
  </xsl:template>
  <xsl:template match="EnumType">
    --RAW
    DECLARE /*typeT INT;*/
    /*typeE INT;*/
    BEGIN
    /*SELECT EntityID INTO typeT FROM T_ENTITIES WHERE EntName = 'ENUM_TYPES';*/
    /*SELECT EntityID INTO typeE FROM T_ENTITIES WHERE EntName = 'ENUMS';*/
    <xsl:variable name="EnumTypeId" select="@id" />
    INSERT INTO T_ENUM_TYPES (EnumTypeId,Name, Description/*, type_*/) VALUES(<xsl:value-of select="$EnumTypeId" />,'<xsl:value-of select="@Name" />', '<xsl:value-of select="@Description" />'/*,typeT*/);
    <xsl:for-each select="Enum">
      INSERT INTO T_ENUMS (EnumID,EnumTypeID, Code, Name, ShortName, Active, Alias/*, type_*/) VALUES(<xsl:value-of select="@id" />,<xsl:value-of select="$EnumTypeId" />, <xsl:value-of select="@Code" />, '<xsl:value-of select="@Name" />','<xsl:value-of select="@ShortName" />',<xsl:value-of select="@Active" />, '<xsl:value-of select="@Alias" />'/*, typeE*/);
    </xsl:for-each>
    COMMIT;
    END;
    --ENDRAW
  </xsl:template>
  <xsl:template match="root/process_access_entries/process_access_entry">
    --RAW
    DECLARE type_ INT;
    BEGIN
    SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'PROCESS_ACCESS_ENTRY';

    INSERT INTO T_BASE_ACCESS_ENTRY
    (EntryID, EntryDescription, type_)
    VALUES
    (TRANSLATE('<xsl:value-of select="@EntryID" />', 'ABCDEF0123456789{}-', 'ABCDEF0123456789'),
    '<xsl:value-of select="@EntryDescription" />',
    type_);
    INSERT INTO T_PROCESS_ACCESS_ENTRY
    (EntryID, LexemContent, ProcessName, Priority, Flags,
    EntityID, CheckInputObject, CheckDBObject)
    VALUES
    (TRANSLATE('<xsl:value-of select="@EntryID" />', 'ABCDEF0123456789{}-', 'ABCDEF0123456789'),
    '<xsl:value-of select="@LexemContent" />',
    '<xsl:value-of select="@ProcessName" />',
    <xsl:value-of select="@Priority" />,
    <xsl:value-of select="@Flags" />,
    <xsl:value-of select="@EntityID" />,
    <xsl:value-of select="@CheckInputObject" />,
    <xsl:value-of select="@CheckDBObject" />);
    COMMIT;
    END;
    --ENDRAW
  </xsl:template>
  <xsl:template match="root/report_access_entries/report_access_entry">
    --RAW
    DECLARE type_ INT;

    BEGIN

    SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'REPORT_ACCESS_ENTRY';

    INSERT INTO T_BASE_ACCESS_ENTRY
    (EntryID, EntryDescription, type_)
    VALUES
    (TRANSLATE('<xsl:value-of select="@EntryID" />', 'ABCDEF0123456789{}-', 'ABCDEF0123456789'),
    '<xsl:value-of select="@EntryDescription" />',
    type_);
    INSERT INTO T_REPORT_ACCESS_ENTRY
    (EntryID, Enabled, ReportID)
    VALUES
    (TRANSLATE('<xsl:value-of select="@EntryID" />', 'ABCDEF0123456789{}-', 'ABCDEF0123456789'),
    <xsl:value-of select="@Enabled" />,
    <xsl:value-of select="@ReportID" />);
    COMMIT;
    END;
    --ENDRAW
  </xsl:template>
  <xsl:template match="root/entity_access_entries/entity_access_entry">
    --RAW
    DECLARE type_ INT;

    BEGIN

    SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'ENTITY_ACCESS_ENTRY';

    INSERT INTO T_BASE_ACCESS_ENTRY
    (EntryID, EntryDescription, type_)
    VALUES
    (TRANSLATE('<xsl:value-of select="@EntryID" />', 'ABCDEF0123456789{}-', 'ABCDEF0123456789'),
    '<xsl:value-of select="@EntryDescription" />',
    type_);
    INSERT INTO T_ENTITY_ACCESS_ENTRY
    (EntryID, Access_, LexemContent, EntityID)
    VALUES
    (TRANSLATE('<xsl:value-of select="@EntryID" />', 'ABCDEF0123456789{}-', 'ABCDEF0123456789'),
    <xsl:value-of select="@Access" />,
    '<xsl:value-of select="@LexemContent" />',
    <xsl:value-of select="@EntityID" />);
    COMMIT;
    END;
    --ENDRAW
  </xsl:template>
  <xsl:template match="root/entity_attribute_access_entries/entity_attribute_access_entry">
    --RAW
    DECLARE type_ INT;

    BEGIN
    SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'ENTITY_ATTRIBUTE_ACCESS_ENTRY';

    INSERT INTO T_BASE_ACCESS_ENTRY
    (EntryID, EntryDescription, type_)
    VALUES
    (TRANSLATE('<xsl:value-of select="@EntryID" />', 'ABCDEF0123456789{}-', 'ABCDEF0123456789'),
    '<xsl:value-of select="@EntryDescription" />',
    type_);
    INSERT INTO T_ENT_ATTR_ACCESS_ENTRY
    (EntryID, Attributes, Access_, LexemContent, EntityID)
    VALUES
    (TRANSLATE('<xsl:value-of select="@EntryID" />', 'ABCDEF0123456789{}-', 'ABCDEF0123456789'),
    '<xsl:value-of select="@Attributes" />',
    <xsl:value-of select="@Access" />,
    '<xsl:value-of select="@LexemContent" />',
    <xsl:value-of select="@EntityID" />);
    COMMIT;
    END;
    --ENDRAW
  </xsl:template>
  <xsl:template match="root/enum_access_entries/enum_access_entry">
    --RAW
    DECLARE type_ INT;

    BEGIN
    SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'ENUM_ACCESS_ENTRY';

    INSERT INTO T_BASE_ACCESS_ENTRY
    (EntryID, EntryDescription, type_)
    VALUES
    (TRANSLATE('<xsl:value-of select="@EntryID" />', 'ABCDEF0123456789{}-', 'ABCDEF0123456789'),
    '<xsl:value-of select="@EntryDescription" />',
    type_);
    INSERT INTO T_ENUM_ACCESS_ENTRY
    (EntryID, Enabled, EnumTypeID)
    VALUES
    (TRANSLATE('<xsl:value-of select="@EntryID" />', 'ABCDEF0123456789{}-', 'ABCDEF0123456789'),
    <xsl:value-of select="@Enabled" />,
    <xsl:value-of select="@EnumTypeID" />);
    COMMIT;
    END;
    --ENDRAW
  </xsl:template>
  <xsl:template match="root/roles/role">
    --RAW
    -- ROLE
    DECLARE
    /*type_ INT;*/
    rc raw(16);
    EnumTypeID_ int;
    EnumID_ int;
    exist int;
    BEGIN
    /*SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'ROLES';*/

    SELECT count(*) INTO exist from T_ROLES WHERE RoleName = '<xsl:value-of select="@RoleName" />';
    IF exist = 0 then
    SELECT EnumTypeID INTO EnumTypeID_ FROM T_ENUM_TYPES WHERE Name = 'Departament';
    BEGIN
    SELECT min(EnumID) INTO EnumID_ FROM T_ENUMS WHERE Name = '<xsl:value-of select="@Departament" />' AND EnumTypeID = EnumTypeID_;
    exception
    when NO_DATA_FOUND then EnumID_ := NULL;
    END;
    rc := SYS_GUID();
    INSERT INTO T_ROLES (RoleId,RoleName, RoleDesc, Departament/*,type_*/)
    VALUES (rc,'<xsl:value-of select="@RoleName" />','<xsl:value-of select="@RoleDesc" />', EnumID_/*,type_*/);
    END IF;
    COMMIT;
    END;
    --ENDRAW

    <xsl:for-each select="users/user">
      --RAW
      -- USER IN ROLE
      DECLARE rc raw(16);/*type_ INT;*/exist int;
      BEGIN
      /*SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'USERS_ROLES';*/

      SELECT count(*) INTO exist FROM T_USERS WHERE Login='<xsl:value-of select="@Login" />';
      if exist = 1 then
      SELECT count(*) INTO exist FROM T_USERS_ROLES
      WHERE UserId=(SELECT UserId FROM T_USERS WHERE Login='<xsl:value-of select="@Login" />')
      AND RoleId=(SELECT RoleId FROM T_ROLES WHERE RoleName='<xsl:value-of select="ancestor::role/@RoleName" />');
      IF exist = 0 then
      rc := SYS_GUID();
      INSERT INTO T_USERS_ROLES (UserRoleId,UserId,RoleId/*,type_*/)
      SELECT rc,
      (SELECT UserID FROM T_USERS WHERE Login = '<xsl:value-of select="@Login" />'),
      (SELECT RoleId FROM T_ROLES WHERE RoleName='<xsl:value-of select="ancestor::role/@RoleName" />')/*,
      type_*/ from dual;
      END IF;
      END IF;
      COMMIT;
      END;
      --ENDRAW
    </xsl:for-each>

    <xsl:for-each select="client_modules/client_module">
      --RAW
      -- roles - clientcomponents
      declare rc raw(16);/* type_ int;*/exist int;
      BEGIN
      /*SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'ROLE_CLIENT_MODULES';*/
      SELECT count(*) into exist FROM T_CLIENT_MODULES WHERE ModuleType = '<xsl:value-of select="@ModuleType" />';
      IF exist = 1 then
      SELECT count(*) into exist FROM T_ROLE_CLIENT_MODULES
      WHERE ClientModuleID=(SELECT ClientModuleID FROM T_CLIENT_MODULES WHERE ModuleType = '<xsl:value-of select="@ModuleType" />')
      AND RoleId=(SELECT RoleId FROM T_ROLES WHERE RoleName='<xsl:value-of select="ancestor::role/@RoleName" />');
      IF exist = 0 then
      rc := SYS_GUID();
      INSERT INTO T_ROLE_CLIENT_MODULES (RoleClientModuleID, ClientModuleID,RoleId/*,type_*/)
      SELECT rc,
      (SELECT ClientModuleID FROM T_CLIENT_MODULES WHERE ModuleType = '<xsl:value-of select="@ModuleType" />'),
      (SELECT RoleId FROM T_ROLES WHERE RoleName='<xsl:value-of select="ancestor::role/@RoleName" />')/*,
      type_*/
      from dual;
      END IF;
      END IF;
      COMMIT;
      END;
      --ENDRAW
    </xsl:for-each>

    <xsl:for-each select="access_entries/access_entry">
      --RAW
      -- roles - access_entries
      declare rc raw(16); role raw(16); entid raw(16); exist int;
      BEGIN
      SELECT RoleId INTO role FROM T_ROLES WHERE RoleName='<xsl:value-of select="ancestor::role/@RoleName" />';
      select EntryID INTO entid FROM T_BASE_ACCESS_ENTRY WHERE EntryDescription='<xsl:value-of select="@EntryDescription" />';
      SELECT count(*) into exist FROM T_BAE_ROLES WHERE EntryID=entid AND RoleId=role;
      IF exist = 0 then
      rc := SYS_GUID();
      INSERT INTO T_BAE_ROLES (EntryRoleID, RoleID,EntryID)
      SELECT rc,role,entid from dual;
      end if;
      COMMIT;
      END;
      --ENDRAW
    </xsl:for-each>

    <xsl:for-each select="report_roles/report_role">
      --RAW
      -- roles - reports
      DECLARE ID RAW(16);
      report RAW(16);
      role RAW(16);
      rights numeric;
      begin
      ID := SYS_GUID();
      SELECT ReportID into report FROM T_REPORTS WHERE RepName = '<xsl:value-of select="@ReportName" />';
      SELECT RoleId into role FROM T_ROLES WHERE RoleName='<xsl:value-of select="ancestor::role/@RoleName" />';
      SELECT EnumID into rights FROM T_ENUMS WHERE Code='<xsl:value-of select="@ReportRightCode" />' and enumtypeID = 7;
      INSERT INTO T_REPORT_ROLES(RepRoleID, ReportID, RoleID, ReportRights)
      VALUES(ID, report,  role, rights );
      COMMIT;
      END;
      --ENDRAW
    </xsl:for-each>

  </xsl:template>
  <xsl:template match="root/reportformats/reportformat">
    --RAW
    -- REPORT FORMATS
    DECLARE /*type_ INT;*/ ID_ raw(16);
    BEGIN

    /*SELECT EntityID INTO type_ FROM T_ENTITIES WHERE EntName = 'REPORT_FORMATS';*/
    ID_ := SYS_GUID();
    INSERT INTO T_REPORT_FORMATS (RepFmtId, RepFmtName, HandlerName/*, type_*/)
    VALUES(ID_, '<xsl:value-of select="@FormatName"/>', '<xsl:value-of select="@HandlerName"/>'/*, type_*/);
    COMMIT;
    END;
    --ENDRAW
  </xsl:template>
  <xsl:template match="root/Reports">
		<xsl:for-each select="Report">
			--RAW
			declare
			repType numeric;
			repTempType numeric;
			expId RAW (16);
			expTempId RAW (16);
			entID numeric;
			repLoadDataTypeQuery numeric;
			repFormTypeTable numeric;

			repTplFileType numeric;
			groupid RAW (16);
			repName varchar2(100);
			repFormatType RAW(16);
			fileID RAW(16);
			begin
			SELECT EnumID into repLoadDataTypeQuery from T_ENUMS where
			EnumTypeID = (select EnumTypeID from T_ENUM_TYPES where Name='ReportLoadDataType') and
			Alias='<xsl:value-of select="@ReportLoadDataType"/>';
			SELECT EnumID into repFormTypeTable from T_ENUMS where
			EnumTypeID = (select EnumTypeID from T_ENUM_TYPES where Name='ReportFormType') and
			Alias='<xsl:value-of select="@ReportFormType"/>';

			SELECT EntityID into repType FROM T_ENTITIES WHERE EntName = 'Report';






			expId := SYS_GUID();
			repName := '<xsl:value-of select="@Name"/>';
			INSERT INTO T_BaseReport(ReportID,RepName,RepDesc,Active,type_)
			VALUES(expId, repName, NULL, <xsl:value-of select="@Active"/>, repType);

			SELECT EntityID into entID FROM T_ENTITIES WHERE EntName='<xsl:value-of select="@Entity"/>';
			INSERT INTO T_Report(ReportID,ForcedFetchAttrs,FilterValue,ReportFormType,ReportLoadDataType,EntityID,Sorting,Descending,ScriptName)
			VALUES(expId,
			'<xsl:value-of select="@ForcedFetchAttrs"/>',
			<xsl:choose>
				<xsl:when test="string(FilterValue)">
					'<xsl:value-of select="FilterValue"/>'
				</xsl:when>
				<xsl:otherwise>NULL</xsl:otherwise>
			</xsl:choose>,
			repFormTypeTable, repLoadDataTypeQuery, entID,
			'<xsl:value-of select="@Sorting"/>',
			'<xsl:value-of select="@Descending"/>', '<xsl:value-of select="@ScriptName"/>');

			--ReportFileGroup
			<xsl:for-each select="ReportFileGroup">
				groupID := SYS_GUID();
				INSERT INTO T_ReportFileGroup(GroupID,ReportID,Name,IsDefault)
				VALUES (groupID, expId, <xsl:choose>
					<xsl:when test="@Name">
						'<xsl:value-of select="@Name"/>'
					</xsl:when>
					<xsl:otherwise>repName</xsl:otherwise>
				</xsl:choose>, <xsl:choose>
					<xsl:when test="@IsDefault">
						'<xsl:value-of select="@IsDefault"/>'
					</xsl:when>
					<xsl:otherwise>1</xsl:otherwise>
				</xsl:choose>);

				--Files
				<xsl:for-each select="File">
					fileID := TRANSLATE('<xsl:value-of select="@ID"/>', 'ABCDEF0123456789-', 'ABCDEF0123456789');
					UPDATE T_FILES SET
					type_ = (SELECT EntityID FROM T_ENTITIES WHERE EntName='ReportFile')
					WHERE FileID = fileID;

					SELECT RepFmtID into repFormatType FROM T_ReportFileFormat
					WHERE Name = 'MS Excel';

					INSERT INTO T_ReportFile
					(FileID, GroupID, RepFmtID, IsSecondary) VALUES
					(fileID, groupID, repFormatType, <xsl:choose>
						<xsl:when test="@IsSecondary">
							'<xsl:value-of select="@IsSecondary"/>'
						</xsl:when>
						<xsl:otherwise>0</xsl:otherwise>
					</xsl:choose>);

					--Parameters
					<xsl:for-each select="Parameter">
						expTempId := SYS_GUID();
						INSERT INTO T_ReportFileParameter(ParamID,FileID,Name,Content)
						VALUES (expTempId, fileID, '<xsl:value-of select="@Name"/>', '<xsl:value-of select="."/>');
					</xsl:for-each>
				</xsl:for-each>
			</xsl:for-each>
			end;
			--ENDRAW
		</xsl:for-each>
	</xsl:template>

  <!--<xsl:template match="StartupProcesses/StartupProcess">
    <xsl:text>INSERT INTO VCLib.T_STARTUP_PROCESS (StartupProcessID, ProcessName, ProcessParameters)&#10;VALUES(gen_random_uuid(), '</xsl:text>
    <xsl:value-of select="@ProcessName"/>
    <xsl:text>', '</xsl:text>
    <xsl:choose>
      <xsl:when test="@ProcessParameters">
        <xsl:text>'</xsl:text>
        <xsl:call-template name="replaceApostrs">
          <xsl:with-param name="text">
            <xsl:value-of select="@ProcessParameters"/>
          </xsl:with-param>
        </xsl:call-template>
        <xsl:text>'</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>NULL</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>');&#10;&#10;</xsl:text>
  </xsl:template>-->

  <!--<xsl:template match="RefreshEntities">
    exec sp_UpdateEntities
    GO
  </xsl:template>-->

</xsl:stylesheet>
