  
 --update previous queue to OLD 
 update  ConfigurationEntityTree set ServiceName=ServiceName+'_old',ServiceId=ServiceId+'_old' where servicename in ('PanelCommandManagerPublisher','PanelCommandManagerSubscriber') 


--new Queue 

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

INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (1, 'F40BE20D-EC50-4A7F-81A7-8B1FDB7275B4') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (2, '9BB8385B-11B5-4E79-B353-450F27CAAC87') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (2, '9BAEBD30-1A88-45DC-A939-5AA27AA46347') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (3, 'FA778A1A-43CE-42D5-859B-BE65EF1EC795') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (3, '4EB974A2-3217-448D-8A65-8B5DDEB3DDB7') 

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='F40BE20D-EC50-4A7F-81A7-8B1FDB7275B4'
select @settingTypeIdExchange = Id from ConfigurationGroups where guidkey='9BB8385B-11B5-4E79-B353-450F27CAAC87'
select @settingTypeIdExchangeDLX = Id from ConfigurationGroups where guidkey='9BAEBD30-1A88-45DC-A939-5AA27AA46347'
select @settingTypeIdQueue = Id from ConfigurationGroups where guidkey='FA778A1A-43CE-42D5-859B-BE65EF1EC795'
select @settingTypeIdQueueDlx = Id from ConfigurationGroups where guidkey='4EB974A2-3217-448D-8A65-8B5DDEB3DDB7'

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdConnection, 1, N'TimerAlertHandlerPanelCommandManagerConnection')
	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'Ex04TimerAlertHandlerPanelCommandManager')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdExchange, 7, N'Delayed') --delayed messages (default value here is topic)
    INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdExchange, 8, N'false')

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdExchangeDLX, 1, N'zExDlx04TimerAlertHandlerPanelCommandManager')
	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'Queue04TimerAlertHandlerPanelCommandManager')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 9, N'DefaultBlWrapperManager')
	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'PanelCommandManager')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 15, N'zExDlx04TimerAlertHandlerPanelCommandManager')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 21, N'20')--instances

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueueDlx, 1, N'zQueueDlx04TimerAlertHandlerPanelCommandManager')
	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 );

--Timer
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 1, @settingTypeIdConnection, N'MachineId', N'PanelCommandManagerPublisher', N'PanelCommandManagerPublisherService', N'ServiceBroker', NULL,'35885F7C-385F-42A3-A123-641D3F449D85')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey)
VALUES ( 2, @settingTypeIdExchange,  N'MachineId', N'PanelCommandManagerPublisher', N'PanelCommandManagerPublisherService', N'ServiceBroker', NULL,'32DBD109-5A26-4AD7-A578-890788FC5A6A')
--AlertHandler																															 
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 1, @settingTypeIdConnection,  N'MachineId', N'PanelCommandManagerSubscriber', N'PanelCommandManagerSubscriberService', N'ServiceBroker', NULL,'E94B8914-5AD2-4A7B-A16D-A449F3D44A4A')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 2, @settingTypeIdExchange, N'MachineId', N'PanelCommandManagerSubscriber', N'PanelCommandManagerSubscriberService', N'ServiceBroker', NULL,'60107593-8E38-4FF4-99AD-11DDAB6D4C89')

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 @EntityIdQExchangeTCPListener int
declare @EntityIdQExchangeParser int

select @EntityIdQExchangeTCPListener = Id from  dbo.ConfigurationEntityTree where guidkey='32DBD109-5A26-4AD7-A578-890788FC5A6A'
select @EntityIdQExchangeParser = Id from  dbo.ConfigurationEntityTree where guidkey='60107593-8E38-4FF4-99AD-11DDAB6D4C89'

--Timer
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 3, @settingTypeIdQueue, N'MachineId', N'PanelCommandManagerPublisher', N'PanelCommandManagerPublisherService', N'ServiceBroker', @EntityIdQExchangeTCPListener,'9ADE4589-3BE1-4916-B078-3CD3C48A0BCA')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 2, @settingTypeIdExchangeDLX, N'MachineId', N'PanelCommandManagerPublisher', N'PanelCommandManagerPublisherService', N'ServiceBroker', @EntityIdQExchangeTCPListener,'A2EE984D-22D6-45D0-B00C-F3D2875D792D')
--AlertHandler
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 3, @settingTypeIdQueue, N'MachineId', N'PanelCommandManagerSubscriber', N'PanelCommandManagerSubscriberService', N'ServiceBroker', @EntityIdQExchangeParser,'CA6AC747-09CB-40E1-B1BB-4E498D481F57')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 2, @settingTypeIdExchangeDLX, N'MachineId', N'PanelCommandManagerSubscriber', N'PanelCommandManagerSubscriberService', N'ServiceBroker', @EntityIdQExchangeParser,'C9EA08EF-0C1C-44F2-B10A-A8B510793FBC')

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 @EntityIdQExchangeDLXTCPListener int
select @EntityIdQExchangeDLXTCPListener = Id from  dbo.ConfigurationEntityTree where guidkey='A2EE984D-22D6-45D0-B00C-F3D2875D792D'

declare @EntityIdQExchangeDLXParser int
select @EntityIdQExchangeDLXParser = Id from  dbo.ConfigurationEntityTree where guidkey='C9EA08EF-0C1C-44F2-B10A-A8B510793FBC'

--Timer
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 3, @settingTypeIdQueueDlx, N'MachineId', N'PanelCommandManagerPublisher', N'PanelCommandManagerPublisherService', N'ServiceBroker', @EntityIdQExchangeDLXTCPListener,'3D2B36B0-A889-4884-A242-26DF6BDACFB2')
--AlertHandler
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
VALUES ( 3, @settingTypeIdQueueDlx, N'MachineId', N'PanelCommandManagerSubscriber', N'PanelCommandManagerSubscriberService', N'ServiceBroker', @EntityIdQExchangeDLXParser,'FF147534-4B6C-4C29-9E39-1A3BD631DF7E')

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.*;
