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

ALTER TABLE TBPMO_FORM
ADD DATE_FORMAT VARCHAR(10)  NOT NULL DEFAULT'dd-MM-yyyy'
GO
ALTER PROCEDURE [dbo].[PRPMO_CREATE_SQL](@pFORM_ID INT)
AS
DECLARE	@COL_NAME		VARCHAR(50),
		@SQL_BEGIN		VARCHAR(1000),
		@SQL_END		VARCHAR(2000),
		@SQL			VARCHAR(5000),
		@CONTROL_ID		INT,
		@RESULT			VARCHAR(8000),
		@TYPEID			INT,
		@TABINDEX		INT,
		@SQL_LBL VARCHAR(2000)
		
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],T.COLOR AS ROW_COLOR, T.FORM_ID AS [Form-ID],'
	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 = 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','')
			IF LEN(ISNULL(@SQL,'')) = 0
				BEGIN
					IF @TYPEID = 10 OR @TYPEID = 11
						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
					ELSE IF @TYPEID = 6
						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
					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
						END
					ELSE IF @TYPEID = 12 OR @TYPEID = 7 OR @TYPEID = 14 
						SET @SQL = '(SELECT [dbo].[FNPMO_GET_CONTROL_VALUES](' + CONVERT(VARCHAR(20),@CONTROL_ID) + ', T.GUID)) AS ' + @COL_NAME
					ELSE
						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
				END
			ELSE
				BEGIN
					IF (@TYPEID = 10) OR (@TYPEID = 11)
						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
					ELSE IF @TYPEID = 6
						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
					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
						END
					ELSE IF @TYPEID = 12 OR @TYPEID = 7 OR @TYPEID = 14
						SET @SQL = @SQL + ',(SELECT [dbo].[FNPMO_GET_CONTROL_VALUES](' + CONVERT(VARCHAR(20),@CONTROL_ID) + ', T.GUID)) AS ' + @COL_NAME
					ELSE
						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
				END
			
			FETCH NEXT FROM c_SPALTENNAMEN INTO @CONTROL_ID,@COL_NAME,@TYPEID,@TABINDEX
		END

	CLOSE c_SPALTENNAMEN
    DEALLOCATE c_SPALTENNAMEN

	SET @RESULT = @SQL_BEGIN + @SQL + @SQL_END

	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 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_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_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 PROCEDURE [dbo].[PRPMO_CREATE_CONSTRUCTOR](@pFORM_ID INT, @pNAME VARCHAR(50))
AS
DECLARE		
	@NEW_CONSTR_ID	INTEGER,
	@FORM_ID		INTEGER,
	@PARENT_ID		INTEGER,
	@PARENT_ID2		INTEGER,
	@LEVEL1_SELECT	INTEGER,
	@SEQUENCE		INTEGER,
	@SEQUENCE2		INTEGER,
	@SQL_SELECT_EBENE1	VARCHAR(4000)
	
	IF NOT EXISTS(SELECT GUID FROM TBPMO_FORM_CONSTRUCTOR WHERE UPPER(FORM_TITLE) = UPPER(@pNAME))
		INSERT INTO TBPMO_FORM_CONSTRUCTOR (FORM_TITLE,SEQUENCE_MENU,ADDED_WHO, SHORT_TITLE) VALUES
		(@pNAME,1, 'CREATE_ROUTINE','SHORT-TITLE')
	
	SELECT @NEW_CONSTR_ID =  MAX(GUID) FROM TBPMO_FORM_CONSTRUCTOR WHERE UPPER(FORM_TITLE) = UPPER(@pNAME)
	
	PRINT 'INSERT1 DURCH' 
	
		
	PRINT 'INSERT2 DURCH' 
DECLARE	c_ENTITY1	CURSOR FOR 
	SELECT 
		T.GUID,	T.PARENT_ID, T.LEVEL
		FROM TBPMO_FORM T, TBPMO_FORM_VIEW T1 WHERE T.GUID = T1.FORM_ID AND T.SHOW_FORM_CONSTRUCT = 1 AND T1.SCREEN_ID = 1  AND T.PARENT_ID = @pFORM_ID ORDER BY T.LEVEL 
BEGIN
	OPEN c_ENTITY1 
    FETCH NEXT FROM c_ENTITY1 INTO @FORM_ID,@PARENT_ID,@SEQUENCE
    
	WHILE @@FETCH_STATUS = 0
		BEGIN
			IF NOT EXISTS(SELECT GUID FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE CONSTRUCT_ID = @NEW_CONSTR_ID AND FORM_ID = @FORM_ID AND PARENT_ID = @PARENT_ID)
				BEGIN
					PRINT 'c_ENTITY1: SELECT * FROM TBPMO_FORM WHERE PARENT_ID = @' + CONVERT(VARCHAR(3),@FORM_ID)
					INSERT INTO TBPMO_FORM_CONSTRUCTOR_DETAIL (
						CONSTRUCT_ID
						,FORM_ID
						,PARENT_ID
						,LEVEL1_SELECT
						,SEQUENCE
						,SQL_SELECT_EBENE1
						,SQL_SELECT_EBENE2
						,ADDED_WHO
					) VALUES (
						@NEW_CONSTR_ID
						,@FORM_ID
						,@PARENT_ID 
						,0
						,@SEQUENCE
						,'SELECT * FROM VWTEMP_PMO_FORM' + CONVERT(VARCHAR(5),@FORM_ID)
						,'SELECT T.* FROM VWTEMP_PMO_FORM' +  CONVERT(VARCHAR(5),@PARENT_ID) + ' AS T, TBPMO_RECORD_CONNECT T1 WHERE T.[Record-ID] = T1.RECORD2_ID AND T1.RECORD1_ID = @RecordID'
						,'CREATE_ROUTINE2'
					)
				END

			If EXISTS(SELECT * FROM TBPMO_FORM WHERE PARENT_ID = @FORM_ID)
				BEGIN
				PRINT 'c_ENTITY2: SELECT * FROM TBPMO_FORM WHERE PARENT_ID = ' + CONVERT(VARCHAR(3),@FORM_ID)
			--###############
					DECLARE	c_ENTITY2	CURSOR FOR 
						SELECT 
							T.GUID,	T.PARENT_ID, T.LEVEL
							FROM TBPMO_FORM T, TBPMO_FORM_VIEW T1 WHERE T.GUID = T1.FORM_ID AND T.SHOW_FORM_CONSTRUCT = 1 AND T1.SCREEN_ID = 1  AND T.PARENT_ID = @FORM_ID ORDER BY T.LEVEL
					BEGIN
						OPEN c_ENTITY2
						FETCH NEXT FROM c_ENTITY2 INTO @FORM_ID,@PARENT_ID2,@SEQUENCE2
    
						WHILE @@FETCH_STATUS = 0
							BEGIN
								INSERT INTO TBPMO_FORM_CONSTRUCTOR_DETAIL (
									CONSTRUCT_ID
									,FORM_ID
									,PARENT_ID
									,LEVEL1_SELECT
									,SEQUENCE
									,SQL_SELECT_EBENE1
									,SQL_SELECT_EBENE2
									,ADDED_WHO
								) VALUES (
									@NEW_CONSTR_ID
									,@FORM_ID
									,@PARENT_ID2 
									,0
									,@SEQUENCE2
									,'SELECT * FROM VWTEMP_PMO_FORM' + CONVERT(VARCHAR(5),@FORM_ID)
									,'SELECT T.* FROM VWTEMP_PMO_FORM' +  CONVERT(VARCHAR(5),@PARENT_ID2) + ' AS T, TBPMO_RECORD_CONNECT T1 WHERE T.[Record-ID] = T1.RECORD2_ID AND T1.RECORD1_ID = @RecordID'
									,'CREATE_ROUTINE3'
								)
								FETCH NEXT FROM c_ENTITY2 INTO @FORM_ID,@PARENT_ID2,@SEQUENCE2
							END
						CLOSE c_ENTITY2
						DEALLOCATE c_ENTITY2
					END
				END
			--###############
				
			FETCH NEXT FROM c_ENTITY1 INTO @FORM_ID,@PARENT_ID,@SEQUENCE
		END

	CLOSE c_ENTITY1
    DEALLOCATE c_ENTITY1
END
GO
CREATE VIEW [dbo].[VWPMO_CONTROL_HINT] AS 
SELECT 
	T2.FORM_ID,
	T1.CONTROL_ID,
	T.HINT 
FROM
	TBPMO_CONTROL_LANGUAGE T,
	TBPMO_CONTROL_SCREEN T1,
	TBPMO_CONTROL T2
WHERE 
	T.CONTROL_SCREEN_ID = T1.GUID
	AND T1.CONTROL_ID = T2.GUID 
	AND T1.SCREEN_ID = 1
	AND (HINT IS NOT NULL AND HINT <> '')
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
	IF OBJECT_ID (N'TBPMO_DOCSEARCH_RESULTLIST_CONFIG', N'U') IS NOT NULL 
		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_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
