--------------------------------------------------------
--  DDL for View HIERARCHICAL_ETYPE_ENVIROMENTS
--------------------------------------------------------

  CREATE OR REPLACE FORCE EDITIONABLE VIEW "ENVX_OPER"."HIERARCHICAL_ETYPE_ENVIROMENTS" ("LEVEL_ALIAS", "LEVEL_ID", "PARENT_ID", "GROUP_ID", "LEVEL_PATH_ID", "LEVEL_ORDER", "LEVEL_NAME", "LEVEL_STATUS", "DESCRIPTION", "LEVEL_REAL_ID") AS 
  SELECT UNIQUE 'T'                              AS level_alias,
              'T' || r.id_enviroment_type      AS level_id,
              NULL                             AS parent_id,
              r.id_enviroment_type             AS group_id,
              to_char(
                 r.id_enviroment_type
              ) AS level_path_id,
              0                                AS level_order,
              r.enviroment_type_name           AS level_name,
              1                                AS level_status,
              r.enviroment_type_description    AS description,
              r.id_enviroment_type             AS level_real_id
               -------------------- FUNCTIONAL INFO -----------------------------
                -----------------------------------------------------------------
  FROM enviroment_type r
UNION ALL
                   ------------------ HIERARCHICAL INFO ------------------------------
SELECT 'E'                        AS level_alias,
       'E' || p.id_enviroment     AS level_id,
       (
          CASE
             WHEN nvl(
                p.id_parent_enviroment, 0
             ) = 0 THEN
                'T' || p.id_enviroment_type
             ELSE
                'E' || p.id_parent_enviroment
          END
       ) AS parent_id,
       p.id_enviroment_type       AS group_id,
       (
          CASE
             WHEN nvl(
                p.id_parent_enviroment, 0
             ) = 0 THEN
                to_char(
                   p.id_enviroment_type
                )
             ELSE
                p.id_enviroment_type
                || ','
                || p.id_parent_enviroment
          END
       )
       || ','
       || p.id_enviroment AS level_path_id,
       0                          AS level_order,
       p.enviroment_name          AS level_name,
       p.enviroment_status        AS level_status,
       p.enviroment_alias         AS description,
       p.id_enviroment            AS level_real_id
  FROM enviroment p
;
