-- PROCEDURE: internal.pdataimportloginsert(bigint, character varying, character, character varying, integer, integer)

-- DROP PROCEDURE IF EXISTS internal.pdataimportloginsert(bigint, character varying, character, character varying, integer, integer);

CREATE OR REPLACE PROCEDURE internal.pdataimportloginsert(
	par_dataimportid bigint,
	par_description character varying,
	par_loglevel character DEFAULT 'I'::bpchar,
	par_data character varying DEFAULT NULL::character varying,
	par_dataid integer DEFAULT NULL::integer,
	par_code integer DEFAULT 0)
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
    var_Msg character varying DEFAULT '';
    var_Severity INTEGER;
    var_LogLevelID SMALLINT;
BEGIN
RAISE NOTICE 'procedure internal.pdataimportloginsert';
   
    IF LOWER(par_LogLevel) = LOWER('I') THEN
        var_Msg := 'INFO';
        var_LogLevelID := 0;
        var_Severity := 0;
    ELSE
        IF LOWER(par_LogLevel) = LOWER('W') THEN
            var_Msg := 'WARNING';
            var_LogLevelID := 1;
            var_Severity := 1;
        ELSE
            IF LOWER(par_LogLevel) = LOWER('E') THEN
                var_Msg := 'ERROR';
                var_LogLevelID := 2;
                var_Severity := 2;
            ELSE
                IF LOWER(par_LogLevel) = LOWER('X') THEN
                    var_Msg := 'ERROR';
                    var_LogLevelID := 2;
                    var_Severity := 16;
                END IF;
            END IF;
        END IF;
    END IF;
-- RAISE NOTICE 'a: s%', par_dataimportid;  
    IF par_Code IS NOT NULL AND par_Code <> 0 THEN
        var_Msg := var_Msg || ' [' || CAST (par_Code AS VARCHAR(30)) || ']';
    END IF;
    var_Msg := var_Msg || ': ' || COALESCE(par_Description, '');
-- RAISE NOTICE 'b: s%', par_dataimportid;  
    IF par_Data IS NOT NULL AND LENGTH(par_Data) > 0 THEN
        var_Msg := var_Msg || ': ' || par_Data;
    END IF;

-- RAISE NOTICE 'par_DataImportID: s%', par_DataImportID;   
-- RAISE NOTICE 'var_LogLevelID: s%', var_LogLevelID;   
-- RAISE NOTICE 'par_DataID: s%', par_DataID;   
-- RAISE NOTICE 'par_Code: s%', par_Code;   
-- RAISE NOTICE 'par_Description:s%', par_Description;     
-- RAISE NOTICE 'par_Data: s%', par_Data;   

    INSERT INTO internal.dataimportlog (dataimportid, loglevelid, dataid, code, description, data)
    SELECT
        par_DataImportID, var_LogLevelID, par_DataID, par_Code, par_Description, par_Data;

END;
$BODY$;
