ALTER TABLE TBPMO_FORM_VIEW
ADD LANGUAGE VARCHAR(5) NOT NULL DEFAULT 'de-DE'
GO

ALTER TRIGGER [dbo].[TBPMO_FORM_VIEW_AFT_UPD] ON [dbo].[TBPMO_FORM_VIEW]
FOR UPDATE
AS
BEGIN TRY
	DECLARE 
		@FORM_TITLE	VARCHAR(50),
		@LANGUAGE VARCHAR(5), 
		@CHANGED_WHO VARCHAR(50),
		@GUID INTEGER,
		@SCREEN_ID INTEGER,
		@PMO_OBJECT_NAME VARCHAR(100)
	SELECT 
		@GUID = GUID,
		@FORM_TITLE = FORM_TITLE,
		@LANGUAGE = [LANGUAGE],
		@SCREEN_ID = SCREEN_ID,
		@CHANGED_WHO = CHANGED_WHO
	FROM INSERTED

	SET @PMO_OBJECT_NAME = 'FORMVIEW_TITLE' + CONVERT(VARCHAR(5),@GUID)

	UPDATE TBPMO_FORM_VIEW SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBPMO_FORM_VIEW.GUID = INSERTED.GUID

	If exists(SELECT * FROM TBPMO_LANGUAGE_OBJECT WHERE PMO_OBJECT_NAME = @PMO_OBJECT_NAME AND
	LANGUAGE_TYPE = @LANGUAGE AND SCREEN_ID = @SCREEN_ID)
		UPDATE TBPMO_LANGUAGE_OBJECT SET CAPTION = @FORM_TITLE,CHANGED_WHO = @CHANGED_WHO WHERE
			PMO_OBJECT_NAME = @PMO_OBJECT_NAME AND
			LANGUAGE_TYPE = @LANGUAGE AND SCREEN_ID = @SCREEN_ID
	ELSE
		BEGIN TRY 
			INSERT INTO TBPMO_LANGUAGE_OBJECT (LANGUAGE_TYPE,SCREEN_ID,PMO_OBJECT_NAME,CAPTION) VALUES
				(@LANGUAGE,@SCREEN_ID,@PMO_OBJECT_NAME,@FORM_TITLE)
		END TRY
		BEGIN CATCH   
			PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ROW 72'
			+ ' - ERROR-MESSAGE: ' 
			+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
		END CATCH
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) + ' - ROW 84'
	+ ' - ERROR-MESSAGE: ' 
	+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO

CREATE TRIGGER [dbo].[TBPMO_FORM_VIEW_AFT_INS] ON [dbo].[TBPMO_FORM_VIEW]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE 
		@FORM_TITLE	VARCHAR(50),
		@LANGUAGE VARCHAR(5), 
		@ADDED_WHO VARCHAR(50),
		@GUID INTEGER,
		@SCREEN_ID INTEGER,
		@PMO_OBJECT_NAME VARCHAR(100)
	SELECT 
		@GUID = GUID,
		@FORM_TITLE = FORM_TITLE,
		@LANGUAGE = [LANGUAGE],
		@SCREEN_ID = SCREEN_ID,
		@ADDED_WHO = CHANGED_WHO
	FROM INSERTED

	SET @PMO_OBJECT_NAME = 'FORMVIEW_TITLE' + CONVERT(VARCHAR(5),@GUID)
		
	INSERT INTO TBPMO_LANGUAGE_OBJECT (LANGUAGE_TYPE,SCREEN_ID,PMO_OBJECT_NAME,CAPTION,ADDED_WHO) VALUES
		(@LANGUAGE,@SCREEN_ID,@PMO_OBJECT_NAME,@FORM_TITLE,@ADDED_WHO)
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE())
	+ ' - ERROR-MESSAGE: ' 
	+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO
CREATE TRIGGER [dbo].[TBPMO_FORM_CONSTRUCTOR_AFT_INS] ON [dbo].[TBPMO_FORM_CONSTRUCTOR]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE 
		@FORM_TITLE	VARCHAR(50),
		@LANGUAGE VARCHAR(5), 
		@ADDED_WHO VARCHAR(50),
		@GUID INTEGER,
		@PMO_OBJECT_NAME VARCHAR(100)
	SELECT 
		@GUID = GUID,
		@FORM_TITLE = FORM_TITLE,
		@LANGUAGE = [LANGUAGE],
		@ADDED_WHO = CHANGED_WHO
	FROM INSERTED

	SET @PMO_OBJECT_NAME = 'CONSTR_MENUE' + CONVERT(VARCHAR(5),@GUID)
		
	INSERT INTO TBPMO_LANGUAGE_OBJECT (LANGUAGE_TYPE,SCREEN_ID,PMO_OBJECT_NAME,CAPTION,ADDED_WHO) VALUES
		(@LANGUAGE,1,@PMO_OBJECT_NAME,@FORM_TITLE,@ADDED_WHO)
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE())
	+ ' - ERROR-MESSAGE: ' 
	+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO

ALTER VIEW [dbo].[VWPMO_CONSTRUCTOR_FORMS] AS
SELECT TOP 100 PERCENT
	T2.GUID,
	T1.GUID FORM_VIEW_ID,
	T1.SCREEN_ID,
	T2.CONSTRUCT_ID,
	T.PARENT_ID,
	T2.SEQUENCE,
	T2.LEVEL1_SELECT,
	T.GUID			AS FORM_ID, 
	T1.FORM_TITLE, 
	T2.SQL_SELECT_EBENE1,
	T2.SQL_SELECT_EBENE2,
	T2.SQL_QUICK_VIEW,
	T2.COLUMN_NAME1,
	T2.COLUMN_NAME2,
	T.FORM_TYPE_ID,
	T2.LOAD_DIRECT
FROM 
	tbpmo_form T, 
	TBPMO_FORM_VIEW T1,
	TBPMO_FORM_CONSTRUCTOR_DETAIL T2 
where 
	T2.FORM_ID = T.GUID AND
	T.GUID = T1.FORM_ID 
ORDER BY 
	T2.CONSTRUCT_ID,T.PARENT_ID,T2.SEQUENCE
GO


DECLARE	@ID		INTEGER,
		@SCREEN_ID			INT,
		@FORM_TITLE VARCHAR(100),
		@LANGUAGE VARCHAR(5),
		@PMO_OBJECT_NAME VARCHAR(100)
		
DECLARE	c_FORM_VIEW	CURSOR FOR 
SELECT GUID ,SCREEN_ID, FORM_TITLE, LANGUAGE FROM TBPMO_FORM_VIEW


BEGIN
	OPEN c_FORM_VIEW 
    FETCH NEXT FROM c_FORM_VIEW INTO @ID, @SCREEN_ID,@FORM_TITLE,@LANGUAGE
    
	WHILE @@FETCH_STATUS = 0
		BEGIN
			SET @PMO_OBJECT_NAME = 'FORMVIEW_TITLE' + CONVERT(VARCHAR(5),@ID)
		
			IF NOT EXISTS(SELECT * FROM TBPMO_LANGUAGE_OBJECT WHERE PMO_OBJECT_NAME = @PMO_OBJECT_NAME AND
			LANGUAGE_TYPE = @LANGUAGE AND SCREEN_ID = @SCREEN_ID)
				INSERT INTO TBPMO_LANGUAGE_OBJECT (LANGUAGE_TYPE,SCREEN_ID,PMO_OBJECT_NAME,CAPTION) VALUES
				(@LANGUAGE,@SCREEN_ID,@PMO_OBJECT_NAME,@FORM_TITLE)
			
			FETCH NEXT FROM c_FORM_VIEW INTO @ID, @SCREEN_ID,@FORM_TITLE,@LANGUAGE
		END
	CLOSE c_FORM_VIEW
    DEALLOCATE c_FORM_VIEW

END
GO
--############## DOKUMENTART #####################
ALTER TABLE TBDD_DOKUMENTART
ADD LANGUAGE VARCHAR(5) NOT NULL DEFAULT 'de-DE'
GO

ALTER TRIGGER [dbo].[TBDD_DOKUMENTART_AFT_UPD] ON [dbo].[TBDD_DOKUMENTART]
FOR UPDATE
AS
BEGIN TRY
	DECLARE 
		@DA_TITLE	VARCHAR(50),
		@LANGUAGE VARCHAR(5), 
		@CHANGED_WHO VARCHAR(50),
		@GUID INTEGER,
		@SCREEN_ID INTEGER,
		@PMO_OBJECT_NAME VARCHAR(100)
	SELECT 
		@GUID = GUID,
		@DA_TITLE = BEZEICHNUNG,
		@LANGUAGE = [LANGUAGE],
		@SCREEN_ID = 1,
		@CHANGED_WHO = GEANDERTWER
	FROM INSERTED

	SET @PMO_OBJECT_NAME = 'DOCTYPE_TITLE' + CONVERT(VARCHAR(5),@GUID)

	 UPDATE TBDD_DOKUMENTART SET GEAENDERTWANN = GETDATE() FROM INSERTED 
	 WHERE TBDD_DOKUMENTART.GUID = INSERTED.GUID

	If exists(SELECT * FROM TBPMO_LANGUAGE_OBJECT WHERE PMO_OBJECT_NAME = @PMO_OBJECT_NAME AND
	LANGUAGE_TYPE = @LANGUAGE AND SCREEN_ID = @SCREEN_ID)
		UPDATE TBPMO_LANGUAGE_OBJECT SET CAPTION = @DA_TITLE,CHANGED_WHO = @CHANGED_WHO WHERE
			PMO_OBJECT_NAME = @PMO_OBJECT_NAME AND
			LANGUAGE_TYPE = @LANGUAGE AND SCREEN_ID = @SCREEN_ID
	ELSE
		INSERT INTO TBPMO_LANGUAGE_OBJECT (LANGUAGE_TYPE,SCREEN_ID,PMO_OBJECT_NAME,CAPTION) VALUES
				(@LANGUAGE,@SCREEN_ID,@PMO_OBJECT_NAME,@DA_TITLE)
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE()) 
		+ ' - ERROR-MESSAGE: ' 
		+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO

CREATE TRIGGER [dbo].[TBDD_DOKUMENTART_AFT_INS] ON [dbo].[TBDD_DOKUMENTART]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE 
		@DA_TITLE	VARCHAR(50),
		@LANGUAGE VARCHAR(5), 
		@ADDED_WHO VARCHAR(50),
		@GUID INTEGER,
		@SCREEN_ID INTEGER,
		@PMO_OBJECT_NAME VARCHAR(100)
	SELECT 
		@GUID = GUID,
		@DA_TITLE = BEZEICHNUNG,
		@LANGUAGE = [LANGUAGE],
		@SCREEN_ID = 1,
		@ADDED_WHO = ERSTELLTWER
	FROM INSERTED

	SET @PMO_OBJECT_NAME = 'DOCTYPE_TITLE' + CONVERT(VARCHAR(5),@GUID)
		
	INSERT INTO TBPMO_LANGUAGE_OBJECT (LANGUAGE_TYPE,SCREEN_ID,PMO_OBJECT_NAME,CAPTION,ADDED_WHO) VALUES
		(@LANGUAGE,@SCREEN_ID,@PMO_OBJECT_NAME,@DA_TITLE,@ADDED_WHO)
END TRY
BEGIN CATCH   
	PRINT 'ERROR IN PROCEDURE: ' + CONVERT(VARCHAR(30),ERROR_PROCEDURE())
	+ ' - ERROR-MESSAGE: ' 
	+ CONVERT(VARCHAR(500),ERROR_MESSAGE())
END CATCH
GO



