--------------------------------------------------------
--  DDL for View HIERARCHICAL_REQUEST_VEW
--------------------------------------------------------

  CREATE OR REPLACE FORCE EDITIONABLE VIEW "ENVX_OPER"."HIERARCHICAL_REQUEST_VEW" ("LEVEL_ALIAS", "ID", "PARENT_ID", "GROUP_ID", "LEVEL_PATH_ID", "LEVEL_ORDER", "LEVEL_NAME", "LEVEL_STATUS", "DESCRIPTION", "LEVEL_REAL_ID") AS 
  SELECT 'R'                                     AS level_alias,
          hg.id_req_hgroup                        AS level_id, -- 'R'||id_request
          NULL                                    AS parent_id,
          hg.id_request                           AS group_id,
          hg.id_req_hgroup                        AS level_path_id,
          0                                       AS level_order,
          'Solicitud: ' || hg.req_hgroup_name     AS level_name,
          r.request_status                        AS level_status,
          nvl(
             to_char(
                r.id_user_create
             ), 'Usuario'
          )
          || ' - '
          || to_char(
             r.creation_date, 'DD/MM/YYYY'
          ) AS description,
          to_char(
             r.id_request
          ) AS level_real_id
               -------------------- FUNCTIONAL INFO -----------------------------
                -----------------------------------------------------------------
     FROM deployment_request       r,
          deployment_req_hgroup    hg
    WHERE r.id_request = hg.id_request
      AND hg.id_req_hgroup_level = 'R'
   UNION ALL
   SELECT 'T'                        AS level_alias,
          hg.id_req_hgroup           AS level_id, -- 'T'||id_deployment_tech
          hg.id_parent_req_hgroup    AS parent_id,
          hg.id_request              AS group_id,
          hg.id_parent_req_hgroup
          || ','
          || hg.id_req_hgroup AS level_path_id,
          hg.req_hgroup_order        AS level_order,
          'Grupo '
          || hg.req_hgroup_order
          || ': '
          || hg.req_hgroup_name AS level_name,
          hg.req_hgroup_status       AS level_status,
          'Grupo de Ambientes'       AS description,
          to_char(
             hg.id_real_req_hgroup
          ) AS level_real_id
               -------------------- FUNCTIONAL INFO -----------------------------
                -----------------------------------------------------------------
     FROM deployment_req_hgroup hg
    WHERE hg.id_req_hgroup_level = 'T'
   UNION ALL
   SELECT 'P'                        AS level_alias,
          hg.id_req_hgroup           AS level_id, -- 'P' || id_pattern
          hg.id_parent_req_hgroup    AS parent_id,
          hg.id_request              AS group_id,
          'R'
          || hg.id_request
          || ','
          || hg.id_parent_req_hgroup
          || ','
          || hg.id_req_hgroup AS level_path_id,
          hg.req_hgroup_order        AS level_order,
          'Tipo '
          || hg.req_hgroup_order
          || ': '
          || hg.req_hgroup_name AS level_name,
          hg.req_hgroup_status       AS level_status,
          'Grupo de Objetos'         AS description,
          to_char(
             hg.id_real_req_hgroup
          ) AS level_real_id
               -------------------- FUNCTIONAL INFO -----------------------------
                -----------------------------------------------------------------
     FROM deployment_req_hgroup hg
    WHERE hg.id_req_hgroup_level = 'P'
   UNION ALL
   SELECT 'O'                         AS level_alias,
          'O' || o.id_object_repo     AS level_id,
          o.id_req_hgroup             AS parent_id, --'P' || id_pattern
          o.id_request                AS group_id,
          (
             SELECT 'R'
                    || hg.id_request
                    || ','
                    || hg.id_parent_req_hgroup
                    || ','
                    || hg.id_req_hgroup
               FROM deployment_req_hgroup hg
              WHERE hg.id_req_hgroup = o.id_req_hgroup
          )
          /*|| o.id_object_repo*/ AS level_path_id,
          o.object_order              AS level_order,
          o.object_order
          || '- '
          || o.object_name
          || ' V'
          || o.object_version AS level_name,
          o.object_dep_status         AS level_status,
          o.object_source_path        AS description,
          o.id_object_repo            AS level_real_id
               -------------------- FUNCTIONAL INFO -----------------------------
                -----------------------------------------------------------------
     FROM deployment_req_object o
;
  GRANT SELECT ON "ENVX_OPER"."HIERARCHICAL_REQUEST_VEW" TO "ENVX_AUDIT";
