UPDATE TBDD_MODULES SET DB_VERSION = '2.2.1.3' where NAME = 'Process-Manager'
GO
-- =============================================
-- Author:		Digital Data JJ
-- Created: 	18.08.2022
-- Description:	Add SAVE_CHANGE_ON_ENABLED Column 
--				when copying profile
-- Changed:		
-- =============================================
USE [DD_ECM]
GO
/****** Object:  StoredProcedure [dbo].[PRPM_COPY_PROFILE]    Script Date: 18.08.2022 14:13:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[PRPM_COPY_PROFILE] (@pProfileID as Integer,@pAddedWho Varchar(100))
AS
	DECLARE @NEWPROFILE_ID INTEGER
	INSERT INTO [dbo].[TBPM_PROFILE]
           ([NAME]
           ,[TITLE]
           ,[WD_OBJECTTYPE]
           ,[PRIORITY]
           ,[DESCRIPTION]
           ,[TYPE]
           ,[LOG_INDEX]
           ,[IN_WORK]
           ,[ACTIVE]
           ,[WD_SEARCH]
           ,[PM_VEKTOR_INDEX]
           ,[NO_OF_DOCUMENTS]
           ,[FINAL_PROFILE]
           ,[FINAL_TEXT]
           ,[MOVE2FOLDER]
           ,[SORT_BY_LATEST]
           ,[WORK_HISTORY_ENTRY]
           ,[ANNOTATE_ALL_WORK_HISTORY_ENTRIES]
           ,[ANNOTATE_WORK_HISTORY_ENTRY]
           ,[SQL_VIEW]
           ,[ADDED_WHO]
           ,[DISPLAY_MODE]
           ,[SQL_BTN_FINISH])
    SELECT  [NAME] + ' - COPY'
           ,[TITLE] + ' - COPY'
           ,[WD_OBJECTTYPE]
           ,[PRIORITY]
           ,[DESCRIPTION]
           ,[TYPE]
           ,[LOG_INDEX]
           ,[IN_WORK]
           ,0
           ,[WD_SEARCH]
           ,[PM_VEKTOR_INDEX]
           ,[NO_OF_DOCUMENTS]
           ,[FINAL_PROFILE]
           ,[FINAL_TEXT]
           ,[MOVE2FOLDER]
           ,[SORT_BY_LATEST]
           ,[WORK_HISTORY_ENTRY]
           ,[ANNOTATE_ALL_WORK_HISTORY_ENTRIES]
           ,[ANNOTATE_WORK_HISTORY_ENTRY]
           ,[SQL_VIEW]
           ,@pAddedWho
           ,[DISPLAY_MODE]
           ,[SQL_BTN_FINISH] FROM TBPM_PROFILE WHERE GUID = @pProfileID;

	SELECT @NEWPROFILE_ID = MAX(GUID) FROM TBPM_PROFILE WHERE ADDED_WHO = @pAddedWho;

	INSERT INTO [dbo].[TBPM_PROFILE_FINAL_INDEXING]
           ([PROFIL_ID]
           ,[CONNECTION_ID]
           ,[SQL_COMMAND]
           ,[INDEXNAME]
           ,[VALUE]
           ,[ACTIVE]
           ,[DESCRIPTION]
           ,[ADDED_WHO]
           ,[PREVENT_DUPLICATES]
           ,[ALLOW_NEW_VALUES]
           ,[IF_VEKTOR_BEHAVIOUR]
		   ,[SEQUENCE]) 
	SELECT 
		   @NEWPROFILE_ID
           ,[CONNECTION_ID]
           ,[SQL_COMMAND]
           ,[INDEXNAME]
           ,[VALUE]
           ,[ACTIVE]
           ,[DESCRIPTION]
           ,[ADDED_WHO]
           ,[PREVENT_DUPLICATES]
           ,[ALLOW_NEW_VALUES]
           ,[IF_VEKTOR_BEHAVIOUR] 
		   ,[SEQUENCE]
		   FROM [TBPM_PROFILE_FINAL_INDEXING] WHERE PROFIL_ID = @pProfileID
    


	
	
	INSERT INTO [dbo].[TBPM_PROFILE_CONTROLS]
           ([PROFIL_ID]
           ,[NAME]
           ,[CTRL_TYPE]
           ,[CTRL_TEXT]
           ,[X_LOC]
           ,[Y_LOC]
           ,[HEIGHT]
           ,[WIDTH]
           ,[INDEX_NAME]
           ,[TYP]
           ,[VALIDATION]
           ,[CHOICE_LIST]
           ,[CONNECTION_ID]
           ,[DEFAULT_VALUE]
           ,[SQL_UEBERPRUEFUNG]
           ,[READ_ONLY]
           ,[LOAD_IDX_VALUE]
           ,[FONT_STYLE]
           ,[FONT_SIZE]
           ,[FONT_FAMILY]
           ,[FONT_COLOR]
           ,[ADDED_WHO]
           ,[MULTISELECT]
           ,[VKT_ADD_ITEM]
           ,[VKT_PREVENT_MULTIPLE_VALUES]
           ,[REGEX_MATCH]
           ,[REGEX_MESSAGE_DE]
           ,[REGEX_MESSAGE_EN]
           ,[IMAGE_CONTROL]
           ,[SQL2]
           ,[SQL_ENABLE]
           ,[BACKGROUND_COLOR]
           ,[OVERWRITE_DATA]
           ,[SET_CONTROL_DATA]
		   ,[SAVE_CHANGE_ON_ENABLED])
    SELECT @NEWPROFILE_ID
           ,[NAME]
           ,[CTRL_TYPE]
           ,[CTRL_TEXT]
           ,[X_LOC]
           ,[Y_LOC]
           ,[HEIGHT]
           ,[WIDTH]
           ,[INDEX_NAME]
           ,[TYP]
           ,[VALIDATION]
           ,[CHOICE_LIST]
           ,[CONNECTION_ID]
           ,[DEFAULT_VALUE]
           ,[SQL_UEBERPRUEFUNG]
           ,[READ_ONLY]
           ,[LOAD_IDX_VALUE]
           ,[FONT_STYLE]
           ,[FONT_SIZE]
           ,[FONT_FAMILY]
           ,[FONT_COLOR]
           ,@pAddedWho
           ,[MULTISELECT]
           ,[VKT_ADD_ITEM]
           ,[VKT_PREVENT_MULTIPLE_VALUES]
           ,[REGEX_MATCH]
           ,[REGEX_MESSAGE_DE]
           ,[REGEX_MESSAGE_EN]
           ,[IMAGE_CONTROL]
           ,[SQL2]
           ,[SQL_ENABLE]
           ,[BACKGROUND_COLOR]
           ,[OVERWRITE_DATA]
           ,[SET_CONTROL_DATA]
		   ,[SAVE_CHANGE_ON_ENABLED]
		   FROM [TBPM_PROFILE_CONTROLS] WHERE PROFIL_ID = @pProfileID;

	DECLARE
		@OLD_ID	INTEGER,
		@NEW_ID	INTEGER
								
	DECLARE cursor1 CURSOR FOR 
	select T.GUID, T1.GUID from TBPM_PROFILE_CONTROLS T,TBPM_PROFILE_CONTROLS T1 WHERE 
		T.CTRL_TYPE = 'TABLE' and T1.CTRL_TYPE = 'TABLE' AND T.PROFIL_ID = @pProfileID AND T1.PROFIL_ID = @NEWPROFILE_ID AND T.NAME = T1.NAME
	OPEN cursor1 
    FETCH NEXT FROM cursor1 INTO @OLD_ID,@NEW_ID
    WHILE @@FETCH_STATUS = 0
	BEGIN
		INSERT INTO [dbo].[TBPM_CONTROL_TABLE]
           ([CONTROL_ID]
           ,[SPALTENNAME]
           ,[SPALTEN_HEADER]
           ,[SPALTENBREITE]
           ,[VALIDATION]
           ,[CHOICE_LIST]
           ,[CONNECTION_ID]
           ,[SQL_COMMAND]
           ,[READ_ONLY]
           ,[LOAD_IDX_VALUE]
           ,[ADDED_WHO]
           ,[REGEX_MESSAGE_DE]
           ,[REGEX_MESSAGE_EN]
           ,[REGEX_MATCH]
           ,[LOAD_AFT_LOAD_CONTROL]
           ,[SEQUENCE])
     SELECT @NEW_ID
           ,[SPALTENNAME]
           ,[SPALTEN_HEADER]
           ,[SPALTENBREITE]
           ,[VALIDATION]
           ,[CHOICE_LIST]
           ,[CONNECTION_ID]
           ,[SQL_COMMAND]
           ,[READ_ONLY]
           ,[LOAD_IDX_VALUE]
           ,@pAddedWho
           ,[REGEX_MESSAGE_DE]
           ,[REGEX_MESSAGE_EN]
           ,[REGEX_MATCH]
           ,[LOAD_AFT_LOAD_CONTROL]
           ,[SEQUENCE] FROM [TBPM_CONTROL_TABLE] WHERE CONTROL_ID = @OLD_ID ORDER BY SEQUENCE


		FETCH NEXT FROM cursor1 INTO @OLD_ID,@NEW_ID
	END
	CLOSE cursor1
	DEALLOCATE cursor1
