--------------------------------------------------------
--  DDL for View HIERARCHICAL_ROLE_PROFILES
--------------------------------------------------------

  CREATE OR REPLACE FORCE EDITIONABLE VIEW "ENVX_OPER"."HIERARCHICAL_ROLE_PROFILES" ("LEVEL_ALIAS", "LEVEL_ID", "PARENT_ID", "GROUP_ID", "LEVEL_PATH_ID", "LEVEL_ORDER", "LEVEL_NAME", "LEVEL_STATUS", "DESCRIPTION", "LEVEL_REAL_ID") AS 
  SELECT UNIQUE 'R'                      AS level_alias,
              'R' || r.id_role AS level_id,
              NULL                     AS parent_id,
              r.id_role                AS group_id,
              to_char(
                 r.id_role
              )       AS level_path_id,
              0                        AS level_order,
              r.role_name              AS level_name,
              1                        AS level_status,
              r.role_description       AS description,
              r.id_role             AS level_real_id
               -------------------- FUNCTIONAL INFO -----------------------------
                -----------------------------------------------------------------
  FROM system_role r
UNION ALL
                   ------------------ HIERARCHICAL INFO ------------------------------
SELECT 'P'                          AS level_alias,
       'P' || p.id_sys_profile      AS level_id,
       (
          CASE
             WHEN nvl(
                p.id_sup_sys_profile, 0
             ) = 0 THEN
                'R' || p.id_role
             ELSE
                'P' || p.id_sup_sys_profile
          END
       ) AS parent_id,
       p.id_role                    AS group_id,
       (
          CASE
             WHEN nvl(
                p.id_sup_sys_profile, 0
             ) = 0 THEN
                to_char(
                   p.id_role
                )
             ELSE
                p.id_role
                || ','
                || p.id_sup_sys_profile
          END
       )
       || ','
       || p.id_sys_profile AS level_path_id,
       0                            AS level_order,
       p.sys_profile_name           AS level_name,
       p.status_profile             AS level_status,
       p.sys_profile_description    AS description,
       p.id_sys_profile             AS level_real_id
  FROM system_profile p
;
