
ALTER TABLE TBDD_BASECONFIG ADD WM_DRIVE VARCHAR(1)	NOT NULL DEFAULT 'W'
GO 
ALTER TABLE TBDD_BASECONFIG ADD DB_VERSION VARCHAR(10)	NOT NULL DEFAULT '1.1'
GO 
ALTER TABLE TBDD_USER ADD DELETED BIT NOT NULL DEFAULT 0
GO 
ALTER TABLE TBDD_USER ADD DELETED_WHEN DATETIME 
GO 
ALTER TABLE TBDD_USER ADD DELETED_WHO DATETIME 
GO 
UPDATE TBDD_BASECONFIG SET DB_VERSION = '1.1'
GO
ALTER TABLE TBDD_MODULES ADD SHORT_NAME VARCHAR(10)
GO 

if not exists (select * from sysobjects where name='TBDD_CLIENT' and xtype='U')  
CREATE TABLE TBDD_CLIENT
(
	GUID					INTEGER	IDENTITY(1,1),         				
	CLIENT_NAME				VARCHAR(50)	NOT NULL UNIQUE,
	SHORTNAME				VARCHAR(30),
	LICENSE_PMO				VARCHAR(5000) NOT NULL DEFAULT '',
	LICENSE_GI				VARCHAR(5000) NOT NULL DEFAULT '',
	LICENSE_PM				VARCHAR(5000) NOT NULL DEFAULT '',
	COMMENT					VARCHAR(200),
	ADDED_WHO				VARCHAR(50)	DEFAULT 'DEFAULT'	NOT NULL,
	ADDED_WHEN				DATETIME 	DEFAULT GETDATE(),
	CHANGED_WHO				VARCHAR(50),
	CHANGED_WHEN			DATETIME,
	CONSTRAINT				PK_TBDD_CLIENT	PRIMARY KEY (GUID)
)
GO
INSERT INTO TBDD_CLIENT(CLIENT_NAME,SHORTNAME) VALUES 
('DEFAULT','DEF')
GO

CREATE TRIGGER TBDD_CLIENT_AFT_UPD ON TBDD_CLIENT
FOR UPDATE
AS
BEGIN 
	UPDATE TBDD_CLIENT SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBDD_CLIENT.GUID = INSERTED.GUID
END	
if not exists (select * from sysobjects where name='TBDD_USER_MODULES' and xtype='U')  
CREATE TABLE TBDD_USER_MODULES
(
	GUID			INTEGER	IDENTITY(1,1),         				
	[USER_ID]		INT										NOT NULL,
	MODULE_ID		INT										NOT NULL,
	IS_ADMIN		BIT										NOT NULL DEFAULT 0,
	RIGHT1			BIT										NOT NULL DEFAULT 0,
	RIGHT2			BIT										NOT NULL DEFAULT 0,
	RIGHT3			BIT										NOT NULL DEFAULT 0,
	RIGHT4			BIT										NOT NULL DEFAULT 0,
	COMMENT			VARCHAR(200),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO		VARCHAR(50),
	CHANGED_WHEN	DATETIME ,
	CONSTRAINT		PK_TBDD_USER_MODULES			PRIMARY KEY (GUID),
	CONSTRAINT		UQ_TBDD_USER_MODULES			UNIQUE([USER_ID],MODULE_ID),
	CONSTRAINT		FK_TBDD_USER_MODULES_MODULE_ID	FOREIGN KEY(MODULE_ID)	REFERENCES TBDD_MODULES(GUID),
	CONSTRAINT		FK_TBDD_USER_MODULES2_USER_ID	FOREIGN KEY([USER_ID])	REFERENCES TBDD_USER(GUID),
)
GO
ALTER TABLE TBDD_USER_MODULES ADD IS_ADMIN BIT	NOT NULL DEFAULT 0
GO 
ALTER TABLE TBDD_USER_MODULES ADD RIGHT1 BIT	NOT NULL DEFAULT 0
GO 
ALTER TABLE TBDD_USER_MODULES ADD RIGHT2 BIT	NOT NULL DEFAULT 0
GO 
ALTER TABLE TBDD_USER_MODULES ADD RIGHT3 BIT	NOT NULL DEFAULT 0
GO 

if not exists (select * from sysobjects where name='TBDD_GROUPS' and xtype='U')  
CREATE TABLE TBDD_GROUPS
(
	GUID			INTEGER	IDENTITY(1,1),         				
	NAME			VARCHAR(50),
	ECM_FK_ID		INTEGER			NOT NULL DEFAULT 0,
	AD_SYNC			BIT				NOT NULL DEFAULT 0,
	INTERNAL		BIT				NOT NULL DEFAULT 0,
	ACTIVE			BIT				NOT NULL DEFAULT 1,
	COMMENT			VARCHAR(200),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO		VARCHAR(50),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PK_TBDD_GROUPS	PRIMARY KEY (GUID),
	CONSTRAINT		UQ_TBDD_GROUPS	UNIQUE(NAME)
)
GO

CREATE TRIGGER TBDD_GROUPS_AFT_UPD ON TBDD_GROUPS
FOR UPDATE
AS
	UPDATE TBDD_GROUPS SET CHANGED_WHEN = GETDATE() FROM INSERTED 
	WHERE TBDD_GROUPS.GUID = INSERTED.GUID
GO


INSERT INTO TBDD_GROUPS (NAME,INTERNAL) VALUES ('PM_USER',1)
GO
INSERT INTO TBDD_GROUPS (NAME,INTERNAL) VALUES ('PM_ADMINS',1)
GO
INSERT INTO TBDD_GROUPS (NAME,INTERNAL) VALUES ('GLOBIX_USER',1)
GO
INSERT INTO TBDD_GROUPS (NAME,INTERNAL) VALUES ('GLOBIX_ADMINS',1)
GO
INSERT INTO TBDD_GROUPS (NAME,INTERNAL) VALUES ('ADDI_USER',1)
GO
INSERT INTO TBDD_GROUPS (NAME,INTERNAL) VALUES ('ADDI_ADMINS',1)
GO
INSERT INTO TBDD_GROUPS (NAME,INTERNAL) VALUES ('CW_USER',1)
GO
INSERT INTO TBDD_GROUPS (NAME,INTERNAL) VALUES ('CW_ADMINS',1)
GO
INSERT INTO TBDD_GROUPS (NAME,INTERNAL) VALUES ('UM_ADMINS',1)
GO

if not exists (select * from sysobjects where name='TBDD_GROUPS_MODULES' and xtype='U')  
CREATE TABLE TBDD_GROUPS_MODULES
(
	GUID			INTEGER	IDENTITY(1,1),         				
	MODULE_ID		INT										NOT NULL,
	GROUP_ID		INT										NOT NULL,
	COMMENT			VARCHAR(200),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT'		NOT NULL,
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO		VARCHAR(50),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PK_TBDD_GROUPS_MODULES	PRIMARY KEY (GUID),
	CONSTRAINT		UC_MODULE_GROUP UNIQUE (MODULE_ID,GROUP_ID),
	CONSTRAINT		FK_TBDD_GROUPS_MODULES_GROUP_ID	FOREIGN KEY(GROUP_ID)	REFERENCES TBDD_GROUPS(GUID),
	CONSTRAINT		FK_TBDD_GROUPS_MODULES_MODULE_ID	FOREIGN KEY(MODULE_ID)	REFERENCES TBDD_MODULES(GUID),
)
GO
if not exists (select * from sysobjects where name='TBDD_GROUPS_CLIENT' and xtype='U')  
CREATE TABLE TBDD_GROUPS_CLIENT
(
	GUID			INTEGER	IDENTITY(1,1),         				
	CLIENT_ID		INT										NOT NULL,
	GROUP_ID		INT										NOT NULL,
	COMMENT			VARCHAR(200),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT'		NOT NULL,
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO		VARCHAR(50),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PK_TBDD_GROUPS_CLIENT	PRIMARY KEY (GUID),
	CONSTRAINT		UC_CLIENT_GROUP UNIQUE (CLIENT_ID,GROUP_ID),
	CONSTRAINT		FK_TBDD_GROUPS_CLIENT_GROUP_ID	FOREIGN KEY(GROUP_ID)	REFERENCES TBDD_GROUPS(GUID),
	CONSTRAINT		FK_TBDD_GROUPS_CLIENT_CLIENT_ID	FOREIGN KEY(CLIENT_ID)	REFERENCES TBDD_CLIENT(GUID),
)
GO
if not exists (select * from sysobjects where name='TBDD_GROUPS_USER' and xtype='U')  
CREATE TABLE TBDD_GROUPS_USER
(
	GUID			INTEGER	IDENTITY(1,1),         				
	[USER_ID]		INT										NOT NULL,
	GROUP_ID		INT										NOT NULL,
	COMMENT			VARCHAR(200),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT'	NOT NULL,
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO		VARCHAR(50),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PK_TBDD_GROUPS_USER	PRIMARY KEY (GUID),
	CONSTRAINT		UC_USER_GROUP UNIQUE (USER_ID,GROUP_ID),
	CONSTRAINT		FK_TBDD_GROUPS_USER_GROUP_ID	FOREIGN KEY(GROUP_ID)	REFERENCES TBDD_GROUPS(GUID),
	CONSTRAINT		FK_TBDD_GROUPS_USER_USER_ID	FOREIGN KEY([USER_ID])	REFERENCES TBDD_USER(GUID),
)
GO
if not exists (select * from sysobjects where name='TBDD_CLIENT_GROUP' and xtype='U')  
CREATE TABLE TBDD_CLIENT_GROUP
(
	GUID			INTEGER	IDENTITY(1,1),         				
	[GROUP_ID]		INT										NOT NULL,
	CLIENT_ID		INT										NOT NULL,
	COMMENT			VARCHAR(200),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CONSTRAINT		PK_TBDD_CLIENT_GROUP	PRIMARY KEY (GUID),
	CONSTRAINT		FK_TBDD_CLIENT_GROUP_GROUP_ID	FOREIGN KEY(GROUP_ID)	REFERENCES TBDD_GROUPS(GUID),
	CONSTRAINT		FK_TBDD_CLIENT_GROUP_CLIENT_ID	FOREIGN KEY(CLIENT_ID)	REFERENCES TBDD_CLIENT(GUID),
)
GO

if not exists (select * from sysobjects where name='TBDD_CLIENT_USER' and xtype='U')  
CREATE TABLE TBDD_CLIENT_USER
(
	GUID			INTEGER	IDENTITY(1,1),         				
	[USER_ID]		INT										NOT NULL,
	CLIENT_ID		INT										NOT NULL,
	COMMENT			VARCHAR(200),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CONSTRAINT		PK_TBDD_CLIENT_USER	PRIMARY KEY (GUID),
	CONSTRAINT		FK_TBDD_CLIENT_USER_GROUP_ID	FOREIGN KEY(CLIENT_ID)	REFERENCES TBDD_CLIENT(GUID),
	CONSTRAINT		FK_TBDD_CLIENT_USER_USER_ID	FOREIGN KEY([USER_ID])	REFERENCES TBDD_USER(GUID),
)
GO
CREATE TABLE TBDD_CLIENT_GROUP
(
	GUID			INTEGER	IDENTITY(1,1),         				
	[GROUP_ID]		INT										NOT NULL,
	CLIENT_ID		INT										NOT NULL,
	COMMENT			VARCHAR(200),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CONSTRAINT		PK_TBDD_CLIENT_GROUP	PRIMARY KEY (GUID),
	CONSTRAINT		FK_TBDD_CLIENT_GROUP_GROUP_ID	FOREIGN KEY(GROUP_ID)	REFERENCES TBDD_GROUPS(GUID),
	CONSTRAINT		FK_TBDD_CLIENT_GROUP_CLIENT_ID	FOREIGN KEY(CLIENT_ID)	REFERENCES TBDD_CLIENT(GUID),
)
GO
if not exists (select * from sysobjects where name='TBDD_USER_LOGIN_OUT' and xtype='U')  
CREATE TABLE TBDD_USER_LOGIN_OUT(
	GUID			INT		IDENTITY(1,1)	NOT NULL,
	CONNECTION_STRING	VARCHAR(500)		NOT NULL DEFAULT '',
	CLIENT_ID		INT						NOT NULL,
	MACHINE_NAME	VARCHAR(250)			NOT NULL DEFAULT '',
	MODULE			VARCHAR(30)				NOT NULL,
	[USER_ID]		INT						NOT NULL,
	VERSION_CLIENT	VARCHAR(30),
	[LOGIN]			DATETIME				DEFAULT (GETDATE()),
	[LOGOUT]		DATETIME,
CONSTRAINT 	   	[PK_TBDD_USER_LOGIN_OUT] 	PRIMARY KEY(GUID),
CONSTRAINT		FK_TBDD_USER_LOGIN_OUT_USER_ID	FOREIGN KEY ([USER_ID]) REFERENCES  TBDD_USER (GUID))
GO



DROP TABLE TBDD_USER_MODULE_LOG_IN
GO
CREATE TABLE TBDD_USER_MODULE_LOG_IN
(
	GUID			INTEGER	IDENTITY(1,1),  
	CLIENT_ID		INTEGER		NOT NULL,
	CONNECTION_STRING VARCHAR(500) NOT NULL DEFAULT '' ,     				
	USER_ID			INTEGER		NOT NULL,
	MACHINE_NAME	VARCHAR(250) NOT NULL DEFAULT '',
	MODULE			VARCHAR(50),
	VERSION_CLIENT	VARCHAR(30),
	ADDED_WHEN		DATETIME 	DEFAULT GETDATE()		NOT NULL,
	CONSTRAINT		PK_TBDD_USER_MODULE_LOG_IN	PRIMARY KEY (GUID)
)
GO
CREATE TRIGGER TBDD_USER_MODULE_LOG_IN_AFT_INS ON TBDD_USER_MODULE_LOG_IN
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE 
		@USER_ID		INTEGER,
		@MODULE			VARCHAR(50),
		@CLIENT_ID		INTEGER,
		@VERSION_CLIENT	VARCHAR(30),
		@MACHINE_NAME	VARCHAR(50),
		@CONNECTION_STRING	VARCHAR(500)
	SELECT
  		@USER_ID = USER_ID,
		@MODULE = MODULE,
		@CLIENT_ID = CLIENT_ID,
		@VERSION_CLIENT = VERSION_CLIENT,
		@MACHINE_NAME = MACHINE_NAME,
		@CONNECTION_STRING = CONNECTION_STRING
	FROM INSERTED
	
	IF @VERSION_CLIENT IS NULL
		SET @VERSION_CLIENT = '1.0.0.0'
	INSERT INTO TBDD_USER_LOGIN_OUT (MODULE,USER_ID,CLIENT_ID,VERSION_CLIENT,MACHINE_NAME,CONNECTION_STRING) VALUES (@MODULE,@USER_ID,@CLIENT_ID,@VERSION_CLIENT,@MACHINE_NAME,@CONNECTION_STRING)
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
GO
CREATE TRIGGER TBDD_USER_MODULE_LOG_IN_AFT_DEL ON TBDD_USER_MODULE_LOG_IN
FOR DELETE
AS
BEGIN TRY
	DECLARE 
		@USER_ID		INTEGER,
		@MODULE			VARCHAR(50),
		@CLIENT_ID		INTEGER
	SELECT
  		@USER_ID = USER_ID,
		@MODULE = MODULE,
		@CLIENT_ID = CLIENT_ID
	FROM DELETED

	UPDATE TBDD_USER_LOGIN_OUT SET LOGOUT = GETDATE() WHERE GUID = (
	SELECT MAX(GUID) FROM TBDD_USER_LOGIN_OUT WHERE USER_ID = @USER_ID AND MODULE = @MODULE AND CLIENT_ID = @CLIENT_ID)
	
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].[VWDD_USER2GROUP] AS 
SELECT TOP 100 PERCENT T.GUID, T.USERNAME, T2.GUID as GROUP_ID, T2.NAME, T2.COMMENT
FROM TBDD_USER T, TBDD_GROUPS_USER T1, TBDD_GROUPS T2
WHERE
	T.GUID = T1.USER_ID AND
	T1.GROUP_ID = T2.GUID
ORDER BY T.USERNAME
GO