--04: ConfigurationGroups
--	Create Instance of the group 
	DECLARE @ConfigurationGroups TABLE (EntityId int NOT NULL,GuidKey uniqueidentifier not null );

	--Job
	INSERT into @ConfigurationGroups (EntityId, GuidKey) VALUES ( 4,'A1FFA0EA-4080-4378-8241-956882659E53')
	--Job parameters
	INSERT into @ConfigurationGroups (EntityId, GuidKey) VALUES ( 5,'2EFDBB60-42AC-47FF-A954-0B20B7C11397')

	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 @configurationGroupIdJob int
	select @configurationGroupIdJob = id from ConfigurationGroups where guidkey='A1FFA0EA-4080-4378-8241-956882659E53'

		declare @configurationGroupIdJobParameters int
	select @configurationGroupIdJobParameters = id from ConfigurationGroups where guidkey='2EFDBB60-42AC-47FF-A954-0B20B7C11397'

	--05: ConfigurationGroupParameters
	--	Fill the group instance
	DECLARE @ConfigurationGroupParameters TABLE (GroupId int NOT NULL,ParameterId int not null, Value nvarchar(max));

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES ( @configurationGroupIdJob, 1, N'JobPushNotificationGroupDispatch')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES ( @configurationGroupIdJob, 16, N'PushNotificationGroupDispatchUmbrellaTimerJob')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES ( @configurationGroupIdJob, 17, N'1')

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 1, N'ParametersPushNotificationGroupDispatch')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 18, N'6660') --Interval 111 minutes
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 19, N'1')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 20, N'100')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 27, N'com.esc.umbrella')
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) VALUES	( @configurationGroupIdJobParameters, 33, N'Apns')

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

	--Job
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) select 4, @configurationGroupIdJob, N'MachineId', N'PushNotificationGroupDispatch', N'PushNotificationGroupDispatchUmbrellaTimerJob',N'', NULL, 'A503835B-22E0-4BDA-B296-21ED9CEF99F1'

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


	-- delete values from temp table
	delete from @ConfigurationEntityTree
	
	declare @treeJobId int
	select @treeJobId = id from ConfigurationEntityTree where guidkey='A503835B-22E0-4BDA-B296-21ED9CEF99F1'

	--Job parameters
	INSERT into @ConfigurationEntityTree ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) select 5, @configurationGroupIdJobParameters, N'MachineId', N'PushNotificationGroupDispatch', N'PushNotificationGroupDispatchUmbrellaTimerJob',N'', @treeJobId, '27DE0764-F0CA-4887-B79C-95BE7AE08894'


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