SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- [PRREC_INSERT_OBJECT]
-- =================================================================
-- Inserts one record into a given Entity
-- 
-- Returns: INT Value - 0 = Everything worked well
--          BIGINT    - Output parameter @oGUID returns the inserted GUID
-- =================================================================
-- Copyright (c) 2025 by Digital Data GmbH
--
-- Digital Data GmbH • Ludwig-Rinn-Strasse 16 • D-35452 Heuchelheim
-- Tel.: 0641/202360 • E-Mail: info-flow@digitaldata.works
-- =================================================================
-- Creation Date / Author:    22.12.2025 / MK
-- Version Date / Editor:     22.12.2025 / MK
-- Version Number:            1.0.0.0
-- =================================================================
-- History:
-- 22.12.2025 / MK - First Version

CREATE OR ALTER PROCEDURE [dbo].[PRREC_INSERT_OBJECT] (
	@pENTITY									NVARCHAR(25)	= 'ACTION',		-- Target entity: ACTION, ENDPOINT, ENDPOINT_AUTH, ENDPOINT_PARAMS, PROFILE, RESULT
	@pADDED_WHO									NVARCHAR(50)	= NULL,			-- User or function of the Insert
	@pADDED_WHEN								DATETIME		= NULL,			-- Datetime of the Insert
	@pACTION_PROFILE_ID							BIGINT			= NULL,			-- ACTION: Profile GUID (required)
	@pACTION_ACTIVE								BIT				= 1,			-- ACTION: Active flag
	@pACTION_SEQUENCE							TINYINT			= 0,        	-- ACTION: Sequence/order
	@pACTION_ENDPOINT_ID						BIGINT			= NULL,     	-- ACTION: Endpoint GUID (required)
	@pACTION_ENDPOINT_AUTH_ID					BIGINT			= NULL,     	-- ACTION: Endpoint auth GUID
	@pACTION_ENDPOINT_PARAMS_ID					SMALLINT		= NULL,     	-- ACTION: Endpoint params group GUID
	@pACTION_SQL_CONNECTION_ID					SMALLINT		= NULL,     	-- ACTION: SQL connection GUID
	@pACTION_TYPE_ID							TINYINT			= 1,        	-- ACTION: HTTP method id (0..9)
	@pACTION_PRE_SQL							NVARCHAR(MAX)	= NULL,     	-- ACTION: SQL to run before request
	@pACTION_HEADER_SQL							NVARCHAR(MAX)	= NULL,     	-- ACTION: SQL to build headers
	@pACTION_BODY_SQL							NVARCHAR(MAX)	= NULL,     	-- ACTION: SQL to build body
	@pACTION_POST_SQL							NVARCHAR(MAX)	= NULL,     	-- ACTION: SQL to run after request
	@pACTION_ERROR_ACTION_ID					TINYINT			= 0,        	-- ACTION: Error behavior (0=STOP,1=CONTINUE)
	@pENDPOINT_ACTIVE							BIT				= 1,			-- ENDPOINT: Active flag
	@pENDPOINT_DESCRIPTION						NVARCHAR(250)	= NULL,     	-- ENDPOINT: Description
	@pENDPOINT_URI								NVARCHAR(2000)	= NULL,     	-- ENDPOINT: Target URI (required)
	@pENDPOINT_AUTH_ACTIVE						BIT				= 1,			-- ENDPOINT_AUTH: Active flag
	@pENDPOINT_AUTH_DESCRIPTION					NVARCHAR(250)	= NULL,     	-- ENDPOINT_AUTH: Description
	@pENDPOINT_AUTH_TYPE_ID						TINYINT			= 0,        	-- ENDPOINT_AUTH_ID: Auth type id (0..9)
	@pENDPOINT_AUTH_API_KEY						NVARCHAR(150)	= NULL,     	-- ENDPOINT_AUTH: API key name/header
	@pENDPOINT_AUTH_API_VALUE					NVARCHAR(150)	= NULL,     	-- ENDPOINT_AUTH: API key value
	@pENDPOINT_AUTH_API_KEY_ADD_TO_ID			BIT				= NULL,			-- ENDPOINT_AUTH: API key placement (0=HEADER,1=QUERY)
	@pENDPOINT_AUTH_TOKEN						NVARCHAR(150)	= NULL,     	-- ENDPOINT_AUTH: Bearer/JWT token
	@pENDPOINT_AUTH_USERNAME					NVARCHAR(100)	= NULL,     	-- ENDPOINT_AUTH: Username
	@pENDPOINT_AUTH_PASSWORD					NVARCHAR(100)	= NULL,     	-- ENDPOINT_AUTH: Password
	@pENDPOINT_AUTH_DOMAIN						NVARCHAR(50)	= NULL,     	-- ENDPOINT_AUTH: Domain (NTLM)
	@pENDPOINT_AUTH_WORKSTATION					NVARCHAR(50)	= NULL,     	-- ENDPOINT_AUTH: Workstation (NTLM)
	@pPROFILE_ACTIVE							BIT				= 1,			-- PROFILE: Active flag
	@pPROFILE_TYPE_ID							TINYINT			= 1,        	-- PROFILE: Protocol type id (1=HTTP,2=HTTPS)
	@pPROFILE_MANDANTOR							NVARCHAR(50)	= 'DEFAULT',	-- PROFILE: Tenant/mandator
	@pPROFILE_NAME								NVARCHAR(50)	= NULL,     	-- PROFILE: Profile name (required)
	@pPROFILE_DESCRIPTION						NVARCHAR(250)	= NULL,     	-- PROFILE: Description
	@pPROFILE_LOG_LEVEL_ID						TINYINT			= 4,        	-- PROFILE: Log level id (0..6)
	@pPROFILE_LANGUAGE_ID						SMALLINT		= 1031,     	-- PROFILE: Language id (1031/1033)
	@pRESULT_ACTION_ID							BIGINT			= NULL,			-- RESULT: Action GUID (required)
	@pRESULT_STATUS_ID							SMALLINT		= NULL,     	-- RESULT: HTTP status code (required)
	@pRESULT_TYPE_ID							TINYINT			= NULL,     	-- RESULT: 1 = Pre; 2 = Main; 3 = Post
	@pRESULT_HEADER								NVARCHAR(MAX)	= NULL,     	-- RESULT: Response header
	@pRESULT_BODY								NVARCHAR(MAX)	= NULL,     	-- RESULT: Response body
	@pRESULT_INFO								NVARCHAR(MAX)	= NULL,     	-- RESULT: Optional Response info text
	@pRESULT_ERROR								NVARCHAR(MAX)	= NULL,     	-- RESULT: Optional Response error text
	@pENDPOINT_PARAMS_ACTIVE					BIT				= 1,			-- ENDPOINT_PARAMS: Active flag
	@pENDPOINT_PARAMS_DESCRIPTION				NVARCHAR(250)	= NULL,     	-- ENDPOINT_PARAMS: Description
	@pENDPOINT_PARAMS_GROUP_ID					SMALLINT		= NULL,     	-- ENDPOINT_PARAMS: Parameter group id (required)
	@pENDPOINT_PARAMS_SEQUENCE					TINYINT			= NULL,     	-- ENDPOINT_PARAMS: Sequence/order
	@pENDPOINT_PARAMS_KEY						NVARCHAR(150)	= NULL,     	-- ENDPOINT_PARAMS: Parameter key/name
	@pENDPOINT_PARAMS_VALUE						NVARCHAR(150)	= NULL,     	-- ENDPOINT_PARAMS: Parameter value
	@oGUID										BIGINT			= NULL OUTPUT	-- Output: inserted GUID
)
AS
BEGIN

	SET NOCOUNT ON;
	SET XACT_ABORT ON;

	-- declare new vars because of parameter sniffing
	DECLARE @ENTITY								NVARCHAR(25)	= UPPER(LTRIM(RTRIM(ISNULL(@pENTITY, 'ACTION')))),
			@ADDED_WHO							NVARCHAR(50)	= ISNULL(@pADDED_WHO, OBJECT_NAME(@@PROCID)),
			@ADDED_WHEN							DATETIME		= ISNULL(@pADDED_WHEN,GetDate()),
			@ACTION_PROFILE_ID					BIGINT			= @pACTION_PROFILE_ID,
			@ACTION_ACTIVE						BIT				= @pACTION_ACTIVE,
			@ACTION_SEQUENCE					TINYINT			= @pACTION_SEQUENCE,
			@ACTION_ENDPOINT_ID					BIGINT			= @pACTION_ENDPOINT_ID,
			@ACTION_ENDPOINT_AUTH_ID			BIGINT			= @pACTION_ENDPOINT_AUTH_ID,
			@ACTION_ENDPOINT_PARAMS_ID			SMALLINT		= @pACTION_ENDPOINT_PARAMS_ID,
			@ACTION_SQL_CONNECTION_ID			SMALLINT		= @pACTION_SQL_CONNECTION_ID,
			@ACTION_TYPE_ID						TINYINT			= @pACTION_TYPE_ID,
			@ACTION_PRE_SQL						NVARCHAR(MAX)	= @pACTION_PRE_SQL,
			@ACTION_HEADER_SQL					NVARCHAR(MAX)	= @pACTION_HEADER_SQL,
			@ACTION_BODY_SQL					NVARCHAR(MAX)	= @pACTION_BODY_SQL,
			@ACTION_POST_SQL					NVARCHAR(MAX)	= @pACTION_POST_SQL,
			@ACTION_ERROR_ACTION_ID				TINYINT			= @pACTION_ERROR_ACTION_ID,
			@ENDPOINT_ACTIVE					BIT				= @pENDPOINT_ACTIVE,
			@ENDPOINT_DESCRIPTION				NVARCHAR(250)	= @pENDPOINT_DESCRIPTION,
			@ENDPOINT_URI						NVARCHAR(2000)	= @pENDPOINT_URI,
			@ENDPOINT_AUTH_ACTIVE				BIT				= @pENDPOINT_AUTH_ACTIVE,
			@ENDPOINT_AUTH_DESCRIPTION			NVARCHAR(250)	= @pENDPOINT_AUTH_DESCRIPTION,
			@ENDPOINT_AUTH_TYPE_ID				TINYINT			= @pENDPOINT_AUTH_TYPE_ID,
			@ENDPOINT_AUTH_API_KEY				NVARCHAR(150)	= @pENDPOINT_AUTH_API_KEY,
			@ENDPOINT_AUTH_API_VALUE			NVARCHAR(150)	= @pENDPOINT_AUTH_API_VALUE,
			@ENDPOINT_AUTH_API_KEY_ADD_TO_ID	BIT				= @pENDPOINT_AUTH_API_KEY_ADD_TO_ID,
			@ENDPOINT_AUTH_TOKEN				NVARCHAR(150)	= @pENDPOINT_AUTH_TOKEN,
			@ENDPOINT_AUTH_USERNAME				NVARCHAR(100)	= @pENDPOINT_AUTH_USERNAME,
			@ENDPOINT_AUTH_PASSWORD				NVARCHAR(100)	= @pENDPOINT_AUTH_PASSWORD,
			@ENDPOINT_AUTH_DOMAIN				NVARCHAR(50)	= @pENDPOINT_AUTH_DOMAIN,
			@ENDPOINT_AUTH_WORKSTATION			NVARCHAR(50)	= @pENDPOINT_AUTH_WORKSTATION,
			@PROFILE_ACTIVE						BIT				= @pPROFILE_ACTIVE,
			@PROFILE_TYPE_ID					TINYINT			= @pPROFILE_TYPE_ID,
			@PROFILE_MANDANTOR					NVARCHAR(50)	= @pPROFILE_MANDANTOR,
			@PROFILE_NAME						NVARCHAR(50)	= @pPROFILE_NAME,
			@PROFILE_DESCRIPTION				NVARCHAR(250)	= @pPROFILE_DESCRIPTION,
			@PROFILE_LOG_LEVEL_ID				TINYINT			= @pPROFILE_LOG_LEVEL_ID,
			@PROFILE_LANGUAGE_ID				SMALLINT		= @pPROFILE_LANGUAGE_ID,
			@RESULT_ACTION_ID					BIGINT			= @pRESULT_ACTION_ID,
			@RESULT_STATUS_ID					SMALLINT		= @pRESULT_STATUS_ID,
			@RESULT_TYPE_ID						TINYINT			= @pRESULT_TYPE_ID,
			@RESULT_HEADER						NVARCHAR(MAX)	= @pRESULT_HEADER,
			@RESULT_BODY						NVARCHAR(MAX)	= @pRESULT_BODY,
			@RESULT_INFO						NVARCHAR(MAX)	= @pRESULT_INFO,
			@RESULT_ERROR						NVARCHAR(MAX)	= @pRESULT_ERROR,
			@ENDPOINT_PARAMS_ACTIVE				BIT				= @pENDPOINT_PARAMS_ACTIVE,
			@ENDPOINT_PARAMS_DESCRIPTION		NVARCHAR(250)	= @pENDPOINT_PARAMS_DESCRIPTION,
			@ENDPOINT_PARAMS_GROUP_ID			SMALLINT		= @pENDPOINT_PARAMS_GROUP_ID,
			@ENDPOINT_PARAMS_SEQUENCE			TINYINT			= @pENDPOINT_PARAMS_SEQUENCE,
			@ENDPOINT_PARAMS_KEY				NVARCHAR(150)	= @pENDPOINT_PARAMS_KEY,
			@ENDPOINT_PARAMS_VALUE				NVARCHAR(150)	= @pENDPOINT_PARAMS_VALUE;

	-- declare runtime vars
	DECLARE @MY_PROCEDURE_NAME	NVARCHAR(128)	= OBJECT_NAME(@@PROCID);
	DECLARE @RETURN_STATUS		INT				= 0,
			@RETURN_STATUS_TEXT	NVARCHAR(MAX)	= 'START PROCEDURE [' + @MY_PROCEDURE_NAME + '] @ ' + CONVERT(VARCHAR(50), GETDATE(), 120);
	----------------------------------------------------------------------------------------------------------------------------

	--=================================================-- Output parameters --================================================--	
	PRINT '====================================================================================================';
	PRINT @RETURN_STATUS_TEXT;
	PRINT '';
	PRINT 'PARAMETER01 - @ENTITY:                          ' + ISNULL(CONVERT(NVARCHAR(25),   @ENTITY), 							'NULL');
	PRINT 'PARAMETER02 - @ADDED_WHO:                       ' + ISNULL(CONVERT(NVARCHAR(50),   @ADDED_WHO), 							'NULL');
	PRINT 'PARAMETER03 - @ADDED_WHEN:                      ' + ISNULL(CONVERT(NVARCHAR(30),   @ADDED_WHEN), 						'NULL');	
	PRINT 'PARAMETER04 - @ACTION_PROFILE_ID:               ' + ISNULL(CONVERT(NVARCHAR(30),   @ACTION_PROFILE_ID), 					'NULL');
	PRINT 'PARAMETER05 - @ACTION_ACTIVE:                   ' + ISNULL(CONVERT(NVARCHAR(1),    @ACTION_ACTIVE), 						'NULL');
	PRINT 'PARAMETER06 - @ACTION_SEQUENCE:                 ' + ISNULL(CONVERT(NVARCHAR(3),    @ACTION_SEQUENCE), 					'NULL');
	PRINT 'PARAMETER07 - @ACTION_ENDPOINT_ID:              ' + ISNULL(CONVERT(NVARCHAR(30),   @ACTION_ENDPOINT_ID), 				'NULL');
	PRINT 'PARAMETER08 - @ACTION_ENDPOINT_AUTH_ID:         ' + ISNULL(CONVERT(NVARCHAR(30),   @ACTION_ENDPOINT_AUTH_ID), 			'NULL');
	PRINT 'PARAMETER09 - @ACTION_ENDPOINT_PARAMS_ID:       ' + ISNULL(CONVERT(NVARCHAR(30),   @ACTION_ENDPOINT_PARAMS_ID), 			'NULL');
	PRINT 'PARAMETER10 - @ACTION_SQL_CONNECTION_ID:        ' + ISNULL(CONVERT(NVARCHAR(30),   @ACTION_SQL_CONNECTION_ID), 			'NULL');
	PRINT 'PARAMETER11 - @ACTION_TYPE_ID:                  ' + ISNULL(CONVERT(NVARCHAR(5),    @ACTION_TYPE_ID), 					'NULL');
	PRINT 'PARAMETER12 - @ACTION_PRE_SQL:                  ' + ISNULL(CONVERT(NVARCHAR(100),  @ACTION_PRE_SQL), 					'NULL');
	PRINT 'PARAMETER13 - @ACTION_HEADER_SQL:               ' + ISNULL(CONVERT(NVARCHAR(100),  @ACTION_HEADER_SQL), 					'NULL');
	PRINT 'PARAMETER14 - @ACTION_BODY_SQL:                 ' + ISNULL(CONVERT(NVARCHAR(100),  @ACTION_BODY_SQL), 					'NULL');
	PRINT 'PARAMETER15 - @ACTION_POST_SQL:                 ' + ISNULL(CONVERT(NVARCHAR(100),  @ACTION_POST_SQL), 					'NULL');
	PRINT 'PARAMETER16 - @ACTION_ERROR_ACTION_ID:          ' + ISNULL(CONVERT(NVARCHAR(5),    @ACTION_ERROR_ACTION_ID), 			'NULL');
	PRINT 'PARAMETER17 - @ENDPOINT_ACTIVE:                 ' + ISNULL(CONVERT(NVARCHAR(1),    @ENDPOINT_ACTIVE), 					'NULL');
	PRINT 'PARAMETER18 - @ENDPOINT_DESCRIPTION:            ' + ISNULL(CONVERT(NVARCHAR(250),  @ENDPOINT_DESCRIPTION), 				'NULL');
	PRINT 'PARAMETER19 - @ENDPOINT_URI:                    ' + ISNULL(CONVERT(NVARCHAR(2000), @ENDPOINT_URI), 						'NULL');
	PRINT 'PARAMETER20 - @ENDPOINT_AUTH_ACTIVE:            ' + ISNULL(CONVERT(NVARCHAR(1),    @ENDPOINT_AUTH_ACTIVE), 				'NULL');
	PRINT 'PARAMETER21 - @ENDPOINT_AUTH_DESCRIPTION:       ' + ISNULL(CONVERT(NVARCHAR(250),  @ENDPOINT_AUTH_DESCRIPTION), 			'NULL');
	PRINT 'PARAMETER22 - @ENDPOINT_AUTH_TYPE_ID:           ' + ISNULL(CONVERT(NVARCHAR(5),    @ENDPOINT_AUTH_TYPE_ID), 				'NULL');
	PRINT 'PARAMETER23 - @ENDPOINT_AUTH_API_KEY:           ' + ISNULL(CONVERT(NVARCHAR(150),  @ENDPOINT_AUTH_API_KEY), 				'NULL');
	PRINT 'PARAMETER24 - @ENDPOINT_AUTH_API_VALUE:         ' + ISNULL(CONVERT(NVARCHAR(150),  @ENDPOINT_AUTH_API_VALUE), 			'NULL');
	PRINT 'PARAMETER25 - @ENDPOINT_AUTH_API_KEY_ADD_TO_ID: ' + ISNULL(CONVERT(NVARCHAR(1),    @ENDPOINT_AUTH_API_KEY_ADD_TO_ID),	'NULL');
	PRINT 'PARAMETER26 - @ENDPOINT_AUTH_TOKEN:             ' + ISNULL(CONVERT(NVARCHAR(150),  @ENDPOINT_AUTH_TOKEN), 				'NULL');
	PRINT 'PARAMETER27 - @ENDPOINT_AUTH_USERNAME:          ' + ISNULL(CONVERT(NVARCHAR(100),  @ENDPOINT_AUTH_USERNAME), 			'NULL');
	PRINT 'PARAMETER28 - @ENDPOINT_AUTH_PASSWORD:          ' + ISNULL(CONVERT(NVARCHAR(100),  @ENDPOINT_AUTH_PASSWORD), 			'NULL');
	PRINT 'PARAMETER29 - @ENDPOINT_AUTH_DOMAIN:            ' + ISNULL(CONVERT(NVARCHAR(50),   @ENDPOINT_AUTH_DOMAIN), 				'NULL');
	PRINT 'PARAMETER30 - @ENDPOINT_AUTH_WORKSTATION:       ' + ISNULL(CONVERT(NVARCHAR(50),   @ENDPOINT_AUTH_WORKSTATION), 			'NULL');
	PRINT 'PARAMETER31 - @PROFILE_ACTIVE:                  ' + ISNULL(CONVERT(NVARCHAR(1),    @PROFILE_ACTIVE), 					'NULL');
	PRINT 'PARAMETER32 - @PROFILE_TYPE_ID:                 ' + ISNULL(CONVERT(NVARCHAR(5),    @PROFILE_TYPE_ID), 					'NULL');
	PRINT 'PARAMETER33 - @PROFILE_MANDANTOR:               ' + ISNULL(CONVERT(NVARCHAR(50),   @PROFILE_MANDANTOR), 					'NULL');
	PRINT 'PARAMETER34 - @PROFILE_NAME:                    ' + ISNULL(CONVERT(NVARCHAR(50),   @PROFILE_NAME), 						'NULL');
	PRINT 'PARAMETER35 - @PROFILE_DESCRIPTION:             ' + ISNULL(CONVERT(NVARCHAR(250),  @PROFILE_DESCRIPTION), 				'NULL');
	PRINT 'PARAMETER36 - @PROFILE_LOG_LEVEL_ID:            ' + ISNULL(CONVERT(NVARCHAR(5),    @PROFILE_LOG_LEVEL_ID),				'NULL');
	PRINT 'PARAMETER37 - @PROFILE_LANGUAGE_ID:             ' + ISNULL(CONVERT(NVARCHAR(10),   @PROFILE_LANGUAGE_ID), 				'NULL');
	PRINT 'PARAMETER38 - @RESULT_ACTION_ID:                ' + ISNULL(CONVERT(NVARCHAR(30),   @RESULT_ACTION_ID), 					'NULL');
	PRINT 'PARAMETER39 - @RESULT_STATUS_ID:                ' + ISNULL(CONVERT(NVARCHAR(10),   @RESULT_STATUS_ID), 					'NULL');
	PRINT 'PARAMETER40 - @RESULT_TYPE_ID:                  ' + ISNULL(CONVERT(NVARCHAR(1),    @RESULT_TYPE_ID), 					'NULL');
	PRINT 'PARAMETER41 - @RESULT_HEADER:                   ' + ISNULL(CONVERT(NVARCHAR(100),  @RESULT_HEADER), 						'NULL');
	PRINT 'PARAMETER42 - @RESULT_BODY:                     ' + ISNULL(CONVERT(NVARCHAR(100),  @RESULT_BODY), 						'NULL');
	PRINT 'PARAMETER43 - @RESULT_INFO:                     ' + ISNULL(CONVERT(NVARCHAR(100),  @RESULT_INFO), 						'NULL');
	PRINT 'PARAMETER44 - @RESULT_ERROR:                    ' + ISNULL(CONVERT(NVARCHAR(100),  @RESULT_ERROR), 						'NULL');
	PRINT 'PARAMETER45 - @ENDPOINT_PARAMS_ACTIVE:          ' + ISNULL(CONVERT(NVARCHAR(1),    @ENDPOINT_PARAMS_ACTIVE), 			'NULL');
	PRINT 'PARAMETER46 - @ENDPOINT_PARAMS_DESCRIPTION:     ' + ISNULL(CONVERT(NVARCHAR(250),  @ENDPOINT_PARAMS_DESCRIPTION), 		'NULL');
	PRINT 'PARAMETER47 - @ENDPOINT_PARAMS_GROUP_ID:        ' + ISNULL(CONVERT(NVARCHAR(10),   @ENDPOINT_PARAMS_GROUP_ID), 			'NULL');
	PRINT 'PARAMETER48 - @ENDPOINT_PARAMS_SEQUENCE:        ' + ISNULL(CONVERT(NVARCHAR(5),    @ENDPOINT_PARAMS_SEQUENCE), 			'NULL');
	PRINT 'PARAMETER49 - @ENDPOINT_PARAMS_KEY:             ' + ISNULL(CONVERT(NVARCHAR(150),  @ENDPOINT_PARAMS_KEY), 				'NULL');
	PRINT 'PARAMETER50 - @ENDPOINT_PARAMS_VALUE:           ' + ISNULL(CONVERT(NVARCHAR(150),  @ENDPOINT_PARAMS_VALUE), 				'NULL');
	PRINT 'PARAMETER51 - @oGUID (OUT):                     ' + ISNULL(CONVERT(NVARCHAR(30),   @oGUID), 								'NULL');
	PRINT '';
	-----------------------------------------------------------------------------------------------------------------------------

	--================================================-- Validation --=======================================================--
	IF (@ENTITY NOT IN ('ACTION', 'ENDPOINT', 'ENDPOINT_AUTH', 'ENDPOINT_PARAMS', 'PROFILE', 'RESULT')) BEGIN
		SET @RETURN_STATUS = 50101;
		SET @RETURN_STATUS_TEXT = 'ERROR: Unknown entity "' + @ENTITY + '". Allowed: ACTION, ENDPOINT, ENDPOINT_AUTH, ENDPOINT_PARAMS, PROFILE, RESULT.';
		RAISERROR(@RETURN_STATUS_TEXT,16,1);
		RETURN @RETURN_STATUS;
	END;
	-----------------------------------------------------------------------------------------------------------------------------

	--===============================================-- Main Processing --===================================================--
	BEGIN TRY

		BEGIN TRANSACTION;

		IF (@ENTITY = 'ACTION') BEGIN

			IF (@ACTION_PROFILE_ID IS NULL OR @ACTION_ENDPOINT_ID IS NULL) BEGIN
				SET @RETURN_STATUS = 50102;
				SET @RETURN_STATUS_TEXT = 'ERROR: ACTION requires @pACTION_PROFILE_ID and @pACTION_ENDPOINT_ID.';
			END; ELSE BEGIN
				INSERT INTO [dbo].[TBREC_CFG_ACTION] (
							[PROFILE_ID],
							[ACTIVE],
							[SEQUENCE],
							[ENDPOINT_ID],
							[ENDPOINT_AUTH_ID],
							[ENDPOINT_PARAMS_ID],
							[SQL_CONNECTION_ID],
							[TYPE_ID],
							[PREPROCESSING_QUERY],
							[HEADER_QUERY],
							[BODY_QUERY],
							[POSTPROCESSING_QUERY],
							[ERROR_ACTION_ID],
							[ADDED_WHO],
							[ADDED_WHEN]
				) VALUES (
							@ACTION_PROFILE_ID,
							ISNULL(@ACTION_ACTIVE,1),
							ISNULL(@ACTION_SEQUENCE,0),
							@ACTION_ENDPOINT_ID,
							@ACTION_ENDPOINT_AUTH_ID,
							@ACTION_ENDPOINT_PARAMS_ID,
							@ACTION_SQL_CONNECTION_ID,
							ISNULL(@ACTION_TYPE_ID,1),
							@ACTION_PRE_SQL,
							@ACTION_HEADER_SQL,
							@ACTION_BODY_SQL,
							@ACTION_POST_SQL,
							ISNULL(@ACTION_ERROR_ACTION_ID,0),
							@ADDED_WHO,
							@ADDED_WHEN
				);

				SET @oGUID = SCOPE_IDENTITY();
				SET @RETURN_STATUS_TEXT = 'INFO: Inserted ACTION with GUID=' + CONVERT(NVARCHAR(30), @oGUID);
				PRINT @RETURN_STATUS_TEXT;
			END;

		END; ELSE IF (@ENTITY = 'ENDPOINT') BEGIN

			IF (@ENDPOINT_URI IS NULL) BEGIN
				SET @RETURN_STATUS = 50103;
				SET @RETURN_STATUS_TEXT = 'ERROR: ENDPOINT requires @pENDPOINT_URI.';
			END; ELSE BEGIN
				INSERT INTO [dbo].[TBREC_CFG_ENDPOINT] (
							[ACTIVE],
							[DESCRIPTION],
							[URI],
							[ADDED_WHO],
							[ADDED_WHEN]
				) VALUES (
							ISNULL(@ENDPOINT_ACTIVE,1),
							@ENDPOINT_DESCRIPTION,
							@ENDPOINT_URI,
							@ADDED_WHO,
							@ADDED_WHEN
				);

				SET @oGUID = SCOPE_IDENTITY();
				SET @RETURN_STATUS_TEXT = 'INFO: Inserted ENDPOINT with GUID=' + CONVERT(NVARCHAR(30), @oGUID);
				PRINT @RETURN_STATUS_TEXT;
			END;

		END; ELSE IF (@ENTITY = 'ENDPOINT_AUTH') BEGIN

			INSERT INTO [dbo].[TBREC_CFG_ENDPOINT_AUTH] (
						[ACTIVE],
						[DESCRIPTION],
						[TYPE_ID],
						[API_KEY],
						[API_VALUE],
						[API_KEY_ADD_TO_ID],
						[TOKEN],
						[USERNAME],
						[PASSWORD],
						[DOMAIN],
						[WORKSTATION],
						[ADDED_WHO],
						[ADDED_WHEN]
			) VALUES (
						ISNULL(@ENDPOINT_AUTH_ACTIVE,1),
						@ENDPOINT_AUTH_DESCRIPTION,
						ISNULL(@ENDPOINT_AUTH_TYPE_ID,0),
						@ENDPOINT_AUTH_API_KEY,
						@ENDPOINT_AUTH_API_VALUE,
						@ENDPOINT_AUTH_API_KEY_ADD_TO_ID,
						@ENDPOINT_AUTH_TOKEN,
						@ENDPOINT_AUTH_USERNAME,
						@ENDPOINT_AUTH_PASSWORD,
						@ENDPOINT_AUTH_DOMAIN,
						@ENDPOINT_AUTH_WORKSTATION,
						@ADDED_WHO,
						@ADDED_WHEN
			);

			SET @oGUID = SCOPE_IDENTITY();
			SET @RETURN_STATUS_TEXT = 'INFO: Inserted ENDPOINT_AUTH with GUID=' + CONVERT(NVARCHAR(30), @oGUID);
			PRINT @RETURN_STATUS_TEXT;

		END; ELSE IF (@ENTITY = 'PROFILE') BEGIN

			IF (@PROFILE_NAME IS NULL) BEGIN
				SET @RETURN_STATUS = 50104;
				SET @RETURN_STATUS_TEXT = 'ERROR: PROFILE requires @pPROFILE_NAME.';
			END; ELSE BEGIN
				INSERT INTO [dbo].[TBREC_CFG_PROFILE] (
							[ACTIVE],
							[TYPE_ID],
							[MANDANTOR],
							[PROFILE_NAME],
							[DESCRIPTION],
							[LOG_LEVEL_ID],
							[LANGUAGE_ID],
							[ADDED_WHO],
							[ADDED_WHEN]
				) VALUES (
							ISNULL(@PROFILE_ACTIVE,1),
							ISNULL(@PROFILE_TYPE_ID,1),
							ISNULL(@PROFILE_MANDANTOR,'DEFAULT'),
							@PROFILE_NAME,
							@PROFILE_DESCRIPTION,
							ISNULL(@PROFILE_LOG_LEVEL_ID,4),
							ISNULL(@PROFILE_LANGUAGE_ID,1031),
							@ADDED_WHO,
							@ADDED_WHEN
				);

				SET @oGUID = SCOPE_IDENTITY();
				IF NOT EXISTS (SELECT 1 FROM [dbo].[TBREC_RUN_PROFILE] WHERE [PROFILE_ID] = @oGUID) BEGIN
					INSERT INTO [dbo].[TBREC_RUN_PROFILE] ([PROFILE_ID])
					VALUES (@oGUID);
				END;
				
				SET @RETURN_STATUS_TEXT = 'INFO: Inserted PROFILE with GUID=' + CONVERT(NVARCHAR(30), @oGUID);
				PRINT @RETURN_STATUS_TEXT;
			END;

		END; ELSE IF (@ENTITY = 'RESULT') BEGIN

			IF (@RESULT_ACTION_ID IS NULL OR @RESULT_STATUS_ID IS NULL OR @RESULT_TYPE_ID IS NULL) BEGIN
				SET @RETURN_STATUS = 50105;
				SET @RETURN_STATUS_TEXT = 'ERROR: RESULT requires @pRESULT_ACTION_ID, @pRESULT_STATUS_ID and @pRESULT_TYPE_ID.';
			END; ELSE BEGIN
				INSERT INTO [dbo].[TBREC_OUT_RESULT] (
							[ACTION_ID],
							[STATUS_ID],
							[RESULT_TYPE_ID],
							[RESULT_HEADER],
							[RESULT_BODY],
							[RESULT_INFO],
							[RESULT_ERROR],
							[ADDED_WHO],
							[ADDED_WHEN]
				) VALUES (
							@RESULT_ACTION_ID,
							@RESULT_STATUS_ID,
							@RESULT_TYPE_ID,
							@RESULT_HEADER,
							@RESULT_BODY,
							@RESULT_INFO,
							@RESULT_ERROR,
							@ADDED_WHO,
							@ADDED_WHEN
				);

				SET @oGUID = SCOPE_IDENTITY();
				SET @RETURN_STATUS_TEXT = 'INFO: Inserted RESULT with GUID=' + CONVERT(NVARCHAR(30), @oGUID);
				PRINT @RETURN_STATUS_TEXT;
			END;

		END; ELSE IF (@ENTITY = 'ENDPOINT_PARAMS') BEGIN

			IF (@ENDPOINT_PARAMS_GROUP_ID IS NULL) BEGIN
				SET @RETURN_STATUS = 50106;
				SET @RETURN_STATUS_TEXT = 'ERROR: ENDPOINT_PARAMS requires @pENDPOINT_PARAMS_GROUP_ID.';
			END; ELSE BEGIN
				INSERT INTO [dbo].[TBREC_CFG_ENDPOINT_PARAMS] (
							[ACTIVE],
							[DESCRIPTION],
							[GROUP_ID],
							[SEQUENCE],
							[KEY],
							[VALUE],
							[ADDED_WHO],
							[ADDED_WHEN]
				) VALUES (
							ISNULL(@ENDPOINT_PARAMS_ACTIVE,1),
							@ENDPOINT_PARAMS_DESCRIPTION,
							@ENDPOINT_PARAMS_GROUP_ID,
							@ENDPOINT_PARAMS_SEQUENCE,
							@ENDPOINT_PARAMS_KEY,
							@ENDPOINT_PARAMS_VALUE,
							@ADDED_WHO,
							@ADDED_WHEN
				);

				SET @oGUID = SCOPE_IDENTITY();
				SET @RETURN_STATUS_TEXT = 'INFO: Inserted ENDPOINT_PARAMS with GUID=' + CONVERT(NVARCHAR(30), @oGUID);
				PRINT @RETURN_STATUS_TEXT;
			END;

		END;

		IF (@RETURN_STATUS > 0) BEGIN
			IF (XACT_STATE() <> 0) ROLLBACK TRANSACTION;
		END; ELSE BEGIN
			COMMIT TRANSACTION;
		END;

		--======================================================-- Output result --======================================================--
		SET @RETURN_STATUS_TEXT = 'END PROCEDURE [' + @MY_PROCEDURE_NAME + '] @ ' + CONVERT(VARCHAR(50), GETDATE(), 120);

		PRINT '';
		PRINT @RETURN_STATUS_TEXT;
		PRINT '====================================================================================================';
		-----------------------------------------------------------------------------------------------------------------------------------
		
		RETURN @RETURN_STATUS;

	END TRY BEGIN CATCH

		IF (XACT_STATE() <> 0) ROLLBACK TRANSACTION;

		--======================================================-- Output result --======================================================--
		SET @RETURN_STATUS_TEXT = 'ERROR: ' + ERROR_MESSAGE();
		RAISERROR(@RETURN_STATUS_TEXT,16,1);

		SET @RETURN_STATUS_TEXT = 'END PROCEDURE [' + @MY_PROCEDURE_NAME + '] @ ' + CONVERT(VARCHAR(50), GETDATE(), 120);

		PRINT '';
		PRINT @RETURN_STATUS_TEXT;
		PRINT '====================================================================================================';
		-----------------------------------------------------------------------------------------------------------------------------------

		RETURN @RETURN_STATUS;

	END CATCH;
	-----------------------------------------------------------------------------------------------------------------------------

END;
