
	declare @configurationGroupIdAffiliatedGuid uniqueidentifier = 'b696e645-a597-4990-a996-99289ad86387'
	declare @configurationGroupIdAffiliated_HBGuid uniqueidentifier = 'A58FE193-433E-45A6-8B6C-BE69431ED1E3'

	declare @configurationGroupIdAffiliatedAccountGetGuid uniqueidentifier = 'E17CAD2D-EFC2-4773-B03A-861B20B88F90'
	declare @configurationGroupIdAffiliatedAccountSetGuid uniqueidentifier = '8EE6AE4F-1ED6-4BE7-B7AC-B947AE8FAB65'

	declare @configurationGroupIdAffiliatedAlarmTokenGuid uniqueidentifier = 'AC624CA5-3C20-4375-B032-DBAB2D869969'
	declare @configurationGroupIdAffiliatedAccountTokenGuid uniqueidentifier = 'AB32AEC8-B37F-4DF1-83D5-D360204BB131'

	--Delete Unnecessary parameters from ConfigurationEntityParameters
	--35 = SslCheck
	--72 = LoginUserName
	--73 = LoginPassword
	--74 = Timeout
	delete from ConfigurationEntityParameters
	where EntityId=14 and ParameterId in (35,72,73,74)

	--Delete Unnecessary parameters from ConfigurationGroupParameters
	declare @configurationGroupIdAffiliated int
	select @configurationGroupIdAffiliated = id from ConfigurationGroups where guidkey=@configurationGroupIdAffiliatedGuid

	declare @configurationGroupIdAffiliated_HB int
	select @configurationGroupIdAffiliated_HB = id from ConfigurationGroups where guidkey=@configurationGroupIdAffiliated_HBGuid

	delete from ConfigurationGroupParameters
	where GroupId in (@configurationGroupIdAffiliated,@configurationGroupIdAffiliated_HB) and ParameterId in (35,72,73,74)

	--Configuration Tables - add Affiliated parameters:
	--01
	--AffiliatedToken ENTITY:
	DECLARE @ConfigurationEntityTypes TABLE ([Id] [int] NOT NULL,[Title] [nvarchar](max) NOT NULL);

	INSERT into @ConfigurationEntityTypes ([Id], [Title]) VALUES (15, N'AffiliatedToken');

	--select * from  dbo.[ConfigurationEntityTypes]
	SET IDENTITY_INSERT [dbo].[ConfigurationEntityTypes] ON;

		MERGE INTO dbo.[ConfigurationEntityTypes] AS TARGET  
			USING @ConfigurationEntityTypes AS SOURCE  
			ON TARGET.Id = SOURCE.Id	
			WHEN MATCHED THEN    
				UPDATE SET Title = SOURCE.Title
			WHEN NOT MATCHED BY TARGET THEN   
				INSERT ([Id], [Title]) VALUES (SOURCE.Id, SOURCE.Title)  
		OUTPUT $action, SOURCE.*;
		
	SET IDENTITY_INSERT [dbo].[ConfigurationEntityTypes] OFF;


	---------------------------------------------------------------
	---------------------------------------------------------------
	--02 new Parameters for AffiliatedToken
	DECLARE @ConfigurationParameterTypes TABLE (Id int NOT NULL,Title nvarchar(max) NOT NULL);
	
	--Affiliated
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (79, N'Grant')

	SET IDENTITY_INSERT dbo.ConfigurationParameterTypes ON

	MERGE INTO dbo.ConfigurationParameterTypes AS TARGET  
		USING @ConfigurationParameterTypes AS SOURCE  
		ON TARGET.Id = SOURCE.Id
		WHEN MATCHED THEN    
			UPDATE SET Title = SOURCE.Title
		WHEN NOT MATCHED BY TARGET THEN   
			INSERT (Id, Title) VALUES (SOURCE.Id, SOURCE.Title)  
	OUTPUT $action, SOURCE.*;

	SET IDENTITY_INSERT dbo.ConfigurationParameterTypes OFF


	---------------------------------------------------------------
	---------------------------------------------------------------
	--03 define parameters for entity AffiliatedToken (15)
	
	DECLARE @ConfigurationEntityParameters TABLE (Id int NOT NULL,EntityId int NOT NULL,ParameterId int not null);

	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 104, 15, 73) --Password
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 105, 15, 75)	--Url
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 106, 15, 72)	--UserName
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 107,15,76)	--ClientId
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 108,15,77)	--ContentType
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 109,15,78)	--Method
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 110,15,79)	--Grant
							 		
	MERGE INTO dbo.ConfigurationEntityParameters AS TARGET  							 
		USING @ConfigurationEntityParameters AS SOURCE  								 
		ON TARGET.Id = SOURCE.Id
		WHEN MATCHED THEN    
			UPDATE SET entityid = SOURCE.entityid,ParameterId=source.ParameterId 
		WHEN NOT MATCHED BY TARGET THEN   
			INSERT (Id, EntityId,ParameterId) VALUES (SOURCE.Id,Source.EntityId, SOURCE.ParameterId)  
	OUTPUT $action, SOURCE.*;    

	---------------------------------------------------------------
	---------------------------------------------------------------
	--04 - create groups for 
	--	Affiliated account Entity
	--	Affiliated token entity
	DECLARE @ConfigurationGroups TABLE (EntityId int NOT NULL,GuidKey uniqueidentifier not null );
	
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (14, @configurationGroupIdAffiliatedAccountGetGuid) --Affiliated account get
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (14, @configurationGroupIdAffiliatedAccountSetGuid) --Affiliated account set

	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (15, @configurationGroupIdAffiliatedAlarmTokenGuid) --Affiliated alarm token
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (15, @configurationGroupIdAffiliatedAccountTokenGuid) --Affiliated account token

	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 @configurationGroupIdAffiliatedAccountGet int
	declare @configurationGroupIdAffiliatedAccountSet int
	declare @configurationGroupIdAffiliatedAlarmToken int
	declare @configurationGroupIdAffiliatedAccountToken int

	select @configurationGroupIdAffiliatedAccountGet = id from ConfigurationGroups where guidkey=@configurationGroupIdAffiliatedAccountGetGuid
	select @configurationGroupIdAffiliatedAccountSet = id from ConfigurationGroups where guidkey=@configurationGroupIdAffiliatedAccountSetGuid
	select @configurationGroupIdAffiliatedAlarmToken = id from ConfigurationGroups where guidkey=@configurationGroupIdAffiliatedAlarmTokenGuid
	select @configurationGroupIdAffiliatedAccountToken = id from ConfigurationGroups where guidkey=@configurationGroupIdAffiliatedAccountTokenGuid
	---------------------------------------------------------------
	---------------------------------------------------------------
	--05 - create groups for Affiliated Entities
		
	DECLARE @ConfigurationGroupParameters TABLE (GroupId int NOT NULL,ParameterId int not null, Value nvarchar(max));
	
	--Affiliated alarm
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,75,  N'https://alarm192.alertmessage.com/api/{0}'		-- N'Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,76,  N'affiliated-alarm'					-- N'ClientId')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,77,  N'application/x-www-form-urlencoded'	-- N'ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated,78,  N'POST'								-- N'Method')
	
	--Affiliated alarm HB
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,75,  N'https://alarm192.alertmessage.com/api/{0}'		-- N'HB_Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,76,  N'affiliated-alarm'					-- N'HB_ClientId')
    INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,77,  N'application/x-www-form-urlencoded'-- N'HB_ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliated_HB,78,  N'POST'								-- N'HB_Method')

	--Affiliated account Get
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountGet,75,  N'https://sb.alertmessage.com/acct/{0}'		-- N'Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountGet,76,  N'affiliated-api'					-- N'ClientId')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountGet,77,  N'application/x-www-form-urlencoded'	-- N'ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountGet,78,  N'GET'								-- N'Method')

	--Affiliated account Set
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountSet,75,  N'https://sb.alertmessage.com/acct/{0}'		-- N'Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountSet,76,  N'affiliated-api'					-- N'ClientId')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountSet,77,  N'application/x-www-form-urlencoded'	-- N'ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountSet,78,  N'POST'								-- N'Method')

	--Affiliated alarm token
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAlarmToken,75,  N'https://alarm192.alertmessage.com/token'	-- N'Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAlarmToken,76,  N'affiliated-api'					-- N'ClientId')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAlarmToken,77,  N'application/x-www-form-urlencoded'	-- N'ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAlarmToken,78,  N'POST'								-- N'Method')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAlarmToken,73,  N'484*fD@wrTT'						-- N'LoginPassword')--
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAlarmToken,72,  N'Essenceapi'						-- N'LoginUserName')--
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAlarmToken,79,  N'password'							-- N'Grant')--

	--Affiliated account token
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountToken,75,  N'https://sb.alertmessage.com/token'		-- N'Url')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountToken,76,  N'affiliated-api'					-- N'ClientId')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountToken,77,  N'application/x-www-form-urlencoded'	-- N'ContentType')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountToken,78,  N'POST'								-- N'Method')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountToken,73,  N'(894FtuEE3@'						-- N'LoginPassword')--
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountToken,72,  N'Essenceapi'						-- N'LoginUserName')--
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdAffiliatedAccountToken,79,  N'password'							-- N'Grant')--

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


	---------------------------------------------------------------
	---------------------------------------------------------------
	--06: ConfigurationEntityTree
	--Set the identifier in the tree for the instance
	
	declare @ConfigurationEntityTreeIdAffiliatedAlarmGuid uniqueidentifier = 'A4E36C1F-6B1F-4029-BE7C-7AE97F95DDF1'

	select @configurationGroupIdAffiliated = id from ConfigurationGroups where guidkey=@configurationGroupIdAffiliatedGuid

	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 );


	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 14, @configurationGroupIdAffiliated, N'MachineId', N'Affiliated', N'AffiliatedDefault',N'AffiliatedAlarm', NULL, @ConfigurationEntityTreeIdAffiliatedAlarmGuid
	
	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.*;

	---------------------------------------------------------------
	--06: ConfigurationEntityTree
	--	Children entities

	--delete temp table from prev run
	delete from @ConfigurationEntityTree

	declare @ConfigurationEntityTreeIdAffiliatedParentId int
	select @ConfigurationEntityTreeIdAffiliatedParentId = id from ConfigurationEntityTree where guidkey=@ConfigurationEntityTreeIdAffiliatedAlarmGuid

	--Affiliated alarm Hb
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 14, @configurationGroupIdAffiliated_HB, N'MachineId', N'Affiliated', N'AffiliatedDefault',N'AffiliatedAlarmHb',@ConfigurationEntityTreeIdAffiliatedParentId, 'FA762A1D-678B-4421-84C1-6DB86814FAE4'

	--Affiliated Account Get
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 14, @configurationGroupIdAffiliatedAccountGet, N'MachineId', N'Affiliated', N'AffiliatedDefault',N'AffiliatedAccountGet',@ConfigurationEntityTreeIdAffiliatedParentId, 'A8C32B3A-DA25-4FAD-94CB-214F36B59E6B'

	--Affiliated Account Set
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 14, @configurationGroupIdAffiliatedAccountSet, N'MachineId', N'Affiliated', N'AffiliatedDefault',N'AffiliatedAccountSet',@ConfigurationEntityTreeIdAffiliatedParentId, '704B827B-818D-40E6-A29C-22D5D14C0B85'

	--Affiliated alarm token
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 15, @configurationGroupIdAffiliatedAlarmToken, N'MachineId', N'Affiliated', N'AffiliatedDefault',N'AffiliatedAlarmToken',@ConfigurationEntityTreeIdAffiliatedParentId, 'B19F4557-1AB3-4C80-8436-8BA233A1E1BA'

	--Affiliated account token
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) 
	select 15, @configurationGroupIdAffiliatedAccountToken, N'MachineId', N'Affiliated', N'AffiliatedDefault',N'AffiliatedAccountToken',@ConfigurationEntityTreeIdAffiliatedParentId, '1BD80278-2A44-489E-B8FE-AE41908CAA2C'

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