UPDATE TBDD_MODULES SET DB_VERSION = '2.4.8.7' where NAME = 'Record-Organizer' 
GO 
--##########################################################################################
ALTER PROCEDURE [dbo].[PRPMO_CREATE_ENTITY_TABLE](@pENTITY_ID INT)
--ALTER PROCEDURE PRPMO_CREATE_ENTITY_TABLE(@pENTITY_ID INT)
AS
DECLARE	@SQL_BEGIN			NVARCHAR(MAX),
		@SQL_END			NVARCHAR(MAX),
		@SQL				NVARCHAR(MAX),
		@SQL_DEF_CONSTR		NVARCHAR(MAX),
		@CONTROL_ID			INT,
		@RESULT				NVARCHAR(MAX),
		@TYPEID				INT,
		@TABINDEX			INT,
		@ZAEHLER			INT = 1,
		@DEFAULT			VARCHAR(50),
		@COL_NAME			VARCHAR(50),
		@ENTITY_TABLENAME	VARCHAR(50)

		--select Top 10 * from VWTEMP_PMO_FORM1
DECLARE	c_SPALTENNAMEN		CURSOR FOR	SELECT		T.GUID AS CONTROL_ID,'[' + T.COL_NAME + ']' AS 'COL_NAME',T2.GUID AS TYP_ID,T1.TAB_INDEX, T.DEFAULT_VALUE 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 = @pENTITY_ID AND LEN(ISNULL(T.COL_NAME,'')) > 0 AND T.CONTROL_TYPE_ID NOT IN (1,5) 
										--UNION
										--SELECT		T.GUID AS CONTROL_ID,'[' + T3.CAPTION + ']' AS 'COL_NAME',T2.GUID as TYP_ID,T1.TAB_INDEX, T.DEFAULT_VALUE FROM TBPMO_CONTROL T,TBPMO_CONTROL_SCREEN T1,TBPMO_CONTROL_TYPE T2, TBPMO_CONTROL_LANGUAGE T3
										--WHERE		T.GUID = T1.CONTROL_ID AND T.CONTROL_TYPE_ID = T2.GUID AND T1.GUID = T3.CONTROL_SCREEN_ID AND T.FORM_ID = 13 AND T.CONTROL_TYPE_ID = 1 AND LEN(ISNULL(T.SQL_COMMAND_1,'')) > 0
										ORDER BY	TAB_INDEX
BEGIN
	SELECT @ENTITY_TABLENAME = ENTITY_TABLENAME FROM TBPMO_KONFIGURATION WHERE GUID = 1
	
	SET @SQL = 'IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ''@TABLE@ENT'')
					drop table @TABLE@ENT' + CHAR(13) 
	SET @SQL = REPLACE(@SQL,'@ENT',CONVERT(VARCHAR(3),@pENTITY_ID))
	SET @SQL = REPLACE(@SQL,'@TABLE',@ENTITY_TABLENAME)
	EXEC sp_executesql @SQL
	SET @SQL_BEGIN = 'CREATE TABLE @TABLE@ENT(' + CHAR(13) + 
														'[Record-ID]	INT NOT NULL,' + CHAR(13) + 
														'ROW_COLOR		VARCHAR(50)		NOT NULL DEFAULT '''',' + CHAR(13)
	SET @SQL_BEGIN = REPLACE(@SQL_BEGIN,'@ENT',CONVERT(VARCHAR(3),@pENTITY_ID))
	SET @SQL_BEGIN = REPLACE(@SQL_BEGIN,'@TABLE',@ENTITY_TABLENAME)

	--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 = '[file_exists] BIT NOT NULL DEFAULT 0,' + CHAR(13) + '[file_count] INT NOT NULL DEFAULT 0,' + CHAR(13) + 'AddedWho VARCHAR(50)' + CHAR(13) + ',AddedWhen DATETIME' + CHAR(13) + ',ChangedWho VARCHAR(50)' + CHAR(13) + ',ChangedWhen DATETIME' + CHAR(13) + ',RecRefreshedWhen DATETIME,' + CHAR(13) + 'CONSTRAINT PK_@TABLE@ENT PRIMARY KEY([Record-ID]))' + CHAR(13)
	SET @SQL_END = REPLACE(@SQL_END,'@ENT',CONVERT(VARCHAR(3),@pENTITY_ID))
	SET @SQL_END = REPLACE(@SQL_END,'@TABLE',@ENTITY_TABLENAME)
	
	OPEN c_SPALTENNAMEN 
    FETCH NEXT FROM c_SPALTENNAMEN INTO @CONTROL_ID,@COL_NAME,@TYPEID,@TABINDEX,@DEFAULT
    
	WHILE @@FETCH_STATUS = 0
		BEGIN
			SET @ZAEHLER = @ZAEHLER + 1
			
			BEGIN
				IF @TYPEID IN(10,11) --CheckBox,RadioBox
					BEGIN
						IF @DEFAULT = 'False'
							SET @DEFAULT = '0'
						ELSE
							SET @DEFAULT = '1'
						SET @SQL_BEGIN = @SQL_BEGIN + @COL_NAME + ' BIT NOT NULL DEFAULT ' + @DEFAULT + ',' + CHAR(13) 
					END
				ELSE IF @TYPEID = 6 --Picturebox
					BEGIN
						SET @SQL_BEGIN = @SQL_BEGIN + @COL_NAME + ' VARBINARY(MAX),' + CHAR(13) 
					ENd
				ELSE IF @TYPEID IN(7,12,14) --GridView,CheckedListBox,GridViewCheckable
					BEGIN
						SET @SQL_BEGIN = @SQL_BEGIN + @COL_NAME + ' VARCHAR(MAX),' + CHAR(13) 
					END
				ELSE IF @TYPEID = 1 --Label
						BEGIN
							SET @SQL_BEGIN = @SQL_BEGIN + @COL_NAME + ' VARCHAR(MAX),' + CHAR(13) 
						END
				ELSE IF @TYPEID = 4 --DatePicker
					BEGIN
						SET @SQL_BEGIN = @SQL_BEGIN + @COL_NAME + ' VARCHAR(10),' + CHAR(13) 
					END
				ELSE
					BEGIN
						SET @SQL_BEGIN = @SQL_BEGIN + @COL_NAME + ' VARCHAR(MAX),' + CHAR(13) 
						IF LEN(ISNULL(@DEFAULT,'')) > 0
							BEGIN
								DECLARE @TEMP_COL_NAME	VARCHAR(50)
								SET @TEMP_COL_NAME = REPLACE(@COL_NAME,' ','')
								SET @TEMP_COL_NAME = REPLACE(@TEMP_COL_NAME,'[','')
								SET @TEMP_COL_NAME = REPLACE(@TEMP_COL_NAME,']','')
								IF LEN(@SQL_DEF_CONSTR) > 0
									SET @SQL_DEF_CONSTR = @SQL_DEF_CONSTR + CHAR(13) + 'ALTER TABLE @TABLE@ENT ADD CONSTRAINT DEF@TABLE@ENT_@COL_NAME DEFAULT N''@DEFAULT_VALUE'' FOR ' +  @COL_NAME + ';'
								ELSE
									SET @SQL_DEF_CONSTR = 'ALTER TABLE @TABLE@ENT ADD CONSTRAINT DEF@TABLE@ENT_@COL_NAME DEFAULT N''@DEFAULT_VALUE'' FOR ' +  @COL_NAME + ';'
								BEGIN
									SET @SQL_DEF_CONSTR = REPLACE(@SQL_DEF_CONSTR,'@DEFAULT_VALUE',@DEFAULT)
									SET @SQL_DEF_CONSTR = REPLACE(@SQL_DEF_CONSTR,'@COL_NAME',@TEMP_COL_NAME)
									SET @SQL_DEF_CONSTR = REPLACE(@SQL_DEF_CONSTR,'@ENT',CONVERT(VARCHAR(3),@pENTITY_ID))
									SET @SQL_DEF_CONSTR = REPLACE(@SQL_DEF_CONSTR,'@TABLE',@ENTITY_TABLENAME)
								END
							END
							
					END
			END
			
			FETCH NEXT FROM c_SPALTENNAMEN INTO @CONTROL_ID,@COL_NAME,@TYPEID,@TABINDEX,@DEFAULT
		END

	CLOSE c_SPALTENNAMEN
    DEALLOCATE c_SPALTENNAMEN

	SET @RESULT = @SQL + @SQL_BEGIN + @SQL_END
	--PRINT @RESULT
	EXEC sp_executesql @RESULT

	IF LEN(@SQL_DEF_CONSTR) > 0
		BEGIN
			--PRINT @SQL_DEF_CONSTR
			EXEC sp_executesql @SQL_DEF_CONSTR
		END
END
GO
-----------------------------------------------------------------------------------------
--##########################################################################################
ALTER PROCEDURE [dbo].[PRPMO_ENTITY_TABLE_IMPORT_RECORDS](@pENTITY_ID INT)
--ALTER PROCEDURE PRPMO_ENTITY_TABLE_IMPORT_RECORDS(@pENTITY_ID INT)
AS
DECLARE	@REC_ID				INT,
		@SQL_INSERT_REC		NVARCHAR(MAX),
		@SQL_DELETE			NVARCHAR(MAX),
		@SQL_UPDATE			NVARCHAR(MAX),
		@CONTROL_ID			INT,
		@INSERT_COMAMND		NVARCHAR(MAX),
		@TYPEID				INT,
		@TABINDEX			INT,
		@DEFAULT			VARCHAR(50),
		@COL_NAME			VARCHAR(50),
		@ENTITY_TABLENAME	VARCHAR(50)
PRINT 'WORKING ON ENTITY ''' + CONVERT(VARCHAR(3),@pENTITY_ID) + ''''
DECLARE	cursor_Records		CURSOR FOR SELECT GUID FROM TBPMO_RECORD WHERE FORM_ID = @pENTITY_ID and PARENT_RECORD = 0 AND DELETED = 0
BEGIN 
	SELECT @ENTITY_TABLENAME = ENTITY_TABLENAME FROM TBPMO_KONFIGURATION WHERE GUID = 1


	--SET @SQL_DELETE = 'DELETE FROM @TABLE@ENT'
	--SET @SQL_DELETE = REPLACE(@SQL_DELETE,'@ENT',CONVERT(VARCHAR(3),@pENTITY_ID))
	--SET @SQL_DELETE = REPLACE(@SQL_DELETE,'@TABLE',@ENTITY_TABLENAME)
	--EXEC sp_executesql @SQL_DELETE
	
	OPEN cursor_Records 
    FETCH NEXT FROM cursor_Records INTO @REC_ID
	WHILE @@FETCH_STATUS = 0
		BEGIN
			--DELETE the Record
			SET @SQL_DELETE = 'DELETE FROM @TABLE@ENT WHERE [Record-ID] = @REC_ID'
			SET @SQL_DELETE = REPLACE(@SQL_DELETE,'@ENT',CONVERT(VARCHAR(3),@pENTITY_ID))
			SET @SQL_DELETE = REPLACE(@SQL_DELETE,'@TABLE',@ENTITY_TABLENAME)
			SET @SQL_DELETE = REPLACE(@SQL_DELETE,'@REC_ID',CONVERT(VARCHAR(10),@REC_ID))
			EXEC sp_executesql @SQL_DELETE

			SET @SQL_INSERT_REC = 'INSERT INTO @TABLE@ENT([Record-ID],ROW_COLOR, AddedWho, AddedWhen, ChangedWho, ChangedWhen,RecRefreshedWhen) SELECT GUID,COLOR,ADDED_WHO,ADDED_WHEN,CHANGED_WHO,CHANGED_WHEN,GETDATE() FROM TBPMO_RECORD WHERE GUID = @REC_ID'
			
			SET @SQL_INSERT_REC = REPLACE(@SQL_INSERT_REC,'@ENT',CONVERT(VARCHAR(3),@pENTITY_ID))
			SET @SQL_INSERT_REC = REPLACE(@SQL_INSERT_REC,'@TABLE',@ENTITY_TABLENAME)
			SET @SQL_INSERT_REC = REPLACE(@SQL_INSERT_REC,'@REC_ID',CONVERT(VARCHAR(10),@REC_ID))
			--PRINT @SQL_INSERT_REC
			EXEC sp_executesql @SQL_INSERT_REC
			EXEC dbo.PRPMO_DOC_RECORD_LINK_UPD_ENTITY_TABLE @REC_ID
			
			DECLARE	cursor_Columns CURSOR FOR	SELECT		T.GUID,'[' + T.COL_NAME + ']' AS 'COL_NAME',T2.GUID AS TYP_ID FROM TBPMO_CONTROL T,TBPMO_CONTROL_SCREEN T1,TBPMO_CONTROL_TYPE T2
												WHERE		T.SHOW_COLUMN = 1 AND T.GUID = T1.CONTROL_ID AND T.CONTROL_TYPE_ID = T2.GUID AND T.FORM_ID = @pENTITY_ID AND LEN(ISNULL(T.COL_NAME,'')) > 0 AND T.CONTROL_TYPE_ID NOT IN (1,5) 
												ORDER BY	TAB_INDEX
			
			BEGIN
				OPEN cursor_Columns 
				FETCH NEXT FROM cursor_Columns INTO @CONTROL_ID,@COL_NAME,@TYPEID
				WHILE @@FETCH_STATUS = 0
					BEGIN
						--PRINT 'CONTROL-ID: ' + CONVERT(VARCHAR(3),@CONTROL_ID) + ' - REC-ID: ' + CONVERT(VARCHAR(10),@REC_ID)
						SET @SQL_UPDATE = 'UPDATE @TABLE@ENT SET @COL_NAME = ''@VALUE'' WHERE [Record-ID] = @REC_ID'
						SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@COL_NAME',@COL_NAME)
						SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@ENT',CONVERT(VARCHAR(3),@pENTITY_ID))
						SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@TABLE',@ENTITY_TABLENAME)
			--			
						IF @TYPEID = 6 --Picturebox
							BEGIN
								DECLARE @VALUE_VBM VARBINARY(MAX)
								
								IF EXISTS(SELECT GUID FROM TBPMO_CONTROL_IMAGE WHERE CONTROL_ID = @CONTROL_ID AND RECORD_ID = @REC_ID)
									BEGIN
										SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'''@VALUE''','(SELECT IMG FROM TBPMO_CONTROL_IMAGE WHERE CONTROL_ID = @CONTROL_ID AND RECORD_ID = @REC_ID)')
										SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@REC_ID',CONVERT(VARCHAR(10),@REC_ID))
										SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@CONTROL_ID',CONVERT(VARCHAR(10),@CONTROL_ID))
										EXEC sp_executesql @SQL_UPDATE
									END
							END
						ELSE
							BEGIN
								DECLARE @VALUE VARCHAR(MAX)
								IF EXISTS(SELECT GUID FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = @CONTROL_ID AND RECORD_ID = @REC_ID)
									BEGIN TRY
										IF @TYPEID IN(10,11) --CheckBox,RadioBox
											BEGIN
												SELECT @VALUE = VALUE FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = @CONTROL_ID AND RECORD_ID = @REC_ID
												SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@VALUE',@VALUE)
											END
										ELSE IF @TYPEID IN(7,12,14)
											BEGIN
												SELECT @VALUE = dbo.FNPMO_GET_CONTROL_VALUES(@CONTROL_ID, @REC_ID)
												SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@VALUE',@VALUE)
											END
										ELSE
											SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'''@VALUE''','(SELECT DISTINCT VALUE FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = @CONTROL_ID AND RECORD_ID = @REC_ID)')
										
										SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@REC_ID',CONVERT(VARCHAR(10),@REC_ID))
										SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@CONTROL_ID',CONVERT(VARCHAR(10),@CONTROL_ID))
										--PRINT @SQL_UPDATE
										EXEC sp_executesql @SQL_UPDATE
									END TRY 
									BEGIN CATCH   
										PRINT 'ERROR in HANDLING CONTROL-ID: ' + CONVERT(VARCHAR(30),@CONTROL_ID) + ' - RECORD-ID: ' + CONVERT(VARCHAR(10),@REC_ID)								
									END CATCH
							END
						
						FETCH NEXT FROM cursor_Columns INTO @CONTROL_ID,@COL_NAME,@TYPEID
					END
				CLOSE cursor_Columns
				DEALLOCATE cursor_Columns
			END
			
			FETCH NEXT FROM cursor_Records INTO @REC_ID
		END
	CLOSE cursor_Records
    DEALLOCATE cursor_Records
END
GO
--##########################################################################################
ALTER PROCEDURE [dbo].[PRPMO_ENTITY_TABLE_RENEW_RECORD](@pENTITY_ID INT,@pRECORD_ID INT)
AS
DECLARE	@REC_ID				INT,
		@SQL_INSERT_REC		NVARCHAR(MAX),
		@SQL_DELETE			NVARCHAR(MAX),
		@SQL_UPDATE			NVARCHAR(MAX),
		@CONTROL_ID			INT,
		@INSERT_COMAMND		NVARCHAR(MAX),
		@TYPEID				INT,
		@TABINDEX			INT,
		@DEFAULT			VARCHAR(50),
		@COL_NAME			VARCHAR(50),
		@ENTITY_TABLENAME	VARCHAR(50)
PRINT 'WORKING ON RECORD ''' + CONVERT(VARCHAR(10),@pRECORD_ID) + ''''
DECLARE	cursor_Records		CURSOR FOR SELECT GUID FROM TBPMO_RECORD WHERE GUID = @pRECORD_ID
BEGIN 
	SELECT @ENTITY_TABLENAME = ENTITY_TABLENAME FROM TBPMO_KONFIGURATION WHERE GUID = 1


	SET @SQL_DELETE = 'DELETE FROM @TABLE@ENT WHERE [Record-ID] = @RECORD_ID'
	SET @SQL_DELETE = REPLACE(@SQL_DELETE,'@ENT',CONVERT(VARCHAR(3),@pENTITY_ID))
	SET @SQL_DELETE = REPLACE(@SQL_DELETE,'@RECORD_ID',CONVERT(VARCHAR(10),@pRECORD_ID))
	SET @SQL_DELETE = REPLACE(@SQL_DELETE,'@TABLE',@ENTITY_TABLENAME)
	PRINT @SQL_DELETE
	EXEC sp_executesql @SQL_DELETE
	
	OPEN cursor_Records 
    FETCH NEXT FROM cursor_Records INTO @REC_ID
	WHILE @@FETCH_STATUS = 0
		BEGIN
			SET @SQL_INSERT_REC = 'INSERT INTO @TABLE@ENT([Record-ID],ROW_COLOR, AddedWho, AddedWhen, ChangedWho, ChangedWhen,RecRefreshedWhen) SELECT GUID,COLOR,ADDED_WHO,ADDED_WHEN,CHANGED_WHO,CHANGED_WHEN,GETDATE() FROM TBPMO_RECORD WHERE GUID = @REC_ID'
			
			SET @SQL_INSERT_REC = REPLACE(@SQL_INSERT_REC,'@ENT',CONVERT(VARCHAR(3),@pENTITY_ID))
			SET @SQL_INSERT_REC = REPLACE(@SQL_INSERT_REC,'@TABLE',@ENTITY_TABLENAME)
			SET @SQL_INSERT_REC = REPLACE(@SQL_INSERT_REC,'@REC_ID',CONVERT(VARCHAR(10),@REC_ID))
			PRINT @SQL_INSERT_REC
			EXEC sp_executesql @SQL_INSERT_REC
			EXEC dbo.PRPMO_DOC_RECORD_LINK_UPD_ENTITY_TABLE @REC_ID
			
			DECLARE	cursor_Columns CURSOR FOR	SELECT		T.GUID,'[' + T.COL_NAME + ']' AS 'COL_NAME',T2.GUID AS TYP_ID FROM TBPMO_CONTROL T,TBPMO_CONTROL_SCREEN T1,TBPMO_CONTROL_TYPE T2
												WHERE		T.SHOW_COLUMN = 1 AND T.GUID = T1.CONTROL_ID AND T.CONTROL_TYPE_ID = T2.GUID AND T.FORM_ID = @pENTITY_ID AND LEN(ISNULL(T.COL_NAME,'')) > 0 AND T.CONTROL_TYPE_ID NOT IN (1,5) 
												ORDER BY	TAB_INDEX
			
			BEGIN
				OPEN cursor_Columns 
				FETCH NEXT FROM cursor_Columns INTO @CONTROL_ID,@COL_NAME,@TYPEID
				WHILE @@FETCH_STATUS = 0
					BEGIN
						--PRINT 'CONTROL-ID: ' + CONVERT(VARCHAR(3),@CONTROL_ID) + ' - REC-ID: ' + CONVERT(VARCHAR(10),@REC_ID)
						SET @SQL_UPDATE = 'UPDATE @TABLE@ENT SET @COL_NAME = ''@VALUE'' WHERE [Record-ID] = @REC_ID'
						SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@COL_NAME',@COL_NAME)
						SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@ENT',CONVERT(VARCHAR(3),@pENTITY_ID))
						SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@TABLE',@ENTITY_TABLENAME)
			--			
						IF @TYPEID = 6 --Picturebox
							BEGIN
								DECLARE @VALUE_VBM VARBINARY(MAX)
								
								IF EXISTS(SELECT GUID FROM TBPMO_CONTROL_IMAGE WHERE CONTROL_ID = @CONTROL_ID AND RECORD_ID = @REC_ID)
									BEGIN
										SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'''@VALUE''','(SELECT IMG FROM TBPMO_CONTROL_IMAGE WHERE CONTROL_ID = @CONTROL_ID AND RECORD_ID = @REC_ID)')
										SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@REC_ID',CONVERT(VARCHAR(10),@REC_ID))
										SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@CONTROL_ID',CONVERT(VARCHAR(10),@CONTROL_ID))
										EXEC sp_executesql @SQL_UPDATE
									END
							END
						ELSE
							BEGIN
								DECLARE @VALUE VARCHAR(MAX)
								IF EXISTS(SELECT GUID FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = @CONTROL_ID AND RECORD_ID = @REC_ID)
									BEGIN TRY
										IF @TYPEID IN(10,11) --CheckBox,RadioBox
											BEGIN
												SELECT @VALUE = VALUE FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = @CONTROL_ID AND RECORD_ID = @REC_ID
												SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@VALUE',@VALUE)
											END
										ELSE IF @TYPEID IN(7,12,14)
											BEGIN
												SELECT @VALUE = dbo.FNPMO_GET_CONTROL_VALUES(@CONTROL_ID, @REC_ID)
												SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@VALUE',@VALUE)
											END
										ELSE
											SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'''@VALUE''','(SELECT DISTINCT VALUE FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = @CONTROL_ID AND RECORD_ID = @REC_ID)')
										
										SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@REC_ID',CONVERT(VARCHAR(10),@REC_ID))
										SET @SQL_UPDATE = REPLACE(@SQL_UPDATE,'@CONTROL_ID',CONVERT(VARCHAR(10),@CONTROL_ID))
										--PRINT @SQL_UPDATE
										EXEC sp_executesql @SQL_UPDATE
									END TRY 
									BEGIN CATCH   
										PRINT 'ERROR in HANDLING CONTROL-ID: ' + CONVERT(VARCHAR(30),@CONTROL_ID) + ' - RECORD-ID: ' + CONVERT(VARCHAR(10),@REC_ID)								
									END CATCH
							END
						
						FETCH NEXT FROM cursor_Columns INTO @CONTROL_ID,@COL_NAME,@TYPEID
					END
				CLOSE cursor_Columns
				DEALLOCATE cursor_Columns
			END
			
			FETCH NEXT FROM cursor_Records INTO @REC_ID
		END
	CLOSE cursor_Records
    DEALLOCATE cursor_Records
END
ALTER TABLE TBPMO_STRUCTURE_NODES ADD COLOR VARCHAR(50) NOT NULL DEFAULT ''
GO 
ALTER TABLE TBPMO_STRUCTURE_NODES ALTER COLUMN COLOR VARCHAR(50) NOT NULL  
GO  
ALTER TABLE TBPMO_STRUCTURE_NODES ADD CONSTRAINT DF_COLOR DEFAULT '' FOR COLOR;
GO
ALTER TABLE TBPMO_STRUCTURE_NODES_CONFIGURATION ALTER COLUMN BACK_COLOR VARCHAR(50) NOT NULL  
GO  
ALTER TABLE TBPMO_STRUCTURE_NODES_CONFIGURATION ADD CONSTRAINT DF_BACK_COLOR DEFAULT N'' FOR BACK_COLOR;
GO
UPDATE TBPMO_STRUCTURE_NODES_CONFIGURATION SET BACK_COLOR = ''
GO
ALTER PROCEDURE [dbo].[PRPMO_CHECK_DOC_RECORD_LINK] (
	@pRECORD_ID INTEGER,
	@pDOC_ID INTEGER,
	@pREASON_CODE VARCHAR(500),
	@pWHO VARCHAR(50)
	) 
	AS
BEGIN
	IF NOT EXISTS(SELECT GUID FROM TBPMO_DOC_RECORD_LINK WHERE RECORD_ID = @pRECORD_ID AND DOC_ID = @pDOC_ID)
		BEGIN
			INSERT INTO TBPMO_DOC_RECORD_LINK (RECORD_ID,DOC_ID,COMMENT,ADDED_WHO) VALUES 
			(@pRECORD_ID,@pDOC_ID,@pREASON_CODE,@pWHO)
		END
	ELSE
		UPDATE TBPMO_DOC_RECORD_LINK SET CHANGED_WHO = @pWHO WHERE RECORD_ID = @pRECORD_ID AND DOC_ID = @pDOC_ID
END
GO
ALTER TABLE TBPMO_STRUCTURE_NODES ADD ACTIVE BIT NOT NULL DEFAULT 1
GO 
ALTER TABLE TBPMO_STRUCTURE_NODES ADD SEQUENCE INTEGER NOT NULL DEFAULT '0'
GO
CREATE NONCLUSTERED INDEX [IDX_STRUCTURE_NODES1]
ON [dbo].[TBPMO_STRUCTURE_NODES] ([NODE_CONFIG_ID],[ACTIVE])
INCLUDE ([GUID],[RECORD_ID],[NODE_CAPTION],[ID1],[ID2],[ID3],[PARENT_GUID],[BOOL1],[BOOL2],[ADDED_WHO],[ADDED_WHEN],[CHANGED_WHO],[CHANGED_WHEN],[SEQUENCE],[COLOR],[FONT_FAMILY],[FONT_COLOR],[FONT_SIZE],[FONT_STYLE])
GO
ALTER TABLE TBPMO_STRUCTURE_NODES ADD COMMENT VARCHAR(500)
GO
ALTER TABLE TBPMO_STRUCTURE_NODES ADD FONT_FAMILY VARCHAR(50) NOT NULL DEFAULT 'Tahoma'
GO 
ALTER TABLE TBPMO_STRUCTURE_NODES ADD FONT_COLOR BIGINT NOT NULL DEFAULT 0
GO  
ALTER TABLE TBPMO_STRUCTURE_NODES ADD FONT_SIZE SMALLINT NOT NULL DEFAULT 10
GO
ALTER TABLE TBPMO_STRUCTURE_NODES ADD FONT_STYLE SMALLINT NOT NULL DEFAULT 0
GO
ALTER VIEW [dbo].[VWPMO_STRUCTURE_NODES] AS 
select 
	T.GUID,
	T.RECORD_ID,
	T.NODE_CONFIG_ID,
	T.NODE_CAPTION,
	T.ID1,
	T.ID2,
	T.ID3,
	T.PARENT_GUID,
	T.BOOL1,
	T.BOOL2,
	T.ADDED_WHO,
	T.ADDED_WHEN,
	T.CHANGED_WHO,
	T.CHANGED_WHEN,
	T.SEQUENCE,
	T1.CONTROL_ID,
	T1.CREATE_RECORD,
	T1.ENTITY_ID,
	T1.NAME,
	T1.TYPE_NODE,
	CASE   
      WHEN T.FONT_FAMILY <> T1.FONT_FAMILY THEN T.FONT_FAMILY
	  ELSE T1.FONT_FAMILY END AS FONT_FAMILY,
	CASE   
      WHEN T.FONT_COLOR <> T1.FONT_COLOR THEN T.FONT_COLOR
	  ELSE T1.FONT_COLOR END AS FONT_COLOR,
	CASE   
      WHEN T.FONT_SIZE <> T1.FONT_SIZE THEN T.FONT_SIZE
	  ELSE T1.FONT_SIZE END AS FONT_SIZE,
	CASE   
      WHEN T.FONT_STYLE <> T1.FONT_STYLE THEN T.FONT_STYLE
	  ELSE T1.FONT_STYLE END AS FONT_STYLE,
	T1.EXPAND AS EXPAND_NODE,
	CASE   
      WHEN T.COLOR <> '' THEN T.COLOR
	  ELSE T1.BACK_COLOR END AS BACK_COLOR 
	 from TBPMO_STRUCTURE_NODES T, TBPMO_STRUCTURE_NODES_CONFIGURATION T1
WHERE T.NODE_CONFIG_ID = T1.GUID
GO
CREATE NONCLUSTERED INDEX [IDX_PMO_STRUCTURE_NODES]
ON [dbo].[TBPMO_STRUCTURE_NODES] ([NODE_CONFIG_ID])
INCLUDE ([GUID],[RECORD_ID],[NODE_CAPTION],[ID1],[ID2],[ID3],[PARENT_GUID],[BOOL1],[BOOL2],[ADDED_WHO],[ADDED_WHEN],[CHANGED_WHO],[CHANGED_WHEN],[SEQUENCE],[COLOR],[FONT_FAMILY],[FONT_COLOR],[FONT_SIZE],[FONT_STYLE])
GO

ALTER PROCEDURE [dbo].[PRPMO_DELETE_RECORD_FINALLY](@pRECORD_ID INT, @WHO VARCHAR(30))
AS
	DELETE FROM TBPMO_STRUCTURE_NODES WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_WORKFLOW_TASK_USER WHERE WF_TASK_ID IN (SELECT GUID FROM TBPMO_WORKFLOW_TASK WHERE RECORD_ID = @pRECORD_ID)
	DELETE FROM TBPMO_WORKFLOW_TASK WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_RECORD_CONNECT WHERE RECORD1_ID = @pRECORD_ID OR RECORD2_ID = @pRECORD_ID
	DELETE FROM TBPMO_WORKFLOW_TASK_HISTORY WHERE WF_TASK_ID IN (SELECT GUID FROM TBPMO_WORKFLOW_TASK WHERE RECORD_ID = @pRECORD_ID)
    DELETE FROM TBPMO_WORKFLOW_TASK WHERE RECORD_ID = @pRECORD_ID
    DELETE FROM TBPMO_CONTROL_VALUE WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_WORKFLOW_REMINDER WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_FOLLUPEMAIL_RECORD WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_RECORD_VARIANT WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_RECORD_GEODATA WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_DOC_VALUES WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_DOC_RECORD_LINK WHERE RECORD_ID = @pRECORD_ID
	DELETE FROM TBPMO_RECORD_LOG WHERE RECORD_ID = @pRECORD_ID
	    
	DECLARE @DELETE_SQL		NVARCHAR(MAX),@ENTITY_ID INT
	SELECT @ENTITY_ID = FORM_ID FROM TBPMO_RECORD WHERE GUID = @pRECORD_ID   
	SET @DELETE_SQL = 'DELETE FROM TBPMO_ENTITY_TABLE@ENT_ID WHERE [Record-ID] = @RECORD_ID'  
	SET @DELETE_SQL = REPLACE(@DELETE_SQL,'@ENT_ID',@ENTITY_ID)
	SET @DELETE_SQL = REPLACE(@DELETE_SQL,'@RECORD_ID',@pRECORD_ID)
	EXEC sp_executesql @DELETE_SQL
	DELETE FROM TBPMO_RECORD WHERE GUID = @pRECORD_ID
	--UPDATE TBPMO_RECORD SET DELETED = 1 WHERE GUID = @pRECORD_ID

	INSERT INTO TBPMO_LOG_ESSENTIALS (REFERENCE_KEY,REFERENCE_STRING,COMMENT,ADDED_WHO) VALUES (@pRECORD_ID,'RECORD-ID','RECORD FINALLY DELETED BY USER',@WHO)
GO









