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

	INSERT into @ConfigurationEntityTypes ([Id], [Title]) VALUES (16, N'TimeoutConfiguration');

	--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
	DECLARE @ConfigurationParameterTypes TABLE (Id int NOT NULL,Title nvarchar(max) NOT NULL);

	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (82, N'RetryCount')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (83, N'InitialSleep')
	INSERT into @ConfigurationParameterTypes (Id, Title) VALUES (84, N'PerStepSleep')

	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 TimeoutConfiguration (16)
	
	DECLARE @ConfigurationEntityParameters TABLE (Id int NOT NULL,EntityId int NOT NULL,ParameterId int not null);

	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 115, 16, 74)	--Timeout
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 111, 16, 78) --Method
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 112, 16, 82) --RetryCount
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 113, 16, 83)	--InitialSleep
	INSERT into @ConfigurationEntityParameters (Id, EntityId, ParameterId) VALUES ( 114, 16, 84)	--PerStepSleep
							 		
	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 3 groups for TimeoutConfiguration Entity
	DECLARE @ConfigurationGroups TABLE (EntityId int NOT NULL,GuidKey uniqueidentifier not null );
	
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (16, '63958901-2767-474D-BBA5-048A116E3FDF') --UdpEthernet 
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (16, '15ED933D-822E-4F6A-ADAF-A27C79339CB9') --UdpCellular
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (16, '9D000B52-206E-446B-8EC6-B89F857984B7') --Sms

	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.*;
	
	---------------------------------------------------------------
	---------------------------------------------------------------
	--05 - create 3 groups for Affiliated Entity		
					
	--UdpEthernet
	declare @configurationGroupIdUdpEthernet int
	select @configurationGroupIdUdpEthernet = id from ConfigurationGroups where guidkey='63958901-2767-474D-BBA5-048A116E3FDF'
		
	--UdpCellular
	declare @configurationGroupIdUdpCellular int
	select @configurationGroupIdUdpCellular = id from ConfigurationGroups where guidkey='15ED933D-822E-4F6A-ADAF-A27C79339CB9'

	--SMS
	declare @configurationGroupIdSms int
	select @configurationGroupIdSms = id from ConfigurationGroups where guidkey='9D000B52-206E-446B-8EC6-B89F857984B7'

	DECLARE @ConfigurationGroupParameters TABLE (GroupId int NOT NULL,ParameterId int not null, Value nvarchar(max));
	
	--UdpEthernet
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpEthernet,74,  N'10000'	-- Timeout im MS
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpEthernet,78,  N'UdpEthernet'	-- Method
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpEthernet,82,  N'1'		-- RetryCount
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpEthernet,83,  N'500'	-- InitialSleep im MS
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpEthernet,84,  N'500'	-- PerStepSleep

	--UdpCellular
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpCellular,74,  N'10000'	-- Timeout im MS
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpCellular,78,  N'UdpCellular'	-- Method
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpCellular,82,  N'1'		-- RetryCount
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpCellular,83,  N'500'	-- InitialSleep im MS
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdUdpCellular,84,  N'500'	-- PerStepSleep

	--SMS
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdSms,74,  N'59000'	-- Timeout im MS
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdSms,78,  N'Sms'	-- Method
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdSms,82,  N'1'		-- RetryCount
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdSms,83,  N'1000'	-- InitialSleep im MS
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @configurationGroupIdSms,84,  N'500'	-- PerStepSleep

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

	--UdpEthernet
	INSERT into @ConfigurationEntityTree (EntityTypeId, GroupId,						  MachineName,  ServiceName,			ServiceId,ModuleIdentifier,	ParentEntityId,	GuidKey) 
	select									16,			@configurationGroupIdUdpEthernet, N'MachineId', N'TimeoutConfiguration', N'',	  N'PanelCommands', NULL,			'F9292CC8-BF7D-48B0-A4A2-44C6676031D0'

	--UdpCellular
	INSERT into @ConfigurationEntityTree (EntityTypeId, GroupId,						  MachineName,  ServiceName,	 ServiceId,  ModuleIdentifier, ParentEntityId, GuidKey) 
	select									16,			@configurationGroupIdUdpCellular, N'MachineId', N'TimeoutConfiguration', N'',N'PanelCommands', NULL,			'F17B69A9-414A-49DB-A92B-E543A40791A5'
	
	--SMS
	INSERT into @ConfigurationEntityTree (EntityTypeId, GroupId,				 MachineName,  ServiceName,				ServiceId,	ModuleIdentifier, ParentEntityId, GuidKey) 
	select									16,			@configurationGroupIdSms,N'MachineId', N'TimeoutConfiguration', N'',		N'PanelCommands', NULL,			  'EBB1E6E5-D746-49F9-8E0E-2D9F1C307739'
	
	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.*;