--------------------------------------------------------
--  DDL for Package Body PKG_SICD_JIRA
--------------------------------------------------------

  CREATE OR REPLACE EDITIONABLE PACKAGE BODY "ENVX_OPER"."PKG_SICD_JIRA" AS


/*--------------- PRC1-------------------*/
    PROCEDURE jira_search (
        username  IN   VARCHAR,
        pass      IN   VARCHAR,
        jql       IN   VARCHAR,
        resp      OUT  CLOB
    ) AS
        --l_clob       CLOB;
        --request_clob CLOB;
    BEGIN

        ---
        log_program_name := 'pkg_sicd_jira.jira_search(PRC1)';
        ---

        -- TODO: Implementation required for PROCEDURE PKG_SICD_JIRA.JIRA_SEARCH
        --request_clob := '{ "expand": ["names"],"jql": "project=ITDES AND assignee=vatrox04","startAt": 0,"maxResults": 20,"fields": ["id","key","summary","assignee","status"]}';

        -- Es necesario fijar esto para que funcione
        apex_web_service.g_request_headers(1).name          := 'Content-Type';
        apex_web_service.g_request_headers(1).value         := 'application/json;charset=UTF-8';

        -- Invocar al API de JIRA con los parametros suministrados.
        resp                                                := apex_web_service.make_rest_request(
            p_url          => 'http://jira.caja.com:20000/rest/api/2/search',
            p_http_method  => 'POST',
            p_body         => to_clob(jql),
            p_username     => username,
            p_password     => pass
        );
        -- Capturar excepciones
        /*
        l_clob := apex_web_service.make_rest_request(
                                                        p_url => 'http://jira.caja.com:20000/rest/api/2/search',
                                                        p_http_method => 'POST',
                                                        p_body => request_clob,
                                                        p_username => 'ldapsed',
                                                        p_password => 'LDAPSEDsistemas20caracteres');*/
    EXCEPTION
    WHEN OTHERS THEN
            ------------------------------------------------------------------        
            log_collector(
                pi_program_name  => log_program_name,
                pi_log_level     => 2, --- WARNING
                pi_log_message   => 'ERROR: ' || sqlerrm
            );
            ------------------------------------------------------------------
    END jira_search;

/*--------------- PRC2-------------------*/
    PROCEDURE jira_search (
        username        IN   VARCHAR DEFAULT NULL,
        pass            IN   VARCHAR DEFAULT NULL,
        project_type    IN   VARCHAR,
        assignee        IN   VARCHAR,
        summary         IN   VARCHAR,
        ticket_key      IN   VARCHAR,
        ticket_status   IN   VARCHAR,
        files_returned  IN   NUMBER DEFAULT 20,
        p_mensaje       OUT  VARCHAR,
        p_error         OUT  VARCHAR
    ) AS

        criteriobusqueda  VARCHAR2(4000);
        responseclob      CLOB;
        p_username        VARCHAR2(50);
        p_password        VARCHAR2(200);
    BEGIN

        ---
        log_program_name := 'pkg_sicd_jira.jira_search(PRC2)';
        ---

        -- Control de parametros de autenticacion.
        IF username IS NULL OR pass IS NULL THEN
            p_username  :=  pkg_sicd_commons.get_parameter_value('JIRA_USERNAME'); -- 
            p_password  :=  pkg_sicd_commons.get_parameter_value('JIRA_PASSWORD');

            --jira.user	ldapsed 
            --jira.password	LDAPSEDsistemas20caracteres
        END IF;

        -- Controlar el Tipo de Proyecto no sea Nulo

        IF project_type IS NULL THEN
            raise_application_error(-20000, 'Tipo de Proyecto deben ser indicado');
        END IF;


        -- Armar el criterio de busqueda
        criteriobusqueda := '{ "expand": ["names"],"jql": "project='
                            || project_type
                            ||
            CASE
                WHEN
                    length(assignee) > 0 THEN
                    ' AND assignee=' || assignee
            END
                            ||
            CASE
                WHEN
                    length(ticket_key) > 0 THEN
                    ' AND key='
                    || project_type
                    || '-'
                    || ticket_key
            END
                            ||
            CASE
                WHEN
                    length(ticket_status) > 0 THEN
                    ' AND status =' || ticket_status
            END
                            ||
            CASE
                WHEN
                    length(summary) > 0 THEN
                    ' AND summary~'''
                    || summary
                    || ''''
            END
                            || '","startAt": 0,"maxResults":'
                            || files_returned
                            || ',"fields": ["id","key","summary","assignee","status"]}';

        --dbms_output.put_line(criteriobusqueda);

        jira_search(p_username, p_password, criteriobusqueda, responseclob);
        DELETE FROM tmp_reference_list;

        INSERT INTO tmp_reference_list (
            id_reference,
            reference_description,
            reference_user,
            reference_status
        )
            SELECT e.*
              FROM
                JSON_TABLE ( responseclob, '$.issues[*]'
                    COLUMNS (
                        key VARCHAR2 ( 200 ) PATH '$.key',
                        summary VARCHAR2 ( 200 ) PATH '$.fields.summary',
                        displayname VARCHAR2 ( 200 ) PATH '$.fields.assignee.displayName',
                        status VARCHAR2 ( 200 ) PATH '$.fields.status.name'
                    )
                )
            e;
    EXCEPTION
    WHEN OTHERS THEN
            ------------------------------------------------------------------        
            log_collector(
                pi_program_name  => log_program_name,
                pi_log_level     => 2, --- WARNING
                pi_log_message   => 'ERROR: ' || sqlerrm
            );
            ------------------------------------------------------------------
    END jira_search;

END pkg_sicd_jira;


/

  GRANT EXECUTE ON "ENVX_OPER"."PKG_SICD_JIRA" TO "APEX_REST_PUBLIC_USER";
  GRANT DEBUG ON "ENVX_OPER"."PKG_SICD_JIRA" TO "APEX_REST_PUBLIC_USER";
