  
 --update previous queue to OLD 
 update  ConfigurationEntityTree set ServiceName=ServiceName+'_old',ServiceId=ServiceId+'_old' where serviceid='EUPMainEventService' and guidkey is null


--new Queue EUPMainEventService

------------New GROUPS for EUPMainEventService queue-----------------------------------------------------
DECLARE @ConfigurationGroups TABLE (EntityId int NOT NULL,GuidKey uniqueidentifier not null );

INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (1, '4105385F-F71E-4792-8918-A3439E41ADB2') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (2, '1895D02D-0030-47C0-AB5B-52CFC003E0C7') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (2, '89589EFB-AB4A-488B-ADF7-9D90E54A3BE3') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (3, '3FEC85E7-0F77-49B2-BA77-AF831AD1AD17') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (3, 'CF5FB7E6-F6E6-4E8C-ABB6-C4BFEC1A3F29') 

MERGE INTO dbo.ConfigurationGroups AS TARGET  
        USING @ConfigurationGroups AS SOURCE  
        ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
        WHEN MATCHED THEN    
                UPDATE SET entityid = SOURCE.entityid
        WHEN NOT MATCHED BY TARGET THEN   
                INSERT (EntityId,GuidKey) VALUES (Source.EntityId,upper(source.GuidKey)) 
OUTPUT $action, SOURCE.*;
---------------------------------------------------------------------------------------------------------

DECLARE @ConfigurationGroupParameters TABLE (GroupId int NOT NULL,ParameterId int not null, Value nvarchar(max));

declare @settingTypeIdConnection int
declare @settingTypeIdExchange int
declare @settingTypeIdExchangeDLX int
declare @settingTypeIdQueue int
declare @settingTypeIdQueueDlx int

select @settingTypeIdConnection = Id from ConfigurationGroups where guidkey='4105385F-F71E-4792-8918-A3439E41ADB2'
select @settingTypeIdExchange = Id from ConfigurationGroups where guidkey='1895D02D-0030-47C0-AB5B-52CFC003E0C7'
select @settingTypeIdExchangeDLX = Id from ConfigurationGroups where guidkey='89589EFB-AB4A-488B-ADF7-9D90E54A3BE3'
select @settingTypeIdQueue = Id from ConfigurationGroups where guidkey='3FEC85E7-0F77-49B2-BA77-AF831AD1AD17'
select @settingTypeIdQueueDlx = Id from ConfigurationGroups where guidkey='CF5FB7E6-F6E6-4E8C-ABB6-C4BFEC1A3F29'

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdConnection, 1, N'ADLParserAlertHandlerConnection')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdConnection, 2, N'x.x.x.x')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdConnection, 3, N'Mozart')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdConnection, 4, N'123123')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdConnection, 5, N'test')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdConnection, 6, N'-1')

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdExchange, 1, N'Ex03EupMainService')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdExchange, 7, N'Topic')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdExchange, 8, N'false')

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdExchangeDLX, 1, N'zExDlx03EupMainService')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdExchangeDLX, 7, N'Fanout')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdExchangeDLX, 8, N'false')

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 1, N'Queue03EupMainService')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 9, N'MultiBlWrapperManager')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 10, N'true')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 11, N'false')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 12, N'false')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 13, N'')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 14, N'EventUploaderServiceBrokerMessageConsumer')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 15, N'zExDlx03EupMainService')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 21, N'1')--instances
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 93, N'3')--priority

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueueDlx, 1, N'zQueueDlx03EupMainService')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueueDlx, 9, N'')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueueDlx, 10, N'true')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueueDlx, 11, N'false')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueueDlx, 12, N'false')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueueDlx, 13, N'')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueueDlx, 14, N'')


MERGE INTO dbo.ConfigurationGroupParameters AS TARGET  
        USING @ConfigurationGroupParameters AS SOURCE  
        ON TARGET.GroupId  = SOURCE.GroupId
        AND TARGET.ParameterId =  SOURCE.ParameterId
        WHEN MATCHED THEN    
                UPDATE SET Value=source.Value
        WHEN NOT MATCHED BY TARGET THEN   
                INSERT ( GroupId,ParameterId,Value) VALUES (Source.GroupId, SOURCE.ParameterId,source.Value)  
OUTPUT $action, SOURCE.*;

---------------Fill ConfigurationEntityTree-------------------------------------------------------------

--tree
DECLARE @ConfigurationEntityTree TABLE (EntityTypeId int not null,GroupId int NOT NULL, MachineName nvarchar(max), ServiceName nvarchar(max), ServiceId nvarchar(max), 
										ModuleIdentifier nvarchar(max),ParentEntityId int null,GuidKey uniqueidentifier not null );

--AlertHandler
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 1, @settingTypeIdConnection, N'MachineId', N'AlertHandler', N'EUPMainEventService', N'ServiceBroker', NULL,'F3225922-3A84-4DB5-BFAE-40B37D01197B')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 2, @settingTypeIdExchange,  N'MachineId', N'AlertHandler', N'EUPMainEventService', N'ServiceBroker', NULL,'4909C1A3-338E-46C5-B389-1780CC8B6683')
--Parser																															 
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 1, @settingTypeIdConnection,  N'MachineId', N'Parser', N'EUPMainEventService', N'ServiceBroker', NULL,'2B450EF8-3145-4B6B-A9AD-FCB3C8E4FAB3')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 2, @settingTypeIdExchange, N'MachineId', N'Parser', N'EUPMainEventService', N'ServiceBroker', NULL,'23227ADF-38C7-48C0-97D2-99AE68CC4147')
--Maximus																															 
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 1, @settingTypeIdConnection,  N'MachineId', N'Maximus', N'EUPMainEventService', N'ServiceBroker', NULL,'C34E7A2A-7A22-4A21-9F86-89AF154EE2E9')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 2, @settingTypeIdExchange, N'MachineId', N'Maximus', N'EUPMainEventService', N'ServiceBroker', NULL,'8A06BAD9-DD2C-4C8E-A3EE-FB5702E80595')
--EventUploader																															 
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 1, @settingTypeIdConnection,  N'MachineId', N'EventUploader', N'EUPMainEventService', N'ServiceBroker', NULL,'403DDB4E-20D4-4BC9-8349-EF5D126F00BA')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 2, @settingTypeIdExchange, N'MachineId', N'EventUploader', N'EUPMainEventService', N'ServiceBroker', NULL,'F7A2B802-46AC-47BB-A6A7-2E48780DA462')

MERGE INTO dbo.ConfigurationEntityTree AS TARGET  
        USING @ConfigurationEntityTree AS SOURCE  
        ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
        WHEN MATCHED THEN    
                UPDATE SET  MachineName=source.MachineName, ServiceName=source.ServiceName, ServiceId=source.ServiceId, 
                ModuleIdentifier=source.ModuleIdentifier, ParentEntityId=source.ParentEntityId
        WHEN NOT MATCHED BY TARGET THEN   
                INSERT ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId,GuidKey) 
                VALUES ( source.EntityTypeId, source.GroupId, source.MachineName, source.ServiceName, source.ServiceId, source.ModuleIdentifier, source.ParentEntityId, upper(source.GuidKey))
OUTPUT $action, SOURCE.*;

--clean to use previous as Parent
delete from @ConfigurationEntityTree

declare @EntityIdQExchangeAlertHandler int
declare @EntityIdQExchangeParser int
declare @EntityIdQExchangeMaximus int
declare @EntityIdQExchangeEUP int

select @EntityIdQExchangeAlertHandler = Id from  dbo.ConfigurationEntityTree where guidkey='4909C1A3-338E-46C5-B389-1780CC8B6683'
select @EntityIdQExchangeParser = Id from  dbo.ConfigurationEntityTree where guidkey='23227ADF-38C7-48C0-97D2-99AE68CC4147'
select @EntityIdQExchangeMaximus = Id from  dbo.ConfigurationEntityTree where guidkey='8A06BAD9-DD2C-4C8E-A3EE-FB5702E80595'
select @EntityIdQExchangeEUP = Id from  dbo.ConfigurationEntityTree where guidkey='F7A2B802-46AC-47BB-A6A7-2E48780DA462'

--AlertHandler
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 3, @settingTypeIdQueue, N'MachineId', N'AlertHandler', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeAlertHandler,'4ACEB226-F54E-474F-ACB9-5C8EAC69765E')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 2, @settingTypeIdExchangeDLX, N'MachineId', N'AlertHandler', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeAlertHandler,'734E6875-6E27-42C1-9F61-4E5D72F7AFD3')
--Parser
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 3, @settingTypeIdQueue, N'MachineId', N'Parser', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeParser,'0133A5DF-D531-42E2-8ACC-47F6E9CF9CAF')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 2, @settingTypeIdExchangeDLX, N'MachineId', N'Parser', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeParser,'62C618B0-02F9-4FDD-A251-4B76FF5A1017')
--Maximus
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 3, @settingTypeIdQueue, N'MachineId', N'Maximus', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeMaximus,'EDE3030B-BC99-422C-B08E-E1D1F79CA4F1')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 2, @settingTypeIdExchangeDLX, N'MachineId', N'Maximus', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeMaximus,'3EE5BA1C-67E7-4D34-86B6-3B3AECC97923')
--EventUploader
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 3, @settingTypeIdQueue, N'MachineId', N'EventUploader', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeEUP,'0DE5B617-5161-42A1-A9D0-AB3DD1517C52')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 2, @settingTypeIdExchangeDLX, N'MachineId', N'EventUploader', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeEUP,'7D08624F-1F2D-4A8C-8A7F-BCC0151F58C9')


MERGE INTO dbo.ConfigurationEntityTree AS TARGET  
        USING @ConfigurationEntityTree AS SOURCE  
        ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
             
        WHEN MATCHED THEN    
                UPDATE SET  MachineName=source.MachineName, ServiceName=source.ServiceName, ServiceId=source.ServiceId, 
                ModuleIdentifier=source.ModuleIdentifier, ParentEntityId=source.ParentEntityId
        WHEN NOT MATCHED BY TARGET THEN   
                INSERT ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId,GuidKey) 
                VALUES ( source.EntityTypeId, source.GroupId, source.MachineName, source.ServiceName, source.ServiceId, source.ModuleIdentifier, source.ParentEntityId, upper(source.GuidKey))
OUTPUT $action, SOURCE.*;

--clean to use previous as Parent
delete from @ConfigurationEntityTree

declare @EntityIdQExchangeDLXAlertHandler int
select @EntityIdQExchangeDLXAlertHandler = Id from  dbo.ConfigurationEntityTree where guidkey='734E6875-6E27-42C1-9F61-4E5D72F7AFD3'

declare @EntityIdQExchangeDLXParser int
select @EntityIdQExchangeDLXParser = Id from  dbo.ConfigurationEntityTree where guidkey='62C618B0-02F9-4FDD-A251-4B76FF5A1017'

declare @EntityIdQExchangeDLXMaximus int
select @EntityIdQExchangeDLXMaximus = Id from  dbo.ConfigurationEntityTree where guidkey='3EE5BA1C-67E7-4D34-86B6-3B3AECC97923'

declare @EntityIdQExchangeDLXEUP int
select @EntityIdQExchangeDLXEUP = Id from  dbo.ConfigurationEntityTree where guidkey='7D08624F-1F2D-4A8C-8A7F-BCC0151F58C9'

--AlertHandler
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 3, @settingTypeIdQueueDlx, N'MachineId', N'AlertHandler', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeDLXAlertHandler,'43778DFC-8864-46CB-A910-4B3A06A2A654')
--Parser
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey)
VALUES ( 3, @settingTypeIdQueueDlx, N'MachineId', N'Parser', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeDLXParser,'267105BB-07A1-47E5-89F0-7673C6A5AF36')
--Maximus
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey)
VALUES ( 3, @settingTypeIdQueueDlx, N'MachineId', N'Maximus', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeDLXMaximus,'16479B7D-226F-4AEA-9248-C08BEBDC24EF')
--EventUploader
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey)
VALUES ( 3, @settingTypeIdQueueDlx, N'MachineId', N'EventUploader', N'EUPMainEventService', N'ServiceBroker', @EntityIdQExchangeDLXEUP,'9C00568F-FF13-4007-8250-5D70A62BAB26')

MERGE INTO dbo.ConfigurationEntityTree AS TARGET  
        USING @ConfigurationEntityTree AS SOURCE  
        ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
             
        WHEN MATCHED THEN    
                UPDATE SET  MachineName=source.MachineName, ServiceName=source.ServiceName, ServiceId=source.ServiceId, 
                ModuleIdentifier=source.ModuleIdentifier, ParentEntityId=source.ParentEntityId
        WHEN NOT MATCHED BY TARGET THEN   
                INSERT ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId,GuidKey) 
                VALUES ( source.EntityTypeId, source.GroupId, source.MachineName, source.ServiceName, source.ServiceId, source.ModuleIdentifier, source.ParentEntityId, upper(source.GuidKey))
OUTPUT $action, SOURCE.*;
