use veproms_BYR2
GO

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[ConvertEnhanced]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
	DROP PROCEDURE [ConvertEnhanced];
GO

CREATE PROCEDURE [dbo].[ConvertEnhanced]
(
	@svid int,
	@bvid int,
	@Type varchar(1),
	@Source varchar(3),
	@Proc varchar(3)
)
WITH EXECUTE AS OWNER
AS
BEGIN TRY -- Try Block
	BEGIN TRANSACTION
	--
update cc set cc.config = ah.newConfig
from
(
select 
replace( case @Source when 'Yes' then t1.config else t2.config end ,'></Config>'
 ,'><Enhanced Type="' + @Type + '" ItemID="' + CAST(case @Source when 'Yes' then t2.ItemID else t1.ItemID end as varchar(8)) + '" /></Config>') newConfig
,t1.ContentID sourceID,t2.ContentID enhancedID from
(
select vi.VersionID,vi.ItemID,cc.ContentID,cc.Config,cc.Type,cc.Text,xHistory.value('@ProcName','nvarchar(8)') ProcName,xHistory.value('@RecID','nvarchar(8)') RecID
from vefn_GetVersionItems(@svid) vi
join (select ContentID,Type,Text,Config,CAST(Config as xml) xConfig from Contents where (@Proc = 'Yes' and type = 0) or (@Proc != 'Yes' and Type >= 10000)) cc on vi.ContentID = cc.ContentID
cross apply xConfig.nodes('//History') tHistory(xHistory)
)  t1
join 
(
select vi.VersionID,vi.ItemID,cc.ContentID,cc.Config,cc.Type,cc.Text,xHistory.value('@ProcName','nvarchar(8)') ProcName,xHistory.value('@RecID','nvarchar(8)') RecID
from vefn_GetVersionItems(@bvid) vi
join (select ContentID,Type,Text,Config,CAST(Config as xml) xConfig from Contents where (@Proc = 'Yes' and type = 0) or (@Proc != 'Yes' and Type >= 10000)) cc on vi.ContentID = cc.ContentID
cross apply xConfig.nodes('//History') tHistory(xHistory)
)  t2 on t1.ProcName = t2.ProcName and (@Proc = 'Yes' or (@Proc != 'Yes' and (SUBSTRING(t1.RecID,3,6) = SUBSTRING(t2.RecID,3,6) and LEFT(t2.RecID,2) = '0L'))) 
) ah
join Contents cc on case @Source when 'Yes' then ah.sourceID else ah.enhancedID end = cc.ContentID	--
	IF( @@TRANCOUNT > 0 ) COMMIT
END TRY
BEGIN CATCH -- Catch Block
	IF( @@TRANCOUNT = 1 ) ROLLBACK -- Only rollback if top level
 	ELSE IF( @@TRANCOUNT > 1 ) COMMIT -- Otherwise commit.  Top level will rollback
	EXEC vlnErrorHandler
END CATCH
GO
exec vesp_TurnChangeManagerOFF
go
declare @svid int
declare @bvid int
--set variable for source
select @svid = VersionID from vefn_GetVersionNames() where FolderName = 'Rev. 2 EOP Master Procedures'
--set variable for background

select @Bvid = VersionID from vefn_GetVersionNames() where FolderName = 'Rev. 2 EOP Backgrounds'
--update docversion for Background
print 'BGDV-1'
update docversions set config = replace(config,'></Config>',
	'><Enhanced Name="Background" Type="1" VersionID="' + cast(@bvid as varchar(25)) + '" PdfX="6" PdfToken="B"/></Config>')
	where VersionID =@svid
print 'BGDV-2'
update docversions set config = replace(config,'></Config>',
	'><Enhanced Name="Source" Type="0" VersionID="' + cast(@svid as varchar(25)) + '" PdfX="6" PdfToken="S"/></Config>')
	where VersionID =@bvid
print 'BG-1'
Exec ConvertEnhanced @svid,@bvid,'1','Yes','Yes'
print 'BG-2'
Exec ConvertEnhanced @svid,@bvid,'0','No','Yes'
print 'BG-3'
Exec ConvertEnhanced @svid,@bvid,'1','Yes','No'
print 'BG-4'
Exec ConvertEnhanced @svid,@bvid,'0','No','No'
select @Bvid = VersionID from vefn_GetVersionNames() where FolderName = 'Rev. 2 EOP Deviations'
--update docversion for Deviations
print 'DDDV-1'
update docversions set config = replace(config,'></Config>',
	'><Enhanced Name="Deviation" Type="2" VersionID="' + cast(@bvid as varchar(25)) + '" PdfX="20" PdfToken="D"/></Config>')
	where VersionID =@svid
print 'DDDV-2'
update docversions set config = replace(config,'></Config>',
	'><Enhanced Name="Source" Type="0" VersionID="' + cast(@svid as varchar(25)) + '" PdfX="6" PdfToken="S"/></Config>')
	where VersionID =@bvid
print 'DD-1'
Exec ConvertEnhanced @svid,@bvid,'2','Yes','Yes'
print 'DD-2'
Exec ConvertEnhanced @svid,@bvid,'0','No','Yes'
print 'DD-3'
Exec ConvertEnhanced @svid,@bvid,'2','Yes','No'
print 'DD-4'
Exec ConvertEnhanced @svid,@bvid,'0','No','No'
GO
exec vesp_TurnChangeManagerON
go
Use Master
go