UPDATE TBDD_MODULES SET PRODUCT_VERSION = '2.4.7.3' where NAME = 'Record-Organizer' 
GO
ALTER TABLE TBPMO_WORKFLOW
	ADD CREATE_ON_REC_CREATE BIT NOT NULL DEFAULT 0
GO
ALTER TABLE TBPMO_WORKFLOW
	ADD CREATE_ON_FOLL_UP BIT NOT NULL DEFAULT 0
GO
ALTER TABLE TBPMO_WORKFLOW_ENTITY_STATE
	ADD INACTIVE BIT NOT NULL DEFAULT 0
GO
ALTER TABLE TBPMO_WORKFLOW_TASK
	ADD FINISHED BIT NOT NULL DEFAULT 0
GO
ALTER TABLE TBPMO_WORKFLOW
	ADD USER_SELECT VARCHAR(3000) NOT NULL DEFAULT ''
GO
ALTER TABLE TBPMO_WORKFLOW_TASK
	ADD USER_SELECT VARCHAR(3000) NOT NULL DEFAULT ''
GO

CREATE VIEW VWPMO_DOC_USER_RELATION AS
SELECT DISTINCT  
	T1.DOC_ID,
	T3.dwUserOrGroupID,
	T2.FORM_ID  AS ENTITY_ID
FROM TBPMO_DOC_RECORD_LINK T1, TBPMO_RECORD T2,VWPMO_WD_OBJECT_RIGHTS T3
WHERE T1.RECORD_ID = T2.GUID 
GO
ALTER FUNCTION [dbo].[FNPMO_GET_RIGHTS_FOR_DOC] (@pDOC_ID INTEGER)
RETURNS @OUTPUT_RIGHTS TABLE (USR_ID INT,USR_NAME VARCHAR(50), USR_ECM_ID INT,USR_RIGHT INT, TYPE_WRIGHT VARCHAR(50))
AS
BEGIN
    DECLARE
        @RECID                INTEGER,
        @ENT_ID                INTEGER,
        @GUID                INTEGER,
        @RIGHT_USER            VARCHAR(50),
        @RIGHT_USER_INT        INTEGER,
        @RIGHT_USER_ECM_ID    INTEGER,
        @AD_DOMAIN            VARCHAR(250)
    SELECT @RIGHT_USER =  T.AD_USER,@RIGHT_USER_INT = T1.GUID,@RIGHT_USER_ECM_ID = T1.USERID_FK_INT_ECM, @AD_DOMAIN = AD_DOMAIN FROM TBPMO_SERVICE_RIGHT_CONFIG T, TBDD_USER T1 WHERE T.GUID = 1 AND T.AD_USER = T1.USERNAME
    
    INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT) VALUES (@RIGHT_USER_INT,@RIGHT_USER,@RIGHT_USER_ECM_ID,7,'MAIN RIGHTUSER')
                            
    DECLARE cursorRecords CURSOR FOR 
        select  T.RECORD_ID, T1.FORM_ID from TBPMO_DOC_RECORD_LINK T, TBPMO_RECORD T1 where T.DOC_ID = @pDOC_ID AND T.RECORD_ID = T1.GUID
    OPEN cursorRecords 
    FETCH NEXT FROM cursorRecords INTO @RECID,@ENT_ID
    WHILE @@FETCH_STATUS = 0
    BEGIN
        --PRINT 'RECORD-ID: ' + CONVERT(VARCHAR(10),@RECID)
        /*CURSOR 1 RECORDS BEGIN*/
        -- ENTITY-SUPERVISOR INSERTEN
        INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT)
        SELECT T.USER_ID, T1.USERNAME, T1.USERID_FK_INT_ECM,T.RIGHT1, 'ENTITY_SUPERVISOR' FROM TBPMO_ENTITY_SUPERVISOR T, TBDD_USER T1 WHERE T.USER_ID = T1.GUID AND T.ENTITY_ID = @ENT_ID
        AND T.USER_ID NOT IN (SELECT USR_ID FROM @OUTPUT_RIGHTS) ORDER BY T1.USERNAME
        -- SUPERVISOR RECORD INSERTEN
        INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT)
        SELECT T.[USER_ID], T1.USERNAME, T1.USERID_FK_INT_ECM,3,'SUPERVISOR_CONTROL'  FROM [VWPMO_SUPERVISOR_CONTROL] T, TBDD_USER T1 where T.USER_ID = T1.GUID AND T.RECORD_ID = @RECID and T.FORM_ID = @ENT_ID AND
        T.USER_ID NOT IN (SELECT USR_ID FROM @OUTPUT_RIGHTS) ORDER BY T1.USERNAME
        /**/
        DECLARE
            @VALUE    VARCHAR(100),
            @RIGHT    INT,
            @TYPE_VALUE    VARCHAR(50),
            @CONTR_TYPE_ID    INT,
            @CONTROL_ID        INT
                                
        DECLARE cursorRecordRightControls CURSOR FOR 
            SELECT T2.VALUE, CASE T.USED_FOR WHEN 'RIGHT READ/WRITE' THEN 3 ELSE 1 END, T.USER_GROUP, T1.CONTROL_TYPE_ID, T1.GUID
            FROM TBPMO_ENTITY_RIGHT_CONTROLS T, TBPMO_CONTROL T1, TBPMO_CONTROL_VALUE T2 WHERE T.CONTROL_ID = T1.GUID AND 
            T.CONTROL_ID = T2.CONTROL_ID AND
            T.ENTITY_ID = @ENT_ID AND T.USED_FOR <> '' AND T2.RECORD_ID = @RECID
        OPEN cursorRecordRightControls 
        FETCH NEXT FROM cursorRecordRightControls INTO @VALUE,@RIGHT,@TYPE_VALUE,@CONTR_TYPE_ID,@CONTROL_ID
        WHILE @@FETCH_STATUS = 0
        BEGIN
            IF @TYPE_VALUE = 'USER' 
                BEGIN
                    DECLARE @TEMP_USRID INT
                    SELECT @TEMP_USRID = [dbo].[FNPMO_GET_USERID_FORSTRING] (@VALUE)
                    IF NOT EXISTS(SELECT USR_ID FROM @OUTPUT_RIGHTS WHERE USR_ID = @TEMP_USRID)
                        BEGIN
                        --UPDATE @USER_TABLE SET USR_RIGHT = @RIGHT WHERE USR_ID = @TEMP_USRID
                        INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT)
                        SELECT @TEMP_USRID, USERNAME, USERID_FK_INT_ECM,@RIGHT,'ENTITY RIGHT CONTROL USER: ' + convert(Varchar(10),@CONTROL_ID) FROM TBDD_USER WHERE GUID = @TEMP_USRID
                        END
                END
            ELSE
                BEGIN
                    DECLARE    @GROUP_USER_ID    INTEGER        
                    DECLARE cursorUserGroup CURSOR FOR 
                        SELECT DISTINCT T.USER_ID FROM TBDD_GROUPS_USER T,TBDD_USER_GROUPS T1, TBDD_USER T2 WHERE T.GROUP_ID = T1.GUID AND T.USER_ID = T2.GUID AND UPPER(T1.NAME) = UPPER(@VALUE) 
                        AND T.USER_ID <> @RIGHT_USER_INT AND T.USER_ID NOT IN (SELECT USR_ID FROM @OUTPUT_RIGHTS)
                    OPEN cursorUserGroup 
                    FETCH NEXT FROM cursorUserGroup INTO @GROUP_USER_ID
                     WHILE @@FETCH_STATUS = 0
                    BEGIN
                        --IF NOT EXISTS(SELECT USR_ID FROM @OUTPUT_RIGHTS WHERE USR_ID = @TEMP_USRID)
                            BEGIN
                                INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT)
                                SELECT @GROUP_USER_ID,USERNAME, USERID_FK_INT_ECM,@RIGHT,'ENTITY RIGHT CONTROL GROUP: ' + convert(Varchar(10),@CONTROL_ID)  FROM TBDD_USER WHERE GUID = @GROUP_USER_ID
                            END
                        --UPDATE @USER_TABLE SET USR_RIGHT = @RIGHT WHERE USR_ID = @GROUP_USER_ID
                        FETCH NEXT FROM cursorUserGroup INTO @GROUP_USER_ID
                    END
                    CLOSE cursorUserGroup
                    DEALLOCATE cursorUserGroup
                END
            FETCH NEXT FROM cursorRecordRightControls INTO @VALUE,@RIGHT,@TYPE_VALUE,@CONTR_TYPE_ID,@CONTROL_ID
        END
        CLOSE cursorRecordRightControls
        DEALLOCATE cursorRecordRightControls
        /*CURSOR 2 RIGHT-CONTROLS END*/
        IF EXISTS(SELECT GUID FROM TBPMO_RIGHT_USER WHERE ENTITY_ID = @ENT_ID)
            BEGIN    
                DECLARE
                    @WD_RIGHT            INTEGER        
				DECLARE c_REDO CURSOR FOR 
					SELECT T.[USER_ID] ,T1.USERID_FK_INT_ECM, T1.USERNAME ,CASE WHEN T.[READ_ONLY_DOC] = 1 THEN 1 ELSE 3 END AS WD_RIGHT FROM [DD_ECM].[dbo].[TBPMO_RIGHT_USER] T, TBDD_USER T1 WHERE 
					T.ENTITY_ID = @ENT_ID AND T.USER_ID = T1.GUID
				OPEN c_REDO 
                FETCH NEXT FROM c_REDO INTO @RIGHT_USER_INT,@RIGHT_USER_ECM_ID,@RIGHT_USER,@WD_RIGHT
                WHILE @@FETCH_STATUS = 0
                BEGIN
                    IF EXISTS(SELECT USR_ID FROM @OUTPUT_RIGHTS WHERE USR_ID = @RIGHT_USER_INT)
                        BEGIN    
                            DECLARE @TEMP_RIGHT INT
                            SELECT @TEMP_RIGHT = USR_RIGHT FROM @OUTPUT_RIGHTS WHERE USR_ID = @RIGHT_USER_INT
                            IF @TEMP_RIGHT < @WD_RIGHT
                                UPDATE @OUTPUT_RIGHTS SET USR_RIGHT = @WD_RIGHT WHERE USR_ID = @RIGHT_USER_INT
                        END
                    ELSE
                        INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT) VALUES (
                        @RIGHT_USER_INT,@RIGHT_USER,@RIGHT_USER_ECM_ID,@WD_RIGHT,'RIGHT FROM USER_RIGHT')

                    FETCH NEXT FROM c_REDO INTO @RIGHT_USER_INT,@RIGHT_USER_ECM_ID,@RIGHT_USER,@WD_RIGHT
                END
            CLOSE c_REDO
            DEALLOCATE c_REDO
            END
      
        FETCH NEXT FROM cursorRecords INTO @RECID,@ENT_ID
        /*CURSOR 1 RECORDS END*/
    END
CLOSE cursorRecords
DEALLOCATE cursorRecords

RETURN
END

GO

ALTER VIEW [dbo].[VWPMO_WF_ACTIVE] AS 
SELECT TOP 100 PERCENT
	W_T.GUID AS WF_TASK_ID
	,R.GUID AS WF_ID
	,W_T.RECORD_ID
	,T.STATE_ID
	,T4.GUID FORM_VIEW_ID
	,R.DESCRIPTION
	,R.MANUALLY
	,R.CREATE_ON_REC_CREATE
	,R.CREATE_ON_FOLL_UP
	,W_T.COMMENT 
	,W_T.DUE_DATE
	,W_T.ADDED_WHEN
	,W_T.CHANGED_WHO
	,W_T.CHANGED_WHEN
	,W_T.TASK_DATE
	,T.FINAL
	,T.ENTITY_ID
	,T5.COLOR
	,dbo.FNPMO_GET_ENTITY_COLUMN_CONTENT(W_T.RECORD_ID) AS RECORD_DESCRIPTION
	,FINISHED
FROM  
	TBPMO_WORKFLOW_ENTITY_STATE T,        
	TBPMO_WORKFLOW_TASK W_T
	,TBPMO_WORKFLOW R
	,TBPMO_FORM T3
	,TBPMO_FORM_VIEW T4
	,TBPMO_WORKFLOW_TASK_STATE T5
	,TBPMO_RECORD REC
WHERE
	--W_T.RECORD_ID = 95 AND 
	--TBPMO_WORKFLOW_ENTITY_STATE
	T.WF_ID = R.GUID AND
	T.ENTITY_ID = T3.GUID AND
	T.STATE_ID = T5.GUID AND
	W_T.RECORD_ID = REC.GUID AND
	T.ENTITY_ID = REC.FORM_ID AND
	W_T.WF_ID = R.GUID AND
	R.GUID = R.GUID AND
	T3.GUID = T4.FORM_ID AND
	W_T.STATE_ID = T5.GUID AND
	T4.SCREEN_ID = 1 AND
	((T.FINAL = 1 AND W_T.DUE_DATE IS NULL) OR T.FINAL = 0) AND
	W_T.ACTIVE = 1 AND
	T.INACTIVE = 0
ORDER BY W_T.RECORD_ID,W_T.GUID
GO
ALTER PROCEDURE [dbo].[PRPMO_CREATE_WF_TASKS] (@FORM_ID INTEGER, @RECORD_ID INTEGER, @MAN_AUTO BIT = 1)
AS
BEGIN
	DECLARE
		@WF_ID			INTEGER,
		@TITLE			VARCHAR(50),
		@SEQUENCE		INTEGER,
		@MELDUNG		VARCHAR(250),
		@STATE_ID		INTEGER,
		@SELECT_USER	VARCHAR(3000)
								
	DECLARE c_TASK CURSOR FOR 
		SELECT DISTINCT T.WF_ID FROM TBPMO_WORKFLOW_ENTITY_STATE T,TBPMO_WORKFLOW T1  WHERE 
		T.WF_ID = T1.GUID AND T.ENTITY_ID = @FORM_ID AND T1.MANUALLY = @MAN_AUTO
  	OPEN c_TASK 
    	FETCH NEXT FROM c_TASK INTO @WF_ID
    	WHILE @@FETCH_STATUS = 0
		BEGIN
			--DELETE FROM TBPMO_WORKFLOW_TASK_HISTORY WHERE WF_TASK_ID IN (SELECT GUID FROM TBPMO_WORKFLOW_TASK WHERE RECORD_ID = @RECORD_ID AND WF_ID = @WF_ID)
			--DELETE FROM TBPMO_WORKFLOW_TASK_USER WHERE WF_TASK_ID IN (SELECT GUID FROM TBPMO_WORKFLOW_TASK WHERE RECORD_ID = @RECORD_ID AND WF_ID = @WF_ID)
			UPDATE TBPMO_WORKFLOW_TASK SET ACTIVE = 0 WHERE GUID IN (SELECT GUID FROM TBPMO_WORKFLOW_TASK WHERE RECORD_ID = @RECORD_ID AND WF_ID = @WF_ID)
			Print 'All data deleted'
			SELECT @STATE_ID = STATE_ID FROM TBPMO_WORKFLOW_ENTITY_STATE WHERE ENTITY_ID = @FORM_ID and WF_ID = @WF_ID AND GUID = (SELECT MIN(GUID) FROM TBPMO_WORKFLOW_ENTITY_STATE WHERE ENTITY_ID = @FORM_ID and WF_ID = @WF_ID)
			SELECT @SELECT_USER = USER_SELECT FROM TBPMO_WORKFLOW WHERE GUID = @WF_ID
			--IF @STATE_ID IS NOT NULL
				BEGIN
					--INSERT DES TASKS
					INSERT INTO TBPMO_WORKFLOW_TASK(
						RECORD_ID,
						WF_ID,
						ACTIVE,
						STATE_ID,
						USER_SELECT)
					VALUES (
						@RECORD_ID,
						@WF_ID,
						1,
						@STATE_ID,
						@SELECT_USER)
				END
						        	
			FETCH NEXT FROM c_TASK INTO @WF_ID
		END
	CLOSE c_TASK
	DEALLOCATE c_TASK
  END
  GO

  DECLARE
	@ENT_ID	INTEGER,
	@DET_ID	INTEGER,		
	@USR_ID	INTEGER							
DECLARE c_REDO CURSOR FOR 
	select  T.ENTITY_ID, T1.GUID, T.USER_ID from TBPMO_RIGHT_USER T, VWPMO_CONSTRUCTOR_FORMS T1 where T.ENTITY_ID = T1.FORM_ID
	
  OPEN c_REDO 
    FETCH NEXT FROM c_REDO INTO @ENT_ID,@DET_ID,@USR_ID	
    WHILE @@FETCH_STATUS = 0
	BEGIN
		IF NOT EXISTS(SELECT GUID FROM TBPMO_USER_CONSTR_VIEW_TYPE  WHERE CONSTRUCTOR_DETAIL_ID = @DET_ID AND [ENTITY_ID] = @ENT_ID AND USER_ID = @USR_ID)
			BEGIN
			PRINT 'USER_ID: ' + CONVERT(VARCHAR(10),@USR_ID) + ' - @DET_ID: ' + CONVERT(VARCHAR(10),@DET_ID)+ ' - @@ENT_ID: ' + CONVERT(VARCHAR(10),@ENT_ID)
			INSERT INTO TBPMO_USER_CONSTR_VIEW_TYPE (USER_ID,CONSTRUCTOR_DETAIL_ID,ENTITY_ID, VIEW_ID,ADDED_WHO) VALUES (
			@USR_ID,@DET_ID,@ENT_ID,1,'DD')
			END
		FETCH NEXT FROM c_REDO INTO @ENT_ID,@DET_ID,@USR_ID	
	END
CLOSE c_REDO
DEALLOCATE c_REDO

DELETE FROm TBPMO_RIGHT_USER WHERE USER_ID NOT IN (SELECT GUID FROM TBDD_USER)

ALTER FUNCTION [dbo].[FNPMO_GET_RIGHTS_FOR_DOC] (@pDOC_ID INTEGER)
RETURNS @OUTPUT_RIGHTS TABLE (USR_ID INT,USR_NAME VARCHAR(50), USR_ECM_ID INT,USR_RIGHT INT, TYPE_WRIGHT VARCHAR(50))
AS
BEGIN
	DECLARE
		@RECID				INTEGER,
		@ENT_ID				INTEGER,
		@GUID				INTEGER,
		@RIGHT_USER			VARCHAR(50),
		@RIGHT_USER_INT		INTEGER,
		@RIGHT_USER_ECM_ID	INTEGER,
		@AD_DOMAIN			VARCHAR(250)
	SELECT @RIGHT_USER =  T.AD_USER,@RIGHT_USER_INT = T1.GUID,@RIGHT_USER_ECM_ID = T1.USERID_FK_INT_ECM, @AD_DOMAIN = AD_DOMAIN FROM TBPMO_SERVICE_RIGHT_CONFIG T, TBDD_USER T1 WHERE T.GUID = 1 AND T.AD_USER = T1.USERNAME
	
	INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT) VALUES (@RIGHT_USER_INT,@RIGHT_USER,@RIGHT_USER_ECM_ID,7,'MAIN RIGHTUSER')
							
	DECLARE cursorRecords CURSOR FOR 
		select  T.RECORD_ID, T1.FORM_ID from TBPMO_DOC_RECORD_LINK T, TBPMO_RECORD T1 where T.DOC_ID = @pDOC_ID AND T.RECORD_ID = T1.GUID
	OPEN cursorRecords 
    FETCH NEXT FROM cursorRecords INTO @RECID,@ENT_ID
    WHILE @@FETCH_STATUS = 0
	BEGIN
		--PRINT 'RECORD-ID: ' + CONVERT(VARCHAR(10),@RECID)
		/*CURSOR 1 RECORDS BEGIN*/
		-- ENTITY-SUPERVISOR INSERTEN
		INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT)
		SELECT T.USER_ID, T1.USERNAME, T1.USERID_FK_INT_ECM,T.RIGHT1, 'ENTITY_SUPERVISOR' FROM TBPMO_ENTITY_SUPERVISOR T, TBDD_USER T1 WHERE T.USER_ID = T1.GUID AND T.ENTITY_ID = @ENT_ID
		AND T.USER_ID NOT IN (SELECT USR_ID FROM @OUTPUT_RIGHTS) ORDER BY T1.USERNAME
		-- SUPERVISOR RECORD INSERTEN
		INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT)
		SELECT T.[USER_ID], T1.USERNAME, T1.USERID_FK_INT_ECM,3,'SUPERVISOR_CONTROL'  FROM [VWPMO_SUPERVISOR_CONTROL] T, TBDD_USER T1 where T.USER_ID = T1.GUID AND T.RECORD_ID = @RECID and T.FORM_ID = @ENT_ID AND
		T.USER_ID NOT IN (SELECT USR_ID FROM @OUTPUT_RIGHTS) ORDER BY T1.USERNAME
		/**/
		DECLARE
			@VALUE	VARCHAR(100),
			@RIGHT	INT,
			@TYPE_VALUE	VARCHAR(50),
			@CONTR_TYPE_ID	INT,
			@CONTROL_ID		INT
								
		DECLARE cursorRecordRightControls CURSOR FOR 
			SELECT T2.VALUE, CASE T.USED_FOR WHEN 'RIGHT READ/WRITE' THEN 3 ELSE 1 END, T.USER_GROUP, T1.CONTROL_TYPE_ID, T1.GUID
			FROM TBPMO_ENTITY_RIGHT_CONTROLS T, TBPMO_CONTROL T1, TBPMO_CONTROL_VALUE T2 WHERE T.CONTROL_ID = T1.GUID AND 
			T.CONTROL_ID = T2.CONTROL_ID AND
			T.ENTITY_ID = @ENT_ID AND T.USED_FOR <> '' AND T2.RECORD_ID = @RECID
		OPEN cursorRecordRightControls 
		FETCH NEXT FROM cursorRecordRightControls INTO @VALUE,@RIGHT,@TYPE_VALUE,@CONTR_TYPE_ID,@CONTROL_ID
		WHILE @@FETCH_STATUS = 0
		BEGIN
			IF @TYPE_VALUE = 'USER' 
				BEGIN
					DECLARE @TEMP_USRID INT
					SELECT @TEMP_USRID = [dbo].[FNPMO_GET_USERID_FORSTRING] (@VALUE)
					IF NOT EXISTS(SELECT USR_ID FROM @OUTPUT_RIGHTS WHERE USR_ID = @TEMP_USRID)
						BEGIN
						--UPDATE @USER_TABLE SET USR_RIGHT = @RIGHT WHERE USR_ID = @TEMP_USRID
						INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT)
						SELECT @TEMP_USRID, USERNAME, USERID_FK_INT_ECM,@RIGHT,'ENTITY RIGHT CONTROL USER: ' + convert(Varchar(10),@CONTROL_ID) FROM TBDD_USER WHERE GUID = @TEMP_USRID
						END
				END
			ELSE
				BEGIN
					DECLARE	@GROUP_USER_ID	INTEGER		
					DECLARE cursorUserGroup CURSOR FOR 
						SELECT DISTINCT T.USER_ID FROM TBDD_GROUPS_USER T,TBDD_USER_GROUPS T1, TBDD_USER T2 WHERE T.GROUP_ID = T1.GUID AND T.USER_ID = T2.GUID AND UPPER(T1.NAME) = UPPER(@VALUE) 
						AND T.USER_ID <> @RIGHT_USER_INT AND T.USER_ID NOT IN (SELECT USR_ID FROM @OUTPUT_RIGHTS)
					OPEN cursorUserGroup 
					FETCH NEXT FROM cursorUserGroup INTO @GROUP_USER_ID
					 WHILE @@FETCH_STATUS = 0
					BEGIN
						--IF NOT EXISTS(SELECT USR_ID FROM @OUTPUT_RIGHTS WHERE USR_ID = @TEMP_USRID)
							BEGIN
								INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT)
								SELECT @GROUP_USER_ID,USERNAME, USERID_FK_INT_ECM,@RIGHT,'ENTITY RIGHT CONTROL GROUP: ' + convert(Varchar(10),@CONTROL_ID)  FROM TBDD_USER WHERE GUID = @GROUP_USER_ID
							END
						--UPDATE @USER_TABLE SET USR_RIGHT = @RIGHT WHERE USR_ID = @GROUP_USER_ID
						FETCH NEXT FROM cursorUserGroup INTO @GROUP_USER_ID
					END
					CLOSE cursorUserGroup
					DEALLOCATE cursorUserGroup
				END
			FETCH NEXT FROM cursorRecordRightControls INTO @VALUE,@RIGHT,@TYPE_VALUE,@CONTR_TYPE_ID,@CONTROL_ID
		END
		CLOSE cursorRecordRightControls
		DEALLOCATE cursorRecordRightControls
		/*CURSOR 2 RIGHT-CONTROLS END*/
		IF EXISTS(SELECT GUID FROM TBPMO_RIGHT_USER WHERE ENTITY_ID = @ENT_ID)
			BEGIN	
				DECLARE
					@WD_RIGHT			INTEGER		
								
			DECLARE c_REDO CURSOR FOR 
				SELECT T.[USER_ID] ,T1.USERID_FK_INT_ECM, T1.USERNAME ,CASE WHEN T.[READ_ONLY_DOC] = 1 THEN 1 ELSE 3 END AS WD_RIGHT FROM TBPMO_RIGHT_USER T, TBDD_USER T1 WHERE 
				T.ENTITY_ID = @ENT_ID AND T.USER_ID = T1.GUID
			  OPEN c_REDO 
				FETCH NEXT FROM c_REDO INTO @RIGHT_USER_INT,@RIGHT_USER_ECM_ID,@RIGHT_USER,@WD_RIGHT
				WHILE @@FETCH_STATUS = 0
				BEGIN
					IF EXISTS(SELECT USR_ID FROM @OUTPUT_RIGHTS WHERE USR_ID = @RIGHT_USER_INT)
						BEGIN	
							DECLARE @TEMP_RIGHT INT
							SELECT @TEMP_RIGHT = USR_RIGHT FROM @OUTPUT_RIGHTS WHERE USR_ID = @RIGHT_USER_INT
							IF @TEMP_RIGHT < @WD_RIGHT
								UPDATE @OUTPUT_RIGHTS SET USR_RIGHT = @WD_RIGHT WHERE USR_ID = @RIGHT_USER_INT
						END
					ELSE
						INSERT INTO @OUTPUT_RIGHTS(USR_ID,USR_NAME,USR_ECM_ID,USR_RIGHT,TYPE_WRIGHT) VALUES (
						@RIGHT_USER_INT,@RIGHT_USER,@RIGHT_USER_ECM_ID,@WD_RIGHT,'RIGHT FROM USER_RIGHT')

					FETCH NEXT FROM c_REDO INTO @RIGHT_USER_INT,@RIGHT_USER_ECM_ID,@RIGHT_USER,@WD_RIGHT
				END
			CLOSE c_REDO
			DEALLOCATE c_REDO
			END
		
		FETCH NEXT FROM cursorRecords INTO @RECID,@ENT_ID
		/*CURSOR 1 RECORDS END*/
	END
CLOSE cursorRecords
DEALLOCATE cursorRecords

RETURN
END







