/**
Create Groups
**/

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

--ClearActivityProcessJobRowsJob
	declare @ClearActivityProcessJobRowsJob_GUID uniqueidentifier = 'B8E6479D-A4FC-4E19-B3D2-083F0701D764', @ClearActivityProcessJobRowsJob_tj_Id int
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (4, @ClearActivityProcessJobRowsJob_GUID) --TimerJob
	declare @ClearActivityProcessJobRowsJob_p_GUID uniqueidentifier = 'E08C6D92-3CAA-41F3-95B4-0ACCD2FEBBFC',  @ClearActivityProcessJobRowsJob_tjp_Id int
	INSERT into @ConfigurationGroups (EntityId,GuidKey) VALUES (5, @ClearActivityProcessJobRowsJob_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 @ClearActivityProcessJobRowsJob_tj_Id=id from ConfigurationGroups where guidkey=@ClearActivityProcessJobRowsJob_GUID
	select @ClearActivityProcessJobRowsJob_tjp_Id=id from ConfigurationGroups where guidkey=@ClearActivityProcessJobRowsJob_p_GUID

/**
Create group parameter 
**/

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

--ClearActivityProcessJobRowsJob
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @ClearActivityProcessJobRowsJob_tj_Id, 1, N'JobStoreProc_ClearActivityProcessJobRows'
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @ClearActivityProcessJobRowsJob_tj_Id, 16, N'StoreProcTimerJob'
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @ClearActivityProcessJobRowsJob_tj_Id, 17, N'1'
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @ClearActivityProcessJobRowsJob_tjp_Id, 1, N'ParametersJobStoreProc_ClearActivityProcessJobRows'
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @ClearActivityProcessJobRowsJob_tjp_Id, 18, N'60'	--once a minute
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @ClearActivityProcessJobRowsJob_tjp_Id, 19, N'1'
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @ClearActivityProcessJobRowsJob_tjp_Id, 80, N'1'
	INSERT into @ConfigurationGroupParameters ( GroupId, ParameterId, Value) select @ClearActivityProcessJobRowsJob_tjp_Id, 81, N'job_ClearActivityProcessJobRows'
	
	
	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 );

--ClearActivityProcessJobRowsJob
	INSERT into @ConfigurationEntityTree1 ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) select 4, @ClearActivityProcessJobRowsJob_tj_Id, N'MachineId', N'StoreProcJobs', N'JobStoreProc_ClearActivityProcessJobRows', N'ServiceBroker', NULL, @ClearActivityProcessJobRowsJob_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 );

--ClearActivityProcessJobRowsJob
	INSERT into @ConfigurationEntityTree2 ( EntityTypeId, GroupId, MachineName, ServiceName, ServiceId, ModuleIdentifier, ParentEntityId, GuidKey) select 5, @ClearActivityProcessJobRowsJob_tjp_Id, N'MachineId', N'StoreProcJobs', N'JobStoreProc_ClearActivityProcessJobRows', N'ServiceBroker', t.id, @ClearActivityProcessJobRowsJob_p_GUID from ConfigurationEntityTree t where t.guidkey=@ClearActivityProcessJobRowsJob_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.*;



