SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- [FNDD_GET_WINDREAM_USERGROUPNAMES]
-- =================================================================
-- Reads the windream database attributes to gives the index value
-- of a regular index or a vector index
-- 
-- Returns: Table
-- =================================================================
-- Copyright (c) 2025 by Digital Data GmbH
--
-- Digital Data GmbH • Ludwig-Rinn-Strasse 16 • D-35452 Heuchelheim
-- Tel.: 0641/202360 • E-Mail: info-flow@digitaldata.works
-- =================================================================
-- Creation Date / Author:    06.06.2025 / MK
-- Version Date / Editor:     06.06.2025 / MK
-- Version Number:            1.0.0.0
-- =================================================================
-- History:
-- 06.06.2025 / MK - First Version

CREATE OR ALTER FUNCTION [FNDD_GET_WINDREAM_USERGROUPNAMES] (
	@pWindreamUsername		NVARCHAR(100),				-- Give windream DocID, eg. 4711
	@pIncludeGroup			NVARCHAR(100),
	@pExcludeGroup			NVARCHAR(100)
)
RETURNS TABLE
AS
RETURN

	----------------------------------------------------------------------------------------------------------------------------------

	SELECT		concat([wdGI].[szNTDomain],'\',[wdGI].[szGroupName]) as 'RightGroups'
	FROM		[windreamDB].[dbo].[BaseAttributes]		as [wdBA]	(NOLOCK)

	INNER JOIN	[windreamDB].[dbo].[UserInfo]			as [wdUI]	(NOLOCK)	ON UPPER([wdBA].[szText22])		= UPPER([wdUI].[szUserName])		-- Username for UserID
	INNER JOIN	[windreamDB].[dbo].[UserGroup]			as [wdU2G]	(NOLOCK)	ON [wdUI].[dwUserID]			= [wdU2G].[dwUserOrGroupID]			-- UserID for GroupsID
	INNER JOIN	[windreamDB].[dbo].[GroupInfo]			as [wdGI]	(NOLOCK)	ON [wdU2G].[dwGroupID]			= [wdGI].[dwGroupID]				-- GroupIDs for Groupnames

	INNER JOIN	[DD_ECM].[dbo].[TBDD_USER]				as [ddUI]	(NOLOCK)	ON [wdUI].[dwUserID]			= [ddUI].[USERID_FK_INT_ECM]		-- windream UserID to DD UserID
	INNER JOIN	[DD_ECM].[dbo].[TBDD_GROUPS_USER]		as [ddU2G]	(NOLOCK)	ON [ddUI].[GUID]				= [ddU2G].[USER_ID]					-- UserID for GroupsID
	INNER JOIN	[DD_ECM].[dbo].[TBDD_GROUPS]			as [ddGI]	(NOLOCK)	ON [ddU2G].[GROUP_ID]			= [ddGI].[GUID]						-- GroupIDs for Groupnames

	WHERE		UPPER([wdGI].[szGroupName])	= UPPER([ddGI].[NAME])
	  AND		[wdBA].[dwDocID]			= @pdwDocID
	  AND		[wdGI].[szGroupName]		LIKE     '%' + ISNULL(@pIncludeGroup,'_')' + '%'
	  AND		[wdGI].[szGroupName]		NOT LIKE '%' + ISNULL(@pExcludeGroup,'')' + '%'