--------------------------------------------------------
--  DDL for View REQUEST_DEPLOY_TRAY
--------------------------------------------------------

  CREATE OR REPLACE FORCE EDITIONABLE VIEW "ENVX_OPER"."REQUEST_DEPLOY_TRAY" ("ID_REQ_HGROUP", "REQ_HGROUP_MEMBERS", "ID_REQUEST", "CREATION_DATE", "ID_DEPLOYMENT_TYPE", "ID_ENVIROMENT_TYPE", "ID_USER_CREATE", "REQUEST_STATUS", "PROJECT_TITTLE", "USE_CASE", "REQUEST_TITTLE", "ENV_HGROUP_ORDER", "ENV_HGROUP_MEMBERS", "OHG_HGROUP_MEMBERS", "ID_ENV_HGROUP", "ENV_HGROUP_NAME", "ID_DEPLOYING_TECH", "ENV_ORDER", "ID_ENVIROMENT", "ENVIROMENT_ALIAS", "ENVIROMENT_NAME", "ID_PARENT_ENVIROMENT", "PARENT_ENVIROMENT_ALIAS", "PARENT_ENVIROMENT_NAME", "OBJ_HGROUP_ORDER", "OBJ_HGROUP_MEMBERS", "ID_OBJ_HGROUP", "OBJ_HGROUP_NAME", "ID_PATTERN", "OBJ_ORDER", "ID_DEP_REQ_OBJECT", "ID_OBJECT_REPO", "OBJECT_SOURCE_PATH", "OBJECT_NAME", "OBJECT_VERSION") AS 
  SELECT 
----- REQUEST  ----
 'R' || r.id_request     AS id_req_hgroup,
       (
           SELECT COUNT(1)
             FROM deployment_req_hgroup g
            WHERE g.id_parent_req_hgroup = 'R' || r.id_request
       )                       AS req_hgroup_members,
       r.id_request,
       r.creation_date,
       r.id_deployment_type,
       r.id_enviroment_type,
       r.id_user_create,
       r.request_status,
       (select tittle from project p where p.id_project = r.id_project) as project_tittle,
       r.use_case,
       tittle as request_tittle,
----- ENVIROMENT GROUP ----
       eg.req_hgroup_order     AS env_hgroup_order,
       (
           SELECT COUNT(1)
             FROM deployment_req_env re
            WHERE re.id_req_hgroup = eg.id_req_hgroup
       )                       AS env_hgroup_members,
       (
           SELECT COUNT(1)
             FROM deployment_req_hgroup og1
            WHERE og1.id_parent_req_hgroup = eg.id_req_hgroup
              AND og1.id_request = r.id_request
       )                       AS ohg_hgroup_members,
       eg.id_req_hgroup        AS id_env_hgroup,
       eg.req_hgroup_name      AS env_hgroup_name,
       eg.id_req_hgroup_ref    AS id_deploying_tech,
----- ENVIROMENTS ----
       re.enviroment_order     AS env_order,
       re.id_enviroment,
       e.enviroment_alias,
       e.enviroment_name,
       e.id_parent_enviroment,
       (
           SELECT pe.enviroment_alias
             FROM enviroment pe
            WHERE pe.id_enviroment = e.id_parent_enviroment
       )                       AS parent_enviroment_alias,
       (
           SELECT pe.enviroment_name
             FROM enviroment pe
            WHERE pe.id_enviroment = e.id_parent_enviroment
       )                       AS parent_enviroment_name,
----- OBJECT GROUP ----
       og.req_hgroup_order     AS obj_hgroup_order,
       (
           SELECT COUNT(1)
             FROM deployment_req_object ro
            WHERE ro.id_req_hgroup = og.id_req_hgroup
       )                       AS obj_hgroup_members,
       og.id_req_hgroup        AS id_obj_hgroup,
       og.req_hgroup_name      AS obj_hgroup_name,
       og.id_req_hgroup_ref    AS id_pattern,         
----- PATTERN STEPS ----
 /*      ps.id_pattern_step,
       ps.exec_sequence,
       ps.id_executor,
       ps.id_exec_category,
       ps.id_step_type,
       ps.id_scope,
       (
           SELECT ec.exec_cat_sn
             FROM executor_category ec
            WHERE ec.id_exec_category = ps.id_exec_category
       )                       AS exec_category,
       (
           SELECT ec.exec_category_type
             FROM executor_category ec
            WHERE ec.id_exec_category = ps.id_exec_category
       )                       AS exec_category_type,
       ps.step_name,
          ---
       ps.id_act_cat_success,
       (
           SELECT ac.action_dep_cat_sn
             FROM dep_action_category ac
            WHERE ac.id_action_dep_cat = ps.id_act_cat_success
       )                       AS success_category,
       ps.status_success,
       ps.id_pstep_success,
          ---
       ps.id_act_cat_warning,
       (
           SELECT ac.action_dep_cat_sn
             FROM dep_action_category ac
            WHERE ac.id_action_dep_cat = ps.id_act_cat_warning
       )                       AS warning_category,
       ps.status_warning,
       ps.id_pstep_warning,
          ---
       ps.id_act_cat_error,
       (
           SELECT ac.action_dep_cat_sn
             FROM dep_action_category ac
            WHERE ac.id_action_dep_cat = ps.id_act_cat_error
       )                       AS error_category,
       ps.status_error,
       ps.id_pstep_error,
          ---
       ps.id_act_cat_abort,
       (
           SELECT ac.action_dep_cat_sn
             FROM dep_action_category ac
            WHERE ac.id_action_dep_cat = ps.id_act_cat_abort
       )                       AS abort_category,
       ps.status_abort,
       ps.id_pstep_abort,*/
----- OBJECTS -----
       ro.object_order         AS obj_order,
       ro.id_dep_req_object,
       ro.id_object_repo,
       ro.object_source_path,
       ro.object_name,
       ro.object_version
-------------------     
  FROM deployment_req_object      ro,
       --deployment_pattern_step    ps,
       deployment_req_hgroup      og,
       deployment_req_env         re,
       enviroment                 e,
       deployment_req_hgroup      eg,
       deployment_request         r
 WHERE ro.id_req_hgroup = og.id_req_hgroup
   AND og.id_parent_req_hgroup = eg.id_req_hgroup
   AND re.id_req_hgroup = eg.id_req_hgroup
   AND re.id_enviroment = e.id_enviroment
   --AND ps.id_pattern = og.id_req_hgroup_ref
   AND eg.id_request = r.id_request
   AND og.id_request = r.id_request
   AND ro.id_request = r.id_request
   AND re.id_request = r.id_request
 ORDER BY r.id_request,
          eg.req_hgroup_order,
          re.enviroment_order,
          og.req_hgroup_order,
         -- ps.exec_sequence,
          ro.object_order
;
