UPDATE TBDD_MODULES SET PRODUCT_VERSION = '2.4.6.4' where NAME = 'Record-Organizer' 
GO

DECLARE @DoctypeID	INTEGER
DECLARE cursor_Doctype CURSOR LOCAL FOR 
	select GUID from TBDD_DOKUMENTART 
 	OPEN cursor_Doctype 
   	FETCH NEXT FROM cursor_Doctype INTO @DoctypeID
   	WHILE @@FETCH_STATUS = 0
	BEGIN
		INSERT INTO TBDD_INDEX_AUTOM (DOCTYPE_ID, INDEXNAME,VALUE,ADDED_WHO) VALUES
		(@DoctypeID,'String 36','ADDI-RELATION','Digital Data')
				
		--#####
		FETCH NEXT FROM cursor_Doctype INTO @DoctypeID
	END
CLOSE cursor_Doctype
DEALLOCATE cursor_Doctype
GO



ALTER VIEW VWPMO_WD_OBJECT_RIGHTS
AS
SELECT dwObjectID, dwUserOrGroupID, dwAccessRight, dwObjectDBID
FROM            [172.24.12.41\BRAINPOOL].windream60.dbo.AccessRight AS AccessRight_1
WHERE dwUserOrGroupID IN (SELECT USERID_FK_INT_ECM FROM TBDD_USER WHERE USERID_FK_INT_ECM <> 0) 
GO

ALTER PROCEDURE [dbo].[PRPMO_DELETE_CONSTRUCTOR](@pCONSTR_ID INT)
AS
BEGIN TRY
	DELETE FROM TBPMO_CLIENT_CONSTRUCTOR WHERE CONSTRUCT_ID = @pCONSTR_ID
    DELETE FROM TBPMO_CONSTRUCTOR_USER WHERE CONSTRUCT_ID = @pCONSTR_ID
	DELETE FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY WHERE CONSTRUCT_ID in (SELECT GUID FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE CONSTRUCT_ID = @pCONSTR_ID)
	DELETE FROM TBPMO_FOLLUPEMAIL_RECORD WHERE FOLLOW_UP_ID IN (SELECT GUID FROM TBPMO_FOLLOW_UP_EMAIL WHERE CONSTRUCTOR_ID = @pCONSTR_ID)
	DELETE FROM TBPMO_FOLLOW_UP_EMAIL WHERE CONSTRUCTOR_ID = @pCONSTR_ID
	DELETE FROM TBPMO_CONSTRUCTOR_USER_SQL WHERE CONSTR_DET_ID in (SELECT GUID FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE CONSTRUCT_ID = @pCONSTR_ID)
	DELETE FROM TBPMO_USER_CONSTR_VIEW_TYPE WHERE CONSTRUCTOR_DETAIL_ID IN (SELECT GUID FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE CONSTRUCT_ID = @pCONSTR_ID)
	DELETE FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE CONSTRUCT_ID = @pCONSTR_ID
	DELETE FROM TBPMO_FORM_CONSTRUCTOR WHERE GUID = @pCONSTR_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
ALTER TRIGGER [dbo].[TBPMO_DOC_VALUES_AFT_UPD] ON [dbo].[TBPMO_DOC_VALUES]
FOR UPDATE
AS
	UPDATE TBPMO_DOC_VALUES SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_DOC_VALUES.GUID = INSERTED.GUID
GO

ALTER TABLE TBPMO_RECORD
ADD PARENT_RECORD INTEGER NOT NULL DEFAULT 0
GO
ALTER TABLE TBPMO_RECORD
ADD [VERSION] INTEGER NOT NULL DEFAULT 1
GO
ALTER PROCEDURE [dbo].[PRPMO_CREATE_SQL](@pFORM_ID INT)
AS
DECLARE	@COL_NAME		VARCHAR(50),
		@SQL_BEGIN		VARCHAR(4000),
		@SQL_END		VARCHAR(4000),
		@SQL			VARCHAR(MAX),
		@CONTROL_ID		INT,
		@RESULT			VARCHAR(MAX),
		@TYPEID			INT,
		@TABINDEX		INT,
		@ZAEHLER		INT = 1,
		@SQL_LBL		VARCHAR(4000)
		
DECLARE	c_SPALTENNAMEN	CURSOR FOR	SELECT		T.GUID,'[' + T.COL_NAME + ']' AS 'COL_NAME',T2.GUID,T1.TAB_INDEX FROM TBPMO_CONTROL T,TBPMO_CONTROL_SCREEN T1,TBPMO_CONTROL_TYPE T2
									WHERE		T.SHOW_COLUMN = 1 AND T.[COL_NAME] <> '' AND T.GUID = T1.CONTROL_ID AND T.CONTROL_TYPE_ID = T2.GUID AND T.FORM_ID = @pFORM_ID AND LEN(ISNULL(T.COL_NAME,'')) > 0 AND T.CONTROL_TYPE_ID NOT IN (1,5)
									UNION
									SELECT		T.GUID,'[' + T.NAME + ']' AS 'COL_NAME',T2.GUID,T1.TAB_INDEX FROM TBPMO_CONTROL T,TBPMO_CONTROL_SCREEN T1,TBPMO_CONTROL_TYPE T2
									WHERE		T.GUID = T1.CONTROL_ID AND T.CONTROL_TYPE_ID = T2.GUID AND T.FORM_ID = @pFORM_ID AND T.CONTROL_TYPE_ID = 1 AND LEN(ISNULL(T.SQL_COMMAND_1,'')) > 0
									ORDER BY	TAB_INDEX
BEGIN
	SET @SQL_BEGIN = 'SELECT T.GUID AS [Record-ID]' + CHAR(13) + ',T.COLOR AS ROW_COLOR' + CHAR(13) + ',T.FORM_ID AS [Form-ID]' + CHAR(13) + ','
	--SET @SQL_END = ',T.ADDED_WHO as AddedWho,T.ADDED_WHEN as AddedWhen, T.CHANGED_WHO As ChangedWho, T.CHANGED_WHEN As ChangedWhen FROM TBPMO_RECORD T, TBPMO_FORM T1 WHERE T.FORM_ID = T1.GUID AND T.FORM_ID = @FORM_ID' 
	SET @SQL_END = ',T.ADDED_WHO AS AddedWho,' + CHAR(13) + 'T.ADDED_WHEN AS AddedWhen,' + CHAR(13) + 'T.CHANGED_WHO AS ChangedWho,' + CHAR(13) + 'T.CHANGED_WHEN AS ChangedWhen FROM TBPMO_RECORD T' + CHAR(13) + 'INNER JOIN TBPMO_FORM T1 ON T.FORM_ID = T1.GUID AND T.[PARENT_RECORD] = 0 AND T.FORM_ID = @FORM_ID' + CHAR(13)

	SET @SQL_END = REPLACE(@SQL_END,'@FORM_ID',@pFORM_ID)
	
	OPEN c_SPALTENNAMEN 
    FETCH NEXT FROM c_SPALTENNAMEN INTO @CONTROL_ID,@COL_NAME,@TYPEID,@TABINDEX
    
	WHILE @@FETCH_STATUS = 0
		BEGIN
			SET @COL_NAME = REPLACE(@COL_NAME,'lbl','')
			SET @ZAEHLER = @ZAEHLER + 1
			IF LEN(ISNULL(@SQL,'')) = 0
				BEGIN
					IF @TYPEID IN(10,11)
						BEGIN
							--SET @SQL = 'ISNULL((SELECT CASE WHEN UPPER(RTRIM(LTRIM(T5.VALUE))) = ''TRUE'' THEN 1 ELSE 0 END FROM TBPMO_CONTROL_VALUE T5 WHERE T5.RECORD_ID = T.GUID AND T5.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + '),0) AS ' + @COL_NAME
							SET @SQL = 'ISNULL(CASE WHEN UPPER(RTRIM(LTRIM(T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.VALUE))) = ''TRUE'' THEN 1 ELSE 0 END,0) AS ' + @COL_NAME + CHAR(13)
							SET @SQL_END = @SQL_END + 'LEFT JOIN TBPMO_CONTROL_VALUE T' + CONVERT(VARCHAR(5),@ZAEHLER) + ' ON T.GUID = T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.RECORD_ID AND T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + CHAR(13)
						END
					ELSE IF @TYPEID = 6
						BEGIN
							--SET @SQL = '(SELECT T6.IMG FROM TBPMO_CONTROL_IMAGE T6 WHERE T6.RECORD_ID = T.GUID AND T6.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + ') AS ' + @COL_NAME
							SET @SQL = 'T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.IMG AS ' + @COL_NAME + CHAR(13)
							SET @SQL_END = @SQL_END + 'LEFT JOIN TBPMO_CONTROL_IMAGE T' + CONVERT(VARCHAR(5),@ZAEHLER) + ' ON T.GUID = T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.RECORD_ID AND T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + CHAR(13)
						ENd
					ELSE IF @TYPEID = 1
						BEGIN
							SELECT @SQL_LBL = SQL_COMMAND_1 FROM TBPMO_CONTROL WHERE GUID = @CONTROL_ID
							SET @SQL_LBL = REPLACE(UPPER(@SQL_LBL),'@RECORD_ID','T.GUID')
							SET @SQL = '(' + @SQL_LBL + ') AS ' + @COL_NAME + CHAR(13)
						END
					ELSE IF @TYPEID IN(7,12,14)
						BEGIN
							SET @SQL = 'dbo.FNPMO_GET_CONTROL_VALUES(' + CONVERT(VARCHAR(20),@CONTROL_ID) + ', T.GUID) AS ' + @COL_NAME  + CHAR(13)
						END
					ELSE IF @TYPEID = 4
						BEGIN
							SET @SQL = 'T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.VALUE AS ' + @COL_NAME + CHAR(13)
							SET @SQL_END = @SQL_END + 'LEFT JOIN TBPMO_CONTROL_VALUE T' + CONVERT(VARCHAR(5),@ZAEHLER) + ' ON T.GUID = T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.RECORD_ID AND T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + CHAR(13)
						END
					ELSE
						BEGIN
							--SET @SQL = '(SELECT T5.VALUE FROM TBPMO_CONTROL_VALUE T5 WHERE T5.RECORD_ID = T.GUID AND T5.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + ') AS ' + @COL_NAME
							SET @SQL = 'ISNULL(T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.VALUE,'''') AS ' + @COL_NAME + CHAR(13)
							SET @SQL_END = @SQL_END + 'LEFT JOIN TBPMO_CONTROL_VALUE T' + CONVERT(VARCHAR(5),@ZAEHLER) + ' ON T.GUID = T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.RECORD_ID AND T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + CHAR(13)
						END
				END
			ELSE
				BEGIN
					IF @TYPEID IN(10,11)
						BEGIN
							--SET @SQL = @SQL + ',ISNULL((SELECT CASE WHEN UPPER(RTRIM(LTRIM(T5.VALUE))) = ''TRUE'' THEN 1 ELSE 0 END FROM TBPMO_CONTROL_VALUE T5 WHERE T5.RECORD_ID = T.GUID AND T5.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + '),0) AS ' + @COL_NAME
							SET @SQL = @SQL + ',ISNULL(CASE WHEN UPPER(RTRIM(LTRIM(T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.VALUE))) = ''TRUE'' THEN 1 ELSE 0 END,0) AS ' + @COL_NAME + CHAR(13)
							SET @SQL_END = @SQL_END + 'LEFT JOIN TBPMO_CONTROL_VALUE T' + CONVERT(VARCHAR(5),@ZAEHLER) + ' ON T.GUID = T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.RECORD_ID AND T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + CHAR(13)
						END
					ELSE IF @TYPEID = 6
						BEGIN
							--SET @SQL = @SQL + ',(SELECT T6.IMG FROM TBPMO_CONTROL_IMAGE T6 WHERE T6.RECORD_ID = T.GUID AND T6.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + ') AS ' + @COL_NAME
							SET @SQL = @SQL + ',T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.IMG AS ' + @COL_NAME + CHAR(13)
							SET @SQL_END = @SQL_END + 'LEFT JOIN TBPMO_CONTROL_IMAGE T' + CONVERT(VARCHAR(5),@ZAEHLER) + ' ON T.GUID = T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.RECORD_ID AND T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + CHAR(13)
						END
					ELSE IF @TYPEID = 1
						BEGIN
							SELECT @SQL_LBL = SQL_COMMAND_1 FROM TBPMO_CONTROL WHERE GUID = @CONTROL_ID
							SET @SQL_LBL = REPLACE(UPPER(@SQL_LBL),'@RECORD_ID','T.GUID')
							SET @SQL = @SQL + ',(' + @SQL_LBL + ') AS ' + @COL_NAME + CHAR(13)
						END
					ELSE IF @TYPEID IN(7,12,14)
						BEGIN
							--SET @SQL = @SQL + ',(SELECT [dbo].[FNPMO_GET_CONTROL_VALUES](' + CONVERT(VARCHAR(20),@CONTROL_ID) + ', T.GUID)) AS ' + @COL_NAME
							SET @SQL = @SQL + ',ISNULL(dbo.FNPMO_GET_CONTROL_VALUES(' + CONVERT(VARCHAR(20),@CONTROL_ID) + ', T.GUID),'''') AS ' + @COL_NAME + CHAR(13)
						END
					ELSE IF @TYPEID = 4
						BEGIN
							SET @SQL = @SQL + ',T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.VALUE AS ' + @COL_NAME + CHAR(13)
							SET @SQL_END = @SQL_END + 'LEFT JOIN TBPMO_CONTROL_VALUE T' + CONVERT(VARCHAR(5),@ZAEHLER) + ' ON T.GUID = T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.RECORD_ID AND T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + CHAR(13)
						END
					ELSE
						BEGIN
							--SET @SQL = @SQL + ',(SELECT T5.VALUE FROM TBPMO_CONTROL_VALUE T5 WHERE T5.RECORD_ID = T.GUID AND T5.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + ') AS ' + @COL_NAME
							SET @SQL = @SQL + ',ISNULL(T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.VALUE,'''') AS ' + @COL_NAME + CHAR(13)
							SET @SQL_END = @SQL_END + 'LEFT JOIN TBPMO_CONTROL_VALUE T' + CONVERT(VARCHAR(5),@ZAEHLER) + ' ON T.GUID = T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.RECORD_ID AND T' + CONVERT(VARCHAR(5),@ZAEHLER) + '.CONTROL_ID = ' + CONVERT(VARCHAR(20),@CONTROL_ID) + CHAR(13)
						END
				END
		
			FETCH NEXT FROM c_SPALTENNAMEN INTO @CONTROL_ID,@COL_NAME,@TYPEID,@TABINDEX
		END

	CLOSE c_SPALTENNAMEN
    DEALLOCATE c_SPALTENNAMEN

	--PRint '1'
	SET @SQL = @SQL + ',(SELECT CASE COUNT(TDS.DocID) WHEN 0 THEN 0 ELSE 1 END AS [files?] FROM VWPMO_DOC_SEARCH TDS, TBPMO_DOC_RECORD_LINK TRL WHERE TDS.DocID = TRL.DOC_ID AND TRL.RECORD_ID = T.GUID) AS [files?]' + CHAR(13)
	--PRint '2'
	SET @SQL = @SQL + ',(SELECT COUNT(TDS.DocID) AS [files?] FROM VWPMO_DOC_SEARCH TDS, TBPMO_DOC_RECORD_LINK TRL WHERE  TDS.DocID = TRL.DOC_ID AND TRL.RECORD_ID = T.GUID) AS [file count]' + CHAR(13)
	--PRint '3'
	SET @RESULT = @SQL_BEGIN + @SQL + @SQL_END
	--PRINT 'LEN: ' + CONVERT(VARCHAR(10),LEN(@RESULT))

	IF EXISTS (SELECT FORM_ID FROM TBPMO_FORM_SQL WHERE FORM_ID = @pFORM_ID)
		UPDATE TBPMO_FORM_SQL SET SQL_COMMAND = @RESULT WHERE FORM_ID = @pFORM_ID
	ELSE	
		INSERT INTO TBPMO_FORM_SQL(FORM_ID,SQL_COMMAND) VALUES(@pFORM_ID,@RESULT)
END
GO

DECLARE
	@ENTITY_ID	INTEGER,
	@CONTROL_ID		INTEGER,
	@GUID			INTEGER,
	@VALUE			VARCHAR(MAX)		
								
DECLARE c_REDO CURSOR FOR 
	SELECT GUID FROM TBPMO_FORM
  OPEN c_REDO 
    FETCH NEXT FROM c_REDO INTO @ENTITY_ID
    WHILE @@FETCH_STATUS = 0
	BEGIN
		EXEC [dbo].[PRPMO_CREATE_SQL] @ENTITY_ID
		FETCH NEXT FROM c_REDO INTO @ENTITY_ID
	END
CLOSE c_REDO
DEALLOCATE c_REDO
GO
EXECUTE [dbo].[PRPMO_CREATE_TEMP_VIEWS] 
GO

CREATE TABLE TBPMO_RECORD_VARIANT(
	[GUID]			INT		IDENTITY(1,1) NOT NULL,
	RECORD_ID		INT					NOT NULL,
	REASON_CODE		VARCHAR(100)		NOT NULL,
	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_TBPMO_RECORD_VARIANT] 	PRIMARY KEY(GUID),
CONSTRAINT		FK_TBPMO_RECORD_VARIANT_RECORD_ID	FOREIGN KEY (RECORD_ID) REFERENCES  TBPMO_RECORD (GUID))
GO

CREATE TRIGGER TBPMO_RECORD_VARIANT_AFT_UPD ON TBPMO_RECORD_VARIANT
FOR UPDATE
AS
	UPDATE TBPMO_RECORD_VARIANT SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_RECORD_VARIANT.GUID = INSERTED.GUID
GO

CREATE TABLE TBPMO_ENTITY_VARIANT_REASONS(
	[GUID]			INT		IDENTITY(1,1) NOT NULL,
	ENTITY_ID		INT					NOT NULL,
	REASON_CODE		VARCHAR(100)		NOT NULL,
	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_TBPMO_ENTITY_VARIANT_REASONS] 	PRIMARY KEY(GUID),
CONSTRAINT		FK_TBPMO_ENTITY_VARIANT_REASONS_ENTITY_ID	FOREIGN KEY (ENTITY_ID) REFERENCES  TBPMO_FORM (GUID))
GO

CREATE TRIGGER TBPMO_ENTITY_VARIANT_REASONS_AFT_UPD ON TBPMO_ENTITY_VARIANT_REASONS
FOR UPDATE
AS
	UPDATE TBPMO_ENTITY_VARIANT_REASONS SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_ENTITY_VARIANT_REASONS.GUID = INSERTED.GUID
GO

ALTER PROCEDURE [dbo].[PRPMO_DELETE_ENTITY](@pENTITY_ID INT)
AS
BEGIN TRY
	DELETE FROM TBPMO_FORM_SQL WHERE FORM_ID = @pENTITY_ID
    DELETE FROM TBPMO_CLIENT_ENTITY WHERE ENTITY_ID = @pENTITY_ID
	DELETE FROM TBPMO_WORKFLOW_ENTITY_STATE WHERE ENTITY_ID = @pENTITY_ID
	DELETE FROM TBPMO_TEMPLATE_ENTITY WHERE ENTITY_ID = @pENTITY_ID
	DELETE FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE FORM_ID = @pENTITY_ID
	DELETE FROM TBPMO_CONTROL WHERE FORM_ID = @pENTITY_ID
	DELETE FROM TBPMO_ENTITY_SUPERVISOR WHERE ENTITY_ID = @pENTITY_ID
	DELETE FROM TBPMO_DOCSEARCH_RESULTLIST_CONFIG WHERE [ENTITY_ID]	 = @pENTITY_ID
	DELETE FROM TBPMO_RECORD_CONNECT WHERE RECORD1_ID in (SELECT GUID FROM TBPMO_RECORD WHERE FORM_ID = @pENTITY_ID)
	DELETE FROM TBPMO_RECORD_CONNECT WHERE RECORD2_ID in (SELECT GUID FROM TBPMO_RECORD WHERE FORM_ID = @pENTITY_ID)
	DELETE FROM TBPMO_RECORD_LOG WHERE RECORD_ID in (SELECT GUID FROM TBPMO_RECORD WHERE FORM_ID = @pENTITY_ID)
	DELETE FROM TBPMO_RECORD WHERE FORM_ID = @pENTITY_ID
	DELETE FROM TBPMO_ENTITY_VARIANT_REASONS WHERE ENTITY_ID = @pENTITY_ID
	DELETE FROM TBPMO_FORM WHERE GUID = @pENTITY_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
ALTER TABLE TBPMO_FORM_CONSTRUCTOR_DETAIL
ADD [DOC_SEARCH_TAB1] BIT NOT NULL DEFAULT 0
GO
ALTER TABLE TBPMO_FORM_CONSTRUCTOR_DETAIL
ADD [ACTIVATE_DOC_SEARCH_ON_EDIT] BIT NOT NULL DEFAULT 0
GO
ALTER VIEW [dbo].[VWPMO_CONSTRUCTOR_FORMS] AS
SELECT TOP 100 PERCENT
	T2.GUID,
	T1.GUID FORM_VIEW_ID,
	T1.SCREEN_ID,
	T2.CONSTRUCT_ID,
	T.PARENT_ID,
	T2.SEQUENCE,
	T2.LEVEL1_SELECT,
	T.GUID			AS FORM_ID, 
	T1.FORM_TITLE, 
	T2.SQL_SELECT_EBENE1,
	T2.SQL_SELECT_EBENE2,
	T2.SQL_QUICK_VIEW,
	T2.COLUMN_NAME1,
	T2.COLUMN_NAME2,
	T.FORM_TYPE_ID,
	T2.LOAD_DIRECT,
	T2.CONTROL_DOCTYPE_MATCH,
	T2.SQL_RIGHT_READ_AND_VIEW_ONLY,
	T2.SQL_RIGHT_WINDREAM_VIEW,
	T2.DOC_SEARCH_TAB1,
	T2.[ACTIVATE_DOC_SEARCH_ON_EDIT],
	T1.DOCUMENT_VIEW,
	T.DATE_FORMAT,
	T.LOCK_CONTROLS_BG_LOAD,
	(SELECT COUNT(GUID) FROm TBPMO_CONTROL WHERE FORM_ID = T1.FORM_ID AND TREE_VIEW = 1) AS COUNT_TREEVIEW
FROM 
	tbpmo_form T, 
	TBPMO_FORM_VIEW T1,
	TBPMO_FORM_CONSTRUCTOR_DETAIL T2 
where 
	T2.FORM_ID = T.GUID AND
	T.GUID = T1.FORM_ID 
ORDER BY 
	T2.CONSTRUCT_ID,T.PARENT_ID,T2.SEQUENCE
GO
ALTER TABLE TBPMO_KONFIGURATION
ADD WM_LAUFWERKBUCHSTABE VARCHAR(1) NOT NULL DEFAULT 'W'
GO

--CREATE FUNCTION FNPMO_GET_WINDREAM_FILE_PATH_NEU (@pDOCID BIGINT)
ALTER FUNCTION [dbo].[FNPMO_GET_WINDREAM_FILE_PATH] (@pDOCID BIGINT)
RETURNS VARCHAR(MAX)
AS 
BEGIN
	DECLARE	
		@RESULT					VARCHAR(MAX),
		@DOCID					BIGINT,
		@a						TINYINT = 1,
		@i						TINYINT = 0,
		@PARENT_ID				BIGINT,
		@DATEINAME				NVARCHAR(255),
		@PARENTNAME				NVARCHAR(255),
		@FSFLAGS				INT,
		@WM_LAUFWERKBUCHSTABE	CHAR(1)		
	
	SELECT @WM_LAUFWERKBUCHSTABE = WM_LAUFWERKBUCHSTABE	FROM TBPMO_KONFIGURATION WHERE GUID = 1

	SELECT @FSFLAGS = dwFSFlags FROM [172.24.12.41\BRAINPOOL].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 [172.24.12.41\brainpool].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 @PARENTNAME = szLongName,@PARENT_ID = dwParentID FROM [172.24.12.41\brainpool].windream60.dbo.BaseAttributes WHERE dwDocID = @DOCID
					SET @i = @i + 1
				END
		END
	
	SET @RESULT = @WM_LAUFWERKBUCHSTABE +':' +  @RESULT + @DATEINAME
	RETURN @RESULT
END
GO

CREATE TABLE TBDD_USER_LOGIN_OUT(
	[GUID]			INT		IDENTITY(1,1)	NOT NULL,
	[MODULE]		VARCHAR(30)				NOT NULL,
	[USER_ID]		INT						NOT NULL,
	[CLIENT_ID]		INT						NOT NULL,
	[LOGIN]			DATETIME				DEFAULT (GETDATE()),
	[LOGOUT]		DATETIME,
CONSTRAINT 	   	[PK_TBDD_USER_LOGIN_OUT] 	PRIMARY KEY(GUID),
CONSTRAINT		FK_TBDD_USER_LOGIN_OUT_USER_ID	FOREIGN KEY ([USER_ID]) REFERENCES  TBDD_USER (GUID))
GO

CREATE TRIGGER TBDD_USER_MODULE_LOG_IN_AFT_INS ON TBDD_USER_MODULE_LOG_IN
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE 
		@USER_ID		INTEGER,
		@MODULE			VARCHAR(50),
		@CLIENT_ID		INTEGER
	SELECT
  		@USER_ID = USER_ID,
		@MODULE = MODULE,
		@CLIENT_ID = CLIENT_ID
	FROM INSERTED
	
	INSERT INTO TBDD_USER_LOGIN_OUT (MODULE,USER_ID,CLIENT_ID) VALUES (@MODULE,@USER_ID,@CLIENT_ID)
	
END TRY
BEGIN CATCH   
	PRINT 'FEHLER IN TRIGGER: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ZEILE: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - FEHLER-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO

CREATE TRIGGER TBDD_USER_MODULE_LOG_IN_AFT_DEL ON TBDD_USER_MODULE_LOG_IN
FOR DELETE
AS
BEGIN TRY
	DECLARE 
		@USER_ID		INTEGER,
		@MODULE			VARCHAR(50),
		@CLIENT_ID		INTEGER
	SELECT
  		@USER_ID = USER_ID,
		@MODULE = MODULE,
		@CLIENT_ID = CLIENT_ID
	FROM DELETED

	UPDATE TBDD_USER_LOGIN_OUT SET LOGOUT = GETDATE() WHERE GUID = (
	SELECT MAX(GUID) FROM TBDD_USER_LOGIN_OUT WHERE USER_ID = @USER_ID AND MODULE = @MODULE AND CLIENT_ID = @CLIENT_ID)
	
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE())
	+ ' - ERROR-MESSAGE: ' 
	+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
ALTER TRIGGER [dbo].[TBPMO_CONSTRUCTOR_USER_AFT_INS] ON [dbo].[TBPMO_CONSTRUCTOR_USER]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE 
		@USER_ID INTEGER,
		@CONSTRUCT_ID INTEGER,
		@ADDED_WHO VARCHAR(50)
	SELECT 
		@CONSTRUCT_ID = CONSTRUCT_ID,
		@USER_ID = GUID,
		@ADDED_WHO = ADDED_WHO
	FROM INSERTED

	DECLARE	@ENTITY_ID INTEGER
	
	DECLARE c_USER CURSOR FOR 
		SELECT 
			FORM_ID	FROM 
		VWPMO_CONSTRUCTOR_FORMS WHERE CONSTRUCT_ID = 2 AND DOCUMENT_VIEW = 1
	OPEN c_USER 
		FETCH NEXT FROM c_USER INTO @ENTITY_ID
    WHILE @@FETCH_STATUS = 0
		BEGIN
			INSERT INTO TBPMO_USER_RIGHTS_JOBS (USER_ID,[ENTITY_ID],COMMENT,ADDED_WHO) VALUES
			(@USER_ID,@ENTITY_ID,'USER ADDED TO CONSTRUCTOR','TBPMO_CONSTRUCTOR_USER_AFT_INS')
			INSERT INTO TBPMO_ENTITY_USER_RIGHTS (USER_ID,ENTITY_ID,READ_ONLY,FILES_VISIBLE) VALUES
			(@USER_ID,@ENTITY_ID,0,1)
					
			FETCH NEXT FROM c_USER INTO @ENTITY_ID
		END
	CLOSE c_USER
	DEALLOCATE c_USER

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

CREATE PROCEDURE [dbo].[PRPMO_RIGHTS_SERVICE_CHECK_USER](@pUSER_ID INT, @pCHECK_WHO VARCHAR(50))
AS
BEGIN TRY
	DECLARE 
		@CONSTR_DET_ID	INTEGER,
		@ENTITY_ID		INTEGER,
		@RECORD_ID		INTEGER,
		--@pUSER_ID		INTEGER,
		@RECORD_SELECT  nvarchar (max),
		@RECORD_WHERE AS nvarchar (max),
		@CREATE_VIEW AS nvarchar (max),
		@USER_RIGHTADMIN_ID_ECM INTEGER--,
		--@pCHECK_WHO VARCHAR(50)
		--SET @pUSER_ID = 2
		--SET @pCHECK_WHO = 'SchreiberM'

	SELECT @USER_RIGHTADMIN_ID_ECM = USERID_FK_INT_ECM FROM TBDD_USER WHERE UPPER(USERNAME) = (
	SELECT UPPER(AD_USER) FROM TBPMO_SERVICE_RIGHT_CONFIG WHERE GUID = 1)
	DELETE FROM TBPMO_RIGHTS_WORK_SERVICE WHERE USER_ID = @pUSER_ID
	DECLARE  Cursor_constructors CURSOR LOCAL  FOR 
		select T1.GUID, T1.FORM_ID from TBPMO_CONSTRUCTOR_USER T,TBPMO_FORM_CONSTRUCTOR_DETAIL T1 where T.USER_ID = @pUSER_ID AND T.CONSTRUCT_ID = T1.CONSTRUCT_ID
		AND T1.FORM_ID IN (select DISTINCT TR.FORM_ID from TBPMO_DOC_RECORD_LINK T, TBPMO_RECORD TR WHERE T.RECORD_ID = TR.GUID)
		--select FORM_ID from TBPMO_CONSTRUCTOR_USER T,TBPMO_FORM_CONSTRUCTOR_DETAIL T1 where T.USER_ID = 1 AND T.CONSTRUCT_ID = T1.GUID
		OPEN cursor_constructors 
			FETCH NEXT FROM cursor_constructors INTO @CONSTR_DET_ID,@ENTITY_ID
			WHILE @@FETCH_STATUS = 0
			BEGIN
				BEGIN
					IF EXISTS(select * FROM sys.views where name = 'VWTEMP_REC_USER')
						DROP VIEW VWTEMP_REC_USER
					
					DECLARE 
						@WD_RIGHT AS INTEGER
					SET @WD_RIGHT = 3
					IF EXISTS(SELECT GUID FROM TBPMO_ENTITY_SUPERVISOR WHERE USER_ID = @pUSER_ID AND ENTITY_ID = @ENTITY_ID)
						SELECT @WD_RIGHT = RIGHT1 FROM TBPMO_ENTITY_SUPERVISOR WHERE USER_ID = @pUSER_ID AND ENTITY_ID = @ENTITY_ID
					ELSE
						IF EXISTS(SELECT GUID FROM TBPMO_ENTITY_USER_RIGHTS WHERE USER_ID = @pUSER_ID AND ENTITY_ID = @ENTITY_ID)
							BEGIN
								DECLARE @CONFIG AS INT
								SELECT @CONFIG = [READ_ONLY] FROM TBPMO_ENTITY_USER_RIGHTS WHERE USER_ID = @pUSER_ID AND ENTITY_ID = @ENTITY_ID
								IF @CONFIG = 1
									BEGIN
										SET @WD_RIGHT = 1
										Print 'User shall only read'
									END
							END


					SET @RECORD_SELECT = N'SELECT * FROM VWTEMP_PMO_FORM'  + CONVERT(NVARCHAR(3),@ENTITY_ID) 
					IF EXISTS(SELECT SQL_COMMAND FROM TBPMO_CONSTRUCTOR_USER_SQL WHERE CONSTR_DET_ID = @CONSTR_DET_ID AND USER_ID = @pUSER_ID AND SQL_COMMAND <> '')
						BEGIN
							
							SELECT @RECORD_WHERE = SQL_COMMAND FROM TBPMO_CONSTRUCTOR_USER_SQL WHERE CONSTR_DET_ID = @CONSTR_DET_ID AND USER_ID = @pUSER_ID
							SET @RECORD_WHERE = REPLACE(@RECORD_WHERE,'@USER_ID',@pUSER_ID)
							SET @RECORD_SELECT = @RECORD_SELECT + ' ' + @RECORD_WHERE
						END
					SET @CREATE_VIEW = N'CREATE VIEW VWTEMP_REC_USER AS ' + @RECORD_SELECT
					--PRINT 'CREATE VIEW: ' + @CREATE_VIEW
					
					EXEC (@CREATE_VIEW)
			
										
					DECLARE cursor_Records CURSOR FOR 
						SELECT [Record-ID] FROM VWTEMP_REC_USER 
						--select DocID from VWPMO_WD_DOC_SEARCH where RECORD_ID = @RECORD_ID
					OPEN cursor_Records 
					FETCH NEXT FROM cursor_Records INTO @RECORD_ID
					WHILE @@FETCH_STATUS = 0
					BEGIN
						DECLARE @DOC_ID AS INTEGER
						DECLARE cursor_Docs CURSOR FOR 
							SELECT T.DOC_ID FROM TBPMO_DOC_RECORD_LINK T, TBPMO_RECORD TR WHERE 
							T.RECORD_ID = @RECORD_ID 
							AND T.RECORD_ID = TR.GUID AND TR.FORM_ID = @ENTITY_ID AND T.DOC_ID IN (
							SELECT dwObjectID FROM VWPMO_WD_OBJECT_RIGHTS WHERE [dwUserOrGroupID] = @USER_RIGHTADMIN_ID_ECM)
							
						OPEN cursor_Docs 
						FETCH NEXT FROM cursor_Docs INTO @DOC_ID
						WHILE @@FETCH_STATUS = 0
						BEGIN
							--PRINT 'INSERTING ENT_ID: '  + CONVERT(VARCHAR(10),@ENTITY_ID) +' - DOC_ID: '  + CONVERT(VARCHAR(10),@DOC_ID) + ' - RIGHT: '+ CONVERT(VARCHAR(10),@WD_RIGHT)
							INSERT INTO TBPMO_RIGHTS_WORK_SERVICE (DOC_ID,USER_ID,RIGHT_MODIFY,FILE_RIGHT,REASON,ADDED_WHO) VALUES
							(@DOC_ID,@pUSER_ID,'ADD RIGHT', @WD_RIGHT,'USER_RIGHTS_RENEW',@pCHECK_WHO)	
		
							FETCH NEXT FROM cursor_Docs INTO @DOC_ID
						END
						CLOSE cursor_Docs
						DEALLOCATE cursor_Docs

						
						FETCH NEXT FROM cursor_Records INTO @RECORD_ID
					END
					CLOSE cursor_Records
					DEALLOCATE cursor_Records
					DECLARE @DEL_SQL NVARCHAR(500)
					SET @DEL_SQL = CONVERT(NVARCHAR(30),N'DROP VIEW VWTEMP_REC_USER')
					EXEC sp_executesql @DEL_SQL
				END
				FETCH NEXT FROM cursor_constructors INTO @CONSTR_DET_ID, @ENTITY_ID
			END
			CLOSE cursor_constructors
			DEALLOCATE cursor_constructors
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 TABLE TBPMO_USER_GLOBAL_SEARCH(
	[GUID]			INT		IDENTITY(1,1) NOT NULL,
	USER_ID			INT					NOT NULL,
	COMMENT			VARCHAR(500),
	ADDED_WHO		VARCHAR(50)			NOT NULL DEFAULT ('DEFAULT'),
	ADDED_WHEN		DATETIME			DEFAULT (GETDATE()),
CONSTRAINT 	   	[PK_TBPMO_USER_GLOBAL_SEARCH] 	PRIMARY KEY(GUID),
CONSTRAINT		FK_TBPMO_USER_GLOBAL_SEARCH_USER_ID	FOREIGN KEY (USER_ID) REFERENCES  TBDD_USER (GUID))
GO








