UPDATE TBDD_MODULES SET PRODUCT_VERSION = '2.4.6.1' where NAME = 'Record-Organizer' 
GO
ALTER TABLE TBPMO_FORM
ADD REDUNDANT_ENTITY INTEGER NOT NULL DEFAULT 0
GO
ALTER TABLE TBPMO_DOCSEARCH_RESULTLIST_CONFIG
ADD EDITABLE BIT NOT NULL DEFAULT 0
GO
ALTER PROCEDURE PRPMO_COPY_ENTITY (@pFORM_ID INT, @pADDEDWHO VARCHAR(50))
AS
DECLARE	
		@NEW_FORM_ID INT,
		@PARENT_ID INT,
		@NAME		VARCHAR(100),
		@LEVEL		VARCHAR(10),
		@SHOW_FORM_CONSTR	BIT,
		@FORM_TYPE_ID INT,
		@CONTROL_GUID INTEGER,
		@CONTROL_TYPE_ID INT,
		@CONTROL_NAME VARCHAR(100)
		,@COL_NAME VARCHAR(100)
		,@REQUIRED BIT
		,@DEFAULT_VALUE VARCHAR(100)
		,@SQL_COMMAND_1 VARCHAR(2000)
		,@SQL_COMMAND_2 VARCHAR(2000)
		,@CONNECTION_ID_1 SMALLINT
		,@CONNECTION_ID_2 SMALLINT
		,@READ_ONLY BIT
		,@VALIDATION BIT
		,@MULTILINE BIT
		,@PARENT_CONTROL_ID INT
		,@MASTER_DATA_ID INT
		,@FORMAT_TYPE VARCHAR(50)
		,@SHOW_COLUMN BIT
		,@STATIC_LIST VARCHAR(2000)

		SELECT @PARENT_ID = PARENT_ID, @NAME = 'COPY_' + NAME,@LEVEL = [LEVEL],@SHOW_FORM_CONSTR =  SHOW_FORM_CONSTRUCT, @FORM_TYPE_ID = FORM_TYPE_ID FROM TBPMO_FORM WHERE GUID = @pFORM_ID
		
		INSERT INTO TBPMO_FORM (
			PARENT_ID,NAME,LEVEL,ADDED_WHO,SHOW_FORM_CONSTRUCT,FORM_TYPE_ID) VALUES (
			@PARENT_ID,@NAME,@LEVEL,@pADDEDWHO,@SHOW_FORM_CONSTR,@FORM_TYPE_ID)

		SELECT @NEW_FORM_ID =  MAX(GUID) FROM TBPMO_FORM
		INSERT INTO TBPMO_FORM_VIEW (FORM_ID, SCREEN_ID, FORM_TITLE, DESCRIPTION, ADDED_WHO) VALUES 
			(@NEW_FORM_ID, 1, 'UNDEFINED_FORMTITLE', '', @pADDEDWHO)

DECLARE	c_CONTROL	CURSOR FOR SELECT GUID,[CONTROL_TYPE_ID]
							  ,'COPY_' + [NAME]
							  ,[COL_NAME]
							  ,[REQUIRED]
							  ,[DEFAULT_VALUE]
							  ,[SQL_COMMAND_1]
							  ,[SQL_COMMAND_2]
							  ,[CONNECTION_ID_1]
							  ,[CONNECTION_ID_2]
							  ,[READ_ONLY]
							  ,[VALIDATION]
							  ,[MULTILINE]
							  ,[PARENT_CONTROL_ID]
							  ,[MASTER_DATA_ID]
							  ,[FORMAT_TYPE]
							  ,[SHOW_COLUMN],STATIC_LIST FROM TBPMO_CONTROL WHERE FORM_ID = @pFORM_ID ORDER BY PARENT_CONTROL_ID


BEGIN
	OPEN c_CONTROL 
    FETCH NEXT FROM c_CONTROL INTO @CONTROL_GUID,@CONTROL_TYPE_ID,
		@CONTROL_NAME 
		,@COL_NAME
		,@REQUIRED
		,@DEFAULT_VALUE
		,@SQL_COMMAND_1
		,@SQL_COMMAND_2
		,@CONNECTION_ID_1
		,@CONNECTION_ID_2
		,@READ_ONLY
		,@VALIDATION
		,@MULTILINE
		,@PARENT_CONTROL_ID
		,@MASTER_DATA_ID
		,@FORMAT_TYPE
		,@SHOW_COLUMN
		,@STATIC_LIST
    
	WHILE @@FETCH_STATUS = 0
		BEGIN
		IF @PARENT_CONTROL_ID > 0
			BEGIN
				DECLARE @CTRLNAME VARCHAR(100)
				SELECT @CTRLNAME = 'COPY_' + [NAME] FROM TBPMO_CONTROL WHERE GUID = @PARENT_CONTROL_ID
				PRINT 'PCTRID = ' + CONVERT(VARCHAR(10),@PARENT_CONTROL_ID)
				PRINT '@CTRLNAME = ' + @CTRLNAME
				SELECT @PARENT_CONTROL_ID = GUID FROM TBPMO_CONTROL WHERE NAME = @CTRLNAME AND FORM_ID = @NEW_FORM_ID
				PRINT 'NEW PCTRID = ' + CONVERT(VARCHAR(10),@PARENT_CONTROL_ID)
			END
			
			INSERT INTO TBPMO_CONTROL (
				FORM_ID
				,[CONTROL_TYPE_ID]
				,[NAME]
				,[COL_NAME]
				,[REQUIRED]
				,[DEFAULT_VALUE]
				,[SQL_COMMAND_1]
				,[SQL_COMMAND_2]
				,[CONNECTION_ID_1]
				,[CONNECTION_ID_2]
				,[READ_ONLY]
				,[VALIDATION]
				,[MULTILINE]
				,[PARENT_CONTROL_ID]
				,[MASTER_DATA_ID]
				,[FORMAT_TYPE]
				,[SHOW_COLUMN]
				,[STATIC_LIST]
				,ADDED_WHO
			) VALUES (
				@NEW_FORM_ID
				,@CONTROL_TYPE_ID
				,@CONTROL_NAME 
				,@COL_NAME
				,@REQUIRED
				,@DEFAULT_VALUE
				,@SQL_COMMAND_1
				,@SQL_COMMAND_2
				,@CONNECTION_ID_1
				,@CONNECTION_ID_2
				,@READ_ONLY
				,@VALIDATION
				,@MULTILINE
				,@PARENT_CONTROL_ID
				,@MASTER_DATA_ID
				,@FORMAT_TYPE
				,@SHOW_COLUMN
				,@STATIC_LIST
				,@pADDEDWHO
			)
				DECLARE @NEW_CONTROLID INTEGER
				SELECT @NEW_CONTROLID = MAX(GUID) FROM TBPMO_CONTROL

				DECLARE
				   @SCREEN_ID	INTEGER
				  ,@CONTROL_TEXT VARCHAR(100)
				  ,@X_LOC	FLOAT
				  ,@Y_LOC	FLOAT
				  ,@HEIGHT INTEGER
				  ,@WIDTH INTEGER
				  ,@FONT_FAMILY VARCHAR(50)
				  ,@FONT_COLOR INTEGER
				  ,@FONT_SIZE INTEGER
				  ,@FONT_STYLE INTEGER
				  ,@TAB_INDEX INTEGER
				  ,@TAB_STOP BIT
				  ,@BACK_COLOR INTEGER

				 SELECT @SCREEN_ID = [SCREEN_ID], @CONTROL_TEXT = [CONTROL_TEXT],@X_LOC = [X_LOC],
					@Y_LOC = [Y_LOC],@HEIGHT = [HEIGHT], @WIDTH = [WIDTH],@FONT_FAMILY = [FONT_FAMILY],
					@FONT_COLOR = [FONT_COLOR], @FONT_SIZE = [FONT_SIZE],@FONT_STYLE = [FONT_STYLE],
					@TAB_INDEX = [TAB_INDEX], @TAB_STOP = [TAB_STOP], @BACK_COLOR = [BACK_COLOR] FROM TBPMO_CONTROL_SCREEN WHERE CONTROL_ID = @CONTROL_GUID
				INSERT INTO TBPMO_CONTROL_SCREEN (
					CONTROL_ID,
					SCREEN_ID
					,CONTROL_TEXT
					,X_LOC
					,Y_LOC
					,HEIGHT
					,WIDTH
					,FONT_FAMILY
					,FONT_COLOR
					,FONT_SIZE
					,FONT_STYLE
					,TAB_INDEX 
					,TAB_STOP
					,BACK_COLOR
					,ADDED_WHO) 
				VALUES(
					@NEW_CONTROLID
					,@SCREEN_ID
					,@CONTROL_TEXT
					,@X_LOC
					,@Y_LOC
					,@HEIGHT
					,@WIDTH
					,@FONT_FAMILY
					,@FONT_COLOR
					,@FONT_SIZE
					,@FONT_STYLE
					,@TAB_INDEX
					,@TAB_STOP
					,@BACK_COLOR
					,@pADDEDWHO
				)
				DECLARE @NEW_ID INT
				SELECT @NEW_ID = MAX(GUID) FROM TBPMO_CONTROL_SCREEN WHERE CONTROL_TEXT = @CONTROL_TEXT
				
				INSERT INTO TBPMO_CONTROL_LANGUAGE ([LANGUAGE_TYPE] ,[CONTROL_SCREEN_ID],[CAPTION],[ADDED_WHO]) VALUES (
				'de-DE',@NEW_ID,@CONTROL_TEXT,@pADDEDWHO)
				INSERT INTO TBPMO_CONTROL_LANGUAGE ([LANGUAGE_TYPE] ,[CONTROL_SCREEN_ID],[CAPTION],[ADDED_WHO]) VALUES (
				'en-EN',@NEW_ID,@CONTROL_TEXT,@pADDEDWHO)
			
			FETCH NEXT FROM c_CONTROL INTO @CONTROL_GUID,@CONTROL_TYPE_ID,
					@CONTROL_NAME 
					,@COL_NAME
					,@REQUIRED
					,@DEFAULT_VALUE
					,@SQL_COMMAND_1
					,@SQL_COMMAND_2
					,@CONNECTION_ID_1
					,@CONNECTION_ID_2
					,@READ_ONLY
					,@VALIDATION
					,@MULTILINE
					,@PARENT_CONTROL_ID
					,@MASTER_DATA_ID
					,@FORMAT_TYPE
					,@SHOW_COLUMN
					,@STATIC_LIST
		END

	CLOSE c_CONTROL
    DEALLOCATE c_CONTROL
END

ALTER FUNCTION [dbo].[FNPMO_DOC_GET_IDX](@pDOCID INTEGER, @pENTITY INTEGER, @DOCIDX VARCHAR(20))
RETURNS VARCHAR(250)
AS
BEGIN
	DECLARE @HEADER_CAPTION VARCHAR(30), @VALUE VARCHAR(250), @RESULT VARCHAR(250), @REDUNDANT_ID INTEGER
	SELECT @HEADER_CAPTION = HEADER_CAPTION FROM TBPMO_DOCSEARCH_RESULTLIST_CONFIG WHERE ENTITY_ID = @pENTITY AND COLUMN_VIEW = @DOCIDX
	IF @HEADER_CAPTION IS NULL
		BEGIN
			SELECT @REDUNDANT_ID = REDUNDANT_ENTITY FROM TBPMO_FORM WHERE GUID = @pENTITY
			IF @REDUNDANT_ID <> 0	
				SELECT @RESULT = dbo.FNPMO_DOC_GET_IDX(@pDOCID,@REDUNDANT_ID,@DOCIDX)
			ELSE
				SET @RESULT = 'NO CONFIGURATION'
		END
	ELSE
		BEGIN
			SELECT @VALUE =  VALUE FROM VWPMO_DOC_INDICES WHERE DocID = @pDOCID AND COLUMN_CAPTION = @HEADER_CAPTION
			IF @VALUE IS NOT NULL
				SET @RESULT = @VALUE
			ELSE
				SET @RESULT = ''
		END
	RETURN @RESULT
END
GO
ALTER TRIGGER [dbo].[TBPMO_DOC_RECORD_LINK_AFT_INS] ON [dbo].[TBPMO_DOC_RECORD_LINK]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE 
		@RECORD_ID		INTEGER,
		@DOC_ID			INTEGER,
		@ADDED_WHO		VARCHAR(50),
		@ENTITY_ID		INTEGER
	SELECT
  		@RECORD_ID = RECORD_ID,
		@DOC_ID = DOC_ID,
		@ADDED_WHO = ADDED_WHO	
	FROM INSERTED

	SELECT @ENTITY_ID = FORM_ID FROM TBPMO_RECORD WHERE GUID = @RECORD_ID
	--Den Defaultvalue der DropdownListe eintragen
	IF (SELECT COUNT(GUID) FROM TBPMO_DOCSEARCH_RESULTLIST_CONFIG WHERE ENTITY_ID = @ENTITY_ID AND TYPE_ID = 4) >= 1
		BEGIN
			INSERT INTO TBPMO_DOC_VALUES (DocID,CONFIG_ID,VALUE,ADDED_WHO) 
			SELECT @DOC_ID,T.GUID,T1.VALUE,@ADDED_WHO FROM TBPMO_DOCSEARCH_RESULTLIST_CONFIG T, TBPMO_DOCRESULT_DROPDOWN_ITEMS T1 WHERE T.GUID = T1.CONFIG_ID AND T1.[DEFAULT] = 1 AND T.ENTITY_ID = @ENTITY_ID AND T.TYPE_ID = 4
		END
	/*###### KUNDEN-INDIVIDUELL - BEGIN ####################################################################*/
	--DECLARE 
	--	@NODE_CONFIG_ID	INTEGER,
	--	@MATERIAL	VARCHAR(50)

	--IF @ADDED_WHO <> 'ZSG-IMPORT'
	--	BEGIN
	--		SELECT @NODE_CONFIG_ID = NODE_CONFIG_ID FROM TBPMO_STRUCTURE_NODES WHERE GUID = (SELECT NODE_GUID FROM TBPMO_RECORD WHERE GUID = @RECORD_ID )
	--		IF @NODE_CONFIG_ID IN (7,8,14)

	--			IF EXISTS(SELECT GUID FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = 14 AND RECORD_ID = @RECORD_ID)
	--				BEGIN
	--					DECLARE @RECID_LINK INTEGER
	--					SELECT @MATERIAL = UPPER(LTRIM(RTRIM(VALUE))) FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = 14 AND RECORD_ID = @RECORD_ID
	--					DECLARE curs_RECORD CURSOR LOCAL FOR 
	--						SELECT RECORD_ID FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = 14 AND UPPER(LTRIM(RTRIM(VALUE))) = @MATERIAL AND RECORD_ID <> @RECORD_ID
	--					OPEN curs_RECORD 
	--					FETCH NEXT FROM curs_RECORD INTO @RECID_LINK
	--					WHILE @@FETCH_STATUS = 0
	--						BEGIN
	--							IF NOT EXISTS(SELECT GUID FROM TBPMO_DOC_RECORD_LINK WHERE DOC_ID = @DOC_ID AND RECORD_ID = @RECID_LINK)
	--								INSERT INTO TBPMO_DOC_RECORD_LINK (RECORD_ID,DOC_ID,COMMENT, ADDED_WHO) VALUES
	--								(@RECID_LINK,@DOC_ID,'MATERIAL INSERT',@ADDED_WHO)
	--							FETCH NEXT FROM curs_RECORD INTO @RECID_LINK
	--						END
					
	--					CLOSE curs_RECORD
	--					DEALLOCATE curs_RECORD
	--				END
	--	END
	/*###### KUNDEN-INDIVIDUELL - ENDE ####################################################################*/
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN TRIGGER: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ROW: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - 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')
					
			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 TABLE TBPMO_RIGHT_USER
(
	GUID			INTEGER			NOT NULL	IDENTITY(1,1),
	USER_ID			INTEGER			NOT NULL,
	ENTITY_ID		INTEGER			NOT NULL,
	EDIT_REC		BIT				NOT NULL	DEFAULT 1,
	ADD_REC			BIT				NOT NULL	DEFAULT 0,			
	DELETE_REC		BIT				NOT NULL	DEFAULT 0,
	ADD_DOC			BIT				NOT NULL	DEFAULT 0,
	READ_ONLY_DOC	BIT				NOT NULL	DEFAULT 0,
	DELETE_DOC		BIT				NOT NULL	DEFAULT 0,
	FILES_VISIBLE	BIT				NOT NULL	DEFAULT 1,
	ADDED_WHEN		DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHEN	DATETIME
	CONSTRAINT		PK_TBPMO_RIGHT_USER PRIMARY KEY (GUID)
)
GO
CREATE TRIGGER TBPMO_RIGHT_USER_AFT_UPD ON TBPMO_RIGHT_USER
FOR UPDATE
AS
	UPDATE TBPMO_RIGHT_USER SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_RIGHT_USER.GUID = INSERTED.GUID
GO
ALTER TABLE TBPMO_KONFIGURATION
ADD WIKI_STRING VARCHAR(500) NOT NULL DEFAULT ''
GO
ALTER PROCEDURE [dbo].[PRPMO_DELETE_RESULTFILE](@pDOCID INT, @pRECORDID INT, @DELETE_FILE INT)
AS
BEGIN TRY
	DECLARE @COUNT INT
	SELECT @COUNT = COUNT(GUID) FROM TBPMO_DOC_RECORD_LINK WHERE DOC_ID = @pDOCID
	IF @COUNT = 1 OR (@DELETE_FILE = 1 AND @COUNT > 1)
		BEGIN
			DELETE FROM TBPMO_DOC_INDICES WHERE DocID = @pDOCID
			DELETE FROM TBPMO_DOC_RECORD_LINK WHERE Doc_ID = @pDOCID
			DELETE FROM TBPMO_DOCRESULT_LIST WHERE DocID = @pDOCID
		END
	ELSE IF @COUNT > 1 and @DELETE_FILE = 0
		BEGIN
			DELETE FROM TBPMO_DOC_RECORD_LINK WHERE Doc_ID = @pDOCID AND RECORD_ID = @pRECORDID
		END
	ELSE IF @DELETE_FILE = 1
		BEGIN
			DELETE FROM TBPMO_DOC_INDICES WHERE DocID = @pDOCID
			DELETE FROM TBPMO_DOC_RECORD_LINK WHERE Doc_ID = @pDOCID
			DELETE FROM TBPMO_DOCRESULT_LIST WHERE DocID = @pDOCID
		END
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ROW: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - ERROR-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
CREATE TRIGGER TBPMO_DOCSEARCH_RESULTLIST_CONFIG_AFT_DEL ON TBPMO_DOCSEARCH_RESULTLIST_CONFIG
FOR DELETE
AS
BEGIN TRY
	DECLARE 
		@CONFIG_ID INTEGER,
		@LANGUAGE  VARCHAR(5)

	SELECT 
		@CONFIG_ID = deleted.GUID,
		@LANGUAGE = deleted.LANGUAGE
	FROM deleted
	
	DELETE FROM TBPMO_DOC_VALUES WHERE CONFIG_ID = @CONFIG_ID
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 TBPMO_DOCRESULT_DROPDOWN_ITEMS
CREATE TABLE TBPMO_DOCRESULT_DROPDOWN_ITEMS
(
	GUID			INTEGER			NOT NULL	IDENTITY(1,1)
	,CONFIG_ID		INTEGER			NOT NULL
	,VALUE			VARCHAR(100)	NOT NULL
	,SEQUENCE		TINYINT			NOT NULL	DEFAULT 0
	,[DEFAULT]		BIT				NOT NULL	DEFAULT 0
	,COLOR			VARCHAR(50)		NOT NULL	DEFAULT ''
	,ADDED_WHO		VARCHAR(50)		NOT NULL	DEFAULT 'DEFAULT'
	,ADDED_WHEN		DATETIME 					DEFAULT GETDATE()
	,CHANGED_WHO     VARCHAR(50)
	,CHANGED_WHEN	DATETIME,
	CONSTRAINT		PK_TBPMO_DOCRESULT_DROPDOWN_ITEMS	PRIMARY KEY (GUID),
	CONSTRAINT		FK_TBPMO_DOCRESULT_DROPDOWN_ITEMSCONFIG_ID	FOREIGN KEY (CONFIG_ID) REFERENCES  TBPMO_DOCSEARCH_RESULTLIST_CONFIG (GUID),
	CONSTRAINT		UQ_TBPMO_DOCRESULT_DROPDOWN_ITEMS UNIQUE (CONFIG_ID,VALUE),
	CONSTRAINT		UQ2_TBPMO_DOCRESULT_DROPDOWN_ITEMS UNIQUE (CONFIG_ID,[DEFAULT]),
)
GO
CREATE TRIGGER TBPMO_DOCRESULT_DROPDOWN_ITEMS_AFT_UPD ON TBPMO_DOCRESULT_DROPDOWN_ITEMS
FOR UPDATE
AS
	UPDATE TBPMO_DOCRESULT_DROPDOWN_ITEMS SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_DOCRESULT_DROPDOWN_ITEMS.GUID = INSERTED.GUID
GO

ALTER FUNCTION [dbo].[FNPMO_DOC_GET_DROPDOWN](@pDOCID INTEGER, @pENTITY INTEGER, @DOC_DD VARCHAR(20), @LANGUAGE VARCHAR(5))
RETURNS VARCHAR(250)
AS
BEGIN
	DECLARE @HEADER_CAPTION VARCHAR(30), 
		@CONFIG_ID INTEGER, @VALUE VARCHAR(250), @RESULT VARCHAR(250)
	SELECT 
		@HEADER_CAPTION = HEADER_CAPTION,
		@CONFIG_ID = GUID FROM TBPMO_DOCSEARCH_RESULTLIST_CONFIG WHERE 
	ENTITY_ID = @pENTITY AND COLUMN_VIEW = @DOC_DD AND LANGUAGE = @LANGUAGE

	IF @HEADER_CAPTION IS NULL
		SET @RESULT = 'NO DROP-DOWN CONFIGURATION'
	ELSE
		BEGIN
			SELECT @VALUE =  VALUE FROM TBPMO_DOC_VALUES WHERE DocID = @pDOCID AND CONFIG_ID = @CONFIG_ID
			IF @VALUE IS NOT NULL
				SET @RESULT = @VALUE
			ELSE
				SET @RESULT = ''
		END
	RETURN @RESULT
END
GO
CREATE TABLE TBPMO_CONTROL_ENTITY_LINK
(
	GUID				INTEGER			NOT NULL	IDENTITY(1,1),
	ORIGIN_CTRL_ID		INTEGER			NOT NULL,
	LINKED_CTRL_ID	INTEGER			NOT NULL,
	ADDED_WHO			VARCHAR(50)		NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN			DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT			PK_TBPMO_CONTROL_ENTITY_LINK						PRIMARY KEY (GUID),
	CONSTRAINT			UNIQUE_ORIGIN_CTRL_ID_LINKED_CONTROL_ID	UNIQUE(ORIGIN_CTRL_ID,LINKED_CTRL_ID),
	CONSTRAINT			FK_TBPMO_CONTROL_ENTITY_LINK_ORIGIN_CTRL_ID			FOREIGN KEY (ORIGIN_CTRL_ID)  REFERENCES  TBPMO_CONTROL (GUID),
	CONSTRAINT			FK_TBPMO_CONTROL_ENTITY_LINKED_CONTROL_ID			FOREIGN KEY (LINKED_CTRL_ID)  REFERENCES  TBPMO_CONTROL (GUID),
)
GO
CREATE TRIGGER TBPMO_CONTROL_ENTITY_LINK_AFT_UPD ON TBPMO_CONTROL_ENTITY_LINK
FOR UPDATE
AS
	UPDATE TBPMO_CONTROL_ENTITY_LINK SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_CONTROL_ENTITY_LINK.GUID = INSERTED.GUID
GO

CREATE TABLE TBPMO_CHART_ENTITY
(
	GUID				INTEGER			NOT NULL	IDENTITY(1,1),
	ENTITY_ID			INTEGER			NOT NULL,
	GROUP_ID			VARCHAR(50)		NOT NULL	DEFAULT '',
	SQL_COMMAND			VARCHAR(MAX)	NOT NULL,
	TYPE_CHART			VARCHAR(30)		NOT NULL,
	ARGUMENT			VARCHAR(30)		NOT NULL,
	VALUE				VARCHAR(30)		NOT NULL,
	TITLE				VARCHAR(30)		NOT NULL	DEFAULT '',
	ADDED_WHO			VARCHAR(50)		NOT NULL	DEFAULT 'DEFAULT',
	ADDED_WHEN			DATETIME 					DEFAULT GETDATE(),
	CHANGED_WHO			VARCHAR(50),
	CHANGED_WHEN		DATETIME,
	CONSTRAINT			PK_TBPMO_CHART_ENTITY						PRIMARY KEY (GUID),
	CONSTRAINT			FK_TBPMO_CHART_ENTITY_ENTITY_ID		FOREIGN KEY (ENTITY_ID)  REFERENCES  TBPMO_FORM (GUID),
)
GO
CREATE TRIGGER TBPMO_CHART_ENTITY_AFT_UPD ON TBPMO_CHART_ENTITY
FOR UPDATE
AS
	UPDATE TBPMO_CHART_ENTITY SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_CHART_ENTITY.GUID = INSERTED.GUID
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
ORDER BY W_T.RECORD_ID,W_T.GUID

GO






