UPDATE TBDD_MODULES SET PRODUCT_VERSION = '2.4.6.3' where NAME = 'Record-Organizer' 
GO
ALTER TRIGGER [dbo].[TBDD_USER_AFT_DEL] ON [dbo].[TBDD_USER]
FOR DELETE
AS
DECLARE @USER_ID INTEGER
BEGIN
	SELECT @USER_ID = GUID FROM DELETED
	IF @USER_ID IN (0,1)
		BEGIN
			ROLLBACK TRANSACTION
			RAISERROR('THIS USER CAN NOT BE DELETED!',16,10)
		END
	ELSE
		BEGIN
			INSERT INTO TBPMO_USER_RIGHTS_JOBS (USER_ID,COMMENT,ADDED_WHO) VALUES
			(@USER_ID,'USER DELETED','TBDD_GROUPS_USER_AFT_DEL')
			INSERT INTO TBPMO_LOG_ESSENTIALS (REFERENCE_KEY,REFERENCE_STRING,COMMENT,ADDED_WHO) VALUES 
				(@USER_ID,'USER-ID','USER DELETED','TBDD_USER_AFT_DEL')
		END
END
GO
ALTER TRIGGER [dbo].[TBDD_GROUPS_USER_AFT_DEL] ON [dbo].[TBDD_GROUPS_USER]
FOR DELETE
AS
	DECLARE @USER_ID INTEGER
	SELECT @USER_ID = deleted.USER_ID FROM deleted
	IF @USER_ID IS NOT NULL
		BEGIN
			INSERT INTO TBPMO_USER_RIGHTS_JOBS (USER_ID,COMMENT,ADDED_WHO) VALUES
			(@USER_ID,'USER DELETED FROM GROUP','TBDD_GROUPS_USER_AFT_DEL')
			INSERT INTO TBPMO_LOG_ESSENTIALS (REFERENCE_KEY,REFERENCE_STRING,COMMENT,ADDED_WHO) VALUES (@USER_ID,'USER-ID','USER DELETED FROM USER-GROUP',(SELECT USERNAME FROM TBDD_USER WHERE GUID = @USER_ID ))
		END
GO


 ALTER TABLE TBPMO_FORM_SQL
 ALTER COLUMN SQL_COMMAND VARCHAR(MAX)
 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.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 = '(SELECT [dbo].[FNPMO_GET_CONTROL_VALUES](' + CONVERT(VARCHAR(20),@CONTROL_ID) + ', T.GUID)) AS ' + @COL_NAME
							SET @SQL = 'dbo.FNPMO_GET_CONTROL_VALUES(' + CONVERT(VARCHAR(20),@CONTROL_ID) + ', T.GUID) AS ' + @COL_NAME  + 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
						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
CREATE VIEW [dbo].[VWPMO_DOC_SEARCH] AS 
SELECT 
	CONVERT(INTEGER,T.DocID) AS DocID
	,T.FULL_FILENAME  AS FULL_FILENAME
	,T.[Filename]
	,T.Doctype
	,T.[Version]
	,T.Creation_DateTime
	,T.Change_DateTime
	,T.OBJECTTYPE
	,T.DISPLAY_NAME
	,T.IN_WORK
	,T.IN_WORK_USER
	,T.IN_WORK_COMMENT
FROM 
	TBPMO_DOCRESULT_LIST T

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
ALTER PROCEDURE [dbo].[PRPMO_RIGHTS_SERVICE_ADD_RECORD](@pDOC_ID INT, @pDOC_PATH VARCHAR(1000), @pUSERSTRING VARCHAR(250), @pRIGHT INT, @pTYPE VARCHAR(100),@FROM_SERVICE INT)
AS
BEGIN TRY
	DECLARE @GUID INTEGER,
		@RIGHT	INTEGER
	SELECT @GUID = ISNULL(MAX(GUID),0) FROM TBPMO_RIGHTS_WORK_SERVICE_ADD_RIGHT WHERE 
			DOC_ID = @pDOC_ID
			AND UPPER(DOC_PATH) = UPPER(@pDOC_PATH)
			AND UPPER(USER_STRING) = UPPER(@pUSERSTRING)
	IF @GUID = 0
		INSERT INTO TBPMO_RIGHTS_WORK_SERVICE_ADD_RIGHT (DOC_ID,DOC_PATH,USER_STRING,FILE_RIGHT,RIGHT_TYPE,ADDED_WHO,[SERVICE]) 
			VALUES (@pDOC_ID,UPPER(@pDOC_PATH),UPPER(@pUSERSTRING),@pRIGHT,@pTYPE,'PRPMO_RIGHT_INSERT',@FROM_SERVICE)
	ELSE
		BEGIN
			SELECT @RIGHT = FILE_RIGHT FROM TBPMO_RIGHTS_WORK_SERVICE_ADD_RIGHT WHERE GUID = @GUID
			IF @RIGHT <> @pRIGHT
				IF @RIGHT < @pRIGHT
					UPDATE TBPMO_RIGHTS_WORK_SERVICE_ADD_RIGHT SET FILE_RIGHT = @pRIGHT,RIGHT_TYPE = @pTYPE WHERE GUID = @GUID
		END
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

