
-- Replace @WINDREAM_DB in code with name of WM-Database 
CREATE FUNCTION [dbo].[FNDD_GET_WINDREAM_FILE_PATH] (@pDOCID BIGINT,@USE_WM_PATH as BIT = 0)
RETURNS VARCHAR(MAX)
AS 
BEGIN
	IF @pDOCID = 4712
		SET @pDOCID = 63
	ELSE
		SELECT @pDOCID = REFERENCE_KEY FROM IDB.dbo.TBIDB_OBJECT WHERE IDB_OBJ_ID = @pDOCID
	
	DECLARE	
		@RESULT					VARCHAR(MAX),
		@DOCID					BIGINT,
		@a						TINYINT = 1,
		@i						TINYINT = 0,
		@PARENT_ID				BIGINT,
		@DATEINAME				NVARCHAR(255),
		@PARENTNAME				NVARCHAR(255),
		@FSFLAGS				INT,
		@WM_PREFIX				VARCHAR(50)		
	
	IF @USE_WM_PATH = 1
		SET @WM_PREFIX = '\\windream\objects'
	ELSE
		SELECT @WM_PREFIX = WM_REL_PATH from tbpm_KONFIGURATION WHERE GUID = 1


	SELECT @FSFLAGS = dwFSFlags FROM [@WINDREAM_DB].dbo.BaseAttributes WHERE dwDocID = @pDOCID
	IF @FSFLAGS = 16
		BEGIN
			SET @RESULT = 'THIS IS NOT A FILE!'
		END
	ELSE
		BEGIN
			SELECT @DATEINAME = szLongName,@PARENT_ID = dwParentID FROM [@WINDREAM_DB].dbo.BaseAttributes WHERE dwDocID = @pDOCID
			WHILE @a > 0
				BEGIN
					IF @i = 0
						BEGIN
							IF @PARENT_ID = 1
								BEGIN
									SET @RESULT = '\'
									
									BREAK
								END
							ELSE
								BEGIN
									SET @DOCID = @PARENT_ID
									SET @RESULT = '\'
								END
						END
					ELSE
						BEGIN
							IF @PARENT_ID = 1
								BEGIN
									SET @RESULT = '\' + @PARENTNAME + @RESULT
									BREAK
								END
							ELSE
								BEGIN
									SET @DOCID = @PARENT_ID
									SET @RESULT = '\' + @PARENTNAME + @RESULT
								END
						END	
					SELECT @PARENTNAME = szLongName,@PARENT_ID = dwParentID FROM [@WINDREAM_DB].dbo.BaseAttributes WHERE dwDocID = @DOCID
					SET @i = @i + 1
				END
		END
	
	SET @RESULT = @WM_PREFIX +  @RESULT + @DATEINAME
	RETURN @RESULT
END
GO


CREATE VIEW [dbo].[VWCW_DOC_SEARCH] AS 
SELECT TOP 100 PERCENT
	T.dwDocID as DocID
	,T.dwParentID
	,dbo.FNDD_GET_WINDREAM_FILE_PATH (T.dwDocID,1)	AS FULL_FILENAME
	,T.szLongName AS [Filename]
      ,T.szText00
      ,T.szText01
      ,T.szText02
      ,T.szText03
      ,T.szText04
      ,T.szText05
      ,T.szText06
      ,T.szText07
      ,T.szText08
      ,T.szText09
      ,T.szText10
      ,T.szText11
      ,T.szText12
      ,T.szText13
      ,T.szText14
      ,T.szText15
      ,T.szText16
      ,T.szText17
      ,T.szText18
      ,T.szText19
      ,T.szText20
      ,T.szText21
      ,T.szText22
      ,T.szText23
      ,T.szText24
      ,T.szText25
      ,T.szText26
      ,T.szText27
      ,T.szText28
      ,T.szText29
      ,T.szText30
      ,T.szText31
      ,T.szText32
      ,T.szText33
      ,T.szText34
      ,T.szText35
	  ,T.szText36
	,ISNULL(T.szText37,'NO DOCTYPE')				AS Doctype
      ,T.szText38
      ,T.szText39
      ,T.dwInteger00
      ,T.dwInteger01
      ,T.dwInteger02
      ,T.dwInteger03
      ,T.dwInteger04
      ,T.dwInteger05
      ,T.dwInteger06
      ,T.dwInteger07
      ,T.dwInteger08
      ,T.dwInteger09
      ,T.dwInteger10
      ,T.dwInteger11
      ,T.dwInteger12
      ,T.dwInteger13
      ,T.dwInteger14
      ,T.dwInteger15
      ,T.dwInteger16
      ,T.dwInteger17
      ,T.dwInteger18
      ,T.dwInteger19
      ,T.dwInteger20
      ,T.dwInteger21
      ,T.dwInteger22
      ,T.dwInteger23
      ,T.dwInteger24
      ,T.lfFloat00
      ,T.lfFloat01
      ,T.lfFloat02
      ,T.lfFloat03
      ,T.lfFloat04
      ,T.lfFloat05
      ,T.lfFloat06
      ,T.lfFloat07
      ,T.lfFloat08
      ,T.lfFloat09
      ,T.lfFloat10
      ,T.lfFloat11
      ,T.lfFloat12
      ,T.lfFloat13
      ,T.lfFloat14
      ,T.blBool00
      ,T.blBool01
      ,T.blBool02
      ,T.blBool03
      ,T.blBool04
      ,T.dwDate00
      ,T.dwDate01
      ,T.dwDate02
      ,T.dwDate03
      ,T.dwDate04
      ,T.dwDate05
      ,T.dwDate06
      ,T.dwDate07
      ,T.dwDate08
      ,T.dwDate09
      ,T.wmVar00
      ,T.wmVar01
      ,T.wmVar02
      ,T.wmVar03
      ,T.wmVar04
      ,T.wmVar05
      ,T.wmVar06
      ,T.wmVar07
      ,T.wmVar08
      ,T.wmVar09
      ,T.wmVar10
      ,T.wmVar11
      ,T.wmVar12
      ,T.wmVar13
      ,T.wmVar14
      ,T.wmVar15
      ,T.wmVar16
      ,T.wmVar17
      ,T.wmVar18
      ,T.wmVar19
      ,T.wmVar20
      ,T.wmVar21
      ,T.wmVar22
      ,T.wmVar23
      ,T.wmVar24
      ,T.wmVar25
      ,T.wmVar26
      ,T.wmVar27
      ,T.wmVar28
      ,T.wmVar29
	,(SELECT ISNULL(T.dwVersionNumber,'1') from @WINDREAM_DB.dbo.BaseAttributes WHERE dwDocID = T.dwDocID)  AS [Version]
	,CAST(CAST(CONVERT(DATE,CONVERT(VARCHAR(10),T.dwCreationDate)) AS DATETIME) + CAST(STUFF(STUFF(REPLICATE('0',6-LEN(T.dwCreation_Time)) + convert(VARCHAR(6),T.dwCreation_Time),3,0,':'),6,0,':') AS DATETIME) AS DATETIME) AS [Creation_DateTime]
	,CAST(CAST(CONVERT(DATE,CONVERT(VARCHAR(10),T.dwChangeDate)) AS DATETIME) + CAST(STUFF(STUFF(REPLICATE('0',6-LEN(T.dwChange_Time)) + convert(VARCHAR(6),T.dwChange_Time),3,0,':'),6,0,':') AS DATETIME) AS DATETIME) AS Change_DateTime,T1.szDocTypeName																AS OBJECTTYPE
	,T.dwObjectTypeID
FROM 
	@WINDREAM_DB.dbo.BaseAttributes T
	,@WINDREAM_DB.dbo.ObjectType T1
WHERE 
	T.dwCatalogID = 1
	AND T.dwObjectTypeID = 4
	 AND T.dwObjectTypeID = T1.dwObjectTypeID 
ORDER BY [Creation_DateTime]
GO