--------------------------------------------------------
--  DDL for View REQUEST_NEXT_STEP_USER
--------------------------------------------------------

  CREATE OR REPLACE FORCE EDITIONABLE VIEW "ENVX_OPER"."REQUEST_NEXT_STEP_USER" ("ID_REQUEST", "ID_ACTION", "ID_NEXT_STEP", "ID_NEXT_USER", "ID_NEXT_ROLE", "ID_NEXT_SYS_PROFILE", "NEXT_USER_STATUS", "NEXT_USER_NAME", "NEXT_USER_MAIL", "PROFILE_TYPE") AS 
  SELECT aux.id_request,
	       aux.id_action,
	       aux.id_next_step,
	       u."ID_USER",
	       u."ID_ROLE",
	       u."ID_SYS_PROFILE",
	       1,
	       upper(u."USER_NAME") AS user_name,
	       --u."LDAP_USER",
	       u."USER_MAIL",
	       profile_type
	  FROM user_roles  u,
	       (
		       SELECT rsw.id_sys_profile,
		              upper(user_name)     AS user_name,
		              id_request,
		              id_action,
		              'P'                  AS profile_type,
		              rsw.id_step_flow     AS id_next_step
		         FROM request_exec_flow      exec,
		              request_step_workflow  rsw
		        WHERE rsw.id_step_flow = pkg_workflow_coordinator.next_step_flow(exec.id_action, exec.id_step_flow)
		       UNION ALL
		       SELECT rap.id_sys_profile,
		              upper(user_name)     AS user_name,
		              id_request,
		              id_action,
		              'A'                  AS profile_type,
		              rap.id_step_flow     AS id_next_step
		         FROM request_exec_flow            exec,
		              request_sw_auxiliar_profile  rap
		        WHERE rap.id_step_flow = pkg_workflow_coordinator.next_step_flow(exec.id_action, exec.id_step_flow)
		       UNION ALL
		       SELECT DISTINCT rsw.id_sys_profile,
		                       upper(user_name)     AS user_name,
		                       id_request,
		                       aa.id_action,
		                       'P'                  AS profile_type,
		                       rsw.id_step_flow     AS id_next_step
		         FROM deployment_request     req,
		              request_workflow       rew,
		              request_step_workflow  rsw,
		              request_action_flow    raf,
		              user_roles             u,
		              (
			              SELECT id_action,
			                     id_req_flow,
			                     step_order
			                FROM request_action_flow    af,
			                     request_step_workflow  sw
			               WHERE af.id_step_flow = sw.id_step_flow
			                 AND sw.step_order = 1
		              )                      aa
		        WHERE req.id_enviroment_type = rew.id_enviroment_type
		          AND req.id_deployment_type    = rew.id_deployment_type
		          AND rew.id_req_flow           = rsw.id_req_flow
		          AND raf.id_step_flow          = rsw.id_step_flow
		          AND u.id_sys_profile          = rsw.id_sys_profile
		          AND aa.id_req_flow            = rew.id_req_flow
		          AND rsw.step_order            = 2
		          AND (
			       SELECT unique 1
			         FROM request_exec_flow fl
			        WHERE fl.id_request = req.id_request
		       ) IS NULL
		       UNION ALL
		       SELECT DISTINCT au.id_sys_profile,
		                       upper(user_name)     AS user_name,
		                       id_request,
		                       aa.id_action,
		                       'A'                  AS profile_type,
		                       rsw.id_step_flow     AS id_next_step
		         FROM deployment_request           req,
		              request_workflow             rew,
		              request_step_workflow        rsw,
		              request_action_flow          raf,
		              user_roles                   u,
		              request_sw_auxiliar_profile  au,
		              (
			              SELECT id_action,
			                     id_req_flow,
			                     step_order
			                FROM request_action_flow    af,
			                     request_step_workflow  sw
			               WHERE af.id_step_flow = sw.id_step_flow
			                 AND sw.step_order = 1
		              )                            aa
		        WHERE req.id_enviroment_type = rew.id_enviroment_type
		          AND req.id_deployment_type    = rew.id_deployment_type
		          AND rew.id_req_flow           = rsw.id_req_flow
		          AND raf.id_step_flow          = rsw.id_step_flow
		          AND u.id_sys_profile          = au.id_sys_profile
		          AND au.id_step_flow           = raf.id_step_flow
		          AND aa.id_req_flow            = rew.id_req_flow
		          AND rsw.step_order            = 2
		          AND (
			       SELECT unique 1
			         FROM request_exec_flow fl
			        WHERE fl.id_request = req.id_request
		       ) IS NULL
	       )           aux
	 WHERE u.id_sys_profile = aux.id_sys_profile
;
