UPDATE TBDD_MODULES SET PRODUCT_VERSION = '1.0.0.1' where SHORT_NAME = 'UM'
GO
BEGIN
	IF NOT EXISTS (SELECT 1 FROM TBDD_GROUPS WHERE NAME = 'UM_ADMINS')
		INSERT INTO TBDD_GROUPS (NAME,INTERNAL) VALUES ('UM_ADMINS',1);
END

ALTER TABLE [dbo].[TBDD_GROUPS_USER] DROP CONSTRAINT [FK_TBDD_GROUPS_USER_GROUP_ID]
GO
ALTER TABLE [dbo].[TBDD_GROUPS_USER]  WITH CHECK ADD  CONSTRAINT [FK_TBDD_GROUPS_USER_GROUP_ID] FOREIGN KEY([GROUP_ID])
REFERENCES [dbo].[TBDD_GROUPS] ([GUID])
GO

ALTER TABLE [dbo].[TBDD_GROUPS_USER] CHECK CONSTRAINT [FK_TBDD_GROUPS_USER_GROUP_ID]
GO

DECLARE @UM_GROUP_ID INT 
SELECT @UM_GROUP_ID = GUID FROM  TBDD_GROUPS WHERE NAME = 'UM_ADMINS'
INSERT INTO TBDD_GROUPS_USER (GROUP_ID,USER_ID)
	SELECT @UM_GROUP_ID,B.GUID FROM TBIMPORT_USER A INNER JOIN TBDD_USER B ON A.USR_NAME = B.USERNAME
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
--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),
--	ACTIVE					BIT			NOT NULL DEFAULT 1,
--	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		
--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
--CREATE TRIGGER TBDD_GROUPS_USER_AFT_UPD ON TBDD_GROUPS_USER
--FOR UPDATE
--AS
--	UPDATE TBDD_GROUPS_USER SET CHANGED_WHEN = GETDATE() FROM INSERTED 
--	WHERE TBDD_GROUPS_USER.GUID = INSERTED.GUID
--GO
--CREATE TRIGGER [dbo].[TBDD_GROUPS_USER_AFT_DEL] ON [dbo].[TBDD_GROUPS_USER]
--FOR DELETE
--AS
--	DECLARE @USER_ID INTEGER
--	SELECT @USER_ID = deleted.USER_ID FROM deleted
--	IF @USER_ID IS NOT NULL
--		BEGIN
--			if exists (select * from sysobjects where name='TBPMO_USER_RIGHTS_JOBS' and xtype='U')  
--				INSERT INTO TBPMO_USER_RIGHTS_JOBS (USER_ID,COMMENT,ADDED_WHO) VALUES (@USER_ID,'USER DELETED FROM GROUP','TBDD_GROUPS_USER_AFT_DEL')
--			if exists (select * from sysobjects where name='TBPMO_LOG_ESSENTIALS' and xtype='U')  
--				INSERT INTO TBPMO_LOG_ESSENTIALS (REFERENCE_KEY,REFERENCE_STRING,COMMENT,ADDED_WHO) VALUES (@USER_ID,'USER-ID','USER DELETED FROM USER-GROUP',(SELECT USERNAME FROM TBDD_USER WHERE GUID = @USER_ID ))
--		END
--GO

CREATE TABLE TBUM_AD_COMMAND
(
	GUID			INTEGER	IDENTITY(1,1),    
	AD_CMD_NAME     VARCHAR(250),
	COMMAND_STRING	VARCHAR(MAX),
	ADDED_WHO		VARCHAR(50)		DEFAULT 'DEFAULT'		NOT NULL,
	ADDED_WHEN		DATETIME 		DEFAULT GETDATE(),
	CHANGED_WHO		VARCHAR(50),
	CHANGED_WHEN	DATETIME,
	CONSTRAINT		PK_TBUM_AD_COMMAND	PRIMARY KEY (GUID),
	CONSTRAINT		UC_TBUM_AD_COMMAND UNIQUE (AD_CMD_NAME),
)
GO
CREATE TRIGGER TBUM_AD_COMMAND_AFT_UPD ON TBUM_AD_COMMAND
FOR UPDATE
AS
	UPDATE TBUM_AD_COMMAND SET CHANGED_WHEN = GETDATE() FROM INSERTED 
	WHERE TBUM_AD_COMMAND.GUID = INSERTED.GUID
GO
CREATE PROCEDURE [dbo].[PRDD_DELETE_GROUP](@pGROUP_ID INT)
AS
BEGIN TRY
	-- COMMON
	IF OBJECT_ID(N'dbo.TBDD_GROUPS_USER', N'U') IS NOT NULL 
		DELETE FROM TBDD_GROUPS_USER WHERE GROUP_ID = @pGROUP_ID
	IF OBJECT_ID(N'dbo.TBDD_GROUPS_CLIENT', N'U') IS NOT NULL 
		DELETE FROM TBDD_GROUPS_CLIENT WHERE GROUP_ID = @pGROUP_ID
	IF OBJECT_ID(N'dbo.TBDD_GROUPS_MODULES', N'U') IS NOT NULL 
		DELETE FROM TBDD_GROUPS_MODULES WHERE GROUP_ID = @pGROUP_ID

	-- FINALLY
	DELETE FROM TBDD_GROUPS WHERE GUID = @pGROUP_ID

	RETURN 0
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())
	RETURN -1
END CATCH

GO