UPDATE TBDD_MODULES SET PRODUCT_VERSION = '2.4.5.2' where NAME = 'Record-Organizer' 
GO
CREATE 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
	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
GO
ALTER TABLE TBPMO_CONTROL
ADD VISIBLE BIT NOT NULL DEFAULT 1
GO
ALTER TABLE TBPMO_CONTROL
ADD TREE_VIEW BIT NOT NULL DEFAULT 0
GO
ALTER VIEW VWPMO_CONTROL_SCREEN AS
SELECT TOP 100 PERCENT
	T.GUID			CONTROL_ID,
	T.NAME			CONTROL_NAME,
	T.REQUIRED		CONTROL_REQUIRED,
	T.DEFAULT_VALUE	CONTROL_DEF_VALUE,
	T.READ_ONLY		CONTROL_READ_ONLY,
	T.MULTILINE		CONTROL_MULTILINE,
	T.PARENT_CONTROL_ID	CONTROL_PARENT_ID,
	T.COL_NAME		CONTROL_COL_NAME,
	T.SHOW_COLUMN   CONTROL_SHOW_COLUMN,
	T.CONNECTION_ID_1	CONTROL_CONNID_1,
	T.SQL_COMMAND_1		CONTROL_SQLCOMMAND_1,
	T.SQL_COMMAND_2		CONTROL_SQLCOMMAND_2,
	T.VISIBLE		CONTROL_VISIBLE,
	T.TREE_VIEW		CONTROL_TREE_VIEW,
	T.STATIC_LIST	CONTROL_STATIC_LIST,
	T4.GUID			CTRLTYPE_ID,
	T4.TYPE_NAME	CTRLTYPE_NAME,
	T.FORMAT_TYPE	CONTROL_FORMAT_TYPE,
	T.FORM_ID,
	T3.NAME			FORM_NAME,
	T3.LEVEL		FORM_LEVEL,
	T1.SCREEN_ID	SCREEN_ID,
	T2.NAME			SCREEN_NAME,
	T1.GUID			CTRLSCR_ID,
	T1.CONTROL_TEXT	CTRLSCR_CAPTION,
	T1.X_LOC		CTRLSCR_X_LOC,
	T1.Y_LOC		CTRLSCR_Y_LOC,
	T1.HEIGHT		CTRLSCR_HEIGHT,
	T1.WIDTH		CTRLSCR_WIDTH,
	T1.FONT_FAMILY	CTRLSCR_FONT_FAMILY,
	T1.FONT_COLOR	CTRLSCR_FONT_COLOR,
	T1.FONT_SIZE	CTRLSCR_FONT_SIZE,
	T1.FONT_STYLE	CTRLSCR_FONT_STYLE,
	T1.TAB_INDEX	CTRLSCR_TAB_INDEX,
	T1.TAB_STOP		CTRLSCR_TAB_STOP,
	T1.BACK_COLOR   CTRLSCR_BACK_COLOR,
	T.MASTER_DATA_ID CTRLSCR_MASTER_DATA_ID
FROM
	TBPMO_CONTROL T,
	TBPMO_CONTROL_SCREEN T1,
	TBPMO_SCREEN T2,
	TBPMO_FORM T3,
	TBPMO_CONTROL_TYPE T4
WHERE
	T.FORM_ID = T3.GUID AND
	T.GUID = T1.CONTROL_ID AND
	T1.SCREEN_ID = T2.GUID AND
	T.CONTROL_TYPE_ID = T4.GUID
ORDER BY 
	T1.SCREEN_ID,
	T.FORM_ID,
	T1.TAB_STOP
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
	,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
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
ALTER TRIGGER [dbo].[TBPMO_FORM_AFT_INS] ON [dbo].[TBPMO_FORM]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE 
		@FORM_ID		INTEGER,
		@FORM_TYPE		INTEGER,
		@ADDED_WHO		VARCHAR(50)
		--@SQL_RECORD1	VARCHAR(500),
		--@SQL_RECORD2	VARCHAR(500)
	SELECT
  		@FORM_ID = GUID,
		@FORM_TYPE = FORM_TYPE_ID,
		@ADDED_WHO = ADDED_WHO	
	FROM INSERTED
	
	IF @FORM_TYPE = 5
		BEGIN	
			INSERT INTO TBPMO_CONTROL (
				FORM_ID, CONTROL_TYPE_ID, DEFAULT_VALUE, NAME, [COL_NAME]) VALUES (
				@FORM_ID,2,'GROUP_RECORD','txtGROUP_RECORD','GROUP_RECORD')
				DECLARE @CTRL_ID INTEGER
				SELECT @CTRL_ID = MAX(GUID) FROM TBPMO_CONTROL WHERE FORM_ID = @FORM_ID
				INSERT INTO TBPMO_CONTROL_SCREEN (
				CONTROL_ID, SCREEN_ID, CONTROL_TEXT, X_LOC, Y_LOC, HEIGHT, WIDTH, TAB_INDEX) VALUES (
				@CTRL_ID, 1, 'GROUP_RECORD', 5, 5, 22, 100,1)
		END
	
	--EXEC dbo.PRPMO_CREATE_SQL @FORM_ID
	--EXEC dbo.PRPMO_CREATE_TEMP_VIEWS
END TRY
BEGIN CATCH   
	PRINT 'FEHLER IN TRIGGER: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ZEILE: ' + CONVERT(VARCHAR(10),ERROR_LINE())
	+ ' - FEHLER-MESSAGE: ' 
    + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
BEGIN TRY
	INSERT INTO TBPMO_CLIENT_ENTITY (CLIENT_ID,ENTITY_ID) 
	SELECT T.CLIENT_ID,@FORM_ID FROM TBDD_CLIENT_USER T, TBDD_USER T1 WHERE T.USER_ID = T1.GUID AND UPPER(T1.USERNAME) = UPPER(@ADDED_WHO)
END TRY
BEGIN CATCH   
	PRINT 'ERROR INSERTING TBPMO_CLIENT_ENTITY-ERROR-MESSAGE: ' + CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH



GO


