UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.0.8'
GO

ALTER TABLE TBDD_USER ADD ACTIVE BIT NOT NULL DEFAULT 1;
GO
UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.0.8'
GO
UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.1.0'
GO
CREATE TABLE TBDD_USER_REPRESENTATION (
	GUID				INTEGER			NOT NULL	IDENTITY(1,1),
	USER_ID				INTEGER			NOT NULL,
	REPR_GROUP			INTEGER			NOT NULL,
	RIGHT_GROUP			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_TBDD_USER_REPRESENTATION PRIMARY KEY (GUID),
	CONSTRAINT			FK_TBDD_USER_REPRESENTATION_USER_ID FOREIGN KEY (USER_ID)  REFERENCES  TBDD_USER (GUID),
	CONSTRAINT			FK_TBDD_USER_REPRESENTATION_REPR_GROUP FOREIGN KEY (REPR_GROUP)  REFERENCES  TBDD_GROUPS (GUID),
	CONSTRAINT			FK_TBDD_USER_REPRESENTATION_RIGHT_GROUP FOREIGN KEY (RIGHT_GROUP)  REFERENCES  TBDD_GROUPS (GUID)
)
GO
CREATE TRIGGER TBDD_USER_REPRESENTATION_AFT_UPD ON TBDD_USER_REPRESENTATION
FOR UPDATE
AS
  UPDATE TBDD_USER_REPRESENTATION SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBDD_USER_REPRESENTATION.GUID = INSERTED.GUID
GO
UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.1.1'
GO
ALTER TABLE TBDD_USER_REPRESENTATION ALTER COLUMN REPR_GROUP INTEGER NULL;
ALTER TABLE TBDD_USER_REPRESENTATION ADD REPR_USER	INTEGER;
ALTER TABLE TBDD_USER_REPRESENTATION
	ADD CONSTRAINT FK_TBDD_USER_REPRESENTATION_REPR_USER
    FOREIGN KEY (REPR_USER)
    REFERENCES TBDD_USER (GUID);
GO
UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.2.0';
ALTER TABLE TBDD_DOKUMENTART ADD IDB_FILESTORE_ID INTEGER NOT NULL DEFAULT 0;
GO
UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.3.0';
CREATE TABLE TBDD_NOTIFICATIONS_SYSTEM
(
	GUID			INTEGER			NOT NULL	IDENTITY (1, 1),
	TITLE			VARCHAR(250)	NOT NULL,
	MODULE			VARCHAR(250)	NOT NULL,
	DISPLAY_TYPE	VARCHAR(100)	NOT NULL,
	MSG_STRING		NVARCHAR(MAX)	NOT NULL,
	DISPLAY_UNTIL	DATE,
	VALIDATE_READ	BIT				NOT NULL	DEFAULT 0,
	ADDED_WHO		VARCHAR(100),
	ADDED_WHEN		DATETIME					DEFAULT GETDATE(),
	CHANGED_WHO	    VARCHAR(100),
	CHANGED_WHEN   	DATETIME,
	CONSTRAINT PK_TBDD_NOTIFICATIONS_SYSTEM    PRIMARY KEY (GUID),
	)
GO
CREATE TRIGGER TTBDD_NOTIFICATIONS_SYSTEM_AFT_UPD ON TBDD_NOTIFICATIONS_SYSTEM
FOR UPDATE
AS
  UPDATE TBDD_NOTIFICATIONS_SYSTEM SET CHANGED_WHEN = GETDATE()
  FROM INSERTED
  WHERE TBDD_NOTIFICATIONS_SYSTEM .GUID = INSERTED.GUID
GO

CREATE TABLE TBDD_NOTIFICATIONS_USER_HISTORY
(
	GUID			INTEGER	IDENTITY(1,1),         				
	NOTIFY_ID		INT			NOT NULL,
	USR_ID			INT				NOT NULL,
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CONSTRAINT		PK_TBDD_NOTIFICATIONS_USER_HISTORY	PRIMARY KEY (GUID),
	CONSTRAINT		FK_TBDD_NOTIFICATIONS_USER_HISTORY_NID	FOREIGN KEY(NOTIFY_ID)	REFERENCES TBDD_NOTIFICATIONS_SYSTEM(GUID),
)
GO
UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.4.0';
USE [DD_ECM]
GO
CREATE FUNCTION [dbo].[FNDD_LANGUAGE_PHRASE](@pTITLE VARCHAR(100),@pLANG_CODE VARCHAR(5), @pMODULE VARCHAR(10))
RETURNS VARCHAR(500)
AS
BEGIN
	DECLARE @RESULT VARCHAR(250), @CTRL_CONFIG_TITLE VARCHAR(100)
	
			SELECT @RESULT = STRING1 FROM TBDD_GUI_LANGUAGE_PHRASE 
			WHERE MODULE = @pMODULE AND TITLE = @pTITLE 
			AND [LANGUAGE] = @pLANG_CODE
			IF LEN(@RESULT) > 0
				SET @RESULT = @RESULT
			ELSE
				BEGIN
					SELECT @RESULT = STRING1 FROM TBDD_GUI_LANGUAGE_PHRASE 
					WHERE MODULE = @pMODULE AND TITLE = @pTITLE 
					AND [LANGUAGE] = 'de-DE'
					SET @RESULT = @RESULT + ' (NoLangConf)' 
				END

	RETURN @RESULT
END
GO
CREATE PROCEDURE [dbo].[PRDD_GUI_LANGUAGE_PHRASE] (@pMODULE VARCHAR(10),@pTITLE VARCHAR(100),@pLANG_CODE VARCHAR(5),@pCAPTION_TYPE VARCHAR(50),@pSTRING1 VARCHAR(900),@pSTRING2 VARCHAR(900),@pWHO VARCHAR(100)
													,@pOBJ_NAME VARCHAR(100),@pINTERNAL BIT)
AS
If NOT EXISTS(SELECT GUID FROm TBDD_GUI_LANGUAGE_PHRASE WHERE [MODULE] = @pMODULE AND TITLE = @pTITLE AND [LANGUAGE] = @pLANG_CODE)
	INSERT INTO [dbo].[TBDD_GUI_LANGUAGE_PHRASE]
           ([MODULE]
           ,[TITLE]
           ,[LANGUAGE]
           ,[CAPT_TYPE]
           ,[STRING1]
		   ,STRING2
           ,[ADDED_WHO]
           ,[OBJ_NAME],INTERNAL) VALUES
			(@pMODULE,@pTITLE,@pLANG_CODE,@pCAPTION_TYPE,@pSTRING1,@pSTRING2,@pWHO,@pOBJ_NAME,@pINTERNAL)
ELSE
	UPDATE [dbo].[TBDD_GUI_LANGUAGE_PHRASE] SET  [CAPT_TYPE] = @pCAPTION_TYPE
           ,[STRING1] = @pSTRING1
		   ,STRING2 = @pSTRING2
           ,CHANGED_WHO = @pWHO
           ,[OBJ_NAME] = @pOBJ_NAME,INTERNAL = @pINTERNAL WHERE 
		   [MODULE] = @pMODULE AND TITLE = @pTITLE AND [LANGUAGE] = @pLANG_CODE
GO
UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.9.4.1';
ALTER TABLE TBDD_NOTIFICATIONS_SYSTEM ADD LANG_CODE VARCHAR(5) NOT NULL DEFAULT 'ALL'
GO 