  
 --update previous queue to OLD 
 update  ConfigurationEntityTree set ServiceName=ServiceName+'_old',ServiceId=ServiceId+'_old' where servicename in ('ACS','parser') and serviceid='AcsADLParserService' and guidkey is null


--new Queue AcsADLParserService

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

INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (1, 'E479220C-099E-4B96-93D5-FE9256DA984C') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (2, 'AEBDAB27-677C-4A9B-94E2-DB4A715B45DF') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (2, 'EEDF584C-BAB2-49B1-AE85-D404CA1E5444') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (3, 'D7815B6A-EAB3-4E39-AABB-69C3BEAC53CE') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (3, 'B41B045A-0C02-4E3A-B285-FBC081CFF27E') 

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='E479220C-099E-4B96-93D5-FE9256DA984C'
select @settingTypeIdExchange = Id from ConfigurationGroups where guidkey='AEBDAB27-677C-4A9B-94E2-DB4A715B45DF'
select @settingTypeIdExchangeDLX = Id from ConfigurationGroups where guidkey='EEDF584C-BAB2-49B1-AE85-D404CA1E5444'
select @settingTypeIdQueue = Id from ConfigurationGroups where guidkey='D7815B6A-EAB3-4E39-AABB-69C3BEAC53CE'
select @settingTypeIdQueueDlx = Id from ConfigurationGroups where guidkey='B41B045A-0C02-4E3A-B285-FBC081CFF27E'

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdConnection, 1, N'TCPListenerParserConnection')
	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'Ex01TCPListenerParser')
	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'zExDlx01TCPListenerParser')
	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'Queue01TCPListenerParser')
	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'ADLParser')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 15, N'zExDlx01TCPListenerParser')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 21, N'10')--instances
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 93, N'3')--priority

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

--TCPListener
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 1, @settingTypeIdConnection, N'MachineId', N'ACS', N'AcsADLParserService', N'ServiceBroker', NULL,'3442CB53-02BB-4E3C-A89E-A9618BCD0FA1')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 2, @settingTypeIdExchange,  N'MachineId', N'ACS', N'AcsADLParserService', N'ServiceBroker', NULL,'69554137-317F-4CE4-BEEB-25ACCF423D1D')
--Parser																															 
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 1, @settingTypeIdConnection,  N'MachineId', N'Parser', N'AcsADLParserService', N'ServiceBroker', NULL,'B2D3B5BA-5D3F-4D56-B069-0765E96E82E3')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 2, @settingTypeIdExchange, N'MachineId', N'Parser', N'AcsADLParserService', N'ServiceBroker', NULL,'DA9D7D09-B2E5-4260-8F24-46DA46F01F0D')

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='69554137-317F-4CE4-BEEB-25ACCF423D1D'
select @EntityIdQExchangeParser = Id from  dbo.ConfigurationEntityTree where guidkey='DA9D7D09-B2E5-4260-8F24-46DA46F01F0D'

--TCPListener
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 3, @settingTypeIdQueue, N'MachineId', N'ACS', N'AcsADLParserService', N'ServiceBroker', @EntityIdQExchangeTCPListener,'0A449953-A578-4D2D-8572-EC3691C71620')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 2, @settingTypeIdExchangeDLX, N'MachineId', N'ACS', N'AcsADLParserService', N'ServiceBroker', @EntityIdQExchangeTCPListener,'3F923B3F-74AB-4B7E-8D98-4DE8900AC580')
--Parser
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 3, @settingTypeIdQueue, N'MachineId', N'Parser', N'AcsADLParserService', N'ServiceBroker', @EntityIdQExchangeParser,'4C327A69-54C6-46B6-BDB4-1015D5416BC7')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 2, @settingTypeIdExchangeDLX, N'MachineId', N'Parser', N'AcsADLParserService', N'ServiceBroker', @EntityIdQExchangeParser,'808FCBA0-E8E1-4B09-9866-7144CF408BAB')

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='3F923B3F-74AB-4B7E-8D98-4DE8900AC580'

declare @EntityIdQExchangeDLXParser int
select @EntityIdQExchangeDLXParser = Id from  dbo.ConfigurationEntityTree where guidkey='808FCBA0-E8E1-4B09-9866-7144CF408BAB'

--TCPListener
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 3, @settingTypeIdQueueDlx, N'MachineId', N'ACS', N'AcsADLParserService', N'ServiceBroker', @EntityIdQExchangeDLXTCPListener,'F389CA05-7595-4A41-91F3-6817FA491E0F')
--Parser
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 3, @settingTypeIdQueueDlx, N'MachineId', N'Parser', N'AcsADLParserService', N'ServiceBroker', @EntityIdQExchangeDLXParser,'09A36F6E-1B70-4587-B013-C3836BF08C6A')

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