--new Queue SmsSending

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

INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (1, 'F5D4F0F9-40D8-481E-81A6-08408A4B279B') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (2, '757B3B16-D542-4F1A-8F61-0641B8F1028C') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (2, '4F69D216-44A9-47C6-B626-986F03E4434F') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (3, '243DC5A7-64F3-427E-BFEC-E6661AFE7BB0') 
INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (3, '9E0B5B8A-CEFF-452C-A50E-410AA629D3A5') 

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='F5D4F0F9-40D8-481E-81A6-08408A4B279B'
select @settingTypeIdExchange = Id from ConfigurationGroups where guidkey='757B3B16-D542-4F1A-8F61-0641B8F1028C'
select @settingTypeIdExchangeDLX = Id from ConfigurationGroups where guidkey='4F69D216-44A9-47C6-B626-986F03E4434F'
select @settingTypeIdQueue = Id from ConfigurationGroups where guidkey='243DC5A7-64F3-427E-BFEC-E6661AFE7BB0'
select @settingTypeIdQueueDlx = Id from ConfigurationGroups where guidkey='9E0B5B8A-CEFF-452C-A50E-410AA629D3A5'

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdConnection, 1, N'SmsSendingConnection')
	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'ExSmsSending')
	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'ExDlxSmsSending')
	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'QueueSmsSending')
	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'SmsSending')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 15, N'ExDlxSmsSending')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES (@settingTypeIdQueue, 21, N'5')--instances

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

--A
----Subscriber
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 1, @settingTypeIdConnection, N'MachineId', N'SmsSendingSubscriber', N'SmsSendingSubscriberService', N'ServiceBroker', NULL,'6E82D757-E2F6-4F26-AD55-4710BEAB64B6')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 2, @settingTypeIdExchange,  N'MachineId', N'SmsSendingSubscriber', N'SmsSendingSubscriberService', N'ServiceBroker', NULL,'87419C96-131A-4CC8-8BA8-82E5D821B6FB')
----Publisher
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 1, @settingTypeIdConnection, N'MachineId', N'SmsSendingPublisher', N'SmsSendingPublisherService', N'ServiceBroker', NULL,'453CAA60-2119-421E-9210-55BA67E17503')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 2, @settingTypeIdExchange,  N'MachineId', N'SmsSendingPublisher', N'SmsSendingPublisherService', N'ServiceBroker', NULL,'22D98C06-483D-4407-9772-0D8C9B660BEB')

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

--B
----Subscriber
declare @EntityIdQExchangeSubscriber int
select @EntityIdQExchangeSubscriber = Id from  dbo.ConfigurationEntityTree where guidkey='87419C96-131A-4CC8-8BA8-82E5D821B6FB'

INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 3, @settingTypeIdQueue, N'MachineId', N'SmsSendingSubscriber', N'SmsSendingSubscriberService', N'ServiceBroker', @EntityIdQExchangeSubscriber,'A7F750B0-04FD-4306-9A0E-7DA556A3913D')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 2, @settingTypeIdExchangeDLX, N'MachineId', N'SmsSendingSubscriber', N'SmsSendingSubscriberService', N'ServiceBroker', @EntityIdQExchangeSubscriber,'EBE84D18-E251-4483-A0AC-58FE932B35CE')

----Publisher
declare @EntityIdQExchangePublisher int
select @EntityIdQExchangePublisher = Id from  dbo.ConfigurationEntityTree where guidkey='22D98C06-483D-4407-9772-0D8C9B660BEB'

INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 3, @settingTypeIdQueue, N'MachineId', N'SmsSendingPublisher', N'SmsSendingPublisherService', N'ServiceBroker', @EntityIdQExchangePublisher,'7EEAD554-4D02-4455-88C9-6133F55B2B14')
INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 2, @settingTypeIdExchangeDLX, N'MachineId', N'SmsSendingPublisher', N'SmsSendingPublisherService', N'ServiceBroker', @EntityIdQExchangePublisher,'C7230031-5E67-4D90-8940-D3BAD2EA32BA')

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

--C
----Subscriber
declare @EntityIdQExchangeSubscriberDLX int
select @EntityIdQExchangeSubscriberDLX = Id from  dbo.ConfigurationEntityTree where guidkey='EBE84D18-E251-4483-A0AC-58FE932B35CE'

INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 3, @settingTypeIdQueueDlx, N'MachineId', N'SmsSendingSubscriber', N'SmsSendingSubscriberService', N'ServiceBroker', @EntityIdQExchangeSubscriberDLX,'41830662-E4E2-4F05-A596-C991B9875412')

----Subscriber
declare @EntityIdQExchangePublisherDLX int
select @EntityIdQExchangePublisherDLX = Id from  dbo.ConfigurationEntityTree where guidkey='C7230031-5E67-4D90-8940-D3BAD2EA32BA'

INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) VALUES ( 3, @settingTypeIdQueueDlx, N'MachineId', N'SmsSendingPublisher', N'SmsSendingPublisherService', N'ServiceBroker', @EntityIdQExchangePublisherDLX,'D8395387-CA97-48F0-93E6-A30BFB0A7120')


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

