IF not EXISTS(SELECT GUID FROM TBDD_MODULES WHERE NAME = 'Clipboard-Watcher' )
	INSERT INTO TBDD_MODULES (NAME, SHORT_NAME, LICENSE,ACTIVE) VALUES ('Clipboard-Watcher','CW','kc6j9HSv/UJVAzFjMZv2A3wHZFy+2nNLKP9Qs8g9EVY=',1)
GO
UPDATE TBDD_MODULES SET DB_VERSION = '2.0.0.1' where UPPER(NAME) = UPPER('Clipboard-Watcher')
GO 
--ADDs THE USER WITH ADMIN-RIGHTS TO USER-MODULE RELATION
INSERT INTO TBDD_USER_MODULES(USER_ID,MODULE_ID) 
SELECT DISTINCT USER_ID,(SELECT GUID FROM TBDD_MODULES WHERE SHORT_NAME = 'CW') 
FROM TBDD_USER_MODULES  where IS_ADMIN = 1 
GO
INSERT INTO TBDD_CLIENT_USER (USER_ID,CLIENT_ID) SELECT GUID,1 FROM TBDD_USER WHERE GUID NOT IN (SELECT USER_ID FROM TBDD_CLIENT_USER WHERE CLIENT_ID = 1)
GO
CREATE TABLE TBCW_CONFIGURATION
(
	GUID           				TINYINT,
	WD_UNICODE					BIT				NOT NULL DEFAULT 1,
	GDPICTURE_LICENSE			VARCHAR(500)	NOT NULL DEFAULT '',
	ADDED_WHEN					DATETIME 		NOT NULL DEFAULT GETDATE(),
	CHANGED_WHO	    			VARCHAR(50),
	CHANGED_WHEN  				DATETIME,
	CONSTRAINT					PK_TBCW_CONFIGURATION_GUID PRIMARY KEY(GUID),
	CONSTRAINT 	  				CH_TBCW_CONFIGURATION_GUID	CHECK(GUID = 1)
)
GO
CREATE TRIGGER TBCW_CONFIGURATION_AFT_UPD ON TBCW_CONFIGURATION
FOR UPDATE
AS
	UPDATE TBCW_CONFIGURATION SET CHANGED_WHEN = GETDATE() FROM INSERTED 
	WHERE TBCW_CONFIGURATION.GUID = INSERTED.GUID
GO
INSERT INTO TBCW_CONFIGURATION (GUID) VALUES (1)
GO
--ALTER TABLE TBDD_CLIENT 
--ADD LICENSE_DDCBSEARCHER VARCHAR(5000) NOT NULL DEFAULT ''
--GO
CREATE TABLE TBCW_PROFILES 
(
	GUID				INTEGER			NOT NULL	IDENTITY(1,1),
	NAME				VARCHAR(100)	NOT NULL,
	PROFILE_TYPE		SMALLINT		NOT NULL DEFAULT 0,
	REGEX_EXPRESSION	VARCHAR(100)	NOT NULL,
	ACTIVE				BIT				NOT NULL DEFAULT 0,
	COMMENT				VARCHAR(500),
	ADDED_WHO			VARCHAR(50)		NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN			DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT			PK_TBCW_PROFILES PRIMARY KEY (GUID),
	CONSTRAINT			UQ_TBCW_PROFILES_NAME	UNIQUE(NAME)
)
GO
CREATE TRIGGER TBCW_PROFILES_AFT_UPD ON TBCW_PROFILES
FOR UPDATE
AS
	UPDATE TBCW_PROFILES SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBCW_PROFILES.GUID = INSERTED.GUID
GO
CREATE TABLE TBCW_USER_PROFILE
( 
	GUID			INT			IDENTITY(1,1),              		    
	PROFILE_ID		INT			NOT NULL,
	[USER_ID]		INT			NOT NULL,
	ADDED_WHO		VARCHAR(50)		NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 					DEFAULT GETDATE(),
	CONSTRAINT		PK_TBCW_USER_PROFILE	PRIMARY KEY (GUID),
	CONSTRAINT		UQ_TBCW_USER_PROFILE_PID_UID UNIQUE (PROFILE_ID,[USER_ID])
)
GO
CREATE TABLE TBCW_PROFILE_PROCESS
( 
	GUID			INT				IDENTITY(1,1),              		    
	PROFILE_ID		INT				NOT NULL,
	PROC_NAME		VARCHAR(100)	NOT NULL,
	ADDED_WHO		VARCHAR(50)		NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 					DEFAULT GETDATE(),
	CONSTRAINT		PK_TBCW_PROFILE_PROCESS	PRIMARY KEY (GUID),
	CONSTRAINT		UQ_TBCW_PROFILE_PROCESS	UNIQUE (PROFILE_ID,PROC_NAME)
)
GO
CREATE TABLE TBCW_PROF_DOC_SEARCH(
	GUID				INTEGER			NOT NULL	IDENTITY(1,1),
	PROFILE_ID			INTEGER			NOT NULL,
	CONN_ID				TinyINT			NOT NULL	DEFAULT 0,
	SQL_COMMAND			VARCHAR(MAX)	NOT NULL,
	COUNT_COMMAND		VARCHAR(MAX)	NOT NULL,
	TAB_INDEX			TINYINT			NOT NULL	DEFAULT 0,
	ACTIVE				BIT				NOT NULL	DEFAULT 0,
	TAB_TITLE			VARCHAR(100)	NOT NULL	DEFAULT 'NOT DEFINED',
	ADDED_WHO			VARCHAR(50)		NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN			DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT			PK_TBCW_PROF_DOC_SEARCH PRIMARY KEY (GUID),
	CONSTRAINT			FK_TBCW_PROF_DOC_SEARCH_PROF_IF FOREIGN KEY (PROFILE_ID)  REFERENCES  TBCW_PROFILES (GUID)
)
GO
CREATE TRIGGER TBCW_PROF_DOC_SEARCH_AFT_UPD ON TBCW_PROF_DOC_SEARCH
FOR UPDATE
AS
  UPDATE TBCW_PROF_DOC_SEARCH SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBCW_PROF_DOC_SEARCH.GUID = INSERTED.GUID
GO
CREATE TRIGGER [dbo].[TBCW_PROF_DOC_SEARCH_AFT_INS] ON [dbo].[TBCW_PROF_DOC_SEARCH]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE @TABINDEX TINYINT
		,@MAX_INDEX TINYINT
		,@PROFILE_ID INT
		,@GUID INTEGER
	SELECT  @TABINDEX = TAB_INDEX
		,@PROFILE_ID = PROFILE_ID
		,@GUID = GUID FROM inserted
	
	SELECT  @MAX_INDEX = ISNULL(MAX(TAB_INDEX),0) FROM TBCW_PROF_DOC_SEARCH WHERE PROFILE_ID = @PROFILE_ID AND GUID <> @GUID
	UPDATE TBCW_PROF_DOC_SEARCH SET TAB_INDEX = @MAX_INDEX
							FROM INSERTED
							WHERE TBCW_PROF_DOC_SEARCH.GUID = INSERTED.GUID

	UPDATE TBCW_PROF_DOC_SEARCH SET ADDED_WHEN = GETDATE() FROM INSERTED WHERE TBCW_PROF_DOC_SEARCH.GUID = INSERTED.GUID

		
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE())
	+ ' - ERROR-MESSAGE: ' 
	+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
--DROP TABLE TBCW_PROF_DATA_SEARCH
--GO
CREATE TABLE TBCW_PROF_DATA_SEARCH(
	GUID				INTEGER			NOT NULL	IDENTITY(1,1),
	PROFILE_ID			INTEGER			NOT NULL,
	CONN_ID				TinyINT			NOT NULL	DEFAULT 0,
	SQL_COMMAND			VARCHAR(MAX)	NOT NULL,
	COUNT_COMMAND		VARCHAR(MAX)	NOT NULL,
	TAB_INDEX			TINYINT			NOT NULL	DEFAULT 0,
	ACTIVE				BIT				NOT NULL	DEFAULT 0,
	TAB_TITLE			VARCHAR(100)	NOT NULL	DEFAULT 'NOT DEFINED',
	ADDED_WHO			VARCHAR(50)		NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN			DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT			PK_TBCW_PROF_DATA_SEARCH PRIMARY KEY (GUID),
	CONSTRAINT			FK_TBCW_PROF_DATA_SEARCH_PROF_IF FOREIGN KEY (PROFILE_ID)  REFERENCES  TBCW_PROFILES (GUID)
)
GO
CREATE TRIGGER TBCW_PROF_DATA_SEARCH_AFT_UPD ON TBCW_PROF_DATA_SEARCH
FOR UPDATE
AS
  UPDATE TBCW_PROF_DATA_SEARCH SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBCW_PROF_DATA_SEARCH.GUID = INSERTED.GUID
GO
CREATE TRIGGER [dbo].[TBCW_PROF_DATA_SEARCH_AFT_INS] ON [dbo].[TBCW_PROF_DATA_SEARCH]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE @TABINDEX TINYINT
		,@MAX_INDEX TINYINT
		,@PROFILE_ID INT
		,@GUID INTEGER
	SELECT  @TABINDEX = TAB_INDEX
		,@PROFILE_ID = PROFILE_ID
		,@GUID = GUID FROM inserted
	
	SELECT  @MAX_INDEX = ISNULL(MAX(TAB_INDEX),0) FROM TBCW_PROF_DATA_SEARCH WHERE PROFILE_ID = @PROFILE_ID AND GUID <> @GUID
	UPDATE TBCW_PROF_DATA_SEARCH SET TAB_INDEX = @MAX_INDEX
							FROM INSERTED
							WHERE TBCW_PROF_DATA_SEARCH.GUID = INSERTED.GUID
	UPDATE TBCW_PROF_DATA_SEARCH SET ADDED_WHEN = GETDATE() FROM INSERTED WHERE TBCW_PROF_DATA_SEARCH.GUID = INSERTED.GUID

		
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE())
	+ ' - ERROR-MESSAGE: ' 
	+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO

CREATE TABLE [dbo].[TBCW_GROUP_PROFILE](
	GUID		INTEGER IDENTITY(1,1) NOT NULL,
	PROFILE_ID	INTEGER NOT NULL,
	GROUP_ID	INTEGER NOT NULL,
	ADDED_WHO	VARCHAR(50) NOT NULL DEFAULT 'DEFAULT',
	ADDED_WHEN	DATETIME NULL	DEFAULT GETDATE(),
	CONSTRAINT	PK_TBCW_GROUP_PROFILE PRIMARY KEY (GUID),
	CONSTRAINT	UQ_TBCW_GROUP_PROFILE_PID_UID UNIQUE (PROFILE_ID,GROUP_ID)
	)

GO

CREATE TABLE TBCW_PROF_REL_WINDOW(
	GUID				INTEGER			NOT NULL	IDENTITY(1,1),
	PROCESS_ID			INTEGER			NOT NULL,
	DESCRIPTION			VARCHAR(250)	NOT NULL	DEFAULT '',
	REGEX				VARCHAR(500)	NOT NULL,
	SEQUENCE			TINYINT			NOT NULL	DEFAULT 0,
	ADDED_WHO			VARCHAR(50)		NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN			DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT			PK_TBCW_PROF_REL_WINDOW PRIMARY KEY (GUID),
	CONSTRAINT			FK_TBCW_PROF_REL_WINDOW_PROCESS_ID FOREIGN KEY (PROCESS_ID)  REFERENCES  TBCW_PROFILE_PROCESS (GUID)
)
GO
CREATE TRIGGER TBCW_PROF_REL_WINDOW_AFT_UPD ON TBCW_PROF_REL_WINDOW
FOR UPDATE
AS
  UPDATE TBCW_PROF_REL_WINDOW SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBCW_PROF_REL_WINDOW.GUID = INSERTED.GUID
GO
CREATE TRIGGER [dbo].[TBCW_PROF_REL_WINDOW_AFT_INS] ON [dbo].TBCW_PROF_REL_WINDOW
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
		DECLARE @SEQUENCE TINYINT
		,@MAX_SEQUENCE TINYINT
		,@PROCESS_ID INT
		,@GUID INTEGER
	SELECT  @SEQUENCE = SEQUENCE
		,@PROCESS_ID = PROCESS_ID
		,@GUID = GUID FROM inserted
	
	SELECT  @MAX_SEQUENCE = ISNULL(MAX(SEQUENCE),0) FROM TBCW_PROF_REL_WINDOW WHERE PROCESS_ID = @PROCESS_ID AND GUID <> @GUID
	UPDATE TBCW_PROF_REL_WINDOW SET SEQUENCE = @MAX_SEQUENCE
							FROM INSERTED
							WHERE TBCW_PROF_REL_WINDOW.GUID = INSERTED.GUID
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE())
	+ ' - ERROR-MESSAGE: ' 
	+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
CREATE TABLE TBCW_PROF_REL_CONTROL(
	GUID				INTEGER			NOT NULL	IDENTITY(1,1),
	DESCRIPTION			VARCHAR(250)	NOT NULL,
	WINDOW_ID			INTEGER			NOT NULL,
	CONTROL_NAME		VARCHAR(500)	NOT NULL	DEFAULT '',
	[TOPLEFT_TOP]		INTEGER		NOT NULL	DEFAULT 0,
	[TOPLEFT_LEFT]		INTEGER	NOT NULL	DEFAULT 0,
	[TOPLEFT_RIGHT]		INTEGER	NOT NULL	DEFAULT 0,
	[TOPLEFT_BOTTOM]	INTEGER	NOT NULL	DEFAULT 0,
	[TOPRIGHT_TOP]		INTEGER	NOT NULL	DEFAULT 0,
	[TOPRIGHT_LEFT]			INTEGER	NOT NULL	DEFAULT 0,
	[TOPRIGHT_RIGHT]		INTEGER	NOT NULL	DEFAULT 0,
	[TOPRIGHT_BOTTOM]		INTEGER NOT NULL	DEFAULT 0,
	[BOTTOMLEFT_TOP]		INTEGER	NOT NULL	DEFAULT 0,
	[BOTTOMLEFT_LEFT]		INTEGER		NOT NULL	DEFAULT 0,
	[BOTTOMLEFT_RIGHT]		INTEGER	NOT NULL	DEFAULT 0,
	[BOTTOMLEFT_BOTTOM]		INTEGER	NOT NULL	DEFAULT 0,
	[BOTTOMRIGHT_TOP]		INTEGER		NOT NULL	DEFAULT 0,
	[BOTTOMRIGHT_LEFT]		INTEGER	NOT NULL	DEFAULT 0,
	[BOTTOMRIGHT_RIGHT]		INTEGER	NOT NULL	DEFAULT 0,
	[BOTTOMRIGHT_BOTTOM]	INTEGER	NOT NULL	DEFAULT 0,
	SEQUENCE				TINYINT			NOT NULL	DEFAULT 0,
	ADDED_WHO			VARCHAR(50)		NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN			DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT			PK_TBCW_PROF_REL_CONTROL PRIMARY KEY (GUID),
	CONSTRAINT			FK_TBCW_PROF_REL_CONTROL_WINDOW_ID FOREIGN KEY (WINDOW_ID)  REFERENCES  TBCW_PROF_REL_WINDOW (GUID)
)
GO
CREATE TRIGGER TBCW_PROF_REL_CONTROL_AFT_UPD ON TBCW_PROF_REL_CONTROL
FOR UPDATE
AS
  UPDATE TBCW_PROF_REL_CONTROL SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBCW_PROF_REL_CONTROL.GUID = INSERTED.GUID
GO

CREATE TRIGGER TBCW_PROF_REL_CONTROL_AFT_INS ON TBCW_PROF_REL_CONTROL
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE @SEQUENCE TINYINT
		,@MAX_SEQUENCE TINYINT
		,@WINDOW_ID INT
		,@GUID INTEGER
	SELECT  @SEQUENCE = SEQUENCE
		,@WINDOW_ID = WINDOW_ID
		,@GUID = GUID FROM inserted
	
	SELECT  @MAX_SEQUENCE = ISNULL(MAX(SEQUENCE),0) FROM TBCW_PROF_REL_CONTROL WHERE WINDOW_ID = @WINDOW_ID AND GUID <> @GUID
	UPDATE TBCW_PROF_REL_CONTROL SET SEQUENCE = @MAX_SEQUENCE
							FROM INSERTED
							WHERE TBCW_PROF_REL_CONTROL.GUID = INSERTED.GUID
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE())
	+ ' - ERROR-MESSAGE: ' 
	+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
CREATE TRIGGER [dbo].[TBCW_PROF_REL_CONTROLAFT_INS] ON [dbo].TBCW_PROF_REL_CONTROL
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE @SEQUENCE TINYINT
		,@MAX_SEQUENCE TINYINT
		,@WINDOW_ID INT
		,@GUID INTEGER
	SELECT  @SEQUENCE = SEQUENCE
		,@WINDOW_ID = @WINDOW_ID
		,@GUID = GUID FROM inserted
	
	SELECT  @MAX_SEQUENCE = ISNULL(MAX(SEQUENCE),0) FROM TBCW_PROF_REL_CONTROL WHERE @WINDOW_ID = @WINDOW_ID AND GUID <> @GUID
	UPDATE TBCW_PROF_REL_CONTROL SET SEQUENCE = @MAX_SEQUENCE
							FROM INSERTED
							WHERE TBCW_PROF_REL_CONTROL.GUID = INSERTED.GUID
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE())
	+ ' - ERROR-MESSAGE: ' 
	+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
/*##### PROCEDURES ####*/
CREATE PROCEDURE [dbo].[PRCW_DELETE_PROFILE](@pPROFILE_ID INT)
AS
BEGIN TRY
	delete from TBCW_PROF_DATA_SEARCH where PROFILE_ID = @pPROFILE_ID
	delete from TBCW_PROF_DOC_SEARCH where PROFILE_ID = @pPROFILE_ID

	delete from TBCW_PROF_REL_CONTROL where WINDOW_ID IN (SELECT GUID FROM TBCW_PROF_REL_WINDOW WHERE PROCESS_ID IN (SELECT GUID FROM TBCW_PROFILE_PROCESS WHERE PROFILE_ID = @pPROFILE_ID))
	delete from TBCW_PROF_REL_WINDOW where PROCESS_ID IN (SELECT GUID FROM TBCW_PROFILE_PROCESS WHERE PROFILE_ID = @pPROFILE_ID)
	delete from TBCW_PROFILE_PROCESS where PROFILE_ID = @pPROFILE_ID
	
	delete from TBCW_USER_PROFILE where PROFILE_ID = @pPROFILE_ID
	delete from TBCW_PROFILES where GUID = @pPROFILE_ID
END TRY
BEGIN CATCH   
	PRINT 'FEHLER IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ZEILE: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - FEHLER-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
CREATE PROCEDURE [dbo].[PRCW_DELETE_PROCESS](@pPROCESS_ID INT)
AS
BEGIN TRY
	delete from TBCW_PROF_REL_CONTROL where WINDOW_ID IN (SELECT GUID FROM TBCW_PROF_REL_WINDOW WHERE PROCESS_ID = @pPROCESS_ID)
	delete from TBCW_PROF_REL_WINDOW where PROCESS_ID = @pPROCESS_ID
	delete from TBCW_PROFILE_PROCESS where GUID = @pPROCESS_ID
END TRY
BEGIN CATCH   
	PRINT 'FEHLER IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ZEILE: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - FEHLER-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
CREATE PROCEDURE [dbo].[PRCW_DELETE_WINDOW](@pWINDOW_ID INT)
AS
BEGIN TRY
	delete from TBCW_PROF_REL_CONTROL where WINDOW_ID = @pWINDOW_ID
	delete from TBCW_PROF_REL_WINDOW where GUID = @pWINDOW_ID
END TRY
BEGIN CATCH   
	PRINT 'FEHLER IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ZEILE: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - FEHLER-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
/*##### FUNCTIONS ####*/

CREATE FUNCTION [dbo].[FNCW_WM_SEARCH] (@pSearchValue Varchar(512),@pUSER_ID INTEGER, @pLike BIT)
RETURNS @TBResult TABLE 
    (DocID INTEGER, 
	INDEX_NAME VARCHAR(50),
	COMMENT VARCHAR(500))
AS
BEGIN
	DECLARE @USER_ECM_FKID INTEGER
	SELECT @USER_ECM_FKID = USERID_FK_INT_ECM FROM TBDD_USER WHERE GUID = @pUSER_ID
	
	IF @pLike = 1
		BEGIN
			INSERT INTO @TBResult (DocID, INDEX_NAME) 
				SELECT DISTINCT T.[dwDocID]
				  ,T.[Idx]
				FROM VWCW_SEARCH1 T INNER JOIN VWDD_WM_ACCESS_RIGHTS T1 ON T.dwDocID = T1.dwObjectID AND T1.dwUserOrGROUPID = @USER_ECM_FKID
				WHERE UPPER(T.Search_Term) LIKE UPPER('%' + @pSearchValue + '%') 

			INSERT INTO @TBResult (DocID, INDEX_NAME) 
				SELECT DISTINCT WMV.dwDocID, WMA.szAttrName 
				FROM sDD_VMP01_APS01_windream60.dbo.Attribute WMA INNER JOIN sDD_VMP01_APS01_windream60.dbo.Vector WMV ON WMA.dwAttrID = WMV.dwAttrID 
				INNER JOIN VWDD_WM_ACCESS_RIGHTS T1 ON WMV.dwDocID = T1.dwObjectID AND T1.dwUserOrGROUPID = @USER_ECM_FKID
				WHERE WMA.szAttrName in ('Vektor String 00','Vektor String 01','Vektor String 03','Vektor String 04','Vektor String 06','Vektor String 25','Vektor String 26','Vektor String 50','Vektor String 51')
				AND UPPER(WMV.szValue) LIKE UPPER('%' + @pSearchValue + '%') 
		END
	ELSE
		BEGIN
			INSERT INTO @TBResult (DocID, INDEX_NAME) -- VALUES (1,'1234')
				SELECT T.[dwDocID]
				,T.[Idx]--,T.Search_Term
				FROM VWCW_SEARCH1 T INNER JOIN VWDD_WM_ACCESS_RIGHTS T1 ON T.dwDocID = T1.dwObjectID AND T1.dwUserOrGROUPID = 1000004
				WHERE UPPER(T.Search_Term) = UPPER(@pSearchValue) 
			INSERT INTO @TBResult (DocID, INDEX_NAME) 
				SELECT DISTINCT WMV.dwDocID, WMA.szAttrName 
				FROM sDD_VMP01_APS01_windream60.dbo.Attribute WMA INNER JOIN sDD_VMP01_APS01_windream60.dbo.Vector WMV ON WMA.dwAttrID = WMV.dwAttrID 
				INNER JOIN VWDD_WM_ACCESS_RIGHTS T1 ON WMV.dwDocID = T1.dwObjectID AND T1.dwUserOrGROUPID = @USER_ECM_FKID
				WHERE WMA.szAttrName in ('Vektor String 00','Vektor String 01','Vektor String 03','Vektor String 04','Vektor String 06','Vektor String 25','Vektor String 26','Vektor String 50','Vektor String 51')
				AND UPPER(WMV.szValue) = UPPER(@pSearchValue) 
		END
		
RETURN
END
GO
CREATE FUNCTION FNCW_GET_SEARCH_COUNT_FOR_CONNECTION(@CONNECTION_ID int)
RETURNS int
AS
BEGIN
	DECLARE @RESULT int

	SELECT @RESULT = SUM(t.CNT) FROM (
		SELECT COUNT(*) AS CNT FROM TBCW_PROF_DATA_SEARCH WHERE CONN_ID = @CONNECTION_ID
		UNION ALL
		SELECT COUNT(*) AS CNT FROM TBCW_PROF_DOC_SEARCH WHERE CONN_ID = @CONNECTION_ID
	) t

	RETURN @RESULT
END
GO
/*##### VIEWS ####*/
CREATE VIEW [dbo].[VWCW_USER_PROFILE] AS
SELECT DISTINCT
	T.GUID, 
	T.NAME,
	T.REGEX_EXPRESSION,
	T.COMMENT,
	T.ACTIVE,
	T.PROFILE_TYPE,
	T2.PROC_NAME,
	T1.USER_ID,
	T3.GROUP_ID
FROM TBCW_PROFILES T
	LEFT JOIN TBCW_USER_PROFILE T1 ON T.GUID = T1.PROFILE_ID
	LEFT JOIN TBCW_PROFILE_PROCESS T2 ON T.GUID = T2.PROFILE_ID
	LEFT JOIN TBCW_GROUP_PROFILE T3 ON T.GUID = T3.PROFILE_ID
WHERE T.ACTIVE = 1
GO
USE [DD_ECM_TEST]
GO

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_TEST.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 [sDD_VMP01_APS01_windream60].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 [sDD_VMP01_APS01_windream60].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  * from  [172.24.12.41\BRAINPOOL].[sDD_VMP01_APS01_windream60].dbo.BaseAttributes 
					SELECT @PARENTNAME = szLongName,@PARENT_ID = dwParentID FROM [sDD_VMP01_APS01_windream60].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 sDD_VMP01_APS01_windream60.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 
	sDD_VMP01_APS01_windream60.dbo.BaseAttributes T
	,sDD_VMP01_APS01_windream60.dbo.ObjectType T1
WHERE 
	T.dwCatalogID = 1
	AND T.dwObjectTypeID = 4
	 AND T.dwObjectTypeID = T1.dwObjectTypeID 
ORDER BY [Creation_DateTime]
GO
CREATE VIEW VWCW_PROFILE_REL_WINDOW AS
SELECT  DISTINCT TOP 100 PERCENT
	T.GUID
	,T.PROCESS_ID
	,T2.USER_ID
	,T1.PROC_NAME AS PROCESS_NAME
	,T1.PROFILE_ID
	,T.DESCRIPTION
	,T.REGEX
	,T.SEQUENCE
FROM 
	TBCW_PROF_REL_WINDOW T
	,TBCW_PROFILE_PROCESS T1
	,VWCW_USER_PROFILE T2 
WHERE 
	T.PROCESS_ID = T1.GUID AND
	T1.PROFILE_ID = T2.GUID
	
ORDER BY T.SEQUENCE
GO

CREATE VIEW VWCW_PROFILE_REL_CONTROL AS
SELECT  DISTINCT TOP 100 PERCENT
	T.GUID
	,T1.USER_ID
	,TP.PROC_NAME AS PROCESS_NAME
	,T1.GUID AS PROFILE_ID
	,T.WINDOW_ID
	,T.DESCRIPTION
	,T.SEQUENCE
	,T.CONTROL_NAME
	,T.TOPLEFT_LEFT
	,T.TOPLEFT_RIGHT
	,T.TOPLEFT_TOP
	,T.TOPLEFT_BOTTOM
	,T.TOPRIGHT_LEFT
	,T.TOPRIGHT_RIGHT
	,T.TOPRIGHT_TOP
	,T.TOPRIGHT_BOTTOM
	,T.BOTTOMLEFT_LEFT
	,T.BOTTOMLEFT_RIGHT
	,T.BOTTOMLEFT_TOP
	,T.BOTTOMLEFT_BOTTOM
	,T.BOTTOMRIGHT_LEFT
	,T.BOTTOMRIGHT_RIGHT
	,T.BOTTOMRIGHT_TOP
	,T.BOTTOMRIGHT_BOTTOM
FROM 
	TBCW_PROF_REL_CONTROL T
	,TBCW_PROF_REL_WINDOW TW
	,TBCW_PROFILE_PROCESS TP
	,VWCW_USER_PROFILE T1 
WHERE 
	T.WINDOW_ID = TW.GUID AND
	TW.PROCESS_ID = TP.GUID AND
	TP.PROFILE_ID = T1.GUID
ORDER BY T.SEQUENCE
GO

CREATE VIEW [dbo].[VWCW_GROUP_PROFILE] AS
SELECT 
	T.GUID, 
	T.NAME,
	T.REGEX_EXPRESSION,
	T.COMMENT,
	T.ACTIVE,
	T1.GROUP_ID,
	T2.PROC_NAME 
FROM 
	TBCW_PROFILES T
	,TBCW_GROUP_PROFILE T1
	,TBCW_PROFILE_PROCESS T2 
WHERE 
	T.GUID = T1.PROFILE_ID 
	AND T.GUID = T2.PROFILE_ID 
	AND T.ACTIVE = 1
GO