/**
Create Groups
**/

--ConfigurationGroups
	DECLARE @ConfigurationGroups TABLE (EntityId int NOT NULL,GuidKey uniqueidentifier not null );

--NonimmediateBatchStatusCheckerJob
	declare @NonimmediateBatchStatusCheckerJob_GUID uniqueidentifier = '61AC1F0C-87E0-4E2B-B839-088F0D86F87C', @NonimmediateBatchStatusCheckerJob_tj_Id int
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (4, @NonimmediateBatchStatusCheckerJob_GUID) --TimerJob
	declare @NonimmediateBatchStatusCheckerJob_p_GUID uniqueidentifier = '4577B809-ACA9-4D6F-BB92-4BB56C548870',  @NonimmediateBatchStatusCheckerJob_tjp_Id int
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (5, @NonimmediateBatchStatusCheckerJob_p_GUID) --TimerJobParameters

	MERGE INTO dbo.ConfigurationGroups AS TARGET  
		USING @ConfigurationGroups AS SOURCE  
		ON upper(TARGET.GuidKey) = upper(SOURCE.GuidKey)
		WHEN NOT MATCHED BY TARGET THEN   
				INSERT (EntityId,GuidKey) VALUES (Source.EntityId,upper(source.GuidKey)) 
	OUTPUT $action, SOURCE.*;

	select @NonimmediateBatchStatusCheckerJob_tj_Id=id from ConfigurationGroups where guidkey=@NonimmediateBatchStatusCheckerJob_GUID
	select @NonimmediateBatchStatusCheckerJob_tjp_Id=id from ConfigurationGroups where guidkey=@NonimmediateBatchStatusCheckerJob_p_GUID

/**
Create group parameter 
**/

	--ConfigurationGroupParameters
	DECLARE @ConfigurationGroupParameters TABLE (GroupId int NOT NULL,ParameterId int not null, Value nvarchar(max));

--NonimmediateBatchStatusCheckerJob
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @NonimmediateBatchStatusCheckerJob_tj_Id, 1, N'Job_NonimmediateBatchStatusChecker'
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @NonimmediateBatchStatusCheckerJob_tj_Id, 16, N'PanelCommandManagerNonimmediate'
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @NonimmediateBatchStatusCheckerJob_tj_Id, 17, N'1'

	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @NonimmediateBatchStatusCheckerJob_tjp_Id, 1, N'ParametersJob_NonimmediateBatchStatusChecker'
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @NonimmediateBatchStatusCheckerJob_tjp_Id, 18, N'30'	--once every 30 sec
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @NonimmediateBatchStatusCheckerJob_tjp_Id, 19, N'1'
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @NonimmediateBatchStatusCheckerJob_tjp_Id, 80, N'1'
	
	
	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.*;


/**
Create Tree for job
**/
	--ConfigurationEntityTree - Job
	DECLARE @ConfigurationEntityTree1 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 );

--NonimmediateBatchStatusCheckerJob
	INSERT into @ConfigurationEntityTree1 ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) select 4, @NonimmediateBatchStatusCheckerJob_tj_Id, N'MachineId', N'PanelCommandManagerNonimmediate', N'Job_NonimmediateBatchStatusChecker', N'ServiceBroker', NULL, @NonimmediateBatchStatusCheckerJob_GUID	--TimerJob

	MERGE INTO dbo.ConfigurationEntityTree AS TARGET  
			USING @ConfigurationEntityTree1 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.*;

/**
Create Tree for job parameters
**/
	--ConfigurationEntityTree- Job parameters
	DECLARE @ConfigurationEntityTree2 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 );

--NonimmediateBatchStatusCheckerJob
	INSERT into @ConfigurationEntityTree2 ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) select 5, @NonimmediateBatchStatusCheckerJob_tjp_Id, N'MachineId', N'PanelCommandManagerNonimmediate', N'Job_NonimmediateBatchStatusChecker', N'ServiceBroker', t.id, @NonimmediateBatchStatusCheckerJob_p_GUID from ConfigurationEntityTree t where t.guidkey=@NonimmediateBatchStatusCheckerJob_GUID --TimerJobParameters			

	MERGE INTO dbo.ConfigurationEntityTree AS TARGET  
			USING @ConfigurationEntityTree2 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.*;



