SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- [PRDEX_WRITE_LOG]
-- =================================================================
-- Writes log mesages to table [TBDEX_RUN_LOG]
-- 
-- Returns: INTEGER; 0 = ok; 0 <> nicht ok
-- =================================================================
-- 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:    01.08.2025 / MK
-- Version Date / Editor:     01.08.2025 / MK
-- Version Number:            1.0.0.0
-- =================================================================
-- History:
-- 01.08.2025 / MK - First Version

CREATE OR ALTER PROCEDURE [dbo].[PRDEX_WRITE_LOG](
	@pLOG_LEVEL			NVARCHAR(25),
	@pPROFILE_ID		BIGINT,
	@pBATCH_ID			NVARCHAR(25),
	@pREFERENCE1		NVARCHAR(150),
	@pREFERENCE2		NVARCHAR(150)	= NULL,
	@pREFERENCE3		NVARCHAR(150)	= NULL,
	@pREFERENCE4		NVARCHAR(150)	= NULL,
	@pREFERENCE5		NVARCHAR(150)	= NULL,
	@pRETURN_CODE		INT,
	@pMESSAGE1			NVARCHAR(max),
	@pMESSAGE2			NVARCHAR(max)	= NULL,
	@pMESSAGE3			NVARCHAR(max)	= NULL,
	@pMESSAGE4			NVARCHAR(max)	= NULL,
	@pMESSAGE5			NVARCHAR(max)	= NULL,
	@pCOMMENT			NVARCHAR(250)	= NULL,
	@pADDED_WHO			NVARCHAR(50)	= NULL
)
AS
BEGIN TRY

	SET NOCOUNT ON;
	SET XACT_ABORT ON;

	-- declare new vars because of parameter sniffing
	DECLARE @LOG_LEVEL							NVARCHAR(25)	= ISNULL(UPPER(@pLOG_LEVEL),'UNKNOWN'),
			@PROFILE_ID							BIGINT			= @pPROFILE_ID,
			@BATCH_ID							NVARCHAR(25)	= @pBATCH_ID,
			@REFERENCE1							NVARCHAR(150)	= ISNULL(@pREFERENCE1,'<NO REFERENCE GIVEN!>'),
			@REFERENCE2							NVARCHAR(150)	= @pREFERENCE2,
			@REFERENCE3							NVARCHAR(150)	= @pREFERENCE3,
			@REFERENCE4							NVARCHAR(150)	= @pREFERENCE4,
			@REFERENCE5							NVARCHAR(150)	= @pREFERENCE5,
			@RETURN_CODE						INT				= ISNULL(@pRETURN_CODE,99999),
			@MESSAGE1							NVARCHAR(max)	= ISNULL(@pMESSAGE1,'<NO MESSAGE GIVEN!>'),
			@MESSAGE2							NVARCHAR(max)	= @pMESSAGE2,
			@MESSAGE3							NVARCHAR(max)	= @pMESSAGE3,
			@MESSAGE4							NVARCHAR(max)	= @pMESSAGE4,
			@MESSAGE5							NVARCHAR(max)	= @pMESSAGE5,
			@COMMENT							NVARCHAR(250)	= @pCOMMENT,
			@ADDED_WHO							NVARCHAR(50)	= ISNULL(@pADDED_WHO,OBJECT_NAME(@@PROCID));

	-- declare runtime vars
    DECLARE @MY_PROCEDURE_NAME					NVARCHAR(128)	= OBJECT_NAME(@@PROCID);
	DECLARE	@ACTIVE_FLAG						BIT				= 1,
			@INACTIVE_FLAG						BIT				= 0,
			@ERROR_MESSAGE						NVARCHAR(4000),
			@ERROR_STATE						INT,
			@ERROR_SEVERITY						INT,
			@RETURN_STATUS						NVARCHAR(50)	= 0,
			@RETURN_STATUS_TEXT					NVARCHAR(MAX)	= concat('START PROCEDURE [',@MY_PROCEDURE_NAME,'] @ ',CONVERT(varchar(50),GETDATE(),120)),
			@RETURN_ERROR_TEXT					NVARCHAR(MAX)	= '';

	--================================================-- write log if table exists --================================================--
	IF NOT EXISTS (SELECT 1 FROM [INFORMATION_SCHEMA].[TABLES] WHERE [TABLE_NAME] = N'TBDEX_RUN_LOG') BEGIN

		--====================================================-- exception / error --====================================================--
		SET @RETURN_STATUS		= 50015;
		SET @RETURN_STATUS_TEXT = concat('','');
		SET @RETURN_ERROR_TEXT	= concat('Cannot find target table ([TBDEX_RUN_LOG]) for logging!','');

        THROW @RETURN_STATUS,@RETURN_ERROR_TEXT,1;
		-----------------------------------------------------------------------------------------------------------------------------------

	END; ELSE BEGIN

		--=====================================================-- Write log line --======================================================--
		BEGIN TRANSACTION;

			INSERT INTO [TBDEX_RUN_LOG]([LOG_LEVEL], [PROFILE_ID], [BATCH_ID], [REFERENCE1], [REFERENCE2], [REFERENCE3], [REFERENCE4], [REFERENCE5], [RETURN_CODE], [MESSAGE1], [MESSAGE2], [MESSAGE3], [MESSAGE4], [MESSAGE5], [COMMENT], [ADDED_WHO], [ADDED_WHEN])
								 VALUES(@LOG_LEVEL,	 @PROFILE_ID,  @BATCH_ID,  @REFERENCE1,  @REFERENCE2,  @REFERENCE3,  @REFERENCE4,  @REFERENCE5,  @RETURN_CODE,  @MESSAGE1,  @MESSAGE2,  @MESSAGE3,  @MESSAGE4,  @MESSAGE5,  @COMMENT,  @ADDED_WHO,  GETDATE());

		IF (@@TRANCOUNT > 0) BEGIN
			COMMIT TRANSACTION;
		END;
		-----------------------------------------------------------------------------------------------------------------------------------

	END;
	-----------------------------------------------------------------------------------------------------------------------------------

	RETURN @RETURN_STATUS;

END TRY BEGIN CATCH

	--====================================================-- exception / error --====================================================--
	IF (@@TRANCOUNT > 0) BEGIN
		ROLLBACK TRANSACTION;
	END;

	PRINT 'ERROR: ' + @RETURN_ERROR_TEXT;
	-----------------------------------------------------------------------------------------------------------------------------------

	--======================================================-- Output result --======================================================--
	SET @RETURN_STATUS		= CASE WHEN @RETURN_STATUS > 50000 THEN @RETURN_STATUS ELSE 50000 END;
	SET @RETURN_STATUS_TEXT = concat('END PROCEDURE [',@MY_PROCEDURE_NAME,'] @ ',CONVERT(NVARCHAR(50),GETDATE(),120));
	SET @RETURN_ERROR_TEXT	= concat('ERROR MESSAGE: ',CONVERT(NVARCHAR(500),ERROR_MESSAGE()));

	PRINT 'ERROR IN PROCEDURE: [' + @MY_PROCEDURE_NAME + ']' + char(13) + @RETURN_ERROR_TEXT;
	PRINT @RETURN_STATUS_TEXT;
	PRINT '====================================================================================================';
	-----------------------------------------------------------------------------------------------------------------------------------

	--==================================================-- Write to sql ERRORLOG --==================================================--
	SELECT	@ERROR_MESSAGE	= ERROR_MESSAGE(),
			@ERROR_SEVERITY	= ERROR_SEVERITY(),
			@ERROR_STATE	= ERROR_STATE();

    RAISERROR (@ERROR_MESSAGE,@ERROR_SEVERITY,@ERROR_STATE);
	-----------------------------------------------------------------------------------------------------------------------------------

	RETURN @RETURN_STATUS;

END CATCH;
GO


