UPDATE TBDD_MODULES SET PRODUCT_VERSION = '2.4.7.0' where NAME = 'Record-Organizer' 
GO
ALTER TABLE TBDD_USER_LOGIN_OUT
 ADD MACHINE_NAME VARCHAR(250) NOT NULL DEFAULT ''
GO
ALTER TABLE TBDD_USER_MODULE_LOG_IN
 ADD MACHINE_NAME VARCHAR(250) NOT NULL DEFAULT ''
GO
ALTER TABLE TBDD_MODULES
 ADD VERSION_UPATE_PATH VARCHAR(500) NOT NULL DEFAULT ''
GO
ALTER TRIGGER [dbo].[TBDD_USER_MODULE_LOG_IN_AFT_INS] ON [dbo].[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)
	SELECT
  		@USER_ID = USER_ID,
		@MODULE = MODULE,
		@CLIENT_ID = CLIENT_ID,
		@VERSION_CLIENT = VERSION_CLIENT,
		@MACHINE_NAME = MACHINE_NAME
	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) VALUES (@MODULE,@USER_ID,@CLIENT_ID,@VERSION_CLIENT,@MACHINE_NAME)
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 TABLE TBDD_VERSION_USER_UPDATE_PATH(
	GUID			INTEGER			NOT NULL	IDENTITY(1,1),
	USER_ID			INT					NOT NULL,
	MODULE_NAME		VARCHAR(30)			NOT NULL,
	UPDATE_PATH		VARCHAR(500)		NOT NULL DEFAULT '',
	ADDED_WHEN		DATETIME			DEFAULT GETDATE(),
	CHANGED_WHEN	DATETIME			DEFAULT GETDATE(),
CONSTRAINT 	   	[PK_TBDD_VERSION_USER_UPDATE_PATH] 	PRIMARY KEY(GUID))
GO
CREATE TRIGGER TBDD_VERSION_USER_UPDATE_PATH_AFT_UPD ON TBDD_VERSION_USER_UPDATE_PATH
FOR UPDATE
AS
	UPDATE TBDD_VERSION_USER_UPDATE_PATH SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE TBDD_VERSION_USER_UPDATE_PATH.GUID = INSERTED.GUID

GO
CREATE TABLE TBDD_VERSION_UPDATE(
	GUID				INTEGER				NOT NULL	IDENTITY(1,1),
	MODULE_NAME			VARCHAR(30)			NOT NULL,
	VERSION_NO			VARCHAR(20)			NOT NULL,
	VERSION_FORCE_UPD	VARCHAR(100)		NOT NULL	DEFAULT '',
	FORCE_UPD			BIT					NOT NULL	DEFAULT 0,
	ADDED_WHEN			DATETIME			DEFAULT GETDATE(),
	CHANGED_WHEN		DATETIME			DEFAULT GETDATE(),
CONSTRAINT 	   	[PK_TBDD_VERSION_UPDATE] 	PRIMARY KEY(GUID))
GO
CREATE TABLE TBDD_VERSION_ITEMS(
	GUID				INTEGER			NOT NULL	IDENTITY(1,1),
	UPDATE_ID			INTEGER			NOT NULL,
	ITEM_INFO			VARCHAR(100)	NOT NULL,
	INFO1				VARCHAR(100),
	INFO2				VARCHAR(100),
	INFO3				VARCHAR(100),
	BIT1				BIT				NOT NULL DEFAULT 0,
	BIT2				BIT				NOT NULL DEFAULT 0,
	BIT3				BIT				NOT NULL DEFAULT 0,
	COMMENT				VARCHAR(250),
	ADDED_WHEN			DATETIME			DEFAULT GETDATE(),
	CHANGED_WHEN		DATETIME			DEFAULT GETDATE(),
CONSTRAINT 	   	[PK_TBDD_VERSION_ITEMS] 	PRIMARY KEY(GUID),
CONSTRAINT		FK_TBDD_VERSION_ITEMS_UPDATE_ID		FOREIGN KEY (UPDATE_ID)  REFERENCES  TBDD_VERSION_UPDATE (GUID))
GO
CREATE TABLE TBDD_USER_MODULES
(
	GUID			INTEGER	IDENTITY(1,1),         				
	[USER_ID]		INT										NOT NULL,
	MODULE_ID		INT										NOT NULL,
	COMMENT			VARCHAR(200),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT',
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	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 PROCEDURE [dbo].[PRPMO_DELETE_USER](@pUSER_ID INT)
AS
BEGIN TRY
	IF OBJECT_ID(N'dbo.TBGI_FOLDERWATCH_USER', N'U') IS NOT NULL 
		DELETE FROM TBGI_FOLDERWATCH_USER WHERE USER_ID = @pUSER_ID
	IF OBJECT_ID(N'dbo.TBHOTKEY_USER_PROFILE', N'U') IS NOT NULL 
		DELETE FROM TBHOTKEY_USER_PROFILE WHERE USER_ID = @pUSER_ID
	
	DELETE FROM TBDD_CLIENT_USER WHERE USER_ID = @pUSER_ID
	DELETE FROM TBDD_GROUPS_USER WHERE USER_ID = @pUSER_ID
	DELETE FROM TBPMO_CONSTRUCTOR_USER_SQL WHERE USER_ID = @pUSER_ID
	DELETE FROM TBPMO_CONSTRUCTOR_USER WHERE USER_ID = @pUSER_ID
	DELETE FROM TBPMO_FOLDERWATCH_USER WHERE USER_ID = @pUSER_ID
	DELETE FROM TBPMO_FOLLUPEMAIL_USER WHERE USER_ID = @pUSER_ID
	DELETE FROM TBPMO_FOLLUPEMAIL_RECORD WHERE USER_ID = @pUSER_ID
	DELETE FROM TBPMO_USER_CONSTR_VIEW_TYPE WHERE USER_ID = @pUSER_ID
	DELETE FROM TBPMO_ENTITY_SUPERVISOR WHERE USER_ID = @pUSER_ID
	DELETE FROM TBDD_USER_MODULES WHERE USER_ID = @pUSER_ID
	DELETE FROM TBPMO_USER_UPDATE_PATH WHERE USER_ID = @pUSER_ID
	DELETE FROM TBDD_USER WHERE GUID = @pUSER_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
DECLARE
	@USER_ID	INTEGER		
								
DECLARE c_REDO CURSOR FOR 
	SELECT GUID FROM TBDD_USER where MODULE_RECORD_ORG = 1
  OPEN c_REDO 
    FETCH NEXT FROM c_REDO INTO @USER_ID
    WHILE @@FETCH_STATUS = 0
	BEGIN
		INSERT INTO TBDD_USER_MODULES (USER_ID,MODULE_ID) VALUES (@USER_ID,0)
		FETCH NEXT FROM c_REDO INTO @USER_ID
	END
CLOSE c_REDO
DEALLOCATE c_REDO
GO
DECLARE
	@USER_ID	INTEGER		
DECLARE c_REDO CURSOR FOR 
	SELECT GUID FROM TBDD_USER where MODULE_RECORD_ORG = 1
  OPEN c_REDO 
    FETCH NEXT FROM c_REDO INTO @USER_ID
    WHILE @@FETCH_STATUS = 0
	BEGIN
		IF NOT EXISTS(SELECT GUID FROM TBDD_VERSION_USER_UPDATE_PATH WHERE USER_ID = @USER_ID AND MODULE_NAME = 'RECORD-ORGANIZER' )
			INSERT INTO TBDD_VERSION_USER_UPDATE_PATH (USER_ID,MODULE_NAME,UPDATE_PATH) VALUES (@USER_ID,'RECORD-ORGANIZER','P:\Projekte DIGITAL DATA\DIGITAL DATA - Entwicklung\TEST\TEST_UPDATE_RECORDGANIZER')
		FETCH NEXT FROM c_REDO INTO @USER_ID
	END
CLOSE c_REDO
DEALLOCATE c_REDO
GO

--########aktualisiere Trigger after insert TBDD_USER To ADD USers to table!
ALTER TRIGGER [dbo].[TBDD_USER_AFT_INS] ON [dbo].[TBDD_USER]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN TRY
	DECLARE 
		@USER_ID INTEGER,
		@CLIENT_ID INTEGER,
		@ADDED_WHO VARCHAR(50),
		@CLIENT_COUNT INTEGER,
		@ECM_USR_ID INTEGER,
		@USRNAME VARCHAR(50),
		@MODULE_RO BIT,
		@UPDATE_PATH VARCHAR(500)
	SELECT 
		@USER_ID = GUID,
		@ADDED_WHO = ADDED_WHO,
		@USRNAME = USERNAME,
		@MODULE_RO = MODULE_RECORD_ORG
		
	FROM INSERTED

	IF @MODULE_RO = 1
		BEGIN
			SELECT @UPDATE_PATH FROM TBDD_MODULES WHERE UPPER(NAME) = UPPER('Record-Organizer')
			INSERT INTO TBDD_VERSION_USER_UPDATE_PATH (USER_ID,MODULE_NAME,UPDATE_PATH) VALUES
				(@USER_ID, 'Record-Organizer', @UPDATE_PATH)
			SELECT @ECM_USR_ID = [dwUserID]
			 FROM [172.24.12.41\BRAINPOOL].windream60.[dbo].[UserInfo] where UPPER(szUserName) = UPPER(@USRNAME)
			IF @ECM_USR_ID IS NOT NULL
				UPDATE TBDD_USER SET USERID_FK_INT_ECM = @ECM_USR_ID
							FROM INSERTED
							WHERE TBDD_USER.GUID = INSERTED.GUID

			SELECT @CLIENT_COUNT = COUNT(*) FROM TBDD_CLIENT
			IF @CLIENT_COUNT = 1
				BEGIN
					SELECT @CLIENT_COUNT = GUID FROM TBDD_CLIENT
					INSERT INTO TBDD_CLIENT_USER (USER_ID,CLIENT_ID,ADDED_WHO) VALUES
					(@USER_ID,@CLIENT_COUNT,@ADDED_WHO)
				END

			INSERT INTO TBDD_USER_MODULES (USER_ID,MODULE_ID) VALUES (@USER_ID,0)
		END
		
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_LOGIN_USER_HISTORY] AS
SELECT TOP (5000) 
	T.GUID, 
	T.MODULE,
	T1.USERNAME, 
	T1.NAME, 
	T1.PRENAME, 
	T1.GUID as USER_ID, 
	T.CLIENT_ID,
	T2.CLIENT_NAME, 
	T.LOGIN, 
	T.LOGOUT, 
	T.VERSION_CLIENT,
	T.MACHINE_NAME
FROM 
	TBDD_USER_LOGIN_OUT T,
	TBDD_USER T1,
	TBDD_CLIENT T2
WHERE 
	T.USER_ID = T1.GUID 
	AND T.CLIENT_ID = T2.GUID
ORDER BY GUID DESC
GO




